Vertex picking
[opengl.git] / model.cpp
index 345309a211e30232e2bd0c50a3945ccf74b43542..4192645c9c003d2de7d490897ff0ca02a1897b46 100644 (file)
--- a/model.cpp
+++ b/model.cpp
@@ -2,11 +2,10 @@
 #include <iostream>
 #include <assimp/quaternion.h>
 #include <glm/gtc/type_ptr.hpp>
+#include <glm/gtx/closest_point.hpp>
 #include "util.hpp"
 
-
-Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) {
-
+Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) : ai(*aiMesh) {
        std::vector<glm::vec3> vertices, normals, tangents, bitangents;
        std::vector<glm::vec2> texCoords;
 
@@ -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<Mesh> &meshes,
                                                const std::vector<Material> &materials,
                                                const Skybox skybox,
@@ -341,8 +348,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 +403,36 @@ 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
+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;
+       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 };
+}