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