2e01fc098fb408e172e8a46f25176d678855edc2
[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                         std::cerr << "Missing tangents: make sure blender has UV maps" << std::endl;
29                         exit(1);
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, "pos");
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, "unscaledNormal");
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                                                 const Skybox skybox,
117                                                 glm::mat4 parentTrans = glm::mat4(1)) const {
118
119         GLuint modelLoc = glGetUniformLocation(progId, "model");
120         glm::mat4 m = parentTrans * aiMatrixToMat4(ai.mTransformation) * model;
121         
122         for (unsigned int i: meshIndices) {
123                 const Mesh &mesh = meshes[i];
124                 glBindVertexArray(mesh.vao);
125
126                 Material material = materials[mesh.materialIndex];
127                 material.bind();
128                 
129                 glUniform1i(glGetUniformLocation(progId, "skybox"), 5);
130                 glActiveTexture(GL_TEXTURE5);
131                 glBindTexture(GL_TEXTURE_CUBE_MAP, skybox.getTexture());
132                 
133                 glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(m));
134
135                 glDrawElements(GL_TRIANGLES, mesh.numIndices, GL_UNSIGNED_INT, 0);
136         }
137         for (Node *child: children) child->draw(meshes, materials, skybox, m);
138 }
139
140 Model::Model(const std::string &path, Program p, Skybox s): program(p), skybox(s) {
141         glUseProgram(p.progId);
142         
143         const aiScene *scene = importer.ReadFile(path, 
144                         aiProcess_Triangulate | aiProcess_CalcTangentSpace | aiProcess_GenNormals);
145         if (!scene) {
146                 std::cerr << importer.GetErrorString() << std::endl;
147                 exit(1);
148         }
149
150         for (int i = 0; i < scene->mNumMeshes; i++) {
151                 const aiMesh *mesh = scene->mMeshes[i];
152                 meshes.push_back(Mesh(mesh, p.progId));
153         }
154
155         for (unsigned int i = 0; i < scene->mNumMaterials; i++) {
156                 const aiMaterial &material = *scene->mMaterials[i];
157                 materials.push_back(Material(material, p.progId));
158         }
159
160         root = new Node(*(scene->mRootNode), p.progId);
161 }
162
163 void Model::draw() const {
164         glUseProgram(program.progId);
165         root->draw(meshes, materials, skybox);
166         program.validate();
167 }
168
169 Model::Node* Model::find(const std::string &name) {
170         const aiNode *node = root->ai.FindNode(aiString(name));
171         Model::Node* res = root->findNode(*node);
172         return res;
173 }
174
175 Model::Node* Model::Node::findNode(const aiNode &aiNode) {
176         if (&ai == &aiNode) return this;
177         for (Model::Node *child: children) {
178                 Model::Node *res = child->findNode(aiNode);
179                 if (res) return res;
180         }
181         return nullptr;
182 }