Tidy up
[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, tangents, bitangents;
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                         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                 if (aiMesh->HasTangentsAndBitangents()) {
23                         aiVector3D t = aiMesh->mTangents[i];
24                         tangents.push_back(glm::vec3(t.x, t.y, t.z));
25                         aiVector3D b = aiMesh->mBitangents[i];
26                         bitangents.push_back(glm::vec3(b.x, b.y, b.z));
27                 } else {
28                         tangents.push_back(glm::vec3(0));
29                         bitangents.push_back(glm::vec3(0));
30                 }
31                 // check for texture coord set 0
32                 if (aiMesh->HasTextureCoords(0)) {
33                         const aiVector3D v = aiMesh->mTextureCoords[0][i];
34                         texCoords.push_back(glm::vec2(v.x, v.y));
35                 } else {
36                         texCoords.push_back(glm::vec2(0));
37                 }
38                 materialIndex = aiMesh->mMaterialIndex;
39         }
40
41         std::vector<GLuint> indices;
42
43         for (int i = 0; i < aiMesh->mNumFaces; i++) {
44                 const aiFace &face = aiMesh->mFaces[i];
45                 if(face.mNumIndices == 3) {
46                         indices.push_back(face.mIndices[0]);
47                         indices.push_back(face.mIndices[1]);
48                         indices.push_back(face.mIndices[2]);
49                 }
50         } 
51         
52         numIndices = indices.size();
53         
54         glGenVertexArrays(1, &vao);
55         glBindVertexArray(vao);
56         
57         GLuint vbos[6];
58         glGenBuffers(6, vbos);
59         GLuint vertexVbo = vbos[0], normalVbo = vbos[1], texCoordVbo = vbos[2], indicesVbo = vbos[3];
60         GLuint tangentVbo = vbos[4], bitangentVbo = vbos[5];
61         
62         GLuint posLoc = glGetAttribLocation(progId, "vPosition");
63         glBindBuffer(GL_ARRAY_BUFFER, vertexVbo);
64         glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
65         glEnableVertexAttribArray(posLoc);
66         glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
67         
68         GLuint normalLoc = glGetAttribLocation(progId, "vNormal");
69         glBindBuffer(GL_ARRAY_BUFFER, normalVbo);
70         glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(glm::vec3), &normals[0], GL_STATIC_DRAW);
71         glEnableVertexAttribArray(normalLoc);
72         glVertexAttribPointer(normalLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
73
74         GLuint texCoordLoc = glGetAttribLocation(progId, "vTexCoord");
75         glBindBuffer(GL_ARRAY_BUFFER, texCoordVbo);
76         glBufferData(GL_ARRAY_BUFFER, texCoords.size() * sizeof(glm::vec2), &texCoords[0], GL_STATIC_DRAW);
77         glEnableVertexAttribArray(texCoordLoc);
78         glVertexAttribPointer(texCoordLoc, 2, GL_FLOAT, GL_FALSE, 0, 0);
79
80         GLuint tangentLoc = glGetAttribLocation(progId, "tangent");
81         glBindBuffer(GL_ARRAY_BUFFER, tangentVbo);
82         glBufferData(GL_ARRAY_BUFFER, tangents.size() * sizeof(glm::vec3), &tangents[0], GL_STATIC_DRAW);
83         glEnableVertexAttribArray(tangentLoc);
84         glVertexAttribPointer(tangentLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
85
86         GLuint bitangentLoc = glGetAttribLocation(progId, "bitangent");
87         glBindBuffer(GL_ARRAY_BUFFER, bitangentVbo);
88         glBufferData(GL_ARRAY_BUFFER, bitangents.size() * sizeof(glm::vec3), &bitangents[0], GL_STATIC_DRAW);
89         glEnableVertexAttribArray(bitangentLoc);
90         glVertexAttribPointer(bitangentLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
91
92         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicesVbo);
93         glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), &indices[0], GL_STATIC_DRAW);
94 };
95
96 Model::Node::Node(const aiNode &node, GLuint progId): ai(node), progId(progId) {
97         for (int i = 0; i < node.mNumMeshes; i++) {
98                 meshIndices.push_back(node.mMeshes[i]);
99         }
100         for (int i = 0; i < node.mNumChildren; i++) {
101                 const aiNode *child = node.mChildren[i];
102                 children.push_back(new Node(*child, progId));
103         }
104 }
105
106 glm::mat4 aiMatrixToMat4(aiMatrix4x4 from) {
107         glm::mat4 to;
108         for (int i = 0; i < 4; i++)
109                 for (int j = 0; j < 4; j++)
110                         to[i][j] = from[j][i];
111         return to;
112 }
113
114 void Model::Node::draw( const std::vector<Mesh> &meshes,
115                                                 const std::vector<Material> &materials,
116                                                 glm::mat4 parentTrans = glm::mat4(1)) const {
117
118         GLuint modelLoc = glGetUniformLocation(progId, "model");
119         glm::mat4 m = parentTrans * aiMatrixToMat4(ai.mTransformation) * model;
120         
121         for (unsigned int i: meshIndices) {
122                 const Mesh &mesh = meshes[i];
123                 glBindVertexArray(mesh.vao);
124
125                 Material material = materials[mesh.materialIndex];
126                 material.bind();
127                 
128                 glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(m));
129
130                 glDrawElements(GL_TRIANGLES, mesh.numIndices, GL_UNSIGNED_INT, 0);
131         }
132         for (Node *child: children) child->draw(meshes, materials, m);
133 }
134
135 Model::Model(const std::string &path, GLuint progId): progId(progId) {
136         const aiScene *scene = importer.ReadFile(path, 
137                         aiProcess_Triangulate | aiProcess_CalcTangentSpace | aiProcess_GenNormals);
138         if (!scene) {
139                 std::cerr << importer.GetErrorString() << std::endl;
140                 exit(1);
141         }
142
143         for (int i = 0; i < scene->mNumMeshes; i++) {
144                 const aiMesh *mesh = scene->mMeshes[i];
145                 meshes.push_back(Mesh(mesh, progId));
146         }
147
148         for (unsigned int i = 0; i < scene->mNumMaterials; i++) {
149                 const aiMaterial &material = *scene->mMaterials[i];
150                 materials.push_back(Material(material, progId));
151         }
152
153         root = new Node(*(scene->mRootNode), progId);
154 }
155
156 void Model::draw() const {
157         root->draw(meshes, materials);
158 }
159
160 Model::Node* Model::find(const std::string &name) {
161         const aiNode *node = root->ai.FindNode(aiString(name));
162         Model::Node* res = root->findNode(*node);
163         return res;
164 }
165
166 Model::Node* Model::Node::findNode(const aiNode &aiNode) {
167         if (&ai == &aiNode) return this;
168         for (Model::Node *child: children) {
169                 Model::Node *res = child->findNode(aiNode);
170                 if (res) return res;
171         }
172         return nullptr;
173 }