Raftel Engine
 
Loading...
Searching...
No Matches
mesh.hpp
Go to the documentation of this file.
1
28
29#ifndef __MESH_H__
30#define __MESH_H__ 1
31
32#pragma once
33#include <vector>
34#include <string>
35#include <assimp/Importer.hpp>
36#include <assimp/scene.h>
37#include <assimp/postprocess.h>
38#include <GL/glew.h>
39#include <glm/glm.hpp>
40#include <raftel/material.hpp>
41#include <raftel/shader.hpp>
42#include <raftel/global_macros.hpp>
43
44#include <raftel/engine.hpp>
45
46namespace Raftel {
54 struct Vertex {
55 glm::vec3 position;
56 glm::vec3 normal;
57 glm::vec2 texCoord;
58
62 Vertex() = default;
63
70 Vertex(const glm::vec3& position, const glm::vec3& normal, const glm::vec2& texCoord)
72 }
73
74 bool operator==(const Vertex& other) const {
75 return position == other.position && normal == other.normal && texCoord == other.texCoord;
76 }
77 };
78
89 class Mesh {
90 public:
91
95 Mesh() = default;
96
101
105 MOVABLE_BUT_NOT_COPYABLE(Mesh)
106
107
113 static std::shared_ptr<Mesh> Create(const std::string& filePath, bool multithread);
114
119 Mesh(const std::string& filePath);
120
127 Mesh(std::vector<std::vector<Vertex>> verticesPerSubmesh,
128 std::vector<std::vector<unsigned int>> indicesPerSubmesh,
129 std::vector<std::shared_ptr<Material>> mats);
130
136 Mesh(std::vector<std::vector<Vertex>> verticesPerSubmesh,
137 std::vector<std::vector<unsigned int>> indicesPerSubmesh);
138
144 bool loadMesh(const std::string& filePath);
145
150 void setMaterials(const std::vector<std::shared_ptr<Material>>& newMaterials) {
151 materials = newMaterials;
152 }
153
158 void setMaterial(const std::shared_ptr<Material>& mat) {
159 materials.clear();
160 materials.push_back(mat);
161 }
162
167 void addMaterial(const std::shared_ptr<Material>& mat) {
168 materials.push_back(mat);
169 }
170
174 void setupMesh();
175
180 void draw(ShaderProgram& shader);
181
186 void setUniforms(GLuint shaderProgramID);
187
193
198 std::string getName() { return nameMesh; }
199
204 void setName(std::string n_) { nameMesh = n_; }
205
210 const std::string& getLastError() const { return lastError; }
211
217 std::shared_ptr<Material> GetMaterialByIndex(int index);
218
223 std::vector<std::shared_ptr<Material>>& GetAllMaterials() { return materials; }
224
229 std::vector<GLuint> GetVAOs() { return VAOs; }
230
235 std::vector<std::vector<unsigned int>> GetSubMeshIndex() { return submeshIndices; }
236
237 private:
238
244 bool loadFromFile(const std::string& filePath);
245
251 bool loadFromAssimp(const std::string& filePath);
252
258 void processNode(aiNode* node, const aiScene* scene);
259
265 void processAssimpMesh(aiMesh* mesh, const aiScene* scene);
266
267 /*@brief Sets an error message.
268 * @param errorMessage The error message.
269 * @param file The source file where the error occurred.
270 * @param line The line number where the error occurred.
271 */
272 void setError(const std::string& errorMessage, const char* file, int line);
273
274 std::vector<std::shared_ptr<Material>> materials;
275
281 std::shared_ptr<Raftel::Material> convertAssimpMaterial(aiMaterial* material);
282
283 std::vector<GLuint> VBOs;
284 std::vector<GLuint> EBOs;
285 std::vector<GLuint> VAOs;
286 std::vector<std::vector<unsigned int>> submeshIndices;
287 std::vector<std::vector<Vertex>> submeshVertex;
288 //TODO: quitar shared_ptr -> simplemente vector de materiales
289 std::string nameMesh;
290
291 std::string lastError;
292 };
293
306 {
307 public:
308
315 static std::shared_ptr<Mesh> createPlane(float width, float height);
316
322 static std::shared_ptr<Mesh> createCube(float size);
323
330 static std::shared_ptr<Mesh> createSphere(float radius, int segments);
331
341 static std::shared_ptr<Mesh> createTerrain(const char* heightMapFile,
342 float size,
343 float heightMultiplier,
344 bool isCentered);
345
362 static std::shared_ptr<Raftel::Mesh> createCone(float radius, float height, int segments);
363
364
365 };
366
367} // namespace Raftel
368#endif // __MESH_HPP__
Represents a material with textures and physical properties.
Definition material.hpp:43
A factory class for creating and loading meshes.
Definition mesh.hpp:306
static std::shared_ptr< Mesh > createCube(float size)
Creates a cube mesh with the given size.
static std::shared_ptr< Mesh > createSphere(float radius, int segments)
Creates a sphere mesh with the given radius and number of segments.
static std::shared_ptr< Mesh > createPlane(float width, float height)
Creates a plane mesh with the given width and height.
static std::shared_ptr< Raftel::Mesh > createCone(float radius, float height, int segments)
Creates a 3D cone mesh.
static std::shared_ptr< Mesh > createTerrain(const char *heightMapFile, float size, float heightMultiplier, bool isCentered)
Creates a terrain mesh from a heightmap image.
Mesh()=default
Default constructor for the Mesh class.
void addMaterial(const std::shared_ptr< Material > &mat)
Adds a material to the mesh.
Definition mesh.hpp:167
void setupMesh()
Initializes OpenGL buffers for the mesh (VAO, VBO, EBO).
void setMaterial(const std::shared_ptr< Material > &mat)
Sets a single material for the mesh.
Definition mesh.hpp:158
std::vector< std::vector< unsigned int > > GetSubMeshIndex()
Retrieves all submesh indices.
Definition mesh.hpp:235
static std::shared_ptr< Mesh > Create(const std::string &filePath, bool multithread)
Move constructor for the Mesh class.
void draw(ShaderProgram &shader)
Renders the mesh using a specified shader.
std::vector< GLuint > GetVAOs()
Retrieves all VAOs associated with the mesh.
Definition mesh.hpp:229
void setUniforms(GLuint shaderProgramID)
Sets the uniform variables for the shader program.
~Mesh()
Destructor for the Mesh class.
void setMaterials(const std::vector< std::shared_ptr< Material > > &newMaterials)
Sets the materials for the mesh.
Definition mesh.hpp:150
std::string getName()
Retrieves the name of the mesh.
Definition mesh.hpp:198
std::vector< std::shared_ptr< Material > > & GetAllMaterials()
Retrieves all materials associated with the mesh.
Definition mesh.hpp:223
std::shared_ptr< Material > GetMaterialByIndex(int index)
Retrieves a material by its index.
void setName(std::string n_)
Sets the name of the mesh.
Definition mesh.hpp:204
const std::string & getLastError() const
Retrieves the last error encountered during mesh loading.
Definition mesh.hpp:210
void renderMaterial(ShaderProgram &shader)
Renders the material of the mesh.
bool loadMesh(const std::string &filePath)
Loads a mesh from a file.
A class to represent an OpenGL shader program, combining vertex and fragment shaders.
Definition shader.hpp:119
Defines the Material class for handling surface properties and textures.
Defines the Shader and ShaderProgram classes for managing shaders in OpenGL.
Represents a 3D vertex with attributes like position, normal, and texture coordinates.
Definition mesh.hpp:54
glm::vec3 position
The position of the vertex in 3D space.
Definition mesh.hpp:55
glm::vec2 texCoord
The texture coordinates of the vertex.
Definition mesh.hpp:57
Vertex()=default
Default constructor for the Vertex structure.
Vertex(const glm::vec3 &position, const glm::vec3 &normal, const glm::vec2 &texCoord)
Constructor for the Vertex structure.
Definition mesh.hpp:70
glm::vec3 normal
The normal vector of the vertex for lighting calculations.
Definition mesh.hpp:56