From: Luke Lau Date: Tue, 11 Feb 2020 12:25:29 +0000 (+0000) Subject: Do boning and animation properly X-Git-Tag: cs7gv5-a2~6 X-Git-Url: https://git.lukelau.me/?p=opengl.git;a=commitdiff_plain;h=dbd855720a9af7d6e599ddc50bbbb0dee85458a5;hp=9e2f64944d28b5e050e37d9995ffeb3dd4cb5b7a Do boning and animation properly --- diff --git a/Makefile b/Makefile index 27cdf31..5d9c00c 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ all: bin/main CXX_FLAGS := -g --std=c++17 -Wall -bin/main: model.o material.o image.o skybox.o program.o main.o +bin/main: model.o material.o image.o skybox.o program.o main.o util.o clang++ $(CXX_FLAGS) $^ \ -I/usr/local/include -L/usr/local/lib \ -lassimp \ diff --git a/main.cpp b/main.cpp index aa78a22..15599b0 100644 --- a/main.cpp +++ b/main.cpp @@ -20,6 +20,7 @@ #include "program.hpp" #include "skybox.hpp" #include "image.hpp" +#include "util.hpp" #pragma clang diagnostic ignored "-Wdeprecated-declarations" @@ -115,6 +116,9 @@ void display() { float d = (float)glutGet(GLUT_ELAPSED_TIME) * 0.001f; + glUseProgram(getUtilProg()->progId); + setProjectionAndViewUniforms(getUtilProg()->progId); + glUseProgram(pbrProg->progId); setProjectionAndViewUniforms(pbrProg->progId); @@ -144,7 +148,8 @@ void display() { glUniform3fv(glGetUniformLocation(pbrProg->progId, "lightPositions"), 6, glm::value_ptr(lightPositions[0])); glUniform3fv(glGetUniformLocation(pbrProg->progId, "lightColors"), 6, glm::value_ptr(lightColors[0])); - /* pbr->getRoot()->model = glm::rotate(glm::mat4(1.f), glm::radians(d * 10), glm::vec3(0, 1, 0)); */ + /* sceneModel->find("Top Bone")->transform = glm::rotate(glm::mat4(1), d / 5.f, { 1, 0, 0}); */ + /* sceneModel->find("Bottom Bone")->transform = glm::rotate(glm::mat4(1), d / 3.f, { 1, 0, 0}); */ sceneModel->draw(skyboxes[activeSkybox], d * 1000); for (Light &light: lights) drawLight(light); @@ -193,6 +198,8 @@ int findNodeTrans(struct aiNode *n, const struct aiString name, glm::mat4 *dest) } void init() { + initUtilProg(); + plainProg = new Program("plainvertex.glsl", "plainfrag.glsl"); glUseProgram(plainProg->progId); setupLightBuffers(plainProg->progId); @@ -257,7 +264,10 @@ void keyboardUp(unsigned char key, int x, int y) { keyStates[key] = false; } +#define ENABLE_MOVEMENT + void timer(int _) { +#ifdef ENABLE_MOVEMENT float xSpeed = 0.f, ySpeed = 0.f, zSpeed = 0.f; if (keyStates['w']) zSpeed = 0.1f; @@ -282,6 +292,7 @@ void timer(int _) { camPos.x += xSpeed * sin(yaw) + zSpeed * cos(yaw); camPos.y += ySpeed; camPos.z += zSpeed * sin(yaw) - xSpeed * cos(yaw); +#endif glutPostRedisplay(); glutTimerFunc(16, timer, 0); } @@ -290,6 +301,7 @@ int prevMouseX, prevMouseY; bool firstMouse = true; void motion(int x, int y) { +#ifdef ENABLE_MOVEMENT if (firstMouse) { prevMouseX = x; prevMouseY = y; @@ -315,6 +327,7 @@ void motion(int x, int y) { } else { camUp = glm::vec3(0, 1, 0); } +#endif } void mouse(int button, int state, int x, int y) { diff --git a/material.cpp b/material.cpp index e1fb5c3..838f673 100644 --- a/material.cpp +++ b/material.cpp @@ -54,38 +54,38 @@ Material::Texture::Texture(const aiString fileName, const aiScene &scene) { void Material::bind() const { if (ambientOcclusion != nullptr) { - glUniform1i(glGetUniformLocation(progId, "material.albedoMap"), 0); + glUniform1i(glGetUniformLocation(progId, "mat.albedoMap"), 0); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, albedo->texId); - glUniform1i(glGetUniformLocation(progId, "material.hasAlbedo"), 1); + glUniform1i(glGetUniformLocation(progId, "mat.hasAlbedo"), 1); } else { - glUniform1i(glGetUniformLocation(progId, "material.hasAlbedo"), 0); + glUniform1i(glGetUniformLocation(progId, "mat.hasAlbedo"), 0); } if (normal != nullptr) { - glUniform1i(glGetUniformLocation(progId, "material.normalMap"), 1); + glUniform1i(glGetUniformLocation(progId, "mat.normalMap"), 1); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, normal->texId); - glUniform1i(glGetUniformLocation(progId, "material.hasNormal"), 1); + glUniform1i(glGetUniformLocation(progId, "mat.hasNormal"), 1); } else { - glUniform1i(glGetUniformLocation(progId, "material.hasNormal"), 0); + glUniform1i(glGetUniformLocation(progId, "mat.hasNormal"), 0); } if (metallicRoughness != nullptr) { glUniform1i(glGetUniformLocation(progId, "metallicRoughnessMap"), 2); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, metallicRoughness->texId); - glUniform1i(glGetUniformLocation(progId, "material.hasMetallicRoughness"), 1); + glUniform1i(glGetUniformLocation(progId, "mat.hasMetallicRoughness"), 1); } else { - glUniform1i(glGetUniformLocation(progId, "material.hasMetallicRoughness"), 0); + glUniform1i(glGetUniformLocation(progId, "mat.hasMetallicRoughness"), 0); } if (ambientOcclusion != nullptr) { glUniform1i(glGetUniformLocation(progId, "aoMap"), 3); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, ambientOcclusion->texId); - glUniform1i(glGetUniformLocation(progId, "material.hasAo"), 1); + glUniform1i(glGetUniformLocation(progId, "mat.hasAo"), 1); } else { - glUniform1i(glGetUniformLocation(progId, "material.hasAo"), 0); + glUniform1i(glGetUniformLocation(progId, "mat.hasAo"), 0); } } diff --git a/model.cpp b/model.cpp index 3c1ecf4..5302df0 100644 --- a/model.cpp +++ b/model.cpp @@ -2,6 +2,7 @@ #include #include #include +#include "util.hpp" Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) { @@ -19,7 +20,7 @@ Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) { normals.push_back(glm::vec3(v.x, v.y, v.z)); } else { std::cerr << "Missing normals" << std::endl; - exit(1); + abort(); } // check for texture coord set 0 if (aiMesh->HasTextureCoords(0)) { @@ -81,12 +82,13 @@ Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) { for (unsigned int i = 0; i < aiMesh->mNumBones; i++) { aiBone *aiBone = aiMesh->mBones[i]; - boneMap[std::string(aiBone->mName.C_Str())] = std::pair(i + 1, aiMatrixToMat4(aiBone->mOffsetMatrix)); + boneMap[std::string(aiBone->mName.C_Str())] = std::pair(i + 1, aiBone); for (int j = 0; j < aiBone->mNumWeights; j++) { aiVertexWeight vw = aiBone->mWeights[j]; - if (!boneWeightMap.count(vw.mVertexId)) boneWeightMap[vw.mVertexId] = std::vector>(); + if (!boneWeightMap.count(vw.mVertexId)) + boneWeightMap[vw.mVertexId] = std::vector>(); boneWeightMap[vw.mVertexId].push_back(std::pair(i + 1, vw.mWeight)); } } @@ -202,33 +204,39 @@ glm::mat4 lerpScaling(const aiNodeAnim *anim, const float tick) { return aiMatrixToMat4(result); } -void Model::Node::draw( const std::vector &meshes, - const std::vector &materials, - const Skybox skybox, - const float tick, - glm::mat4 parentTrans = glm::mat4(1), - BoneTransforms boneTransforms = BoneTransforms()) const { - - GLuint modelLoc = glGetUniformLocation(progId, "model"); - - glm::mat4 animTrans(1.f); +glm::mat4 Model::Node::totalTrans(const glm::mat4 parentTrans, const float tick) const { + glm::mat4 aiTrans = aiMatrixToMat4(ai.mTransformation); if (animMap->count(std::string(ai.mName.C_Str()))) { for (const Animation anim: animMap->at(std::string(ai.mName.C_Str()))) { + // animations are *absolute* + // they replace aiNode.mTransformation!! + aiTrans = glm::mat4(1); float t = fmod(tick, anim.duration); for (const aiNodeAnim *nodeAnim: anim.nodeAnims) { - animTrans *= lerpPosition(nodeAnim, t); - animTrans *= lerpRotation(nodeAnim, t); - animTrans *= lerpScaling(nodeAnim, t); + aiTrans *= lerpPosition(nodeAnim, t); + aiTrans *= lerpRotation(nodeAnim, t); + aiTrans *= lerpScaling(nodeAnim, t); + } } } + + glm::mat4 m = parentTrans * aiTrans * transform; + return m; } +void Model::Node::draw( const std::vector &meshes, + const std::vector &materials, + const Skybox skybox, + const float tick, + const BoneTransforms &boneTransforms, + glm::mat4 parentTrans = glm::mat4(1)) const { - glm::mat4 m = parentTrans * animTrans * aiMatrixToMat4(ai.mTransformation); + GLuint modelLoc = glGetUniformLocation(progId, "model"); + glm::mat4 m = totalTrans(parentTrans, tick); - /* for (auto child: children) { */ - /* boneTransforms[std::string(ai.mName.C_Str())] = m; */ - /* } */ +#ifdef DEBUG_NODES + drawDebugNode(m); +#endif for (unsigned int i: meshIndices) { const Mesh &mesh = meshes[i]; @@ -238,35 +246,23 @@ void Model::Node::draw( const std::vector &meshes, std::vector idBones(17, glm::mat4(1.f)); glUniformMatrix4fv(glGetUniformLocation(progId, "bones"), 17, GL_FALSE, glm::value_ptr(idBones[0])); - for (std::pair> pair: mesh.boneMap) { - - std::string nodeName = pair.first; + // bonemap: map from bone nodes to bone ids and aiBones + for (auto pair: mesh.boneMap) { - if (animMap->count(nodeName) <= 0) break; + std::string boneName = pair.first; unsigned int boneId = pair.second.first; + aiBone *bone = pair.second.second; // This is actually an inverse-bind matrix - // i.e. position of the mesh in bone space + // i.e. transforms bone space -> mesh space // so no need to inverse again! // https://github.com/assimp/assimp/pull/1803/files - glm::mat4 boneOffset = pair.second.second; - - glm::mat4 boneTrans(1.f); - /* if (boneTransforms.count(nodeName)) { */ - /* std::cerr << "got bone transform from map" << std::endl; */ - /* boneTrans = boneTransforms[nodeName]; */ - /* } */ - for (const Animation anim: animMap->at(nodeName)) { - float t = fmod(tick, anim.duration); - for (const aiNodeAnim *nodeAnim: anim.nodeAnims) { - boneTrans = boneTrans * lerpPosition(nodeAnim, t); - boneTrans = boneTrans * lerpRotation(nodeAnim, t); - boneTrans = boneTrans * lerpScaling(nodeAnim, t); - } - } + glm::mat4 boneOffset = aiMatrixToMat4(bone->mOffsetMatrix); - boneTrans = boneTrans * boneOffset; + if (!boneTransforms.count(boneName)) abort(); + glm::mat4 boneTrans = boneTransforms.at(boneName); + boneTrans = boneTrans * boneOffset; std::string boneLocStr = "bones[" + std::to_string(boneId) + "]"; GLuint boneLoc = glGetUniformLocation(progId, boneLocStr.c_str()); @@ -292,7 +288,23 @@ void Model::Node::draw( const std::vector &meshes, glDrawElements(GL_TRIANGLES, mesh.numIndices, GL_UNSIGNED_INT, 0); } - for (Node *child: children) child->draw(meshes, materials, skybox, tick, m, boneTransforms); + for (Node *child: children) child->draw(meshes, materials, skybox, tick, boneTransforms, m); +} + +void printMatrix4x4(aiMatrix4x4 m) { + fprintf(stderr, "%f, %f, %f, %f\n", m.a1, m.a2, m.a3, m.a4); + fprintf(stderr, "%f, %f, %f, %f\n", m.b1, m.b2, m.b3, m.b4); + fprintf(stderr, "%f, %f, %f, %f\n", m.c1, m.c2, m.c3, m.c4); + fprintf(stderr, "%f, %f, %f, %f\n", m.d1, m.d2, m.d3, m.d4); +} + +void printHierarchy(aiNode *n, int indent = 0) { + for (int i = 0; i < indent; i++) + fprintf(stderr, "\t"); + fprintf(stderr,"%s\n", n->mName.C_Str()); + printMatrix4x4(n->mTransformation); + for (int i = 0; i < n->mNumChildren; i++) + printHierarchy(n->mChildren[i], indent + 1); } Model::Model(const aiScene *scene, Program p): program(p) { @@ -303,13 +315,11 @@ Model::Model(const aiScene *scene, Program p): program(p) { meshes.push_back(Mesh(mesh, p.progId)); } - // TODO: handle default material inserted at the end by assimp for (unsigned int i = 0; i < scene->mNumMaterials; i++) { const aiMaterial &material = *scene->mMaterials[i]; materials.push_back(Material(material, *scene, p.progId)); } - AnimMap *animMap = new AnimMap(); for (int i = 0; i < scene->mNumAnimations; i++) { const aiAnimation *aiAnim = scene->mAnimations[i]; @@ -327,44 +337,49 @@ Model::Model(const aiScene *scene, Program p): program(p) { for (std::pair> pair: nodeAnims) { std::string nodeName = pair.first; - if (!animMap->count(nodeName)) (*animMap)[nodeName] = std::vector(); - (*animMap)[nodeName].push_back({ aiAnim->mDuration, pair.second }); + if (!animMap.count(nodeName)) animMap[nodeName] = std::vector(); + animMap[nodeName].push_back({ aiAnim->mDuration, pair.second }); } } - root = new Node(*(scene->mRootNode), p.progId, animMap); + root = new Node(*(scene->mRootNode), p.progId, &animMap); } -/* void Model::calcBoneTransforms(aiNode &node, glm::mat4 parentTrans = glm::mat4(1), BoneTransforms boneTrans = BoneTransforms()) { */ -/* glm::mat4 animTrans(1.f); */ -/* if (animMap->count(std::string(ai.mName.C_Str()))) { */ -/* for (const Animation anim: animMap->at(std::string(ai.mName.C_Str()))) { */ -/* float t = fmod(tick, anim.duration); */ -/* for (const aiNodeAnim *nodeAnim: anim.nodeAnims) { */ -/* animTrans *= lerpPosition(nodeAnim, t); */ -/* animTrans *= lerpRotation(nodeAnim, t); */ -/* animTrans *= lerpScaling(nodeAnim, t); */ -/* } */ -/* } */ -/* } */ +std::map Model::calcBoneTransforms(const Node &n, const float tick, const std::set bones, const glm::mat4 parentTrans = glm::mat4(1)) const { + std::string name = std::string(n.ai.mName.C_Str()); -/* glm::mat4 m = parentTrans * animTrans * aiMatrixToMat4(ai.mTransformation) * model; */ -/* } */ + glm::mat4 m = n.totalTrans(parentTrans, tick); + + BoneTransforms res; + if (bones.count(name) > 0) + res[std::string(n.ai.mName.C_Str())] = m; // take part in hierarchy + else + m = glm::mat4(1); // ignore this node transformation + for (const auto child: n.getChildren()) + res.merge(calcBoneTransforms(*child, tick, bones, m)); + return res; +} void Model::draw(Skybox skybox, const float tick) const { glUseProgram(program.progId); + std::set bones; + for (auto m: this->meshes) { + for (auto b: m.boneMap) { + bones.insert(b.first); + } + } + auto boneTransforms = calcBoneTransforms(*root, tick, bones); - - root->draw(meshes, materials, skybox, tick); + root->draw(meshes, materials, skybox, tick, boneTransforms); } -Model::Node* Model::find(const std::string &name) { +Model::Node* Model::find(const std::string &name) const { return find(aiString(name)); } -Model::Node* Model::find(const aiString name) { +Model::Node* Model::find(const aiString name) const { const aiNode *node = root->ai.FindNode(name); Model::Node* res = root->findNode(*node); return res; diff --git a/model.hpp b/model.hpp index 92aab0d..bf6f08f 100644 --- a/model.hpp +++ b/model.hpp @@ -1,5 +1,6 @@ #include #include +#include #ifdef __APPLE__ #include #else @@ -26,7 +27,7 @@ class Model { std::vector nodeAnims; }; - typedef std::map> BoneMap; + typedef std::map> BoneMap; typedef std::map> AnimMap; typedef std::map BoneTransforms; @@ -49,11 +50,16 @@ class Model { class Node { public: Node(const aiNode &aiNode, GLuint progId, AnimMap *animMap); - void draw(const std::vector &meshes, const std::vector &materials, const Skybox s, const float tick, glm::mat4 parentModel, BoneTransforms boneTransforms) const; + void draw(const std::vector &meshes, const std::vector &materials, const Skybox s, const float tick, const BoneTransforms &boneTransforms, glm::mat4 parentModel) const; const std::vector &getChildren() const { return children; } Node* findNode(const aiNode &aiNode); const aiNode &ai; + // an extra transform + glm::mat4 transform = glm::mat4(1); + + glm::mat4 totalTrans(const glm::mat4 parentTrans, const float tick) const; + private: const GLuint progId; @@ -63,8 +69,8 @@ class Model { }; Node* getRoot() { return root; } - Node* find(const aiString name); - Node* find(const std::string &name); + Node* find(const aiString name) const; + Node* find(const std::string &name) const; private: const Program program; @@ -74,5 +80,8 @@ class Model { std::vector materials; + AnimMap animMap; + + BoneTransforms calcBoneTransforms(const Node &n, const float tick, const std::set bones, const glm::mat4 parentTrans) const; void loadModel(const std::string &path); }; diff --git a/models/cowedboy.blend b/models/cowedboy.blend index 1d916c0..5fe3aa3 100644 Binary files a/models/cowedboy.blend and b/models/cowedboy.blend differ diff --git a/models/ik.blend b/models/ik.blend index 03af46d..9aad769 100644 Binary files a/models/ik.blend and b/models/ik.blend differ diff --git a/models/ik.glb b/models/ik.glb index b992585..33539b7 100644 Binary files a/models/ik.glb and b/models/ik.glb differ diff --git a/pbrvert.glsl b/pbrvert.glsl index 775cebf..5763380 100644 --- a/pbrvert.glsl +++ b/pbrvert.glsl @@ -25,7 +25,7 @@ void main() { boneTrans += bones[boneIds.z] * boneWeights.z; boneTrans += bones[boneIds.w] * boneWeights.w; - mat4 bonedModel = boneTrans * model; + mat4 bonedModel = model * boneTrans; worldPos = vec3(bonedModel * vec4(pos, 1.f)); diff --git a/program.cpp b/program.cpp index 74744fd..2275dcb 100644 --- a/program.cpp +++ b/program.cpp @@ -26,7 +26,7 @@ void attachShader(GLuint progId, string filePath, GLenum type) { if (!success) { GLchar log[1024]; glGetShaderInfoLog(shader, 1024, NULL, log); - fprintf(stderr, "error: %s\n%s\n", filePath.c_str(), log); + fprintf(stderr, "Error compiling %s\n%s\n", filePath.c_str(), log); exit(1); } glAttachShader(progId, shader); @@ -57,7 +57,7 @@ void Program::validate() const { if (!success) { GLchar log[1024]; glGetProgramInfoLog(progId, sizeof(log), NULL, log); - fprintf(stderr, "error: %s\n", log); + fprintf(stderr, "Error validating: %s\n", log); exit(1); } } diff --git a/shapes.hpp b/shapes.hpp index 63263b5..a1a42eb 100644 --- a/shapes.hpp +++ b/shapes.hpp @@ -86,3 +86,28 @@ constexpr array cube() { return vertices; } + +constexpr glm::vec3 pyramid[18] = { + glm::vec3(0.0f, 1.0f, 0.0f), + glm::vec3(1.0f, -1.0f, -1.0f), + glm::vec3(-1.0f, -1.0f, -1.0f), + + glm::vec3(0.0f, 1.0f, 0.0f), + glm::vec3(-1.0f, -1.0f, 1.0f), + glm::vec3(1.0f, -1.0f, 1.0f), + + glm::vec3(0.0f, 1.0f, 0.0f), + glm::vec3(-1.0f, -1.0f, -1.0f), + glm::vec3(-1.0f, -1.0f, 1.0f), + + glm::vec3(0.0f, 1.0f, 0.0f), + glm::vec3(1.0f, -1.0f, 1.0f), + glm::vec3(1.0f, -1.0f, -1.0f), + + glm::vec3(1, -1, 1), + glm::vec3(-1, -1, 1), + glm::vec3(-1, -1, -1), + glm::vec3(-1, -1, -1), + glm::vec3(1, -1, -1), + glm::vec3(1, -1, 1) +}; diff --git a/skybox.cpp b/skybox.cpp index c0a9ff6..83f1308 100644 --- a/skybox.cpp +++ b/skybox.cpp @@ -207,8 +207,6 @@ void Skybox::draw(glm::mat4 proj, glm::mat4 view) const { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexId); glDrawArrays(GL_TRIANGLES, 0, 36); - - if (glGetError()) exit(1); } template diff --git a/util.cpp b/util.cpp new file mode 100644 index 0000000..796017d --- /dev/null +++ b/util.cpp @@ -0,0 +1,47 @@ +#include +#include +#include "shapes.hpp" +#include "util.hpp" + +GLuint utilVao; +Program *utilProg; + +Program *getUtilProg() { return utilProg; } + +void initUtilProg() { + utilProg = new Program("plainvertex.glsl", "plainfrag.glsl"); + + glGenVertexArrays(1, &utilVao); + GLuint vbo; + glBindVertexArray(utilVao); + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(pyramid), pyramid, GL_STATIC_DRAW); + GLuint posLoc = glGetAttribLocation(utilProg->progId, "vPosition"); + glEnableVertexAttribArray(posLoc); + glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, 0); + + if (glGetError()) abort(); +} + +void drawDebugNode(glm::mat4 model, glm::vec4 color) { + GLint prevProg; glGetIntegerv(GL_CURRENT_PROGRAM, &prevProg); + + glUseProgram(utilProg->progId); + + glDepthRange(0, 0.01); + + glBindVertexArray(utilVao); + model = glm::scale(model, {0.1, 0.3, 0.1}); + GLuint modelLoc = glGetUniformLocation(utilProg->progId, "model"); + glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); + + GLuint colorLoc = glGetUniformLocation(utilProg->progId, "color"); + glUniform4fv(colorLoc, 1, glm::value_ptr(color)); + + glDrawArrays(GL_TRIANGLES, 0, 36); + + glDepthRange(0, 1.f); + + glUseProgram(prevProg); +} diff --git a/util.hpp b/util.hpp new file mode 100644 index 0000000..89236e2 --- /dev/null +++ b/util.hpp @@ -0,0 +1,6 @@ +#include "program.hpp" +#include + +void initUtilProg(); +Program *getUtilProg(); +void drawDebugNode(glm::mat4 transform, glm::vec4 color = {1, 0.5, 1, 1});