29#include "engine_logger.hpp"
33using namespace Brenta;
35Model::Model(std::string
const &path, GLint wrapping, GLint filtering_min,
36 GLint filtering_mag, GLboolean hasMipmap, GLint mipmap_min,
37 GLint mipmap_mag,
bool flip)
51 for (
unsigned int i = 0; i < meshes.size(); i++)
53 meshes[i].Draw(shader);
57void Model::loadModel(std::string path)
60 Assimp::Importer importer;
61 const aiScene *scene =
62 importer.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs);
64 if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE
67 ERROR(
"Could not load model with assimp: ", importer.GetErrorString());
70 directory = path.substr(0, path.find_last_of(
'/'));
72 processNode(scene->mRootNode, scene);
75void Model::processNode(aiNode *node,
const aiScene *scene)
77 for (
unsigned int i = 0; i < node->mNumMeshes; i++)
79 aiMesh *mesh = scene->mMeshes[node->mMeshes[i]];
80 meshes.push_back(processMesh(mesh, scene));
82 for (
unsigned int i = 0; i < node->mNumChildren; i++)
84 processNode(node->mChildren[i], scene);
88Mesh Model::processMesh(aiMesh *mesh,
const aiScene *scene)
90 std::vector<Types::Vertex> vertices;
91 std::vector<unsigned int> indices;
92 std::vector<Types::Texture> textures;
94 for (
unsigned int i = 0; i < mesh->mNumVertices; i++)
98 vector.x = mesh->mVertices[i].x;
99 vector.y = mesh->mVertices[i].y;
100 vector.z = mesh->mVertices[i].z;
101 vertex.Position = vector;
103 vector.x = mesh->mNormals[i].x;
104 vector.y = mesh->mNormals[i].y;
105 vector.z = mesh->mNormals[i].z;
106 vertex.Normal = vector;
108 if (mesh->mTextureCoords[0])
111 vec.x = mesh->mTextureCoords[0][i].x;
112 vec.y = mesh->mTextureCoords[0][i].y;
113 vertex.TexCoords = vec;
116 vertex.TexCoords = glm::vec2(0.0f, 0.0f);
118 vertices.push_back(vertex);
121 for (
unsigned int i = 0; i < mesh->mNumFaces; i++)
123 aiFace face = mesh->mFaces[i];
124 for (
unsigned int j = 0; j < face.mNumIndices; j++)
125 indices.push_back(face.mIndices[j]);
128 if (mesh->mMaterialIndex >= 0)
130 aiMaterial *material = scene->mMaterials[mesh->mMaterialIndex];
131 std::vector<Types::Texture> diffuseMaps = loadMaterialTextures(
132 material, aiTextureType_DIFFUSE,
"texture_diffuse");
133 textures.insert(textures.end(), diffuseMaps.begin(), diffuseMaps.end());
134 std::vector<Types::Texture> specularMaps = loadMaterialTextures(
135 material, aiTextureType_SPECULAR,
"texture_specular");
136 textures.insert(textures.end(), specularMaps.begin(),
140 return Mesh(vertices, indices, textures, this->
wrapping,
145std::vector<Types::Texture> Model::loadMaterialTextures(aiMaterial *mat,
147 std::string typeName)
149 std::vector<Types::Texture> textures;
150 for (
unsigned int i = 0; i < mat->GetTextureCount(type); i++)
153 mat->GetTexture(type, i, &str);
155 for (
unsigned int j = 0; j < textures_loaded.size(); j++)
157 if (std::strcmp(textures_loaded[j].path.c_str(), str.C_Str()) == 0)
159 textures.push_back(textures_loaded[j]);
167 std::string path = directory +
"/" + std::string(str.C_Str());
172 texture.type = typeName;
173 texture.path = str.C_Str();
174 textures.push_back(texture);
175 textures_loaded.push_back(texture);
229Model Model::Builder::build()
The Mesh class represents a 3D model.
GLint filtering_mag
Texture filtering mode.
GLint wrapping
Texture wrapping mode.
void Draw(Types::ShaderName shader)
Draw the model.
GLint filtering_min
Texture filtering mode.
GLint mipmap_mag
Mipmap filtering mode.
bool flip
If the texture should be flipped.
GLboolean hasMipmap
If the texture has a mipmap.
GLint mipmap_min
Mipmap filtering mode.
Model()
Empty constructor.
static unsigned int LoadTexture(std::string path, GLint wrapping=GL_REPEAT, GLint filtering_min=GL_NEAREST, GLint filtering_mag=GL_NEAREST, GLboolean hasMipmap=GL_TRUE, GLint mipmap_min=GL_LINEAR_MIPMAP_LINEAR, GLint mipmap_mag=GL_LINEAR, bool flip=true)
Load a texture from a file.
The Texture struct represents a texture of a 3D model.
The Vertex struct represents a vertex of a 3D model.