13 glGenVertexArrays(1, &vao);
15 view_matrix =
nullptr;
16 projection_matrix =
nullptr;
18 bounding_box = { glm::vec3(0, 0, 0), glm::vec3(0, 0, 0) };
42 glDeleteVertexArrays(1, &vao);
50 glm::mat4 *projection_matrix) {
51 if(!view_matrix || !projection_matrix)
54 this->view_matrix = view_matrix;
55 this->projection_matrix = projection_matrix;
102 if(name.length() > 0)
110 if(name.length() > 0)
118 if(name.length() > 0)
125 if(name.length() > 0)
132 if(name.length() > 0)
139 if(name.length() > 0)
146 if(name.length() > 0)
153 if(name.length() > 0)
161 unsigned int index) {
163 textures.push_back({name, texture, index});
172 unsigned int index) {
174 attached_buffers.push_back(
const_cast<Buffer*
>(buffer));
175 attached_buffers_targets.push_back(target);
176 attached_buffers_indices.push_back(index);
180 unsigned int buffer_index,
181 GLint number_elements,
184 const GLvoid *pointer,
185 unsigned int divisor) {
191 int loc =
shader->get_attribute_location(attrib_name);
197 stride, pointer, divisor);
201 unsigned int buffer_index,
202 GLint number_elements,
205 const GLvoid *pointer,
206 unsigned int divisor) {
208 if(attrib_location < 0) {
212 if(buffer_index >= vbo.size()) {
216 glBindVertexArray(vao);
217 vbo[buffer_index]->bind();
219 glEnableVertexAttribArray(attrib_location);
222 case GL_UNSIGNED_BYTE:
224 case GL_UNSIGNED_SHORT:
226 case GL_UNSIGNED_INT:
227 glVertexAttribIPointer(attrib_location, number_elements,
228 type, stride, (
void *)pointer);
231 glVertexAttribLPointer(attrib_location, number_elements,
232 type, stride, (
void *)pointer);
235 glVertexAttribPointer(attrib_location, number_elements,
236 type, GL_FALSE, stride,
241 glVertexAttribDivisor(attrib_location, divisor);
242 glBindVertexArray(0);
248 GLint number_elements,
251 const GLvoid *pointer,
252 unsigned int divisor) {
258 int loc =
shader->get_attribute_location(attrib_name);
264 stride, pointer, divisor);
269 GLint number_elements,
272 const GLvoid *pointer,
273 unsigned int divisor) {
275 if(attrib_location < 0) {
279 glBindVertexArray(vao);
282 glEnableVertexAttribArray(attrib_location);
285 case GL_UNSIGNED_BYTE:
287 case GL_UNSIGNED_SHORT:
289 case GL_UNSIGNED_INT:
290 glVertexAttribIPointer(attrib_location, number_elements,
291 type, stride, (
void *)pointer);
294 glVertexAttribLPointer(attrib_location, number_elements,
295 type, stride, (
void *)pointer);
298 glVertexAttribPointer(attrib_location, number_elements,
299 type, GL_FALSE, stride,
304 glVertexAttribDivisor(attrib_location, divisor);
305 glBindVertexArray(0);
309void Mesh::material_uniform() {
317 int num_textures = 0;
320 if(texture_loc >= 0) {
322 const_cast<Texture&
>(std::get<1>(tex)).bind(num_textures++);
326 texture_loc =
shader->get_uniform_location(std::get<0>(tex));
327 if(texture_loc >= 0) {
328 shader->set_uniform(texture_loc + std::get<2>(tex), num_textures);
329 const_cast<Texture&
>(std::get<1>(tex)).bind(num_textures++);
335 draw(mode, 0,
nullptr);
343 unsigned int index_buffer,
362 NM = glm::transpose(glm::inverse(glm::mat3(M)));
366 MV = (*view_matrix) * M;
370 if(projection_matrix) {
372 MVP = (*projection_matrix) * MV;
373 VP = (*projection_matrix) * (*view_matrix);
376 MVP = (*projection_matrix) * M;
386 for(
unsigned int i = 0; i < attached_buffers.size(); i++) {
387 attached_buffers[i]->bind(attached_buffers_targets[i],
388 attached_buffers_indices[i]);
391 glBindVertexArray(vao);
392 ibo[index_buffer]->bind();
393 if(
shader->transform_feedback) {
394 GLenum primitive_type = tf_mode;
395 if(primitive_type == GL_NONE) {
398 primitive_type = GL_POINTS;
403 case GL_LINES_ADJACENCY:
404 case GL_LINE_STRIP_ADJACENCY:
405 primitive_type = GL_LINES;
408 case GL_TRIANGLE_STRIP:
409 case GL_TRIANGLE_FAN:
410 case GL_TRIANGLES_ADJACENCY:
411 case GL_TRIANGLE_STRIP_ADJACENCY:
412 primitive_type = GL_TRIANGLES;
418 glBeginTransformFeedback(primitive_type);
420 glDrawElements(mode, ibo[index_buffer]->num_elements,
421 index_type, (
void*)0);
422 if(
shader->transform_feedback) {
423 glEndTransformFeedback();
425 ibo[index_buffer]->unbind();
426 glBindVertexArray(0);
428 for(
unsigned int i = 0; i < attached_buffers.size(); i++) {
429 attached_buffers[i]->unbind();
438 unsigned int num_instances) {
445 glm::mat4 VP = (*projection_matrix) * (*view_matrix);
453 for(
unsigned int i = 0; i < attached_buffers.size(); i++) {
454 attached_buffers[i]->bind(attached_buffers_targets[i],
455 attached_buffers_indices[i]);
458 glBindVertexArray(vao);
459 ibo[index_buffer]->bind();
460 if(
shader->transform_feedback) {
461 GLenum primitive_type = tf_mode;
462 if(primitive_type == GL_NONE) {
465 primitive_type = GL_POINTS;
470 case GL_LINES_ADJACENCY:
471 case GL_LINE_STRIP_ADJACENCY:
472 primitive_type = GL_LINES;
475 case GL_TRIANGLE_STRIP:
476 case GL_TRIANGLE_FAN:
477 case GL_TRIANGLES_ADJACENCY:
478 case GL_TRIANGLE_STRIP_ADJACENCY:
479 primitive_type = GL_TRIANGLES;
485 glBeginTransformFeedback(primitive_type);
487 glDrawElementsInstanced(mode, ibo[index_buffer]->num_elements,
488 index_type, (
void*)0, num_instances);
489 if(
shader->transform_feedback) {
490 glEndTransformFeedback();
492 ibo[index_buffer]->unbind();
493 glBindVertexArray(0);
495 for(
unsigned int i = 0; i < attached_buffers.size(); i++) {
496 attached_buffers[i]->unbind();
static std::vector< std::string > error_string
A list of all error strings.
void bind()
Binds the buffer object to the target it was previously bound to or GL_ARRAY_BUFFER if the buffer has...
glm::mat4 view_matrix
The view matrix.
glm::mat4 projection_matrix
The perspective projection matrix.
void set_projection_matrix_name(const std::string &name)
Sets the name of the projection matrix in the shader.
int set_buffer_vertex_attribute(const std::string &attrib_name, sgltk::Buffer *buffer, GLint number_elements, GLenum type, GLsizei stride, const GLvoid *pointer, unsigned int divisor=0)
Sets pointers to vertex attributes.
void attach_buffer(const sgltk::Buffer *buffer, GLuint target, unsigned int index=0)
Attaches a buffer that is automatically bound before each draw call.
glm::vec4 color_diffuse
The diffuse color component of the material.
std::string model_view_projection_matrix_name
The name of the model-view-projection matrix in the shader.
std::string view_proj_matrix_name
The name of the view-projection matrix in the shader.
std::vector< std::tuple< std::string, const Texture &, unsigned int > > textures
Attached user textures.
std::string ambient_color_name
The name of the ambient materiel component.
std::string diffuse_color_name
The name of the diffuse materiel component.
void set_shininess_name(const std::string &name)
Sets the name of the shininess of the material in the shader.
void set_model_view_matrix_name(const std::string &name)
Sets the name of the model-view matrix in the shader.
void set_normal_matrix_name(const std::string &name)
Sets the name of the normal matrix in the shader.
std::string projection_matrix_name
The name of the projection matrix in the shader.
std::vector< std::tuple< std::string, const Texture &, unsigned int > > auto_textures
Attached textures.
void attach_texture(const std::string &name, const sgltk::Texture &texture, unsigned int index=0)
Attaches a texture to the mesh.
void draw_instanced(GLenum mode, unsigned int num_instances)
Renders the mesh multiple times.
void draw(GLenum mode)
Renders the mesh using the first index buffer.
void set_ambient_color_name(const std::string &name)
Sets the name of the ambient color in the shader.
std::string model_matrix_name
The name of the model matrix in the shader.
void set_model_matrix_name(const std::string &name)
Sets the name of the model matrix in the shader.
std::vector< glm::vec3 > bounding_box
The bounding box.
std::string shininess_strength_name
The name of the specular exponent.
void set_diffuse_color_name(const std::string &name)
Sets the name of the diffuse color in the shader.
std::string normal_matrix_name
The name of the normal matrix in the shader.
unsigned int num_col
Number of vertex colors.
void set_shininess_strength_name(const std::string &name)
Sets the name of the shininess strength of the material in the shader.
void setup_shader(Shader *shader)
Specifies the shader to use to render the mesh.
std::string model_view_matrix_name
The name of the model-view matrix in the shader.
Shader * shader
The shader being used to draw the mesh.
float shininess_strength
The strength of the shininess of the material.
glm::vec4 color_ambient
The ambient color component of the material.
void set_specular_color_name(const std::string &name)
Sets the name of the specular color in the shader.
void set_model_view_proj_name(const std::string &name)
Sets the name of the model-view-projection matrix in the shader.
std::string view_matrix_name
The name of the view matrix in the shader.
std::string shininess_name
The name of the specular factor.
bool setup_camera(glm::mat4 *view_matrix, glm::mat4 *projection_matrix)
Sets up the view and projection matrices that will be used by the mesh.
glm::vec4 color_specular
The specular color component of the material.
unsigned int num_vertices
Number of vertices.
void set_transform_feedback_mode(GLenum mode)
Sets the output type for transform feedback operations.
glm::mat4 model_matrix
The model matrix.
int set_vertex_attribute(const std::string &attrib_name, unsigned int buffer_index, GLint number_elements, GLenum type, GLsizei stride, const GLvoid *pointer, unsigned int divisor=0)
Sets pointers to vertex attributes.
unsigned int num_uv
Number of texture coordinates.
std::string specular_color_name
The name of the specular materiel component.
void set_view_proj_matrix_name(const std::string &name)
Sets the name of the view-projection matrix in the shader.
float shininess
The shininess of the material.
void set_view_matrix_name(const std::string &name)
Sets the name of the view matrix in the shader.
void set_uniform(int location, T v0)
Sets the uniform.
int get_uniform_location(const std::string &name)
Returns the location of a uniform variable.