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));
29 std::vector<GLuint> indices;
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]);
40 numIndices = indices.size();
42 glGenVertexArrays(1, &vao);
43 glBindVertexArray(vao);
46 glGenBuffers(3, vbos);
47 GLuint vertexVbo = vbos[0], normalVbo = vbos[1], indicesVbo = vbos[2];
49 GLuint posLoc = glGetAttribLocation(progId, "vPosition");
50 GLuint normalLoc = glGetAttribLocation(progId, "vNormal");
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);
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);
62 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicesVbo);
63 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), &indices[0], GL_STATIC_DRAW);
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]);
70 for (int i = 0; i < node.mNumChildren; i++) {
71 const aiNode *child = node.mChildren[i];
72 children.push_back(new Node(*child, progId));
76 glm::mat4 aiMatrixToMat4(aiMatrix4x4 from) {
78 for (int i = 0; i < 4; i++)
79 for (int j = 0; j < 4; j++)
80 to[i][j] = from[j][i];
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;
88 for (unsigned int i: meshIndices) {
89 const Mesh &mesh = meshes[i];
91 glBindVertexArray(mesh.vao);
93 glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(m));
95 glDrawElements(GL_TRIANGLES, mesh.numIndices, GL_UNSIGNED_INT, 0);
97 for (Node *child: children) child->draw(meshes, m);
100 Model::Model(const std::string &path, GLuint progId): progId(progId) {
101 const aiScene *scene = importer.ReadFile(path,
102 aiProcess_Triangulate |
103 aiProcess_GenNormals);
105 std::cerr << importer.GetErrorString() << std::endl;
109 for (int i = 0; i < scene->mNumMeshes; i++) {
110 const aiMesh *mesh = scene->mMeshes[i];
111 meshes.push_back(Mesh(mesh, progId));
114 for (int i = 0; i < scene->mNumMaterials; i++) {
115 const aiMaterial &material = *scene->mMaterials[i];
117 for (int j = 0; j < material.GetTextureCount(aiTextureType_DIFFUSE); j++) {
119 material.GetTexture(aiTextureType_DIFFUSE, j, &path);
121 std::cout << material.GetTextureCount(aiTextureType_DIFFUSE) << std::endl;
122 std::cout << path.C_Str() << std::endl;
125 root = new Node(*(scene->mRootNode), progId);
128 void Model::draw() const {
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);
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);