Object hierarchies
[opengl.git] / model.cpp
index 29e3d8ae8f2889a65e4ca569046715649cfe1b1d..29f44158e4afc25e48c2b67699c8b42358dabf29 100644 (file)
--- a/model.cpp
+++ b/model.cpp
@@ -1,10 +1,13 @@
 #include "model.hpp"
 #include <iostream>
-#include <assimp/Importer.hpp>
-#include <assimp/scene.h>
 #include <assimp/postprocess.h>
+#include <glm/gtc/type_ptr.hpp>
 
 Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) {
+
+       std::vector<glm::vec3> vertices, normals;
+       std::vector<glm::vec2> 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<GLuint> 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<Mesh> &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;
 }