Brenta Engine 1.2
Loading...
Searching...
No Matches
mesh.cpp
1// SPDX-License-Identifier: MIT
2// Author: Giovanni Santini
3// Mail: giovanni.santini@proton.me
4// Github: @San7o
5
6#include <brenta/logger.hpp>
7#include <brenta/mesh.hpp>
8#include <iostream>
9
10using namespace brenta;
11
12const mesh::config mesh::default_config = {
13 {},
14 {},
15 {},
16 GL_REPEAT,
17 GL_NEAREST,
18 GL_LINEAR,
19 GL_TRUE,
20 GL_LINEAR_MIPMAP_LINEAR,
21 GL_LINEAR,
22};
23
24mesh::mesh(config&& conf)
25{
26 this->vertices = conf.vertices;
27 this->indices = conf.indices;
28 this->textures = std::move(conf.textures);
29 this->wrapping = conf.wrapping;
30 this->filtering_min = conf.filtering_min;
31 this->filtering_mag = conf.filtering_mag;
32 this->has_mipmap = conf.has_mipmap;
33 this->mipmap_min = conf.mipmap_min;
34 this->mipmap_mag = conf.mipmap_max;
35
36 this->init();
37 DEBUG("mesh: created");
38}
39
40mesh::~mesh()
41{
42 DEBUG("mesh: deleted");
43}
44
45void mesh::init()
46{
47 this->vao.init();
48 this->vao.bind();
49 this->vbo.init(GL_ARRAY_BUFFER);
50 this->ebo.init(GL_ELEMENT_ARRAY_BUFFER);
51 this->vbo.copy_vertices(this->vertices.size() * sizeof(types::vertex),
52 &this->vertices[0], GL_STATIC_DRAW);
53 this->ebo.copy_indices(this->indices.size() * sizeof(unsigned int),
54 &this->indices[0], GL_STATIC_DRAW);
55 this->vao.set_vertex_data(this->vbo, 0, 3, GL_FLOAT, GL_FALSE,
56 sizeof(types::vertex), (void *) 0);
57 this->vao.set_vertex_data(this->vbo, 1, 3, GL_FLOAT, GL_FALSE,
58 sizeof(types::vertex),
59 (void *) offsetof(types::vertex, normal));
60 this->vao.set_vertex_data(this->vbo, 2, 2, GL_FLOAT, GL_FALSE,
61 sizeof(types::vertex),
62 (void *) offsetof(types::vertex, tex_coords));
63
64 gl::bind_vertex_array(0);
65}
66
67void mesh::draw(shader::name_t shader_name) const
68{
69 if (this->vao.get_id() == 0)
70 {
71 ERROR("mesh::draw: not initialized");
72 return;
73 }
74
75 unsigned int diffuseNr = 1;
76 unsigned int specularNr = 1;
77 for (unsigned int i = 0; i < this->textures.size(); i++)
78 {
79 texture::active_texture(GL_TEXTURE0 + i);
80
81 std::string number;
82 std::string name = textures[i]->type;
83 if (name == "texture_diffuse")
84 number = std::to_string(diffuseNr++);
85 else if (name == "texture_specular")
86 number = std::to_string(specularNr++);
87
88 shader::set_int(shader_name, ("material." + name + number).c_str(), i);
89 textures[i]->bind(GL_TEXTURE_2D, this->wrapping,
90 this->filtering_min, this->filtering_mag,
91 this->has_mipmap, this->mipmap_min, this->mipmap_mag);
92 }
93
94 texture::active_texture(GL_TEXTURE0);
95
96 // draw mesh
97 this->vao.bind();
98 gl::draw_elements(GL_TRIANGLES, this->indices.size(), GL_UNSIGNED_INT, 0);
99 this->vao.unbind();
100
101 texture::active_texture(GL_TEXTURE0);
102}
103
104//
105// Builder functions
106//
107
108mesh::builder &mesh::builder::vertices(std::vector<types::vertex> vertices)
109{
110 this->conf.vertices = vertices;
111 return *this;
112}
113
114mesh::builder &mesh::builder::indices(std::vector<unsigned int> indices)
115{
116 this->conf.indices = indices;
117 return *this;
118}
119
120mesh::builder &mesh::builder::textures(std::vector<std::shared_ptr<texture>> textures)
121{
122 this->conf.textures = std::move(textures);
123 return *this;
124}
125
126mesh::builder &mesh::builder::wrapping(GLint wrapping)
127{
128 this->conf.wrapping = wrapping;
129 return *this;
130}
131
132mesh::builder &mesh::builder::filtering_min(GLint filtering_min)
133{
134 this->conf.filtering_min = filtering_min;
135 return *this;
136}
137
138mesh::builder &mesh::builder::filtering_mag(GLint filtering_mag)
139{
140 this->conf.filtering_mag = filtering_mag;
141 return *this;
142}
143
144mesh::builder &mesh::builder::has_mipmap(GLboolean has_mipmap)
145{
146 this->conf.has_mipmap = has_mipmap;
147 return *this;
148}
149
150mesh::builder &mesh::builder::mipmap_min(GLint mipmap_min)
151{
152 this->conf.mipmap_min = mipmap_min;
153 return *this;
154}
155
156mesh mesh::builder::build()
157{
158 return mesh(std::move(this->conf));
159}
static void draw_elements(GLenum mode, int count, GLenum type, const void *indices)
Draw Elements.
Definition gl.cpp:165
The Builder class is used to build a Mesh object.
Definition mesh.hpp:210
The Mesh class represents a 3D model.
Definition mesh.hpp:51
GLint filtering_min
Minifying texture filtering.
Definition mesh.hpp:89
GLint mipmap_mag
Type of mipmap magnifying texture filtering.
Definition mesh.hpp:167
GLint wrapping
Type of texture wrapping.
Definition mesh.hpp:79
GLboolean has_mipmap
Should the texture have a mipmap?
Definition mesh.hpp:103
std::vector< unsigned int > indices
indices of the mesh
Definition mesh.hpp:62
GLint mipmap_min
Type of mipmap minifying texture filtering.
Definition mesh.hpp:135
GLint filtering_mag
Magnifying texture filtering.
Definition mesh.hpp:99
static bool set_int(shader::name_t shader_name, const GLchar *name, int value)
Set an integer in the shader.
Definition shader.cpp:63
static void active_texture(GLenum texture)
Activate a texture unit.
Definition texture.cpp:39
void copy_indices(GLsizeiptr size, const void *data, GLenum usage)
Copy data to the buffer object.
Definition buffer.cpp:27
void copy_vertices(GLsizeiptr size, const void *data, GLenum usage)
Copy data to the buffer object.
Definition buffer.cpp:36
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:56
The Vertex struct represents a vertex of a 3D model.
Definition mesh.hpp:35