3 #include <assimp/postprocess.h>
4 #include <glm/gtc/type_ptr.hpp>
6 Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) {
8 std::vector<glm::vec3> vertices, normals, tangents, bitangents;
9 std::vector<glm::vec2> texCoords;
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));
16 if (aiMesh->HasNormals()) {
17 aiVector3D v = aiMesh->mNormals[i];
18 normals.push_back(glm::vec3(v.x, v.y, v.z));
20 std::cerr << "Missing normals" << std::endl;
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));
29 std::cerr << "Missing tangents: make sure blender has UV maps" << std::endl;
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));
37 texCoords.push_back(glm::vec2(0));
39 materialIndex = aiMesh->mMaterialIndex;
42 std::vector<GLuint> indices;
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]);
53 numIndices = indices.size();
55 glGenVertexArrays(1, &vao);
56 glBindVertexArray(vao);
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];
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);
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);
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);
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); */
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); */
93 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicesVbo);
94 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), &indices[0], GL_STATIC_DRAW);
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]);
101 for (int i = 0; i < node.mNumChildren; i++) {
102 const aiNode *child = node.mChildren[i];
103 children.push_back(new Node(*child, progId));
107 glm::mat4 aiMatrixToMat4(aiMatrix4x4 from) {
109 for (int i = 0; i < 4; i++)
110 for (int j = 0; j < 4; j++)
111 to[i][j] = from[j][i];
115 void Model::Node::draw( const std::vector<Mesh> &meshes,
116 const std::vector<Material> &materials,
118 glm::mat4 parentTrans = glm::mat4(1)) const {
120 GLuint modelLoc = glGetUniformLocation(progId, "model");
121 glm::mat4 m = parentTrans * aiMatrixToMat4(ai.mTransformation) * model;
123 for (unsigned int i: meshIndices) {
124 const Mesh &mesh = meshes[i];
125 glBindVertexArray(mesh.vao);
127 Material material = materials[mesh.materialIndex];
130 glUniform1i(glGetUniformLocation(progId, "irradianceMap"), 4);
131 glActiveTexture(GL_TEXTURE4);
132 glBindTexture(GL_TEXTURE_CUBE_MAP, skybox.getIrradianceMap());
134 glUniform1i(glGetUniformLocation(progId, "prefilterMap"), 5);
135 glActiveTexture(GL_TEXTURE5);
136 glBindTexture(GL_TEXTURE_CUBE_MAP, skybox.getPrefilterMap());
138 glUniform1i(glGetUniformLocation(progId, "brdfMap"), 6);
139 glActiveTexture(GL_TEXTURE6);
140 glBindTexture(GL_TEXTURE_2D, skybox.getBRDFMap());
142 glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(m));
144 glDrawElements(GL_TRIANGLES, mesh.numIndices, GL_UNSIGNED_INT, 0);
146 for (Node *child: children) child->draw(meshes, materials, skybox, m);
149 Model::Model(const std::string &path, Program p, Skybox s): program(p), skybox(s) {
150 glUseProgram(p.progId);
152 const aiScene *scene = importer.ReadFile(path,
153 aiProcess_Triangulate | aiProcess_CalcTangentSpace | aiProcess_GenNormals);
155 std::cerr << importer.GetErrorString() << std::endl;
159 for (int i = 0; i < scene->mNumMeshes; i++) {
160 const aiMesh *mesh = scene->mMeshes[i];
161 meshes.push_back(Mesh(mesh, p.progId));
164 for (unsigned int i = 0; i < scene->mNumMaterials; i++) {
165 const aiMaterial &material = *scene->mMaterials[i];
166 materials.push_back(Material(material, p.progId));
169 root = new Node(*(scene->mRootNode), p.progId);
172 void Model::draw() const {
173 glUseProgram(program.progId);
174 root->draw(meshes, materials, skybox);
178 Model::Node* Model::find(const std::string &name) {
179 const aiNode *node = root->ai.FindNode(aiString(name));
180 Model::Node* res = root->findNode(*node);
184 Model::Node* Model::Node::findNode(const aiNode &aiNode) {
185 if (&ai == &aiNode) return this;
186 for (Model::Node *child: children) {
187 Model::Node *res = child->findNode(aiNode);