Guest User

Untitled

a guest
Aug 22nd, 2026
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.22 KB | None | 0 0
  1. ProcessMesh Function
  2. -----------------------------
  3. ModelMaintenance ModelObject::ProcessMesh(aiMesh* Mesh, const aiScene* Scene) {
  4.  // data to fill
  5.     std::vector<Vertex> vertices;
  6.     std::vector<unsigned int> indices;
  7.     std::vector<Texture> textures;
  8.  
  9.     //std::string Name = Mesh->mName.C_Str();
  10.  
  11.     // walk through each of the mesh's vertices
  12.     for(unsigned int i = 0; i < Mesh->mNumVertices; i++)
  13.     {
  14.         Vertex vertex;
  15.  
  16.         SetVertexBoneDataToDefault(vertex);
  17.  
  18.         vertex.Position = UniversalHelpers::GetGLMvec(Mesh->mVertices[i]);
  19.         vertex.Normal = UniversalHelpers::GetGLMvec(Mesh->mNormals[i]);
  20.  
  21.         glm::vec3 vector; // we declare a placeholder vector since assimp uses its own vector class that doesn't directly convert to glm's vec3 class so we transfer the data to this placeholder glm::vec3 first.
  22.         // positions
  23.         vector.x = Mesh->mVertices[i].x;
  24.         vector.y = Mesh->mVertices[i].y;
  25.         vector.z = Mesh->mVertices[i].z;
  26.         vertex.Position = vector;
  27.         // normals
  28.         if (Mesh->HasNormals())
  29.         {
  30.             vector.x = Mesh->mNormals[i].x;
  31.             vector.y = Mesh->mNormals[i].y;
  32.             vector.z = Mesh->mNormals[i].z;
  33.             vertex.Normal = vector;
  34.         }
  35.         // texture coordinates
  36.         if(Mesh->mTextureCoords[0]) // does the mesh contain texture coordinates?
  37.         {
  38.             glm::vec2 vec;
  39.             // a vertex can contain up to 8 different texture coordinates. We thus make the assumption that we won't
  40.             // use models where a vertex can have multiple texture coordinates so we always take the first set (0).
  41.             vec.x = Mesh->mTextureCoords[0][i].x;
  42.             vec.y = Mesh->mTextureCoords[0][i].y;
  43.             vertex.TexCoords = vec;
  44.             // tangent
  45.             //vector.x = Mesh->mTangents[i].x;
  46.             //vector.y = Mesh->mTangents[i].y;
  47.             //vector.z = Mesh->mTangents[i].z;
  48.             //vertex.Tangent = vector;
  49.             //// bitangent
  50.             //vector.x = Mesh->mBitangents[i].x;
  51.             //vector.y = Mesh->mBitangents[i].y;
  52.             //vector.z = Mesh->mBitangents[i].z;
  53.             //vertex.Bitangents = vector;
  54.         }
  55.         else {
  56.             vertex.TexCoords = glm::vec2(0.0f, 0.0f);
  57.         }
  58.             vertices.push_back(vertex);
  59.     }
  60.  
  61.     ExtractBoneWeightForVertices(vertices, Mesh, Scene);
  62.     // now wak through each of the mesh's faces (a face is a mesh its triangle) and retrieve the corresponding vertex indices.
  63.     for(unsigned int i = 0; i < Mesh->mNumFaces; i++)
  64.     {
  65.         aiFace face = Mesh->mFaces[i];
  66.  
  67.         //if(Name == "Plane.004") {
  68.             if (face.mNumIndices == 3) {
  69.                 indices.push_back(face.mIndices[0]);                    
  70.                 indices.push_back(face.mIndices[1]);
  71.                 indices.push_back(face.mIndices[2]);
  72.                 //for all the indices in each face, add each indice
  73.             } else {
  74.                 indices.push_back(i);
  75.             }
  76.         //}
  77.         // retrieve all indices of the face and store them in the indices vector
  78.     }
  79.     // process materials
  80.     aiMaterial* material = Scene->mMaterials[Mesh->mMaterialIndex];    
  81.     // we assume a convention for sampler names in the shaders. Each diffuse texture should be named
  82.     // as 'texture_diffuseN' where N is a sequential number ranging from 1 to MAX_SAMPLER_NUMBER.
  83.     // Same applies to other texture as the following list summarizes:
  84.     // diffuse: texture_diffuseN
  85.     // specular: texture_specularN
  86.     // normal: texture_normal  
  87.     // 1. diffuse maps
  88.     std::vector<Texture> diffuseMaps = loadMaterialTextures(material, aiTextureType_DIFFUSE, "texture_diffuse");
  89.     textures.insert(textures.end(), diffuseMaps.begin(), diffuseMaps.end());
  90.     // 2. specular maps
  91.     std::vector<Texture> specularMaps = loadMaterialTextures(material, aiTextureType_SPECULAR, "texture_specular");
  92.     textures.insert(textures.end(), specularMaps.begin(), specularMaps.end());
  93.     // 3. normal maps
  94.     std::vector<Texture> normalMaps = loadMaterialTextures(material, aiTextureType_HEIGHT, "texture_normal");
  95.     textures.insert(textures.end(), normalMaps.begin(), normalMaps.end());
  96.     // 4. height maps
  97.     std::vector<Texture> heightMaps = loadMaterialTextures(material, aiTextureType_AMBIENT, "texture_height");
  98.     textures.insert(textures.end(), heightMaps.begin(), heightMaps.end());
  99.  
  100.     // return a mesh object created from the extracted mesh data
  101.     return ModelMaintenance(vertices, textures, indices);
  102. }
  103.  
  104. worlds update method
  105. -----------------------------------
  106. void World::Update() {
  107.     player.Update(Programs);
  108.     DrawObjects();
  109.  
  110.     for(int i = 0; i < 100; i++) { //Blender model faces for my orientation goes from bootom left, bottom right, top right, bottom left, top right, top left
  111.         std::cout << "Vertex " << i << "(" << entityManager.Enviroment.Meshes[0].Verts[entityManager.Enviroment.Meshes[0].Indicies[i]].Position.x << "," << entityManager.Enviroment.Meshes[0].Verts[entityManager.Enviroment.Meshes[0].Indicies[i]].Position.y << ", " << entityManager.Enviroment.Meshes[0].Verts[entityManager.Enviroment.Meshes[0].Indicies[i]].Position.z << std::endl;
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment