Fix stupid typo
[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 const Model::Node &Model::Node::getRoot() const {
228         const Model::Node *rootPtr = this;
229         while (rootPtr->parent != nullptr)
230                 rootPtr = rootPtr->parent;
231         const Model::Node &root = *rootPtr;
232         return root;
233 }
234
235 void Model::Node::draw( const std::vector<Mesh> &meshes,
236                                                 const std::vector<Material> &materials,
237                                                 const Skybox skybox,
238                                                 const float tick,
239                                                 const BoneTransforms &boneTransforms,
240                                                 glm::mat4 parentTrans = glm::mat4(1)) const {
241
242         GLuint modelLoc = glGetUniformLocation(progId, "model");
243         glm::mat4 m = totalTrans(parentTrans, tick);
244
245 #ifdef DEBUG_NODES
246         if (isBone)
247                 drawDebugNode(m, {0, 0.5, 1, 1});
248         else
249                 drawDebugNode(m);
250 #endif
251
252         for (unsigned int i: meshIndices) {
253                 const Mesh &mesh = meshes[i];
254                 glBindVertexArray(mesh.vao);
255
256                 // bones
257                 std::vector<glm::mat4> idBones(17, glm::mat4(1.f));
258                 glUniformMatrix4fv(glGetUniformLocation(progId, "bones"), 17, GL_FALSE, glm::value_ptr(idBones[0]));
259
260                 // bonemap: map from bone nodes to bone ids and aiBones
261                 for (auto pair: mesh.boneMap) {
262
263                         std::string boneName = pair.first;
264
265                         unsigned int boneId = pair.second.first;
266                         aiBone *bone = pair.second.second;
267                         // This is actually an inverse-bind matrix
268                         // i.e. transforms bone space -> mesh space
269                         // so no need to inverse again!
270                         // https://github.com/assimp/assimp/pull/1803/files
271                         glm::mat4 boneOffset = aiMatrixToMat4(bone->mOffsetMatrix);
272
273                         if (!boneTransforms.count(boneName)) abort();
274                         glm::mat4 boneTrans = boneTransforms.at(boneName);
275
276                         boneTrans = boneTrans * boneOffset;
277
278                         std::string boneLocStr = "bones[" + std::to_string(boneId) + "]";
279                         GLuint boneLoc = glGetUniformLocation(progId, boneLocStr.c_str());
280                         glUniformMatrix4fv(boneLoc, 1, GL_FALSE, glm::value_ptr(boneTrans));
281                 }
282
283                 Material material = materials[mesh.materialIndex];
284                 material.bind();
285
286                 glUniform1i(glGetUniformLocation(progId, "irradianceMap"), 4);
287                 glActiveTexture(GL_TEXTURE4);
288                 glBindTexture(GL_TEXTURE_CUBE_MAP, skybox.getIrradianceMap());
289
290                 glUniform1i(glGetUniformLocation(progId, "prefilterMap"), 5);
291                 glActiveTexture(GL_TEXTURE5);
292                 glBindTexture(GL_TEXTURE_CUBE_MAP, skybox.getPrefilterMap());
293
294                 glUniform1i(glGetUniformLocation(progId, "brdfMap"), 6);
295                 glActiveTexture(GL_TEXTURE6);
296                 glBindTexture(GL_TEXTURE_2D, skybox.getBRDFMap());
297                 
298                 glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(m));
299
300                 glDrawElements(GL_TRIANGLES, mesh.numIndices, GL_UNSIGNED_INT, 0);
301         }
302         for (Node *child: children) child->draw(meshes, materials, skybox, tick, boneTransforms, m);
303 }
304
305 void printHierarchy(aiNode *n, int indent = 0) {
306         for (int i = 0; i < indent; i++)
307                 fprintf(stderr, "\t");
308         fprintf(stderr,"%s\n", n->mName.C_Str());
309         printMatrix4x4(n->mTransformation);
310         for (int i = 0; i < n->mNumChildren; i++)
311                 printHierarchy(n->mChildren[i], indent + 1);
312 }
313
314 Model::Model(const aiScene *scene, Program p): program(p) {
315         glUseProgram(p.progId);
316
317         std::set<std::string> allBones;
318         for (int i = 0; i < scene->mNumMeshes; i++) {
319                 const aiMesh *mesh = scene->mMeshes[i];
320                 meshes.push_back(Mesh(mesh, p.progId));
321                 for (int j = 0; j < mesh->mNumBones; j++)
322                         allBones.insert(std::string(mesh->mBones[j]->mName.C_Str()));
323         }
324
325         for (unsigned int i = 0; i < scene->mNumMaterials; i++) {
326                 const aiMaterial &material = *scene->mMaterials[i];
327                 materials.push_back(Material(material, *scene, p.progId));
328         }
329
330         for (int i = 0; i < scene->mNumAnimations; i++) {
331                 const aiAnimation *aiAnim = scene->mAnimations[i];
332
333                 std::map<std::string, std::vector<const aiNodeAnim*>> nodeAnims;
334
335                 for (int j = 0; j < aiAnim->mNumChannels; j++) {
336                         const aiNodeAnim *nodeAnim = aiAnim->mChannels[j];
337                         std::string nodeName = std::string(nodeAnim->mNodeName.C_Str());
338                         
339                         if (!nodeAnims.count(nodeName)) nodeAnims[nodeName] = std::vector<const aiNodeAnim*>();
340
341                         nodeAnims[nodeName].push_back(nodeAnim);
342                 }
343
344                 for (std::pair<std::string, std::vector<const aiNodeAnim*>> pair: nodeAnims) {
345                         std::string nodeName = pair.first;
346
347                         if (!animMap.count(nodeName)) animMap[nodeName] = std::vector<const Animation>();
348                         animMap[nodeName].push_back({ aiAnim->mDuration, pair.second });
349                 }
350         }
351
352         root = new Node(*(scene->mRootNode), p.progId, &animMap, allBones, nullptr);
353 }
354
355
356 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 {
357         std::string name = std::string(n.ai.mName.C_Str());
358
359         glm::mat4 m = n.totalTrans(parentTrans, tick);
360
361         BoneTransforms res;
362         if (bones.count(name) > 0)
363                 res[std::string(n.ai.mName.C_Str())] = m; // take part in hierarchy
364         else
365                 m = glm::mat4(1); // ignore this node transformation
366         for (const auto child: n.getChildren())
367                 res.merge(calcBoneTransforms(*child, tick, bones, m));
368         return res;
369 }
370
371 void Model::draw(Skybox skybox, const float tick) const {
372         glUseProgram(program.progId);
373
374         std::set<std::string> bones;
375         for (auto m: this->meshes) {
376                 for (auto b: m.boneMap) {
377                         bones.insert(b.first);
378                 }
379         }
380         auto boneTransforms = calcBoneTransforms(*root, tick, bones);
381
382         root->draw(meshes, materials, skybox, tick, boneTransforms);
383 }
384
385 Model::Node* Model::find(const std::string &name) const {
386         return find(aiString(name));
387 }
388
389 Model::Node* Model::find(const aiString name) const {
390         const aiNode *node = root->ai.FindNode(name);
391         Model::Node* res = root->findNode(*node);
392         return res;
393 }
394
395 Model::Node* Model::Node::findNode(const aiNode &aiNode) {
396         if (&ai == &aiNode) return this;
397         for (Model::Node *child: children) {
398                 Model::Node *res = child->findNode(aiNode);
399                 if (res) return res;
400         }
401         return nullptr;
402 }
403
404 bool Model::Node::operator==(const Model::Node &rhs) const {
405         return &ai == &rhs.ai;
406 }