First pass at blendshapes
[opengl.git] / model.cpp
index 4192645c9c003d2de7d490897ff0ca02a1897b46..0a1e3526c8297e20f19b24d8fc1082fd0afb5352 100644 (file)
--- a/model.cpp
+++ b/model.cpp
@@ -5,7 +5,7 @@
 #include <glm/gtx/closest_point.hpp>
 #include "util.hpp"
 
-Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) : ai(*aiMesh) {
+Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) : progId(progId), ai(*aiMesh) {
        std::vector<glm::vec3> vertices, normals, tangents, bitangents;
        std::vector<glm::vec2> texCoords;
 
@@ -47,13 +47,12 @@ Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) : ai(*aiMesh) {
        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);
@@ -114,6 +113,15 @@ Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) : ai(*aiMesh) {
        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<std::string> 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]);
@@ -407,32 +415,38 @@ bool Model::Node::operator==(const Model::Node &rhs) const {
 // Returns closest vertex in world space and distance
 // a and b define the line in 3d space
 std::pair<glm::vec3, float> Model::closestVertex(Model::Node &n, glm::vec3 a, glm::vec3 b, glm::mat4 parentTrans) const {
-       float shortestDist = FLT_MAX;
-       glm::vec3 closest;
+       float closestDist = FLT_MAX;
+       glm::vec3 closestVert;
+
        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);
+                       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 < shortestDist) {
-                               closest = glm::vec3(vPos);
-                               shortestDist = dist;
+                       if (dist < closestDist) {
+                               closestVert = glm::vec3(vPos);
+                               closestDist = 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;
+               auto childRes = closestVertex(*child, a, b, parentTrans * aiMatrixToMat4(n.ai.mTransformation));
+               if (childRes.second < closestDist) {
+                       closestVert = childRes.first;
+                       closestDist = childRes.second;
                }
        }
 
-       return { closest, shortestDist };
+       return { closestVert, closestDist };
 }