X-Git-Url: http://git.lukelau.me/?p=opengl.git;a=blobdiff_plain;f=model.cpp;h=efc7f222b9db70856e362cf50b15549b5d5c4d27;hp=345309a211e30232e2bd0c50a3945ccf74b43542;hb=HEAD;hpb=d26c67a51e58c3f70d1689e265e9bebe578b50ad diff --git a/model.cpp b/model.cpp index 345309a..9e4cfc6 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) : progId(progId), ai(*aiMesh) { std::vector vertices, normals, tangents, bitangents; std::vector texCoords; @@ -48,13 +47,12 @@ Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) { glGenVertexArrays(1, &vao); glBindVertexArray(vao); - GLuint vbos[6]; glGenBuffers(6, vbos); - GLuint vertexVbo = vbos[0], normalVbo = vbos[1], texCoordVbo = vbos[2], indicesVbo = vbos[3]; + GLuint posVbo = vbos[0], normalVbo = vbos[1], texCoordVbo = vbos[2], indicesVbo = vbos[3]; GLuint boneVbo = vbos[4]; GLuint posLoc = glGetAttribLocation(progId, "pos"); - glBindBuffer(GL_ARRAY_BUFFER, vertexVbo); + glBindBuffer(GL_ARRAY_BUFFER, posVbo); glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW); glEnableVertexAttribArray(posLoc); glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, 0); @@ -115,6 +113,15 @@ Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) { glVertexAttribPointer(boneWeightLoc, 4, GL_FLOAT, GL_FALSE, sizeof(VertBones), (const GLvoid *)sizeof(VertBones::ids)); } +void Model::Mesh::updatePosBuffer() const { + GLuint posLoc = glGetAttribLocation(progId, "pos"); + GLuint posVbo = vbos[0]; + glBindBuffer(GL_ARRAY_BUFFER, posVbo); + glBufferData(GL_ARRAY_BUFFER, ai.mNumVertices * sizeof(aiVector3D), ai.mVertices, GL_STATIC_DRAW); + glEnableVertexAttribArray(posLoc); + glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, 0); +} + 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]); @@ -224,6 +231,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, @@ -341,8 +356,6 @@ Model::Model(const aiScene *scene, Program p): program(p) { } } - printHierarchy(scene->mRootNode); - root = new Node(*(scene->mRootNode), p.progId, &animMap, allBones, nullptr); } @@ -398,3 +411,42 @@ Model::Node* Model::Node::findNode(const aiNode &aiNode) { 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 +Model::VertexLookup Model::closestVertex(Model::Node &n, glm::vec3 a, glm::vec3 b, glm::mat4 parentTrans) const { + Model::VertexLookup closest; + closest.distance = FLT_MAX; + + 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++) { + if (mesh.HasNormals()) { + auto n = aiVector3DToVec3(mesh.mNormals[j]); + if (glm::dot(n, glm::normalize(b - a)) > 0) + continue; + } + glm::vec4 vPos = glm::vec4(aiVector3DToVec3(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 < closest.distance) { + closest.pos = glm::vec3(vPos); + closest.distance = dist; + closest.meshIdx = i; + closest.vertIdx = j; + } + } + } + + for (auto child: n.getChildren()) { + auto childRes = closestVertex(*child, a, b, parentTrans * aiMatrixToMat4(n.ai.mTransformation)); + if (childRes.distance < closest.distance) + closest = childRes; + } + + return closest; +}