Symphony Of Empires
mesh.cpp
Go to the documentation of this file.
1 // Eng3D - General purpouse game engine
2 // Copyright (C) 2021, Eng3D contributors
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16 //
17 // ----------------------------------------------------------------------------
18 // Name:
19 // mesh.cpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #ifdef E3D_BACKEND_OPENGL
26 # include <GL/glew.h>
27 # include <GL/gl.h>
28 #elif defined E3D_BACKEND_GLES
29 # include <GLES3/gl3.h>
30 #elif defined E3D_BACKEND_RGX
31 # include <gccore.h>
32 #endif
33 
34 #include "eng3d/mesh.hpp"
35 #include "eng3d/string.hpp"
36 
37 Eng3D::OpenGL::VAO::VAO(unsigned int _id)
38  : id{ _id }
39 {
40 
41 }
42 
44  glGenVertexArrays(1, &id);
45 }
46 
48  glDeleteVertexArrays(1, &id);
49 }
50 
52  this->id = rhs.id;
53  rhs.id = 0;
54 }
55 
57  this->id = rhs.id;
58  rhs.id = 0;
59  return *this;
60 }
61 
63  glBindVertexArray(id);
64 }
65 
66 unsigned int Eng3D::OpenGL::VAO::get_id() const {
67  return id;
68 }
69 
70 Eng3D::OpenGL::VBO::VBO(unsigned int _id)
71  : id{ _id }
72 {
73 
74 }
75 
77  glGenBuffers(1, &id);
78 }
79 
81  glDeleteBuffers(1, &id);
82 }
83 
85  this->id = rhs.id;
86  rhs.id = 0;
87 }
88 
90  this->id = rhs.id;
91  rhs.id = 0;
92  return *this;
93 }
94 
96  glBindBuffer(GL_ARRAY_BUFFER, id);
97 }
98 
99 unsigned int Eng3D::OpenGL::VBO::get_id() const {
100  return id;
101 }
102 
103 Eng3D::OpenGL::EBO::EBO(unsigned int _id)
104  : id{ _id }
105 {
106 
107 }
108 
110  glGenBuffers(1, &id);
111 }
112 
114  glDeleteBuffers(1, &id);
115 }
116 
118  this->id = rhs.id;
119  rhs.id = 0;
120 }
121 
123  this->id = rhs.id;
124  rhs.id = 0;
125  return *this;
126 }
127 
129  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
130 }
131 
132 unsigned int Eng3D::OpenGL::EBO::get_id() const {
133  return id;
134 }
135 
136 void Eng3D::draw(const Eng3D::OpenGL::VAO& vao, MeshMode mode, const void*, size_t n_indices, const void*, size_t n_buffer, int instances) {
137 #if defined E3D_BACKEND_OPENGL || defined E3D_BACKEND_GLES
138  int i_mode = 0;
139  switch(mode) {
141  i_mode = GL_TRIANGLE_FAN;
142  break;
144  i_mode = GL_TRIANGLE_STRIP;
145  break;
147  i_mode = GL_TRIANGLES;
148  break;
150  i_mode = GL_LINES;
151  break;
153  i_mode = GL_LINE_STRIP;
154  break;
155  default:
156  CXX_THROW(std::runtime_error, Eng3D::translate_format("Unknown mesh mode %i", static_cast<int>(mode)));
157  }
158 
159  vao.bind();
160  if(instances) {
161  // Instanced
162  if(n_indices)
163  glDrawElementsInstanced(i_mode, n_indices, GL_UNSIGNED_INT, nullptr, instances);
164  else if(n_buffer)
165  glDrawArraysInstanced(i_mode, 0, n_buffer, instances);
166  } else {
167  // Single-draw
168  if(n_indices)
169  glDrawElements(i_mode, n_indices, GL_UNSIGNED_INT, nullptr);
170  else if(n_buffer)
171  glDrawArrays(i_mode, 0, n_buffer);
172  }
173 #endif
174 }
175 
176 void Eng3D::upload(const Eng3D::OpenGL::VAO& vao, const Eng3D::OpenGL::VBO& vbo, const Eng3D::OpenGL::EBO& ebo, const void *buffer, size_t n_buffer, size_t sz_buffer, size_t sz_buffer_vert, size_t sz_buffer_tex, size_t v_len, size_t t_len, size_t c_len, const void *indices, size_t n_indices, size_t sz_indices, bool has_color) {
177 #if defined E3D_BACKEND_OPENGL || defined E3D_BACKEND_GLES
178  vao.bind();
179  vbo.bind();
180  glBufferData(GL_ARRAY_BUFFER, n_buffer * sz_buffer, buffer, GL_STATIC_DRAW);
181  ebo.bind();
182  glBufferData(GL_ELEMENT_ARRAY_BUFFER, n_indices * sz_indices, indices, GL_STATIC_DRAW);
183 
184  // Vertices
185  glVertexAttribPointer(0, v_len, GL_FLOAT, GL_FALSE, sz_buffer, nullptr);
186  glEnableVertexAttribArray(0);
187 
188  // Texcoords
189  size_t tex_stride = sz_buffer_vert;
190  glVertexAttribPointer(1, t_len, GL_FLOAT, GL_FALSE, sz_buffer, (void*)((uintptr_t)tex_stride));
191  glEnableVertexAttribArray(1);
192 
193  if(has_color) {
194  // Color
195  size_t color_stride = tex_stride + sz_buffer_tex;
196  glVertexAttribPointer(2, c_len, GL_FLOAT, GL_FALSE, sz_buffer, (void*)((uintptr_t)color_stride));
197  glEnableVertexAttribArray(2);
198  }
199 #endif
200 }
201 
202 void Eng3D::instancing(const Eng3D::OpenGL::VAO& vao, const Eng3D::OpenGL::VBO& i_vbo, const void *buffer, size_t index_size, int instances) {
203 #if defined E3D_BACKEND_OPENGL || defined E3D_BACKEND_GLES
204  if(!instances) return;
205 
206  vao.bind();
207  i_vbo.bind();
208  glBufferData(GL_ARRAY_BUFFER, index_size * instances, buffer, GL_STATIC_DRAW);
209  glBindBuffer(GL_ARRAY_BUFFER, 0);
210 
211  glEnableVertexAttribArray(2);
212  i_vbo.bind();
213  glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, index_size, nullptr);
214  glBindBuffer(GL_ARRAY_BUFFER, 0);
215  glVertexAttribDivisor(2, 1);
216 #endif
217 }
void bind() const
Definition: mesh.cpp:128
EBO & operator=(EBO &rhs)
Definition: mesh.cpp:122
unsigned int get_id() const
Definition: mesh.cpp:132
VAO & operator=(VAO &rhs)
Definition: mesh.cpp:56
void bind() const
Definition: mesh.cpp:62
unsigned int get_id() const
Definition: mesh.cpp:66
void bind() const
Definition: mesh.cpp:95
VBO & operator=(VBO &rhs)
Definition: mesh.cpp:89
unsigned int get_id() const
Definition: mesh.cpp:99
void upload(const Eng3D::OpenGL::VAO &vao, const Eng3D::OpenGL::VBO &vbo, const Eng3D::OpenGL::EBO &ebo, const void *buffer, size_t n_buffer, size_t sz_buffer, size_t sz_buffer_vert, size_t sz_buffer_tex, size_t v_len, size_t t_len, size_t c_len, const void *indices, size_t n_indices, size_t sz_indices, bool has_color)
Definition: mesh.cpp:176
void draw(const Eng3D::OpenGL::VAO &vao, MeshMode mode, const void *indices, size_t n_indices, const void *buffer, size_t n_buffer, int instances)
Definition: mesh.cpp:136
void instancing(const Eng3D::OpenGL::VAO &vao, const Eng3D::OpenGL::VBO &i_vbo, const void *buffer, size_t index_size, int instances)
Definition: mesh.cpp:202
std::string translate_format(const std::string_view format, Args &&... args)
String formatter, with translation.
Definition: string.hpp:128
MeshMode
Definition: mesh.hpp:81
#define CXX_THROW(class,...)
Definition: utils.hpp:98