From: Luke Lau Date: Tue, 30 Oct 2018 00:54:20 +0000 (+0000) Subject: Object hierarchies X-Git-Tag: cs7gv3-a3~28 X-Git-Url: http://git.lukelau.me/?p=opengl.git;a=commitdiff_plain;h=ba5e08bf25de722d907748ce55b27a45b2b270bf Object hierarchies --- diff --git a/main.cpp b/main.cpp index 0037237..3703395 100644 --- a/main.cpp +++ b/main.cpp @@ -29,6 +29,7 @@ glm::vec3 camUp = glm::vec3(0.0f, 1.0f, 0.0f); float yaw = 1.57, pitch = 0; bool doScale, doRotate, doTranslate; Model *monkeyHead, *chest; +glm::vec3 chestPos(0); const int WIDTH = 800, HEIGHT = 600; const float ASPECT = (float)WIDTH / (float)HEIGHT; @@ -165,16 +166,31 @@ void display() { glUseProgram(solidProgId); setProjectionAndViewUniforms(solidProgId); setLightColorAndPos(solidProgId, lightPos, lightColor); + GLuint colorLoc = glGetUniformLocation(solidProgId, "color"); - glm::vec4 c(1.f, 0, 1.f, 1.f); + glm::vec4 c(1); glUniform4fv(colorLoc, 1, glm::value_ptr(c)); - GLuint modelLoc = glGetUniformLocation(solidProgId, "model"); - glm::mat4 model(1); - model = glm::scale(model, glm::vec3(2.0)); - glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); + + monkeyHead->getRoot()->model = glm::translate(glm::mat4(1), glm::vec3(2, 0, 0)); monkeyHead->draw(); - model = glm::translate(model, glm::vec3(3, 0, 0)); - glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); + + Model::Node *top = chest->find("top"); + top->model = glm::translate(glm::mat4(1), glm::vec3(0, 1, -1)); + top->model = glm::rotate(top->model, sin(d / 2.5f) * 0.5f, glm::vec3(1, 0, 0)); + top->model = glm::translate(top->model, glm::vec3(0, -1, 1)); + + Model::Node *jewels = chest->find("jewels"); + jewels->model = glm::scale(glm::mat4(1), glm::vec3((sin(d) + 1.2f) / 2.f)); + + Model::Node *lock = chest->find("lock"); + lock->model = glm::translate(glm::mat4(1), glm::vec3(0.22610, 3.36478, -0.75649)); + lock->model = glm::rotate(lock->model, (d / 2.5f), glm::vec3(0, 1, 0.4)); + lock->model = glm::translate(lock->model, -glm::vec3(0.22610, 3.36478, -0.75649)); + + Model::Node *key = chest->find("key"); + key->model = glm::translate(glm::mat4(1), glm::vec3(0, 0, sin(d))); + + chest->getRoot()->model = glm::translate(glm::mat4(1), chestPos); chest->draw(); /* drawTeapot(teapotCamera, false, d, lightPos, lightColor); */ @@ -491,10 +507,10 @@ void init() { validateProgram(solidProgId); monkeyHead = new Model("monkeyhead_smooth.dae", solidProgId); - chest = new Model("chest.obj", solidProgId); + chest = new Model("chest.dae", solidProgId); glEnable(GL_DEPTH_TEST); - /* glEnable(GL_CULL_FACE); */ + glEnable(GL_CULL_FACE); } bool* keyStates = new bool[256]; @@ -528,6 +544,11 @@ void timer(int _) { if (keyStates['e']) ySpeed = -0.1f; + if (keyStates['j']) chestPos.z += 0.1f; + if (keyStates['k']) chestPos.z -= 0.1f; + if (keyStates['h']) chestPos.x -= 0.1f; + if (keyStates['l']) chestPos.x += 0.1f; + camPos.x += xSpeed * sin(yaw) + zSpeed * cos(yaw); camPos.y += ySpeed; camPos.z += zSpeed * sin(yaw) - xSpeed * cos(yaw); diff --git a/model.cpp b/model.cpp index 29e3d8a..29f4415 100644 --- a/model.cpp +++ b/model.cpp @@ -1,10 +1,13 @@ #include "model.hpp" #include -#include -#include #include +#include Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) { + + std::vector vertices, normals; + std::vector texCoords; + for (int i = 0; i < aiMesh->mNumVertices; i++) { if (aiMesh->HasPositions()) { aiVector3D v = aiMesh->mVertices[i]; @@ -13,6 +16,8 @@ Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) { if (aiMesh->HasNormals()) { const aiVector3D v = aiMesh->mNormals[i]; normals.push_back(glm::vec3(v.x, v.y, v.z)); + } else { + normals.push_back(glm::vec3(0)); } // check for texture coord set 0 if (aiMesh->HasTextureCoords(0)) { @@ -21,6 +26,8 @@ Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) { } } + std::vector indices; + for (int i = 0; i < aiMesh->mNumFaces; i++) { const aiFace &face = aiMesh->mFaces[i]; if(face.mNumIndices == 3) { @@ -30,12 +37,14 @@ Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) { } } + numIndices = indices.size(); + glGenVertexArrays(1, &vao); glBindVertexArray(vao); GLuint vbos[3]; glGenBuffers(3, vbos); - vertexVbo = vbos[0], normalVbo = vbos[1], indicesVbo = vbos[2]; + GLuint vertexVbo = vbos[0], normalVbo = vbos[1], indicesVbo = vbos[2]; GLuint posLoc = glGetAttribLocation(progId, "vPosition"); GLuint normalLoc = glGetAttribLocation(progId, "vNormal"); @@ -54,10 +63,43 @@ Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) { glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), &indices[0], GL_STATIC_DRAW); }; -void Model::loadModel(const std::string &file) { - Assimp::Importer importer; - const aiScene *scene = importer.ReadFile(file, - aiProcess_Triangulate | aiProcess_PreTransformVertices | +Model::Node::Node(const aiNode &node, GLuint progId): ai(node), progId(progId) { + for (int i = 0; i < node.mNumMeshes; i++) { + meshIndices.push_back(node.mMeshes[i]); + } + for (int i = 0; i < node.mNumChildren; i++) { + const aiNode *child = node.mChildren[i]; + children.push_back(new Node(*child, progId)); + } +} + +glm::mat4 aiMatrixToMat4(aiMatrix4x4 from) { + glm::mat4 to; + for (int i = 0; i < 4; i++) + for (int j = 0; j < 4; j++) + to[i][j] = from[j][i]; + return to; +} + +void Model::Node::draw(const std::vector &meshes, glm::mat4 parentTrans = glm::mat4(1)) const { + GLuint modelLoc = glGetUniformLocation(progId, "model"); + glm::mat4 m = parentTrans * aiMatrixToMat4(ai.mTransformation) * model; + + for (unsigned int i: meshIndices) { + const Mesh &mesh = meshes[i]; + + glBindVertexArray(mesh.vao); + + glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(m)); + + glDrawElements(GL_TRIANGLES, mesh.numIndices, GL_UNSIGNED_INT, 0); + } + for (Node *child: children) child->draw(meshes, m); +} + +Model::Model(const std::string &path, GLuint progId): progId(progId) { + const aiScene *scene = importer.ReadFile(path, + aiProcess_Triangulate | aiProcess_GenNormals); if (!scene) { std::cerr << importer.GetErrorString() << std::endl; @@ -68,11 +110,36 @@ void Model::loadModel(const std::string &file) { const aiMesh *mesh = scene->mMeshes[i]; meshes.push_back(Mesh(mesh, progId)); } + + for (int i = 0; i < scene->mNumMaterials; i++) { + const aiMaterial &material = *scene->mMaterials[i]; + + for (int j = 0; j < material.GetTextureCount(aiTextureType_DIFFUSE); j++) { + aiString path; + material.GetTexture(aiTextureType_DIFFUSE, j, &path); + } + std::cout << material.GetTextureCount(aiTextureType_DIFFUSE) << std::endl; + std::cout << path.C_Str() << std::endl; } -void Model::draw() { - for (Mesh &mesh: meshes) { - glBindVertexArray(mesh.vao); - glDrawElements(GL_TRIANGLES, mesh.indices.size(), GL_UNSIGNED_INT, 0); + root = new Node(*(scene->mRootNode), progId); +} + +void Model::draw() const { + root->draw(meshes); +} + +Model::Node* Model::find(const std::string &name) { + const aiNode *node = root->ai.FindNode(aiString(name)); + Model::Node* res = root->findNode(*node); + return res; +} + +Model::Node* Model::Node::findNode(const aiNode &aiNode) { + if (&ai == &aiNode) return this; + for (Model::Node *child: children) { + Model::Node *res = child->findNode(aiNode); + if (res) return res; } + return nullptr; } diff --git a/model.hpp b/model.hpp index 0339538..98c630e 100644 --- a/model.hpp +++ b/model.hpp @@ -6,27 +6,44 @@ #endif #include #include +#include class Model { - public: - Model(const std::string &path, GLuint progId): progId(progId) { - loadModel(path); - } - void draw(); - private: - const GLuint progId; struct Mesh { Mesh(const aiMesh *aiMesh, GLuint progId); + GLuint vao; + GLuint numIndices; + }; - GLuint vao, vertexVbo, normalVbo, indicesVbo; + public: + Model(const std::string &path, GLuint progId); + void draw() const; + + class Node { + public: + Node(const aiNode &aiNode, GLuint progId); + void draw(const std::vector &meshes, glm::mat4 parentModel) const; + const std::vector &getChildren() const { return children; } + Node* findNode(const aiNode &aiNode); + glm::mat4 model = glm::mat4(1); + const aiNode &ai; + private: + const GLuint progId; - std::vector vertices; - std::vector normals; - std::vector texCoords; - std::vector indices; + std::vector children; + std::vector meshIndices; }; + Node* getRoot() { return root; } + Node* find(const std::string &name); + + private: + const GLuint progId; + std::vector meshes; + Node *root; void loadModel(const std::string &path); + + Assimp::Importer importer; };