X-Git-Url: https://git.lukelau.me/?p=opengl.git;a=blobdiff_plain;f=model.cpp;h=4192645c9c003d2de7d490897ff0ca02a1897b46;hp=5302df06238038d32a686e8bffb2c62881f19a91;hb=b472351f3c80cec8c7e9ec30cb4c113c947c0ff7;hpb=dbd855720a9af7d6e599ddc50bbbb0dee85458a5 diff --git a/model.cpp b/model.cpp index 5302df0..4192645 100644 --- a/model.cpp +++ b/model.cpp @@ -2,11 +2,10 @@ #include #include #include +#include #include "util.hpp" - -Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) { - +Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) : ai(*aiMesh) { std::vector vertices, normals, tangents, bitangents; std::vector texCoords; @@ -115,13 +114,13 @@ Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) { glVertexAttribPointer(boneWeightLoc, 4, GL_FLOAT, GL_FALSE, sizeof(VertBones), (const GLvoid *)sizeof(VertBones::ids)); } -Model::Node::Node(const aiNode &node, GLuint progId, AnimMap *am): ai(node), progId(progId), animMap(am) { +Model::Node::Node(aiNode &node, GLuint progId, AnimMap *am, std::set allBones, Node *p): ai(node), parent(p), progId(progId), animMap(am), isBone(allBones.count(std::string(node.mName.C_Str())) > 0) { for (int i = 0; i < node.mNumMeshes; i++) { meshIndices.push_back(node.mMeshes[i]); } for (int i = 0; i < node.mNumChildren; i++) { - const aiNode *child = node.mChildren[i]; - children.push_back(new Node(*child, progId, am)); + aiNode *child = node.mChildren[i]; + children.push_back(new Node(*child, progId, am, allBones, this)); } } @@ -224,6 +223,14 @@ glm::mat4 Model::Node::totalTrans(const glm::mat4 parentTrans, const float tick) return m; } +const Model::Node &Model::Node::getRoot() const { + const Model::Node *rootPtr = this; + while (rootPtr->parent != nullptr) + rootPtr = rootPtr->parent; + const Model::Node &root = *rootPtr; + return root; +} + void Model::Node::draw( const std::vector &meshes, const std::vector &materials, const Skybox skybox, @@ -235,6 +242,9 @@ void Model::Node::draw( const std::vector &meshes, glm::mat4 m = totalTrans(parentTrans, tick); #ifdef DEBUG_NODES + if (isBone) + drawDebugNode(m, {0, 0.5, 1, 1}); + else drawDebugNode(m); #endif @@ -291,13 +301,6 @@ void Model::Node::draw( const std::vector &meshes, for (Node *child: children) child->draw(meshes, materials, skybox, tick, boneTransforms, m); } -void printMatrix4x4(aiMatrix4x4 m) { - fprintf(stderr, "%f, %f, %f, %f\n", m.a1, m.a2, m.a3, m.a4); - fprintf(stderr, "%f, %f, %f, %f\n", m.b1, m.b2, m.b3, m.b4); - fprintf(stderr, "%f, %f, %f, %f\n", m.c1, m.c2, m.c3, m.c4); - fprintf(stderr, "%f, %f, %f, %f\n", m.d1, m.d2, m.d3, m.d4); -} - void printHierarchy(aiNode *n, int indent = 0) { for (int i = 0; i < indent; i++) fprintf(stderr, "\t"); @@ -310,9 +313,12 @@ void printHierarchy(aiNode *n, int indent = 0) { Model::Model(const aiScene *scene, Program p): program(p) { glUseProgram(p.progId); + std::set allBones; for (int i = 0; i < scene->mNumMeshes; i++) { const aiMesh *mesh = scene->mMeshes[i]; meshes.push_back(Mesh(mesh, p.progId)); + for (int j = 0; j < mesh->mNumBones; j++) + allBones.insert(std::string(mesh->mBones[j]->mName.C_Str())); } for (unsigned int i = 0; i < scene->mNumMaterials; i++) { @@ -342,7 +348,7 @@ Model::Model(const aiScene *scene, Program p): program(p) { } } - root = new Node(*(scene->mRootNode), p.progId, &animMap); + root = new Node(*(scene->mRootNode), p.progId, &animMap, allBones, nullptr); } @@ -393,3 +399,40 @@ Model::Node* Model::Node::findNode(const aiNode &aiNode) { } return nullptr; } + +bool Model::Node::operator==(const Model::Node &rhs) const { + return &ai == &rhs.ai; +} + +// Returns closest vertex in world space and distance +// a and b define the line in 3d space +std::pair Model::closestVertex(Model::Node &n, glm::vec3 a, glm::vec3 b, glm::mat4 parentTrans) const { + float shortestDist = FLT_MAX; + glm::vec3 closest; + for (int i = 0; i < n.ai.mNumMeshes; i++) { + int meshIdx = n.ai.mMeshes[i]; + const aiMesh &mesh = meshes[meshIdx].ai; + + for (int j = 0; j < mesh.mNumVertices; j++) { + glm::vec4 vPos = glm::vec4(aiVector3DToMat4(mesh.mVertices[j]), 1); + // Move from model space -> world space + vPos = parentTrans * aiMatrixToMat4(n.ai.mTransformation) * vPos; + float dist = glm::distance(glm::vec3(vPos), + glm::closestPointOnLine(glm::vec3(vPos), a, b)); + if (dist < shortestDist) { + closest = glm::vec3(vPos); + shortestDist = dist; + } + } + } + + for (auto child: n.getChildren()) { + auto res = closestVertex(*child, a, b, parentTrans * aiMatrixToMat4(n.ai.mTransformation)); + if (res.second < shortestDist) { + closest = res.first; + shortestDist = res.second; + } + } + + return { closest, shortestDist }; +}