Vertex picking
[opengl.git] / model.cpp
1 #include "model.hpp"
2 #include <iostream>
3 #include <assimp/quaternion.h>
4 #include <glm/gtc/type_ptr.hpp>
5 #include <glm/gtx/closest_point.hpp>
6 #include "util.hpp"
7
8 Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) : ai(*aiMesh) {
9         std::vector<glm::vec3> vertices, normals, tangents, bitangents;
10         std::vector<glm::vec2> texCoords;
11
12         for (int i = 0; i < aiMesh->mNumVertices; i++) {
13                 if (aiMesh->HasPositions()) {
14                         aiVector3D v = aiMesh->mVertices[i];
15                         vertices.push_back(glm::vec3(v.x, v.y, v.z));
16                 }
17                 if (aiMesh->HasNormals()) {
18                         aiVector3D v = aiMesh->mNormals[i];
19                         normals.push_back(glm::vec3(v.x, v.y, v.z));
20                 } else {
21                         std::cerr << "Missing normals" << std::endl;
22                         abort();
23                 }
24                 // check for texture coord set 0
25                 if (aiMesh->HasTextureCoords(0)) {
26                         const aiVector3D v = aiMesh->mTextureCoords[0][i];
27                         texCoords.push_back(glm::vec2(v.x, v.y));
28                 } else {
29                         texCoords.push_back(glm::vec2(0));
30                 }
31                 materialIndex = aiMesh->mMaterialIndex;
32         }
33
34         std::vector<GLuint> indices;
35
36         for (int i = 0; i < aiMesh->mNumFaces; i++) {
37                 const aiFace &face = aiMesh->mFaces[i];
38                 if(face.mNumIndices == 3) {
39                         indices.push_back(face.mIndices[0]);
40                         indices.push_back(face.mIndices[1]);
41                         indices.push_back(face.mIndices[2]);
42                 }
43         } 
44         
45         numIndices = indices.size();
46         
47         glGenVertexArrays(1, &vao);
48         glBindVertexArray(vao);
49         
50         GLuint vbos[6];
51         glGenBuffers(6, vbos);
52         GLuint vertexVbo = vbos[0], normalVbo = vbos[1], texCoordVbo = vbos[2], indicesVbo = vbos[3];
53         GLuint boneVbo = vbos[4];
54         
55         GLuint posLoc = glGetAttribLocation(progId, "pos");
56         glBindBuffer(GL_ARRAY_BUFFER, vertexVbo);
57         glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
58         glEnableVertexAttribArray(posLoc);
59         glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
60         
61         GLuint normalLoc = glGetAttribLocation(progId, "unscaledNormal");
62         glBindBuffer(GL_ARRAY_BUFFER, normalVbo);
63         glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(glm::vec3), &normals[0], GL_STATIC_DRAW);
64         glEnableVertexAttribArray(normalLoc);
65         glVertexAttribPointer(normalLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
66
67         GLuint texCoordLoc = glGetAttribLocation(progId, "vTexCoord");
68         glBindBuffer(GL_ARRAY_BUFFER, texCoordVbo);
69         glBufferData(GL_ARRAY_BUFFER, texCoords.size() * sizeof(glm::vec2), &texCoords[0], GL_STATIC_DRAW);
70         glEnableVertexAttribArray(texCoordLoc);
71         glVertexAttribPointer(texCoordLoc, 2, GL_FLOAT, GL_FALSE, 0, 0);
72
73         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicesVbo);
74         glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), &indices[0], GL_STATIC_DRAW);
75
76         // bones
77         std::vector<VertBones> vertBones(aiMesh->mNumVertices);
78
79         std::map<unsigned int, std::vector<std::pair<unsigned int, float>>> boneWeightMap;
80
81         for (unsigned int i = 0; i < aiMesh->mNumBones; i++) {
82                 aiBone *aiBone = aiMesh->mBones[i];
83
84                 boneMap[std::string(aiBone->mName.C_Str())] = std::pair(i + 1, aiBone);
85
86                 for (int j = 0; j < aiBone->mNumWeights; j++) {
87                         aiVertexWeight vw = aiBone->mWeights[j];
88
89                         if (!boneWeightMap.count(vw.mVertexId))
90                                 boneWeightMap[vw.mVertexId] = std::vector<std::pair<unsigned int, float>>();
91                         boneWeightMap[vw.mVertexId].push_back(std::pair(i + 1, vw.mWeight));
92                 }
93         }
94
95         for (auto pair: boneWeightMap) {
96                 unsigned int vertexId = pair.first;
97                 for (int i = 0; i < pair.second.size() && i < 4; i++) {
98                         unsigned int boneId = pair.second[i].first;
99                         float weight = pair.second[i].second;
100                         vertBones[vertexId].ids[i] = boneId;
101                         vertBones[vertexId].weights[i] = weight;
102                 }
103         }
104         
105         glBindBuffer(GL_ARRAY_BUFFER, boneVbo);
106         glBufferData(GL_ARRAY_BUFFER, sizeof(VertBones) * vertBones.size(), &vertBones[0], GL_STATIC_DRAW);
107
108         GLuint boneIdLoc = glGetAttribLocation(progId, "boneIds");
109         glEnableVertexAttribArray(boneIdLoc);
110         glVertexAttribIPointer(boneIdLoc, 4, GL_INT, sizeof(VertBones), 0);
111
112         GLuint boneWeightLoc = glGetAttribLocation(progId, "boneWeights");
113         glEnableVertexAttribArray(boneWeightLoc);
114         glVertexAttribPointer(boneWeightLoc, 4, GL_FLOAT, GL_FALSE, sizeof(VertBones), (const GLvoid *)sizeof(VertBones::ids));
115 }
116
117 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) {
118         for (int i = 0; i < node.mNumMeshes; i++) {
119                 meshIndices.push_back(node.mMeshes[i]);
120         }
121         for (int i = 0; i < node.mNumChildren; i++) {
122                 aiNode *child = node.mChildren[i];
123                 children.push_back(new Node(*child, progId, am, allBones, this));
124         }
125 }
126
127 glm::mat4 lerpPosition(const aiNodeAnim *anim, const float tick) {
128         if (anim->mNumPositionKeys == 0) return glm::mat4(1.f);
129
130         int yIndex = -1;
131         for (int i = 0; i < anim->mNumPositionKeys; i++) {
132                 aiVectorKey vk = anim->mPositionKeys[i];
133                 if (vk.mTime > tick) {
134                         yIndex = i;
135                         break;
136                 }
137         }
138         aiVector3D lerpPos;
139         if (yIndex == 0) {
140                 lerpPos = anim->mPositionKeys[0].mValue;
141         } else if (yIndex == -1) {
142                 lerpPos = anim->mPositionKeys[anim->mNumPositionKeys - 1].mValue;
143         } else {
144                 auto X = anim->mPositionKeys[yIndex - 1];
145                 auto Y = anim->mPositionKeys[yIndex];
146
147                 lerpPos = (X.mValue * (float)(Y.mTime - tick) + Y.mValue * (float)(tick - X.mTime)) / (float)(Y.mTime - X.mTime);
148         }
149         aiMatrix4x4 result;
150         aiMatrix4x4::Translation(lerpPos, result);
151         return aiMatrixToMat4(result);
152 }
153
154 glm::mat4 lerpRotation(const aiNodeAnim *anim, const float tick) {
155         int yIndex = -1;
156         for (int i = 0; i < anim->mNumRotationKeys; i++) {
157                 aiQuatKey vk = anim->mRotationKeys[i];
158                 if (vk.mTime > tick) {
159                         yIndex = i;
160                         break;
161                 }
162         }
163
164         aiQuaternion result;
165         if (yIndex < 1) {
166                 result = anim->mRotationKeys[0].mValue;
167         } else if (yIndex == -1) {
168                 result = anim->mRotationKeys[anim->mNumRotationKeys - 1].mValue;
169         } else {
170
171                 auto X = anim->mRotationKeys[yIndex - 1];
172                 auto Y = anim->mRotationKeys[yIndex];
173
174                 float mix = (tick - X.mTime) / (Y.mTime - X.mTime);
175
176                 aiQuaternion::Interpolate(result, X.mValue, Y.mValue, mix);
177
178         }
179         return aiMatrixToMat4(aiMatrix4x4(result.GetMatrix()));
180 }
181
182 glm::mat4 lerpScaling(const aiNodeAnim *anim, const float tick) {
183         int yIndex = -1;
184         for (int i = 0; i < anim->mNumScalingKeys; i++) {
185                 aiVectorKey vk = anim->mScalingKeys[i];
186                 if (vk.mTime > tick) {
187                         yIndex = i;
188                         break;
189                 }
190         }
191
192         aiVector3D lerpPos;
193         if (yIndex < 1) {
194                 lerpPos = anim->mScalingKeys[0].mValue;
195         } else {
196                 auto X = anim->mScalingKeys[yIndex - 1];
197                 auto Y = anim->mScalingKeys[yIndex];
198
199                 lerpPos = (X.mValue * (float)(Y.mTime - tick) + Y.mValue * (float)(tick - X.mTime)) / (float)(Y.mTime - X.mTime);
200         }
201         aiMatrix4x4 result;
202         aiMatrix4x4::Scaling(lerpPos, result);
203         return aiMatrixToMat4(result);
204 }
205
206 glm::mat4 Model::Node::totalTrans(const glm::mat4 parentTrans, const float tick) const {
207         glm::mat4 aiTrans = aiMatrixToMat4(ai.mTransformation);
208         if (animMap->count(std::string(ai.mName.C_Str()))) {
209                 for (const Animation anim: animMap->at(std::string(ai.mName.C_Str()))) {
210                         // animations are *absolute*
211                         // they replace aiNode.mTransformation!!
212                         aiTrans = glm::mat4(1);
213                         float t = fmod(tick, anim.duration);
214                         for (const aiNodeAnim *nodeAnim: anim.nodeAnims) {
215                                 aiTrans *= lerpPosition(nodeAnim, t);
216                                 aiTrans *= lerpRotation(nodeAnim, t);
217                                 aiTrans *= lerpScaling(nodeAnim, t);
218                         }
219                 }
220         }
221
222         glm::mat4 m = parentTrans * aiTrans * transform;
223         return m;
224 }
225
226 const Model::Node &Model::Node::getRoot() const {
227         const Model::Node *rootPtr = this;
228         while (rootPtr->parent != nullptr)
229                 rootPtr = rootPtr->parent;
230         const Model::Node &root = *rootPtr;
231         return root;
232 }
233
234 void Model::Node::draw( const std::vector<Mesh> &meshes,
235                                                 const std::vector<Material> &materials,
236                                                 const Skybox skybox,
237                                                 const float tick,
238                                                 const BoneTransforms &boneTransforms,
239                                                 glm::mat4 parentTrans = glm::mat4(1)) const {
240
241         GLuint modelLoc = glGetUniformLocation(progId, "model");
242         glm::mat4 m = totalTrans(parentTrans, tick);
243
244 #ifdef DEBUG_NODES
245         if (isBone)
246                 drawDebugNode(m, {0, 0.5, 1, 1});
247         else
248                 drawDebugNode(m);
249 #endif
250
251         for (unsigned int i: meshIndices) {
252                 const Mesh &mesh = meshes[i];
253                 glBindVertexArray(mesh.vao);
254
255                 // bones
256                 std::vector<glm::mat4> idBones(17, glm::mat4(1.f));
257                 glUniformMatrix4fv(glGetUniformLocation(progId, "bones"), 17, GL_FALSE, glm::value_ptr(idBones[0]));
258
259                 // bonemap: map from bone nodes to bone ids and aiBones
260                 for (auto pair: mesh.boneMap) {
261
262                         std::string boneName = pair.first;
263
264                         unsigned int boneId = pair.second.first;
265                         aiBone *bone = pair.second.second;
266                         // This is actually an inverse-bind matrix
267                         // i.e. transforms bone space -> mesh space
268                         // so no need to inverse again!
269                         // https://github.com/assimp/assimp/pull/1803/files
270                         glm::mat4 boneOffset = aiMatrixToMat4(bone->mOffsetMatrix);
271
272                         if (!boneTransforms.count(boneName)) abort();
273                         glm::mat4 boneTrans = boneTransforms.at(boneName);
274
275                         boneTrans = boneTrans * boneOffset;
276
277                         std::string boneLocStr = "bones[" + std::to_string(boneId) + "]";
278                         GLuint boneLoc = glGetUniformLocation(progId, boneLocStr.c_str());
279                         glUniformMatrix4fv(boneLoc, 1, GL_FALSE, glm::value_ptr(boneTrans));
280                 }
281
282                 Material material = materials[mesh.materialIndex];
283                 material.bind();
284
285                 glUniform1i(glGetUniformLocation(progId, "irradianceMap"), 4);
286                 glActiveTexture(GL_TEXTURE4);
287                 glBindTexture(GL_TEXTURE_CUBE_MAP, skybox.getIrradianceMap());
288
289                 glUniform1i(glGetUniformLocation(progId, "prefilterMap"), 5);
290                 glActiveTexture(GL_TEXTURE5);
291                 glBindTexture(GL_TEXTURE_CUBE_MAP, skybox.getPrefilterMap());
292
293                 glUniform1i(glGetUniformLocation(progId, "brdfMap"), 6);
294                 glActiveTexture(GL_TEXTURE6);
295                 glBindTexture(GL_TEXTURE_2D, skybox.getBRDFMap());
296                 
297                 glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(m));
298
299                 glDrawElements(GL_TRIANGLES, mesh.numIndices, GL_UNSIGNED_INT, 0);
300         }
301         for (Node *child: children) child->draw(meshes, materials, skybox, tick, boneTransforms, m);
302 }
303
304 void printHierarchy(aiNode *n, int indent = 0) {
305         for (int i = 0; i < indent; i++)
306                 fprintf(stderr, "\t");
307         fprintf(stderr,"%s\n", n->mName.C_Str());
308         printMatrix4x4(n->mTransformation);
309         for (int i = 0; i < n->mNumChildren; i++)
310                 printHierarchy(n->mChildren[i], indent + 1);
311 }
312
313 Model::Model(const aiScene *scene, Program p): program(p) {
314         glUseProgram(p.progId);
315
316         std::set<std::string> allBones;
317         for (int i = 0; i < scene->mNumMeshes; i++) {
318                 const aiMesh *mesh = scene->mMeshes[i];
319                 meshes.push_back(Mesh(mesh, p.progId));
320                 for (int j = 0; j < mesh->mNumBones; j++)
321                         allBones.insert(std::string(mesh->mBones[j]->mName.C_Str()));
322         }
323
324         for (unsigned int i = 0; i < scene->mNumMaterials; i++) {
325                 const aiMaterial &material = *scene->mMaterials[i];
326                 materials.push_back(Material(material, *scene, p.progId));
327         }
328
329         for (int i = 0; i < scene->mNumAnimations; i++) {
330                 const aiAnimation *aiAnim = scene->mAnimations[i];
331
332                 std::map<std::string, std::vector<const aiNodeAnim*>> nodeAnims;
333
334                 for (int j = 0; j < aiAnim->mNumChannels; j++) {
335                         const aiNodeAnim *nodeAnim = aiAnim->mChannels[j];
336                         std::string nodeName = std::string(nodeAnim->mNodeName.C_Str());
337                         
338                         if (!nodeAnims.count(nodeName)) nodeAnims[nodeName] = std::vector<const aiNodeAnim*>();
339
340                         nodeAnims[nodeName].push_back(nodeAnim);
341                 }
342
343                 for (std::pair<std::string, std::vector<const aiNodeAnim*>> pair: nodeAnims) {
344                         std::string nodeName = pair.first;
345
346                         if (!animMap.count(nodeName)) animMap[nodeName] = std::vector<const Animation>();
347                         animMap[nodeName].push_back({ aiAnim->mDuration, pair.second });
348                 }
349         }
350
351         root = new Node(*(scene->mRootNode), p.progId, &animMap, allBones, nullptr);
352 }
353
354
355 std::map<std::string, glm::mat4> Model::calcBoneTransforms(const Node &n, const float tick, const std::set<std::string> bones, const glm::mat4 parentTrans = glm::mat4(1)) const {
356         std::string name = std::string(n.ai.mName.C_Str());
357
358         glm::mat4 m = n.totalTrans(parentTrans, tick);
359
360         BoneTransforms res;
361         if (bones.count(name) > 0)
362                 res[std::string(n.ai.mName.C_Str())] = m; // take part in hierarchy
363         else
364                 m = glm::mat4(1); // ignore this node transformation
365         for (const auto child: n.getChildren())
366                 res.merge(calcBoneTransforms(*child, tick, bones, m));
367         return res;
368 }
369
370 void Model::draw(Skybox skybox, const float tick) const {
371         glUseProgram(program.progId);
372
373         std::set<std::string> bones;
374         for (auto m: this->meshes) {
375                 for (auto b: m.boneMap) {
376                         bones.insert(b.first);
377                 }
378         }
379         auto boneTransforms = calcBoneTransforms(*root, tick, bones);
380
381         root->draw(meshes, materials, skybox, tick, boneTransforms);
382 }
383
384 Model::Node* Model::find(const std::string &name) const {
385         return find(aiString(name));
386 }
387
388 Model::Node* Model::find(const aiString name) const {
389         const aiNode *node = root->ai.FindNode(name);
390         Model::Node* res = root->findNode(*node);
391         return res;
392 }
393
394 Model::Node* Model::Node::findNode(const aiNode &aiNode) {
395         if (&ai == &aiNode) return this;
396         for (Model::Node *child: children) {
397                 Model::Node *res = child->findNode(aiNode);
398                 if (res) return res;
399         }
400         return nullptr;
401 }
402
403 bool Model::Node::operator==(const Model::Node &rhs) const {
404         return &ai == &rhs.ai;
405 }
406
407 // Returns closest vertex in world space and distance
408 // a and b define the line in 3d space
409 std::pair<glm::vec3, float> Model::closestVertex(Model::Node &n, glm::vec3 a, glm::vec3 b, glm::mat4 parentTrans) const {
410         float shortestDist = FLT_MAX;
411         glm::vec3 closest;
412         for (int i = 0; i < n.ai.mNumMeshes; i++) {
413                 int meshIdx = n.ai.mMeshes[i];
414                 const aiMesh &mesh = meshes[meshIdx].ai;
415
416                 for (int j = 0; j < mesh.mNumVertices; j++) {
417                         glm::vec4 vPos = glm::vec4(aiVector3DToMat4(mesh.mVertices[j]), 1);
418                         // Move from model space -> world space
419                         vPos = parentTrans * aiMatrixToMat4(n.ai.mTransformation) * vPos;
420                         float dist = glm::distance(glm::vec3(vPos),
421                                                         glm::closestPointOnLine(glm::vec3(vPos), a, b));
422                         if (dist < shortestDist) {
423                                 closest = glm::vec3(vPos);
424                                 shortestDist = dist;
425                         }
426                 }
427         }
428         
429         for (auto child: n.getChildren()) {
430                 auto res = closestVertex(*child, a, b, parentTrans * aiMatrixToMat4(n.ai.mTransformation));
431                 if (res.second < shortestDist) {
432                         closest = res.first;
433                         shortestDist = res.second;
434                 }
435         }
436
437         return { closest, shortestDist };
438 }