Advertisement
Kaine4855

C++ Assimp Model Loading Code

Mar 19th, 2018
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.72 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <GLFW\glfw3.h>
  4. #include <glad\glad.h>
  5. #include <string>
  6. #include <fstream>
  7. #include <sstream>
  8. #include <iostream>
  9. #include <map>
  10. #include <vector>
  11. #include <glm\glm.hpp>
  12. #include "lodepng.h"
  13. #include <assimp\Importer.hpp>
  14. #include <assimp\scene.h>
  15. #include <assimp\postprocess.h>
  16. #include "Models.h"
  17.  
  18. struct Vertex
  19. {
  20.     glm::vec3 position;
  21.     glm::vec3 normal;
  22.     glm::vec2 uv;
  23. };
  24.  
  25. static Mesh process_mesh(aiMesh *mesh, const aiScene *scene)
  26. {
  27.     std::vector<Vertex> vertices;
  28.     std::vector<GLuint> indices;
  29.  
  30.     for (GLuint i = 0; i < mesh->mNumVertices; i++)
  31.     {
  32.         Vertex vertex;
  33.         glm::vec3 vector;
  34.  
  35.         vector.x = mesh->mVertices[i].x;
  36.         vector.y = mesh->mVertices[i].y;
  37.         vector.z = mesh->mVertices[i].z;
  38.         vertex.position = vector;
  39.  
  40.         if (mesh->mNormals != nullptr)
  41.         {
  42.             vector.x = mesh->mNormals[i].x;
  43.             vector.y = mesh->mNormals[i].y;
  44.             vector.z = mesh->mNormals[i].z;
  45.             vertex.normal = vector;
  46.         }
  47.         else
  48.         {
  49.             vertex.normal = glm::vec3(0, 0, 0);
  50.         }
  51.  
  52.         if (mesh->mTextureCoords[0])
  53.         {
  54.             glm::vec2 uv;
  55.             uv.x = mesh->mTextureCoords[0][i].x;
  56.             uv.y = mesh->mTextureCoords[0][i].y;
  57.             vertex.uv = uv;
  58.         }
  59.         else
  60.         {
  61.             vertex.uv = glm::vec2(0, 0);
  62.         }
  63.  
  64.         vertices.push_back(vertex);
  65.     }
  66.  
  67.     for (GLuint i = 0; i < mesh->mNumFaces; i++)
  68.     {
  69.         aiFace face = mesh->mFaces[i];
  70.  
  71.         for (GLuint j = 0; j < face.mNumIndices; j++)
  72.         {
  73.             indices.push_back(face.mIndices[j]);
  74.         }
  75.     }
  76.  
  77.     std::vector<GLfloat> vertices_float;
  78.     for (GLuint i = 0; i < vertices.size(); i++)
  79.     {
  80.         Vertex v = vertices[i];
  81.    
  82.         vertices_float.push_back(v.position.x);
  83.         vertices_float.push_back(v.position.y);
  84.         vertices_float.push_back(v.position.z);
  85.  
  86.         vertices_float.push_back(v.normal.x);
  87.         vertices_float.push_back(v.normal.y);
  88.         vertices_float.push_back(v.normal.z);
  89.  
  90.         vertices_float.push_back(v.uv.x);
  91.         vertices_float.push_back(v.uv.y);
  92.     }
  93.  
  94.     return Mesh(vertices_float, indices);
  95.  
  96. }
  97.  
  98. static void process_node(aiNode *node, const aiScene *scene, std::vector<Mesh> *meshes)
  99. {
  100.  
  101.     for (GLuint i = 0; i < node->mNumMeshes; i++)
  102.     {
  103.         aiMesh *mesh = scene->mMeshes[node->mMeshes[i]];
  104.  
  105.         meshes->push_back(process_mesh(mesh, scene));
  106.     }
  107.  
  108.     for (GLuint i = 0; i < node->mNumChildren; i++)
  109.     {
  110.         process_node(node->mChildren[i], scene, meshes);
  111.     }
  112.  
  113. }
  114.  
  115. static Model load_model(const char *path)
  116. {  
  117.     Assimp::Importer importer;
  118.     const aiScene *assimp_scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs);
  119.    
  120.     if (!assimp_scene || assimp_scene->mFlags == AI_SCENE_FLAGS_INCOMPLETE || !assimp_scene->mRootNode)
  121.     {
  122.         std::cout << "LOADER::LOAD_MODEL::ASSIMP::ERROR\n" << importer.GetErrorString() << "\n";
  123.         return Model(std::vector<Mesh>());
  124.     }
  125.    
  126.     std::vector<Mesh> meshes = std::vector<Mesh>();
  127.     process_node(assimp_scene->mRootNode, assimp_scene, &meshes);
  128.  
  129.     importer.~Importer();
  130.     return Model(meshes);
  131. }
  132.  
  133. static GLuint load_texture(const char *path)
  134. {
  135.     GLuint texture;
  136.     glGenTextures(1, &texture);
  137.     glBindTexture(GL_TEXTURE_2D, texture);
  138.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  139.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  140.  
  141.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  142.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  143.  
  144.     std::vector<unsigned char> buf;
  145.     unsigned int error, width, height;
  146.     if ((error = lodepng::decode(buf, width, height, path, LodePNGColorType::LCT_RGBA)) != 0)
  147.     {
  148.         std::cout << "LOADER::LOAD_TEXTURE::ERROR::TEXTURE_LOAD_FAILED";
  149.     }
  150.  
  151.     unsigned char* image = buf.data();
  152.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
  153.     glGenerateMipmap(GL_TEXTURE_2D);
  154.     glBindTexture(GL_TEXTURE_2D, 0);
  155.  
  156.     return texture;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement