29f44158e4afc25e48c2b67699c8b42358dabf29
[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                 }
27         }
28
29         std::vector<GLuint> indices;
30
31         for (int i = 0; i < aiMesh->mNumFaces; i++) {
32                 const aiFace &face = aiMesh->mFaces[i];
33                 if(face.mNumIndices == 3) {
34                         indices.push_back(face.mIndices[0]);
35                         indices.push_back(face.mIndices[1]);
36                         indices.push_back(face.mIndices[2]);
37                 }
38         }
39
40         numIndices = indices.size();
41         
42         glGenVertexArrays(1, &vao);
43         glBindVertexArray(vao);
44         
45         GLuint vbos[3];
46         glGenBuffers(3, vbos);
47         GLuint vertexVbo = vbos[0], normalVbo = vbos[1], indicesVbo = vbos[2];
48         
49         GLuint posLoc = glGetAttribLocation(progId, "vPosition");
50         GLuint normalLoc = glGetAttribLocation(progId, "vNormal");
51         
52         glBindBuffer(GL_ARRAY_BUFFER, vertexVbo);
53         glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
54         glEnableVertexAttribArray(posLoc);
55         glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
56         
57         glBindBuffer(GL_ARRAY_BUFFER, normalVbo);
58         glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(glm::vec3), &normals[0], GL_STATIC_DRAW);
59         glEnableVertexAttribArray(normalLoc);
60         glVertexAttribPointer(normalLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
61
62         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicesVbo);
63         glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), &indices[0], GL_STATIC_DRAW);
64 };
65
66 Model::Node::Node(const aiNode &node, GLuint progId): ai(node), progId(progId) {
67         for (int i = 0; i < node.mNumMeshes; i++) {
68                 meshIndices.push_back(node.mMeshes[i]);
69         }
70         for (int i = 0; i < node.mNumChildren; i++) {
71                 const aiNode *child = node.mChildren[i];
72                 children.push_back(new Node(*child, progId));
73         }
74 }
75
76 glm::mat4 aiMatrixToMat4(aiMatrix4x4 from) {
77         glm::mat4 to;
78         for (int i = 0; i < 4; i++)
79                 for (int j = 0; j < 4; j++)
80                         to[i][j] = from[j][i];
81         return to;
82 }
83
84 void Model::Node::draw(const std::vector<Mesh> &meshes, glm::mat4 parentTrans = glm::mat4(1)) const {
85         GLuint modelLoc = glGetUniformLocation(progId, "model");
86         glm::mat4 m = parentTrans * aiMatrixToMat4(ai.mTransformation) * model;
87         
88         for (unsigned int i: meshIndices) {
89                 const Mesh &mesh = meshes[i];
90
91                 glBindVertexArray(mesh.vao);
92                 
93                 glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(m));
94
95                 glDrawElements(GL_TRIANGLES, mesh.numIndices, GL_UNSIGNED_INT, 0);
96         }
97         for (Node *child: children) child->draw(meshes, m);
98 }
99
100 Model::Model(const std::string &path, GLuint progId): progId(progId) {
101         const aiScene *scene = importer.ReadFile(path, 
102                         aiProcess_Triangulate |
103                         aiProcess_GenNormals);
104         if (!scene) {
105                 std::cerr << importer.GetErrorString() << std::endl;
106                 exit(1);
107         }
108
109         for (int i = 0; i < scene->mNumMeshes; i++) {
110                 const aiMesh *mesh = scene->mMeshes[i];
111                 meshes.push_back(Mesh(mesh, progId));
112         }
113
114         for (int i = 0; i < scene->mNumMaterials; i++) {
115                 const aiMaterial &material = *scene->mMaterials[i];
116
117                 for (int j = 0; j < material.GetTextureCount(aiTextureType_DIFFUSE); j++) {
118                         aiString path;
119                         material.GetTexture(aiTextureType_DIFFUSE, j, &path);
120                 }
121                 std::cout << material.GetTextureCount(aiTextureType_DIFFUSE) << std::endl;
122                 std::cout << path.C_Str() << std::endl;
123         }
124
125         root = new Node(*(scene->mRootNode), progId);
126 }
127
128 void Model::draw() const {
129         root->draw(meshes);
130 }
131
132 Model::Node* Model::find(const std::string &name) {
133         const aiNode *node = root->ai.FindNode(aiString(name));
134         Model::Node* res = root->findNode(*node);
135         return res;
136 }
137
138 Model::Node* Model::Node::findNode(const aiNode &aiNode) {
139         if (&ai == &aiNode) return this;
140         for (Model::Node *child: children) {
141                 Model::Node *res = child->findNode(aiNode);
142                 if (res) return res;
143         }
144         return nullptr;
145 }