Add basics of materials
[opengl.git] / model.cpp
index 29f44158e4afc25e48c2b67699c8b42358dabf29..4c5e2d33278f0120a72f1c07d2c72813bf20b713 100644 (file)
--- a/model.cpp
+++ b/model.cpp
@@ -23,7 +23,10 @@ Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) {
                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;
@@ -42,12 +45,13 @@ Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) {
        glGenVertexArrays(1, &vao);
        glBindVertexArray(vao);
        
-       GLuint vbos[3];
-       glGenBuffers(3, vbos);
-       GLuint vertexVbo = vbos[0], normalVbo = vbos[1], indicesVbo = vbos[2];
+       GLuint vbos[4];
+       glGenBuffers(4, vbos);
+       GLuint vertexVbo = vbos[0], normalVbo = vbos[1], texCoordVbo = vbos[2], indicesVbo = vbos[3];
        
        GLuint posLoc = glGetAttribLocation(progId, "vPosition");
        GLuint normalLoc = glGetAttribLocation(progId, "vNormal");
+       GLuint texCoordLoc = glGetAttribLocation(progId, "vTexCoord");
        
        glBindBuffer(GL_ARRAY_BUFFER, vertexVbo);
        glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
@@ -59,6 +63,11 @@ Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) {
        glEnableVertexAttribArray(normalLoc);
        glVertexAttribPointer(normalLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
 
+       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);
+
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicesVbo);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), &indices[0], GL_STATIC_DRAW);
 };
@@ -81,20 +90,25 @@ glm::mat4 aiMatrixToMat4(aiMatrix4x4 from) {
        return to;
 }
 
-void Model::Node::draw(const std::vector<Mesh> &meshes, glm::mat4 parentTrans = glm::mat4(1)) const {
+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, m);
+       for (Node *child: children) child->draw(meshes, materials, m);
 }
 
 Model::Model(const std::string &path, GLuint progId): progId(progId) {
@@ -111,22 +125,16 @@ Model::Model(const std::string &path, GLuint progId): progId(progId) {
                meshes.push_back(Mesh(mesh, progId));
        }
 
-       for (int i = 0; i < scene->mNumMaterials; i++) {
+       for (unsigned 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;
+               materials.push_back(Material(material, progId));
        }
 
        root = new Node(*(scene->mRootNode), progId);
 }
 
 void Model::draw() const {
-       root->draw(meshes);
+       root->draw(meshes, materials);
 }
 
 Model::Node* Model::find(const std::string &name) {