Guest User

Untitled

a guest
Jan 25th, 2026
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.28 KB | None | 0 0
  1. void printMat4(const glm::mat4& mat) {
  2. for (int i = 0; i < 4; ++i) {
  3. for (int j = 0; j < 4; ++j) {
  4. // Access elements using mat[column][row] due to GLM's column-major storage
  5. std::cout << mat[j][i] << "\t";
  6. }
  7. std::cout << std::endl;
  8. }
  9. }
  10. AssimpModel::AssimpModel(std::string path)
  11. {
  12. loadModel(path);
  13. }
  14.  
  15. glm::vec3 assimp_to_glm(aiVector3D vec)
  16. {
  17. return {vec.x, vec.y, vec.z};
  18. }
  19. void AssimpModel::Draw(Shader& shader)
  20. {
  21. for (int i = 0; i < meshes.size(); i++) {
  22. meshes[i].Draw(shader);
  23. }
  24. }
  25.  
  26. void AssimpModel::loadModel(std::string path) {
  27. Assimp::Importer import;
  28. const aiScene * scene = import.ReadFile(path, aiProcess_Triangulate | aiProcess_GenSmoothNormals |
  29. aiProcess_CalcTangentSpace | aiProcess_FlipUVs);
  30. if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
  31. {
  32. std::cout << "ERROR::ASSIMP::" << import.GetErrorString() << std::endl;
  33. return;
  34. }
  35. directory = std::filesystem::path(path).parent_path().string();
  36. processNode(scene->mRootNode, scene, glm::mat4(1.0f));
  37. }
  38.  
  39. void AssimpModel::processNode(aiNode* node, const aiScene* scene, glm::mat4 parent_transformation)
  40. {
  41. std::cout << node->mName.C_Str() << std::endl;
  42. aiMatrix4x4 nodetrans = node->mTransformation;
  43. glm::mat4 glmnodetrans = ConvertMatrixToGLMFormat(nodetrans);
  44. glm::mat4 globalTransformation = parent_transformation * glmnodetrans;
  45. if (changescale == true) {
  46. globalTransformation = glm::scale(globalTransformation, glm::vec3(0.1, 0.1, 0.1));
  47. }
  48. // process all the node's meshes (if any)
  49. for (unsigned int i = 0; i < node->mNumMeshes; i++)
  50. {
  51. aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
  52. meshes.push_back(processMesh(mesh, scene, globalTransformation));
  53. }
  54. // then do the same for each of its children
  55. for (unsigned int i = 0; i < node->mNumChildren; i++)
  56. {
  57. processNode(node->mChildren[i], scene, globalTransformation);
  58. }
  59. }
  60.  
  61. Mesh AssimpModel::processMesh(aiMesh* mesh, const aiScene* scene, glm::mat4& transformation)
  62. {
  63. std::vector<Vertex> vertices;
  64. std::vector<std::uint32_t> indices;
  65. std::vector<Texture> textures;
  66. //glm::mat4 nodetransformation;
  67. //nodetransformation = transformation * ConvertMatrixToGLMFormat(scene->mRootNode->mTransformation);
  68. //nodetransformation = glm::scale(nodetransformation,glm::vec3(0.1f));
  69. //nodetransformation = glm::translate(nodetransformation,glm::vec3(0.0f, 0.0f, 0.0f));
  70. for (unsigned int i = 0; i < mesh->mNumVertices; i++)
  71. {
  72. Vertex vertex{};
  73. // process vertex positions, normals and texture coordinates
  74. glm::vec3 vector{};
  75. vector = assimp_to_glm(mesh->mVertices[i]);
  76. vertex.Position = vector;
  77.  
  78. vector = assimp_to_glm(mesh->mNormals[i]);
  79. vertex.Normal = vector;
  80.  
  81. if (mesh->mTextureCoords[0]) // does the mesh contain texture coordinates?
  82. {
  83. //texture
  84. glm::vec2 vec;
  85. vec.x = mesh->mTextureCoords[0][i].x;
  86. vec.y = mesh->mTextureCoords[0][i].y;
  87. vertex.TexCoord = vec;
  88.  
  89. // tangent
  90. vector.x = mesh->mTangents[i].x;
  91. vector.y = mesh->mTangents[i].y;
  92. vector.z = mesh->mTangents[i].z;
  93. vertex.Tangent = vector;
  94. //// bitangent
  95. vector.x = mesh->mBitangents[i].x;
  96. vector.y = mesh->mBitangents[i].y;
  97. vector.z = mesh->mBitangents[i].z;
  98. vertex.Bitangent = vector;
  99. }
  100. else
  101. vertex.TexCoord = glm::vec2(0.0f, 0.0f);
  102.  
  103. vertices.push_back(vertex);
  104. }
  105. // process indices
  106. for (unsigned int i = 0; i < mesh->mNumFaces; i++)
  107. {
  108. aiFace face = mesh->mFaces[i];
  109. for (unsigned int j = 0; j < face.mNumIndices; j++)
  110. indices.push_back(face.mIndices[j]);
  111. }
  112. // process material
  113. aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex];
  114. std::cout << material->GetTextureCount(aiTextureType_DIFFUSE) << std::endl;
  115.  
  116. std::vector<Texture> diffuseMaps = loadMaterialTextures(material,
  117. aiTextureType_DIFFUSE, "texture_diffuse");
  118. textures.insert(textures.end(), diffuseMaps.begin(), diffuseMaps.end());
  119.  
  120. std::vector<Texture> specularMaps = loadMaterialTextures(material,
  121. aiTextureType_SPECULAR, "texture_specular");
  122. //if (specularMaps.size() == 0) {
  123. //specularMaps = loadMaterialTextures(material, aiTextureType_DIFFUSE, "texture_diffuse");
  124. // }
  125. textures.insert(textures.end(), specularMaps.begin(), specularMaps.end());
  126.  
  127. std::vector<Texture> normalMaps = loadMaterialTextures(material, aiTextureType_NORMALS, "texture_normal");
  128. //if (normalMaps.size() == 0) {
  129. //normalMaps =loadMaterialTextures(material, aiTextureType_HEIGHT, "texture_normal");
  130. //}
  131.  
  132. textures.insert(textures.end(), normalMaps.begin(), normalMaps.end());
  133. // 4. height maps
  134. std::vector<Texture> heightMaps = loadMaterialTextures(material, aiTextureType_AMBIENT, "texture_height");
  135. textures.insert(textures.end(), heightMaps.begin(), heightMaps.end());
  136.  
  137. return Mesh(vertices, indices, textures, transformation);
  138. }
Advertisement
Add Comment
Please, Sign In to add comment