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 normals.push_back(glm::vec3(0));
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));
28 tangents.push_back(glm::vec3(0));
29 bitangents.push_back(glm::vec3(0));
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));
36 texCoords.push_back(glm::vec2(0));
38 materialIndex = aiMesh->mMaterialIndex;
41 std::vector<GLuint> indices;
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]);
52 numIndices = indices.size();
54 glGenVertexArrays(1, &vao);
55 glBindVertexArray(vao);
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];
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);
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);
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);
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);
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);
92 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicesVbo);
93 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), &indices[0], GL_STATIC_DRAW);
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]);
100 for (int i = 0; i < node.mNumChildren; i++) {
101 const aiNode *child = node.mChildren[i];
102 children.push_back(new Node(*child, progId));
106 glm::mat4 aiMatrixToMat4(aiMatrix4x4 from) {
108 for (int i = 0; i < 4; i++)
109 for (int j = 0; j < 4; j++)
110 to[i][j] = from[j][i];
114 void Model::Node::draw( const std::vector<Mesh> &meshes,
115 const std::vector<Material> &materials,
116 glm::mat4 parentTrans = glm::mat4(1)) const {
118 GLuint modelLoc = glGetUniformLocation(progId, "model");
119 glm::mat4 m = parentTrans * aiMatrixToMat4(ai.mTransformation) * model;
121 for (unsigned int i: meshIndices) {
122 const Mesh &mesh = meshes[i];
123 glBindVertexArray(mesh.vao);
125 Material material = materials[mesh.materialIndex];
128 glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(m));
130 glDrawElements(GL_TRIANGLES, mesh.numIndices, GL_UNSIGNED_INT, 0);
132 for (Node *child: children) child->draw(meshes, materials, m);
135 Model::Model(const std::string &path, Program p): program(p) {
136 const aiScene *scene = importer.ReadFile(path,
137 aiProcess_Triangulate | aiProcess_CalcTangentSpace | aiProcess_GenNormals);
139 std::cerr << importer.GetErrorString() << std::endl;
143 for (int i = 0; i < scene->mNumMeshes; i++) {
144 const aiMesh *mesh = scene->mMeshes[i];
145 meshes.push_back(Mesh(mesh, p.progId));
148 for (unsigned int i = 0; i < scene->mNumMaterials; i++) {
149 const aiMaterial &material = *scene->mMaterials[i];
150 materials.push_back(Material(material, p.progId));
153 root = new Node(*(scene->mRootNode), p.progId);
156 void Model::draw() const {
157 glUseProgram(program.progId);
158 root->draw(meshes, materials);
161 Model::Node* Model::find(const std::string &name) {
162 const aiNode *node = root->ai.FindNode(aiString(name));
163 Model::Node* res = root->findNode(*node);
167 Model::Node* Model::Node::findNode(const aiNode &aiNode) {
168 if (&ai == &aiNode) return this;
169 for (Model::Node *child: children) {
170 Model::Node *res = child->findNode(aiNode);