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