Brenta Engine 1.0
Loading...
Searching...
No Matches
mesh.cpp
1/*
2 * MIT License
3 *
4 * Copyright (c) 2024 Giovanni Santini
5
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 */
26
27#include "mesh.hpp"
28
29#include "engine_logger.hpp"
30
31#include <iostream>
32
33using namespace Brenta;
34using namespace Brenta::Utils;
35
36Mesh::Mesh(std::vector<Types::Vertex> vertices,
37 std::vector<unsigned int> indices,
38 std::vector<Types::Texture> textures, GLint wrapping,
39 GLint filtering_min, GLint filtering_mag, GLboolean hasMipmap,
40 GLint mipmap_min, GLint mipmap_max)
41{
42 this->vao.Init();
43 this->vertices = vertices;
44 this->indices = indices;
45 this->textures = textures;
46 this->vbo = Types::Buffer(GL_ARRAY_BUFFER);
47 this->ebo = Types::Buffer(GL_ELEMENT_ARRAY_BUFFER);
48 this->wrapping = wrapping;
49 this->filtering_min = filtering_min;
50 this->filtering_mag = filtering_mag;
51 this->hasMipmap = hasMipmap;
52 this->mipmap_min = mipmap_min;
53 this->mipmap_mag = mipmap_max;
54
55 setupMesh();
56}
57
58void Mesh::Draw(Types::ShaderName shader_name)
59{
60 if (this->vao.GetVAO() == 0)
61 {
62 ERROR("Mesh not initialized");
63 return;
64 }
65
66 unsigned int diffuseNr = 1;
67 unsigned int specularNr = 1;
68 for (unsigned int i = 0; i < this->textures.size(); i++)
69 {
70 Texture::ActiveTexture(GL_TEXTURE0 + i);
71 std::string number;
72 std::string name = textures[i].type;
73 if (name == "texture_diffuse")
74 number = std::to_string(diffuseNr++);
75 else if (name == "texture_specular")
76 number = std::to_string(specularNr++);
77 Shader::SetInt(shader_name, ("material." + name + number).c_str(), i);
78 Texture::BindTexture(GL_TEXTURE_2D, textures[i].id, this->wrapping,
79 this->filtering_min, this->filtering_mag,
80 this->hasMipmap, this->mipmap_min,
81 this->mipmap_mag);
82 }
83 Texture::ActiveTexture(GL_TEXTURE0);
84
85 // draw mesh
86 this->vao.Bind();
87 GL::DrawElements(GL_TRIANGLES, this->indices.size(), GL_UNSIGNED_INT, 0);
88 this->vao.Unbind();
89
90 Texture::ActiveTexture(GL_TEXTURE0);
91}
92
93void Mesh::setupMesh()
94{
95 this->vbo.CopyVertices(this->vertices.size() * sizeof(Types::Vertex),
96 &this->vertices[0], GL_STATIC_DRAW);
97 this->ebo.CopyIndices(this->indices.size() * sizeof(unsigned int),
98 &this->indices[0], GL_STATIC_DRAW);
99 this->vao.SetVertexData(this->vbo, 0, 3, GL_FLOAT, GL_FALSE,
100 sizeof(Types::Vertex), (void *) 0);
101 this->vao.SetVertexData(this->vbo, 1, 3, GL_FLOAT, GL_FALSE,
102 sizeof(Types::Vertex),
103 (void *) offsetof(Types::Vertex, Normal));
104 this->vao.SetVertexData(this->vbo, 2, 2, GL_FLOAT, GL_FALSE,
105 sizeof(Types::Vertex),
106 (void *) offsetof(Types::Vertex, TexCoords));
107
109}
110
111Mesh::Builder &Mesh::Builder::set_vertices(std::vector<Types::Vertex> vertices)
112{
113 this->vertices = vertices;
114 return *this;
115}
116
117Mesh::Builder &Mesh::Builder::set_indices(std::vector<unsigned int> indices)
118{
119 this->indices = indices;
120 return *this;
121}
122
123Mesh::Builder &Mesh::Builder::set_textures(std::vector<Types::Texture> textures)
124{
125 this->textures = textures;
126 return *this;
127}
128
129Mesh::Builder &Mesh::Builder::set_wrapping(GLint wrapping)
130{
131 this->wrapping = wrapping;
132 return *this;
133}
134
135Mesh::Builder &Mesh::Builder::set_filtering_min(GLint filtering_min)
136{
137 this->filtering_min = filtering_min;
138 return *this;
139}
140
141Mesh::Builder &Mesh::Builder::set_filtering_mag(GLint filtering_mag)
142{
143 this->filtering_mag = filtering_mag;
144 return *this;
145}
146
147Mesh::Builder &Mesh::Builder::set_hasMipmap(GLboolean hasMipmap)
148{
149 this->hasMipmap = hasMipmap;
150 return *this;
151}
152
153Mesh::Builder &Mesh::Builder::set_mipmap_min(GLint mipmap_min)
154{
155 this->mipmap_min = mipmap_min;
156 return *this;
157}
158
159Mesh Mesh::Builder::build()
160{
161 return Mesh(this->vertices, this->indices, this->textures, this->wrapping,
162 this->filtering_min, this->filtering_mag, this->hasMipmap,
163 this->mipmap_min, this->mipmap_mag);
164}
static void DrawElements(GLenum mode, int count, GLenum type, const void *indices)
Draw Elements.
static void BindVertexArray(unsigned int n)
Enable Depth Test.
The Builder class is used to build a Mesh object.
Definition mesh.hpp:251
The Mesh class represents a 3D model.
Definition mesh.hpp:84
GLint wrapping
Type of texture wrapping.
Definition mesh.hpp:118
GLint filtering_min
Minifying texture filtering.
Definition mesh.hpp:128
std::vector< Types::Vertex > vertices
vertices of the mesh
Definition mesh.hpp:89
GLint mipmap_min
Type of mipmap minifying texture filtering.
Definition mesh.hpp:174
GLboolean hasMipmap
Should the texture have a mipmap?
Definition mesh.hpp:142
void Draw(Types::ShaderName shader_name)
Draw the mesh.
Definition mesh.cpp:58
Mesh(std::vector< Types::Vertex > vertices, std::vector< unsigned int > indices, std::vector< Types::Texture > textures, GLint wrapping=GL_REPEAT, GLint filtering_min=GL_NEAREST, GLint filtering_mag=GL_LINEAR, GLboolean hasMipmap=GL_TRUE, GLint mipmap_min=GL_LINEAR_MIPMAP_LINEAR, GLint mipmap_max=GL_LINEAR)
Construct a new Mesh object.
Definition mesh.cpp:36
std::vector< Types::Texture > textures
textures of the mesh
Definition mesh.hpp:102
GLint filtering_mag
Magnifying texture filtering.
Definition mesh.hpp:138
GLint mipmap_mag
Type of mipmap magnifying texture filtering.
Definition mesh.hpp:206
std::vector< unsigned int > indices
indices of the mesh
Definition mesh.hpp:98
static void SetInt(Types::ShaderName shader_name, const std::string &name, int value)
Set an integer in the shader.
Definition shader.cpp:67
static void BindTexture(GLenum target, unsigned int texture, 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)
Bind a texture.
Definition texture.cpp:56
static void ActiveTexture(GLenum texture)
Activate a texture unit.
Definition texture.cpp:51
Buffer wrapper around OpenGL buffer objects.
Definition buffer.hpp:50
void CopyIndices(GLsizeiptr size, const void *data, GLenum usage)
Copy data to the buffer object.
Definition buffer.cpp:46
void CopyVertices(GLsizeiptr size, const void *data, GLenum usage)
Copy data to the buffer object.
Definition buffer.cpp:54
void Init()
Init Constructor.
Definition vao.cpp:33
unsigned int GetVAO()
Get the VAO.
Definition vao.cpp:39
void Unbind()
Unbind the VAO.
Definition vao.cpp:59
void Bind()
Bind the VAO.
Definition vao.cpp:49
void SetVertexData(Buffer buffer, unsigned int index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void *pointer)
Set the vertex data.
Definition vao.cpp:64
The Vertex struct represents a vertex of a 3D model.
Definition mesh.hpp:54