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