Brenta Engine 1.1
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;
34
35mesh::mesh(std::vector<types::vertex> vertices,
36 std::vector<unsigned int> indices,
37 std::vector<types::texture> textures, GLint wrapping,
38 GLint filtering_min, GLint filtering_mag, GLboolean has_mipmap,
39 GLint mipmap_min, GLint mipmap_max)
40{
41 this->vao.init();
42 this->vertices = vertices;
43 this->indices = indices;
44 this->textures = textures;
45 this->vbo = types::buffer(GL_ARRAY_BUFFER);
46 this->ebo = types::buffer(GL_ELEMENT_ARRAY_BUFFER);
47 this->wrapping = wrapping;
48 this->filtering_min = filtering_min;
49 this->filtering_mag = filtering_mag;
50 this->has_mipmap = has_mipmap;
51 this->mipmap_min = mipmap_min;
52 this->mipmap_mag = mipmap_max;
53
54 setup_mesh();
55}
56
57void mesh::draw(types::shader_name_t shader_name)
58{
59 if (this->vao.get_vao() == 0)
60 {
61 ERROR("Mesh not initialized");
62 return;
63 }
64
65 unsigned int diffuseNr = 1;
66 unsigned int specularNr = 1;
67 for (unsigned int i = 0; i < this->textures.size(); i++)
68 {
69 texture::active_texture(GL_TEXTURE0 + i);
70 std::string number;
71 std::string name = textures[i].type;
72 if (name == "texture_diffuse")
73 number = std::to_string(diffuseNr++);
74 else if (name == "texture_specular")
75 number = std::to_string(specularNr++);
76 shader::set_int(shader_name, ("material." + name + number).c_str(), i);
77 texture::bind_texture(GL_TEXTURE_2D, textures[i].id, this->wrapping,
78 this->filtering_min, this->filtering_mag,
79 this->has_mipmap, this->mipmap_min,
80 this->mipmap_mag);
81 }
82 texture::active_texture(GL_TEXTURE0);
83
84 // draw mesh
85 this->vao.bind();
86 gl::draw_elements(GL_TRIANGLES, this->indices.size(), GL_UNSIGNED_INT, 0);
87 this->vao.unbind();
88
89 texture::active_texture(GL_TEXTURE0);
90}
91
92void mesh::setup_mesh()
93{
94 this->vbo.copy_vertices(this->vertices.size() * sizeof(types::vertex),
95 &this->vertices[0], GL_STATIC_DRAW);
96 this->ebo.copy_indices(this->indices.size() * sizeof(unsigned int),
97 &this->indices[0], GL_STATIC_DRAW);
98 this->vao.set_vertex_data(this->vbo, 0, 3, GL_FLOAT, GL_FALSE,
99 sizeof(types::vertex), (void *) 0);
100 this->vao.set_vertex_data(this->vbo, 1, 3, GL_FLOAT, GL_FALSE,
101 sizeof(types::vertex),
102 (void *) offsetof(types::vertex, normal));
103 this->vao.set_vertex_data(this->vbo, 2, 2, GL_FLOAT, GL_FALSE,
104 sizeof(types::vertex),
105 (void *) offsetof(types::vertex, tex_coords));
106
108}
109
110mesh::builder &mesh::builder::set_vertices(std::vector<types::vertex> vertices)
111{
112 this->vertices = vertices;
113 return *this;
114}
115
116mesh::builder &mesh::builder::set_indices(std::vector<unsigned int> indices)
117{
118 this->indices = indices;
119 return *this;
120}
121
122mesh::builder &mesh::builder::set_textures(std::vector<types::texture> textures)
123{
124 this->textures = textures;
125 return *this;
126}
127
128mesh::builder &mesh::builder::set_wrapping(GLint wrapping)
129{
130 this->wrapping = wrapping;
131 return *this;
132}
133
134mesh::builder &mesh::builder::set_filtering_min(GLint filtering_min)
135{
136 this->filtering_min = filtering_min;
137 return *this;
138}
139
140mesh::builder &mesh::builder::set_filtering_mag(GLint filtering_mag)
141{
142 this->filtering_mag = filtering_mag;
143 return *this;
144}
145
146mesh::builder &mesh::builder::set_has_mipmap(GLboolean has_mipmap)
147{
148 this->has_mipmap = has_mipmap;
149 return *this;
150}
151
152mesh::builder &mesh::builder::set_mipmap_min(GLint mipmap_min)
153{
154 this->mipmap_min = mipmap_min;
155 return *this;
156}
157
158mesh mesh::builder::build()
159{
160 return mesh(this->vertices, this->indices, this->textures, this->wrapping,
161 this->filtering_min, this->filtering_mag, this->has_mipmap,
162 this->mipmap_min, this->mipmap_mag);
163}
static void draw_elements(GLenum mode, int count, GLenum type, const void *indices)
Draw Elements.
static void bind_vertex_array(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
std::vector< types::texture > textures
textures of the mesh
Definition mesh.hpp:102
GLint filtering_min
Minifying texture filtering.
Definition mesh.hpp:128
GLint mipmap_mag
Type of mipmap magnifying texture filtering.
Definition mesh.hpp:206
void draw(types::shader_name_t shader_name)
Draw the mesh.
Definition mesh.cpp:57
GLint wrapping
Type of texture wrapping.
Definition mesh.hpp:118
GLboolean has_mipmap
Should the texture have a mipmap?
Definition mesh.hpp:142
std::vector< unsigned int > indices
indices of the mesh
Definition mesh.hpp:98
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
GLint filtering_mag
Magnifying texture filtering.
Definition mesh.hpp:138
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:35
static void set_int(types::shader_name_t shader_name, const std::string &name, int value)
Set an integer in the shader.
Definition shader.cpp:66
static void active_texture(GLenum texture)
Activate a texture unit.
Definition texture.cpp:52
static void bind_texture(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:57
Buffer wrapper around OpenGL buffer objects.
Definition buffer.hpp:50
void copy_indices(GLsizeiptr size, const void *data, GLenum usage)
Copy data to the buffer object.
Definition buffer.cpp:46
void copy_vertices(GLsizeiptr size, const void *data, GLenum usage)
Copy data to the buffer object.
Definition buffer.cpp:54
void bind()
Bind the VAO.
Definition vao.cpp:49
void set_vertex_data(buffer buffer, unsigned int index, GLint size, GLenum type, GLboolean is_normalized, GLsizei stride, const void *pointer)
Set the vertex data.
Definition vao.cpp:64
unsigned int get_vao()
Get the VAO.
Definition vao.cpp:39
void unbind()
Unbind the VAO.
Definition vao.cpp:59
void init()
Init Constructor.
Definition vao.cpp:33
The Vertex struct represents a vertex of a 3D model.
Definition mesh.hpp:54