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;
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 const 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 // 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));
27 texCoords.push_back(glm::vec2(0));
29 materialIndex = aiMesh->mMaterialIndex;
32 std::vector<GLuint> indices;
34 for (int i = 0; i < aiMesh->mNumFaces; i++) {
35 const aiFace &face = aiMesh->mFaces[i];
36 if(face.mNumIndices == 3) {
37 indices.push_back(face.mIndices[0]);
38 indices.push_back(face.mIndices[1]);
39 indices.push_back(face.mIndices[2]);
43 numIndices = indices.size();
45 glGenVertexArrays(1, &vao);
46 glBindVertexArray(vao);
49 glGenBuffers(4, vbos);
50 GLuint vertexVbo = vbos[0], normalVbo = vbos[1], texCoordVbo = vbos[2], indicesVbo = vbos[3];
52 GLuint posLoc = glGetAttribLocation(progId, "vPosition");
53 GLuint normalLoc = glGetAttribLocation(progId, "vNormal");
54 GLuint texCoordLoc = glGetAttribLocation(progId, "vTexCoord");
56 glBindBuffer(GL_ARRAY_BUFFER, vertexVbo);
57 glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
58 glEnableVertexAttribArray(posLoc);
59 glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
61 glBindBuffer(GL_ARRAY_BUFFER, normalVbo);
62 glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(glm::vec3), &normals[0], GL_STATIC_DRAW);
63 glEnableVertexAttribArray(normalLoc);
64 glVertexAttribPointer(normalLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
66 glBindBuffer(GL_ARRAY_BUFFER, texCoordVbo);
67 glBufferData(GL_ARRAY_BUFFER, texCoords.size() * sizeof(glm::vec2), &texCoords[0], GL_STATIC_DRAW);
68 glEnableVertexAttribArray(texCoordLoc);
69 glVertexAttribPointer(texCoordLoc, 2, GL_FLOAT, GL_FALSE, 0, 0);
71 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicesVbo);
72 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), &indices[0], GL_STATIC_DRAW);
75 Model::Node::Node(const aiNode &node, GLuint progId): ai(node), progId(progId) {
76 for (int i = 0; i < node.mNumMeshes; i++) {
77 meshIndices.push_back(node.mMeshes[i]);
79 for (int i = 0; i < node.mNumChildren; i++) {
80 const aiNode *child = node.mChildren[i];
81 children.push_back(new Node(*child, progId));
85 glm::mat4 aiMatrixToMat4(aiMatrix4x4 from) {
87 for (int i = 0; i < 4; i++)
88 for (int j = 0; j < 4; j++)
89 to[i][j] = from[j][i];
93 void Model::Node::draw( const std::vector<Mesh> &meshes,
94 const std::vector<Material> &materials,
95 glm::mat4 parentTrans = glm::mat4(1)) const {
97 GLuint modelLoc = glGetUniformLocation(progId, "model");
98 glm::mat4 m = parentTrans * aiMatrixToMat4(ai.mTransformation) * model;
100 for (unsigned int i: meshIndices) {
101 const Mesh &mesh = meshes[i];
102 glBindVertexArray(mesh.vao);
104 Material material = materials[mesh.materialIndex];
107 glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(m));
109 glDrawElements(GL_TRIANGLES, mesh.numIndices, GL_UNSIGNED_INT, 0);
111 for (Node *child: children) child->draw(meshes, materials, m);
114 Model::Model(const std::string &path, GLuint progId): progId(progId) {
115 const aiScene *scene = importer.ReadFile(path,
116 aiProcess_Triangulate |
117 aiProcess_GenNormals);
119 std::cerr << importer.GetErrorString() << std::endl;
123 for (int i = 0; i < scene->mNumMeshes; i++) {
124 const aiMesh *mesh = scene->mMeshes[i];
125 meshes.push_back(Mesh(mesh, progId));
128 for (unsigned int i = 0; i < scene->mNumMaterials; i++) {
129 const aiMaterial &material = *scene->mMaterials[i];
130 materials.push_back(Material(material, progId));
133 root = new Node(*(scene->mRootNode), progId);
136 void Model::draw() const {
137 root->draw(meshes, materials);
140 Model::Node* Model::find(const std::string &name) {
141 const aiNode *node = root->ai.FindNode(aiString(name));
142 Model::Node* res = root->findNode(*node);
146 Model::Node* Model::Node::findNode(const aiNode &aiNode) {
147 if (&ai == &aiNode) return this;
148 for (Model::Node *child: children) {
149 Model::Node *res = child->findNode(aiNode);