Add basics of materials
[opengl.git] / model.cpp
1 #include "model.hpp"
2 #include <iostream>
3 #include <assimp/postprocess.h>
4 #include <glm/gtc/type_ptr.hpp>
5
6 Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) {
7
8         std::vector<glm::vec3> vertices, normals;
9         std::vector<glm::vec2> texCoords;
10
11         for (int i = 0; i < aiMesh->mNumVertices; i++) {
12                 if (aiMesh->HasPositions()) {
13                         aiVector3D v = aiMesh->mVertices[i];
14                         vertices.push_back(glm::vec3(v.x, v.y, v.z));
15                 }
16                 if (aiMesh->HasNormals()) {
17                         const aiVector3D v = aiMesh->mNormals[i];
18                         normals.push_back(glm::vec3(v.x, v.y, v.z));
19                 } else {
20                         normals.push_back(glm::vec3(0));
21                 }
22                 // check for texture coord set 0
23                 if (aiMesh->HasTextureCoords(0)) {
24                         const aiVector3D v = aiMesh->mTextureCoords[0][i];
25                         texCoords.push_back(glm::vec2(v.x, v.y));
26                 } else {
27                         texCoords.push_back(glm::vec2(0));
28                 }
29                 materialIndex = aiMesh->mMaterialIndex;
30         }
31
32         std::vector<GLuint> indices;
33
34         for (int i = 0; i < aiMesh->mNumFaces; i++) {
35                 const aiFace &face = aiMesh->mFaces[i];
36                 if(face.mNumIndices == 3) {
37                         indices.push_back(face.mIndices[0]);
38                         indices.push_back(face.mIndices[1]);
39                         indices.push_back(face.mIndices[2]);
40                 }
41         }
42
43         numIndices = indices.size();
44         
45         glGenVertexArrays(1, &vao);
46         glBindVertexArray(vao);
47         
48         GLuint vbos[4];
49         glGenBuffers(4, vbos);
50         GLuint vertexVbo = vbos[0], normalVbo = vbos[1], texCoordVbo = vbos[2], indicesVbo = vbos[3];
51         
52         GLuint posLoc = glGetAttribLocation(progId, "vPosition");
53         GLuint normalLoc = glGetAttribLocation(progId, "vNormal");
54         GLuint texCoordLoc = glGetAttribLocation(progId, "vTexCoord");
55         
56         glBindBuffer(GL_ARRAY_BUFFER, vertexVbo);
57         glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
58         glEnableVertexAttribArray(posLoc);
59         glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
60         
61         glBindBuffer(GL_ARRAY_BUFFER, normalVbo);
62         glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(glm::vec3), &normals[0], GL_STATIC_DRAW);
63         glEnableVertexAttribArray(normalLoc);
64         glVertexAttribPointer(normalLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
65
66         glBindBuffer(GL_ARRAY_BUFFER, texCoordVbo);
67         glBufferData(GL_ARRAY_BUFFER, texCoords.size() * sizeof(glm::vec2), &texCoords[0], GL_STATIC_DRAW);
68         glEnableVertexAttribArray(texCoordLoc);
69         glVertexAttribPointer(texCoordLoc, 2, GL_FLOAT, GL_FALSE, 0, 0);
70
71         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicesVbo);
72         glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), &indices[0], GL_STATIC_DRAW);
73 };
74
75 Model::Node::Node(const aiNode &node, GLuint progId): ai(node), progId(progId) {
76         for (int i = 0; i < node.mNumMeshes; i++) {
77                 meshIndices.push_back(node.mMeshes[i]);
78         }
79         for (int i = 0; i < node.mNumChildren; i++) {
80                 const aiNode *child = node.mChildren[i];
81                 children.push_back(new Node(*child, progId));
82         }
83 }
84
85 glm::mat4 aiMatrixToMat4(aiMatrix4x4 from) {
86         glm::mat4 to;
87         for (int i = 0; i < 4; i++)
88                 for (int j = 0; j < 4; j++)
89                         to[i][j] = from[j][i];
90         return to;
91 }
92
93 void Model::Node::draw( const std::vector<Mesh> &meshes,
94                                                 const std::vector<Material> &materials,
95                                                 glm::mat4 parentTrans = glm::mat4(1)) const {
96
97         GLuint modelLoc = glGetUniformLocation(progId, "model");
98         glm::mat4 m = parentTrans * aiMatrixToMat4(ai.mTransformation) * model;
99         
100         for (unsigned int i: meshIndices) {
101                 const Mesh &mesh = meshes[i];
102                 glBindVertexArray(mesh.vao);
103
104                 Material material = materials[mesh.materialIndex];
105                 material.bind();
106                 
107                 glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(m));
108
109                 glDrawElements(GL_TRIANGLES, mesh.numIndices, GL_UNSIGNED_INT, 0);
110         }
111         for (Node *child: children) child->draw(meshes, materials, m);
112 }
113
114 Model::Model(const std::string &path, GLuint progId): progId(progId) {
115         const aiScene *scene = importer.ReadFile(path, 
116                         aiProcess_Triangulate |
117                         aiProcess_GenNormals);
118         if (!scene) {
119                 std::cerr << importer.GetErrorString() << std::endl;
120                 exit(1);
121         }
122
123         for (int i = 0; i < scene->mNumMeshes; i++) {
124                 const aiMesh *mesh = scene->mMeshes[i];
125                 meshes.push_back(Mesh(mesh, progId));
126         }
127
128         for (unsigned int i = 0; i < scene->mNumMaterials; i++) {
129                 const aiMaterial &material = *scene->mMaterials[i];
130                 materials.push_back(Material(material, progId));
131         }
132
133         root = new Node(*(scene->mRootNode), progId);
134 }
135
136 void Model::draw() const {
137         root->draw(meshes, materials);
138 }
139
140 Model::Node* Model::find(const std::string &name) {
141         const aiNode *node = root->ai.FindNode(aiString(name));
142         Model::Node* res = root->findNode(*node);
143         return res;
144 }
145
146 Model::Node* Model::Node::findNode(const aiNode &aiNode) {
147         if (&ai == &aiNode) return this;
148         for (Model::Node *child: children) {
149                 Model::Node *res = child->findNode(aiNode);
150                 if (res) return res;
151         }
152         return nullptr;
153 }