Stuff
[opengl.git] / model.cpp
index 29e3d8ae8f2889a65e4ca569046715649cfe1b1d..8046e29434fcf467b29942dc56aa5b60b76b84cf 100644 (file)
--- a/model.cpp
+++ b/model.cpp
@@ -1,26 +1,45 @@
 #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, tangents, bitangents;
+       std::vector<glm::vec2> texCoords;
+
        for (int i = 0; i < aiMesh->mNumVertices; i++) {
                if (aiMesh->HasPositions()) {
                        aiVector3D v = aiMesh->mVertices[i];
                        vertices.push_back(glm::vec3(v.x, v.y, v.z));
                }
                if (aiMesh->HasNormals()) {
-                       const aiVector3D v = aiMesh->mNormals[i];
+                       aiVector3D v = aiMesh->mNormals[i];
                        normals.push_back(glm::vec3(v.x, v.y, v.z));
+               } else {
+                       normals.push_back(glm::vec3(0));
+               }
+               if (aiMesh->HasTangentsAndBitangents()) {
+                       aiVector3D t = aiMesh->mTangents[i];
+                       tangents.push_back(glm::vec3(t.x, t.y, t.z));
+                       aiVector3D b = aiMesh->mBitangents[i];
+                       bitangents.push_back(glm::vec3(b.x, b.y, b.z));
+               } else {
+                       tangents.push_back(glm::vec3(0));
+                       bitangents.push_back(glm::vec3(0));
                }
                // check for texture coord set 0
                if (aiMesh->HasTextureCoords(0)) {
                        const aiVector3D v = aiMesh->mTextureCoords[0][i];
                        texCoords.push_back(glm::vec2(v.x, v.y));
+               } else {
+                       texCoords.push_back(glm::vec2(0));
                }
+               materialIndex = aiMesh->mMaterialIndex;
        }
 
+       std::vector<GLuint> indices;
+
        for (int i = 0; i < aiMesh->mNumFaces; i++) {
                const aiFace &face = aiMesh->mFaces[i];
                if(face.mNumIndices == 3) {
@@ -30,35 +49,92 @@ 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 vbos[6];
+       glGenBuffers(6, vbos);
+       GLuint vertexVbo = vbos[0], normalVbo = vbos[1], texCoordVbo = vbos[2], indicesVbo = vbos[3];
+       GLuint tangentVbo = vbos[4], bitangentVbo = vbos[5];
        
        GLuint posLoc = glGetAttribLocation(progId, "vPosition");
-       GLuint normalLoc = glGetAttribLocation(progId, "vNormal");
-       
        glBindBuffer(GL_ARRAY_BUFFER, vertexVbo);
        glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
        glEnableVertexAttribArray(posLoc);
        glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
        
+       GLuint normalLoc = glGetAttribLocation(progId, "vNormal");
        glBindBuffer(GL_ARRAY_BUFFER, normalVbo);
        glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(glm::vec3), &normals[0], GL_STATIC_DRAW);
        glEnableVertexAttribArray(normalLoc);
        glVertexAttribPointer(normalLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
 
+       GLuint texCoordLoc = glGetAttribLocation(progId, "vTexCoord");
+       glBindBuffer(GL_ARRAY_BUFFER, texCoordVbo);
+       glBufferData(GL_ARRAY_BUFFER, texCoords.size() * sizeof(glm::vec2), &texCoords[0], GL_STATIC_DRAW);
+       glEnableVertexAttribArray(texCoordLoc);
+       glVertexAttribPointer(texCoordLoc, 2, GL_FLOAT, GL_FALSE, 0, 0);
+
+       GLuint tangentLoc = glGetAttribLocation(progId, "tangent");
+       glBindBuffer(GL_ARRAY_BUFFER, tangentVbo);
+       glBufferData(GL_ARRAY_BUFFER, tangents.size() * sizeof(glm::vec3), &tangents[0], GL_STATIC_DRAW);
+       glEnableVertexAttribArray(tangentLoc);
+       glVertexAttribPointer(tangentLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
+
+       GLuint bitangentLoc = glGetAttribLocation(progId, "bitangent");
+       glBindBuffer(GL_ARRAY_BUFFER, bitangentVbo);
+       glBufferData(GL_ARRAY_BUFFER, bitangents.size() * sizeof(glm::vec3), &bitangents[0], GL_STATIC_DRAW);
+       glEnableVertexAttribArray(bitangentLoc);
+       glVertexAttribPointer(bitangentLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
+
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicesVbo);
        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 |
-                       aiProcess_GenNormals);
+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,
+                                               const std::vector<Material> &materials,
+                                               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);
+
+               Material material = materials[mesh.materialIndex];
+               material.bind();
+               
+               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, materials, m);
+}
+
+Model::Model(const std::string &path, GLuint progId): progId(progId) {
+       const aiScene *scene = importer.ReadFile(path, 
+                       aiProcess_Triangulate | aiProcess_CalcTangentSpace | aiProcess_GenNormals);
        if (!scene) {
                std::cerr << importer.GetErrorString() << std::endl;
                exit(1);
@@ -68,11 +144,30 @@ void Model::loadModel(const std::string &file) {
                const aiMesh *mesh = scene->mMeshes[i];
                meshes.push_back(Mesh(mesh, progId));
        }
+
+       for (unsigned int i = 0; i < scene->mNumMaterials; i++) {
+               const aiMaterial &material = *scene->mMaterials[i];
+               materials.push_back(Material(material, progId));
        }
 
-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, materials);
+}
+
+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;
 }