3 #include <assimp/quaternion.h>
4 #include <glm/gtc/type_ptr.hpp>
5 #include <glm/gtx/closest_point.hpp>
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;
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));
17 if (aiMesh->HasNormals()) {
18 aiVector3D v = aiMesh->mNormals[i];
19 normals.push_back(glm::vec3(v.x, v.y, v.z));
21 std::cerr << "Missing normals" << std::endl;
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));
29 texCoords.push_back(glm::vec2(0));
31 materialIndex = aiMesh->mMaterialIndex;
34 std::vector<GLuint> indices;
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]);
45 numIndices = indices.size();
47 glGenVertexArrays(1, &vao);
48 glBindVertexArray(vao);
51 glGenBuffers(6, vbos);
52 GLuint vertexVbo = vbos[0], normalVbo = vbos[1], texCoordVbo = vbos[2], indicesVbo = vbos[3];
53 GLuint boneVbo = vbos[4];
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);
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);
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);
73 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicesVbo);
74 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), &indices[0], GL_STATIC_DRAW);
77 std::vector<VertBones> vertBones(aiMesh->mNumVertices);
79 std::map<unsigned int, std::vector<std::pair<unsigned int, float>>> boneWeightMap;
81 for (unsigned int i = 0; i < aiMesh->mNumBones; i++) {
82 aiBone *aiBone = aiMesh->mBones[i];
84 boneMap[std::string(aiBone->mName.C_Str())] = std::pair(i + 1, aiBone);
86 for (int j = 0; j < aiBone->mNumWeights; j++) {
87 aiVertexWeight vw = aiBone->mWeights[j];
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));
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;
105 glBindBuffer(GL_ARRAY_BUFFER, boneVbo);
106 glBufferData(GL_ARRAY_BUFFER, sizeof(VertBones) * vertBones.size(), &vertBones[0], GL_STATIC_DRAW);
108 GLuint boneIdLoc = glGetAttribLocation(progId, "boneIds");
109 glEnableVertexAttribArray(boneIdLoc);
110 glVertexAttribIPointer(boneIdLoc, 4, GL_INT, sizeof(VertBones), 0);
112 GLuint boneWeightLoc = glGetAttribLocation(progId, "boneWeights");
113 glEnableVertexAttribArray(boneWeightLoc);
114 glVertexAttribPointer(boneWeightLoc, 4, GL_FLOAT, GL_FALSE, sizeof(VertBones), (const GLvoid *)sizeof(VertBones::ids));
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]);
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));
127 glm::mat4 lerpPosition(const aiNodeAnim *anim, const float tick) {
128 if (anim->mNumPositionKeys == 0) return glm::mat4(1.f);
131 for (int i = 0; i < anim->mNumPositionKeys; i++) {
132 aiVectorKey vk = anim->mPositionKeys[i];
133 if (vk.mTime > tick) {
140 lerpPos = anim->mPositionKeys[0].mValue;
141 } else if (yIndex == -1) {
142 lerpPos = anim->mPositionKeys[anim->mNumPositionKeys - 1].mValue;
144 auto X = anim->mPositionKeys[yIndex - 1];
145 auto Y = anim->mPositionKeys[yIndex];
147 lerpPos = (X.mValue * (float)(Y.mTime - tick) + Y.mValue * (float)(tick - X.mTime)) / (float)(Y.mTime - X.mTime);
150 aiMatrix4x4::Translation(lerpPos, result);
151 return aiMatrixToMat4(result);
154 glm::mat4 lerpRotation(const aiNodeAnim *anim, const float tick) {
156 for (int i = 0; i < anim->mNumRotationKeys; i++) {
157 aiQuatKey vk = anim->mRotationKeys[i];
158 if (vk.mTime > tick) {
166 result = anim->mRotationKeys[0].mValue;
167 } else if (yIndex == -1) {
168 result = anim->mRotationKeys[anim->mNumRotationKeys - 1].mValue;
171 auto X = anim->mRotationKeys[yIndex - 1];
172 auto Y = anim->mRotationKeys[yIndex];
174 float mix = (tick - X.mTime) / (Y.mTime - X.mTime);
176 aiQuaternion::Interpolate(result, X.mValue, Y.mValue, mix);
179 return aiMatrixToMat4(aiMatrix4x4(result.GetMatrix()));
182 glm::mat4 lerpScaling(const aiNodeAnim *anim, const float tick) {
184 for (int i = 0; i < anim->mNumScalingKeys; i++) {
185 aiVectorKey vk = anim->mScalingKeys[i];
186 if (vk.mTime > tick) {
194 lerpPos = anim->mScalingKeys[0].mValue;
196 auto X = anim->mScalingKeys[yIndex - 1];
197 auto Y = anim->mScalingKeys[yIndex];
199 lerpPos = (X.mValue * (float)(Y.mTime - tick) + Y.mValue * (float)(tick - X.mTime)) / (float)(Y.mTime - X.mTime);
202 aiMatrix4x4::Scaling(lerpPos, result);
203 return aiMatrixToMat4(result);
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);
222 glm::mat4 m = parentTrans * aiTrans * transform;
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;
234 void Model::Node::draw( const std::vector<Mesh> &meshes,
235 const std::vector<Material> &materials,
238 const BoneTransforms &boneTransforms,
239 glm::mat4 parentTrans = glm::mat4(1)) const {
241 GLuint modelLoc = glGetUniformLocation(progId, "model");
242 glm::mat4 m = totalTrans(parentTrans, tick);
246 drawDebugNode(m, {0, 0.5, 1, 1});
251 for (unsigned int i: meshIndices) {
252 const Mesh &mesh = meshes[i];
253 glBindVertexArray(mesh.vao);
256 std::vector<glm::mat4> idBones(17, glm::mat4(1.f));
257 glUniformMatrix4fv(glGetUniformLocation(progId, "bones"), 17, GL_FALSE, glm::value_ptr(idBones[0]));
259 // bonemap: map from bone nodes to bone ids and aiBones
260 for (auto pair: mesh.boneMap) {
262 std::string boneName = pair.first;
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);
272 if (!boneTransforms.count(boneName)) abort();
273 glm::mat4 boneTrans = boneTransforms.at(boneName);
275 boneTrans = boneTrans * boneOffset;
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));
282 Material material = materials[mesh.materialIndex];
285 glUniform1i(glGetUniformLocation(progId, "irradianceMap"), 4);
286 glActiveTexture(GL_TEXTURE4);
287 glBindTexture(GL_TEXTURE_CUBE_MAP, skybox.getIrradianceMap());
289 glUniform1i(glGetUniformLocation(progId, "prefilterMap"), 5);
290 glActiveTexture(GL_TEXTURE5);
291 glBindTexture(GL_TEXTURE_CUBE_MAP, skybox.getPrefilterMap());
293 glUniform1i(glGetUniformLocation(progId, "brdfMap"), 6);
294 glActiveTexture(GL_TEXTURE6);
295 glBindTexture(GL_TEXTURE_2D, skybox.getBRDFMap());
297 glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(m));
299 glDrawElements(GL_TRIANGLES, mesh.numIndices, GL_UNSIGNED_INT, 0);
301 for (Node *child: children) child->draw(meshes, materials, skybox, tick, boneTransforms, m);
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);
313 Model::Model(const aiScene *scene, Program p): program(p) {
314 glUseProgram(p.progId);
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()));
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));
329 for (int i = 0; i < scene->mNumAnimations; i++) {
330 const aiAnimation *aiAnim = scene->mAnimations[i];
332 std::map<std::string, std::vector<const aiNodeAnim*>> nodeAnims;
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());
338 if (!nodeAnims.count(nodeName)) nodeAnims[nodeName] = std::vector<const aiNodeAnim*>();
340 nodeAnims[nodeName].push_back(nodeAnim);
343 for (std::pair<std::string, std::vector<const aiNodeAnim*>> pair: nodeAnims) {
344 std::string nodeName = pair.first;
346 if (!animMap.count(nodeName)) animMap[nodeName] = std::vector<const Animation>();
347 animMap[nodeName].push_back({ aiAnim->mDuration, pair.second });
351 root = new Node(*(scene->mRootNode), p.progId, &animMap, allBones, nullptr);
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());
358 glm::mat4 m = n.totalTrans(parentTrans, tick);
361 if (bones.count(name) > 0)
362 res[std::string(n.ai.mName.C_Str())] = m; // take part in hierarchy
364 m = glm::mat4(1); // ignore this node transformation
365 for (const auto child: n.getChildren())
366 res.merge(calcBoneTransforms(*child, tick, bones, m));
370 void Model::draw(Skybox skybox, const float tick) const {
371 glUseProgram(program.progId);
373 std::set<std::string> bones;
374 for (auto m: this->meshes) {
375 for (auto b: m.boneMap) {
376 bones.insert(b.first);
379 auto boneTransforms = calcBoneTransforms(*root, tick, bones);
381 root->draw(meshes, materials, skybox, tick, boneTransforms);
384 Model::Node* Model::find(const std::string &name) const {
385 return find(aiString(name));
388 Model::Node* Model::find(const aiString name) const {
389 const aiNode *node = root->ai.FindNode(name);
390 Model::Node* res = root->findNode(*node);
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);
403 bool Model::Node::operator==(const Model::Node &rhs) const {
404 return &ai == &rhs.ai;
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;
412 for (int i = 0; i < n.ai.mNumMeshes; i++) {
413 int meshIdx = n.ai.mMeshes[i];
414 const aiMesh &mesh = meshes[meshIdx].ai;
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);
429 for (auto child: n.getChildren()) {
430 auto res = closestVertex(*child, a, b, parentTrans * aiMatrixToMat4(n.ai.mTransformation));
431 if (res.second < shortestDist) {
433 shortestDist = res.second;
437 return { closest, shortestDist };