sgltk 0.6
Simple OpenGL Tool Kit
Loading...
Searching...
No Matches
mesh.h
1#ifndef __MESH_H__
2#define __MESH_H__
3
4#include "app.h"
5#include "buffer.h"
6#include "shader.h"
7#include "camera.h"
8#include "texture.h"
9
10namespace sgltk {
11
16typedef EXPORT struct Vertex {
20 glm::vec4 position;
24 glm::vec3 normal;
28 glm::vec4 tangent;
32 glm::vec4 bitangent;
36 glm::vec4 color;
40 glm::vec3 tex_coord;
41
42 Vertex() {
43 position = {0, 0, 0, 1};
44
45 normal = {0, 0, 0};
46
47 tangent = {0, 0, 0, 1};
48
49 bitangent = glm::vec4(glm::cross(normal, glm::vec3(tangent)), 1);
50
51 color = {0, 0, 0, 0};
52 };
53
58 Vertex(glm::vec3 p, glm::vec3 n) {
59 position = glm::vec4(p, 1);
60
61 normal = n;
62
63 tangent = {0, 0, 0, 1};
64
65 bitangent = glm::vec4(glm::cross(normal, glm::vec3(tangent)), 1);
66
67 color = {0, 0, 0, 0};
68
69 tex_coord = {0, 0, 0};
70 };
71
77 Vertex(glm::vec3 p, glm::vec3 n, glm::vec3 tc) {
78 position = glm::vec4(p, 1);
79
80 normal = n;
81
82 tangent = {0, 0, 0, 1};
83
84 bitangent = glm::vec4(glm::cross(normal, glm::vec3(tangent)), 1);
85
86 color = {0, 0, 0, 0};
87
88 tex_coord = tc;
89 };
90
97 Vertex(glm::vec3 p, glm::vec3 n, glm::vec3 t, glm::vec3 tc) {
98 position = glm::vec4(p, 1);
99
100 normal = n;
101
102 tangent = glm::vec4(t, 1);
103
104 bitangent = glm::vec4(glm::cross(normal, glm::vec3(tangent)), 1);
105
106 color = {0, 0, 0, 0};
107
108 tex_coord = tc;
109 };
110
118 Vertex(glm::vec3 p, glm::vec3 n, glm::vec3 t,
119 glm::vec4 c, glm::vec3 tc) {
120 position = glm::vec4(p, 1);
121
122 normal = n;
123
124 tangent = glm::vec4(t, 1);
125
126 bitangent = glm::vec4(glm::cross(normal, glm::vec3(tangent)), 1);
127
128 color = c;
129
130 tex_coord = tc;
131 };
132} Vertex;
133
134
139class Mesh {
140 GLuint vao;
141 GLenum tf_mode;
142
143 glm::mat4 *view_matrix;
144 glm::mat4 *projection_matrix;
145
146 std::vector<std::unique_ptr<Buffer> > vbo;
147
148 GLenum index_type;
149 std::vector<std::unique_ptr<Buffer> > ibo;
150
151 std::vector<Buffer*> attached_buffers;
152 std::vector<GLuint> attached_buffers_targets;
153 std::vector<unsigned int> attached_buffers_indices;
154
155 void material_uniform();
156public:
160 unsigned int num_uv;
164 unsigned int num_col;
168 unsigned int num_vertices;
184 std::string shininess_name;
189
193 std::string model_matrix_name;
197 std::string view_matrix_name;
218
226 std::vector<glm::vec3> bounding_box;
230 glm::mat4 model_matrix;
242 glm::vec4 color_ambient;
246 glm::vec4 color_diffuse;
250 glm::vec4 color_specular;
254 std::vector<std::tuple<std::string, const Texture&, unsigned int> > auto_textures;
258 std::vector<std::tuple<std::string, const Texture&, unsigned int> > textures;
259
269
270 EXPORT Mesh();
271 EXPORT ~Mesh();
272
277 EXPORT void setup_shader(Shader *shader);
285 EXPORT bool setup_camera(glm::mat4 *view_matrix,
286 glm::mat4 *projection_matrix);
293 EXPORT bool setup_camera(Camera *camera);
300 EXPORT void set_model_matrix_name(const std::string& name);
307 EXPORT void set_view_matrix_name(const std::string& name);
314 EXPORT void set_projection_matrix_name(const std::string& name);
321 EXPORT void set_model_view_matrix_name(const std::string& name);
328 EXPORT void set_view_proj_matrix_name(const std::string& name);
336 EXPORT void set_model_view_proj_name(const std::string& name);
343 EXPORT void set_normal_matrix_name(const std::string& name);
350 EXPORT void set_ambient_color_name(const std::string& name);
357 EXPORT void set_diffuse_color_name(const std::string& name);
364 EXPORT void set_specular_color_name(const std::string& name);
372 EXPORT void set_shininess_name(const std::string& name);
380 EXPORT void set_shininess_strength_name(const std::string& name);
387 EXPORT void attach_texture(const std::string& name,
388 const sgltk::Texture& texture,
389 unsigned int index = 0);
395 EXPORT void set_transform_feedback_mode(GLenum mode);
407 EXPORT void attach_buffer(const sgltk::Buffer *buffer, GLuint target, unsigned int index = 0);
417 template <typename T>
418 unsigned int attach_vertex_buffer(const T *vertexdata,
419 unsigned int number_elements,
420 GLenum usage = GL_STATIC_DRAW);
429 template <typename T>
430 unsigned int attach_vertex_buffer(const std::vector<T>& vertexdata,
431 GLenum usage = GL_STATIC_DRAW);
439 template <typename T>
440 bool replace_buffer_data(unsigned int buffer_index,
441 const T *data,
442 unsigned int number_elements);
449 template <typename T>
450 bool replace_buffer_data(unsigned int buffer_index,
451 const std::vector<T>& data);
460 template <typename T>
461 bool replace_partial_data(unsigned int buffer_index,
462 unsigned int offset,
463 const T *data,
464 unsigned int number_elements);
472 template <typename T>
473 bool replace_partial_data(unsigned int buffer_index,
474 unsigned int offset,
475 const std::vector<T>& data);
489 template <typename T>
490 int add_vertex_attribute(std::string attrib_name,
491 GLint number_elements,
492 GLenum type,
493 const T *data,
494 GLenum usage = GL_STATIC_DRAW);
508 template <typename T>
509 int add_vertex_attribute(std::string attrib_name,
510 GLint number_elements,
511 GLenum type,
512 const std::vector<T>& data,
513 GLenum usage = GL_STATIC_DRAW);
527 template <typename T>
528 int add_vertex_attribute(int attrib_location,
529 GLint number_elements,
530 GLenum type,
531 const T *data,
532 GLenum usage = GL_STATIC_DRAW);
546 template <typename T>
547 int add_vertex_attribute(int attrib_location,
548 GLint number_elements,
549 GLenum type,
550 const std::vector<T>& data,
551 GLenum usage = GL_STATIC_DRAW);
569 EXPORT int set_buffer_vertex_attribute(const std::string& attrib_name,
570 sgltk::Buffer *buffer,
571 GLint number_elements,
572 GLenum type,
573 GLsizei stride,
574 const GLvoid *pointer,
575 unsigned int divisor = 0);
593 EXPORT int set_buffer_vertex_attribute(int attrib_location,
594 sgltk::Buffer *buffer,
595 GLint number_elements,
596 GLenum type,
597 GLsizei stride,
598 const GLvoid *pointer,
599 unsigned int divisor = 0);
619 EXPORT int set_vertex_attribute(const std::string& attrib_name,
620 unsigned int buffer_index,
621 GLint number_elements,
622 GLenum type,
623 GLsizei stride,
624 const GLvoid *pointer,
625 unsigned int divisor = 0);
645 EXPORT int set_vertex_attribute(int attrib_location,
646 unsigned int buffer_index,
647 GLint number_elements,
648 GLenum type,
649 GLsizei stride,
650 const GLvoid *pointer,
651 unsigned int divisor = 0);
660 template <typename T>
661 int attach_index_buffer(const std::vector<T>& indices);
662
669 template <typename T>
670 void compute_bounding_box(const std::vector<T>& vertexdata, unsigned int pointer);
671
677 EXPORT void draw(GLenum mode);
678
686 EXPORT void draw(GLenum mode, const glm::mat4 *model_matrix);
687
696 EXPORT void draw(GLenum mode, unsigned int index_buffer,
697 const glm::mat4 *model_matrix);
698
705 EXPORT void draw_instanced(GLenum mode, unsigned int num_instances);
706
714 EXPORT void draw_instanced(GLenum mode, unsigned int index_buffer,
715 unsigned int num_instances);
716};
717
718template <typename T>
719int Mesh::add_vertex_attribute(std::string attrib_name,
720 GLint number_elements,
721 GLenum type,
722 const T *data,
723 GLenum usage) {
724
725 std::vector<T> tmp(data, data + number_elements);
726 unsigned int buffer_index = attach_vertex_buffer<T>(tmp, usage);
727 return set_vertex_attribute(attrib_name, buffer_index, number_elements, type, 0, 0);
728}
729
730template <typename T>
731int Mesh::add_vertex_attribute(std::string attrib_name,
732 GLint number_elements,
733 GLenum type,
734 const std::vector<T>& data,
735 GLenum usage) {
736
737 unsigned int buffer_index = attach_vertex_buffer<T>(data, usage);
738 return set_vertex_attribute(attrib_name, buffer_index, number_elements, type, 0, 0);
739}
740
741template <typename T>
742int Mesh::add_vertex_attribute(int attrib_location,
743 GLint number_elements,
744 GLenum type,
745 const T *data,
746 GLenum usage) {
747
748 std::vector<T> tmp(data, data + number_elements);
749 unsigned int buffer_index = attach_vertex_buffer<T>(tmp, usage);
750 return set_vertex_attribute(attrib_location, buffer_index, number_elements, type, 0, 0);
751}
752
753template <typename T>
754int Mesh::add_vertex_attribute(int attrib_location,
755 GLint number_elements,
756 GLenum type,
757 const std::vector<T>& data,
758 GLenum usage) {
759
760 unsigned int buffer_index = attach_vertex_buffer<T>(data, usage);
761 return set_vertex_attribute(attrib_location, buffer_index, number_elements, type, 0, 0);
762}
763
764template <typename T>
765unsigned int Mesh::attach_vertex_buffer(const T *vertexdata,
766 unsigned int number_elements,
767 GLenum usage) {
768
769 std::vector<T> tmp(vertexdata, vertexdata + number_elements);
770 return attach_vertex_buffer<T>(tmp, usage);
771}
772
773template <typename T>
774unsigned int Mesh::attach_vertex_buffer(const std::vector<T>& vertexdata,
775 GLenum usage) {
776
777 std::unique_ptr<Buffer> buf = std::make_unique<Buffer>(GL_ARRAY_BUFFER);
778 buf->load<T>(vertexdata, usage);
779 vbo.push_back(std::move(buf));
780
781 return vbo.size() - 1;
782}
783
784template <typename T>
785bool Mesh::replace_buffer_data(unsigned int buffer_index,
786 const T *data,
787 unsigned int number_elements) {
788
789 std::vector<T> tmp(data, data + number_elements);
790 return replace_buffer_data<T>(buffer_index, tmp);
791}
792
793template <typename T>
794bool Mesh::replace_buffer_data(unsigned int buffer_index,
795 const std::vector<T>& data) {
796
797 if(buffer_index >= vbo.size()) {
798 App::error_string.push_back("The value of the variable"
799 "buffer_index if greater than the number"
800 "of vertex buffers.");
801 return false;
802 }
803
804 vbo[buffer_index]->replace_data(data);
805
806 return true;
807}
808
809template <typename T>
810bool Mesh::replace_partial_data(unsigned int buffer_index,
811 unsigned int offset,
812 const T *data,
813 unsigned int number_elements) {
814
815 std::vector<T> tmp(data, data + number_elements);
816 return replace_partial_data<T>(buffer_index, offset, tmp);
817}
818
819template <typename T>
820bool Mesh::replace_partial_data(unsigned int buffer_index,
821 unsigned int offset,
822 const std::vector<T>& data) {
823
824 if(buffer_index >= vbo.size()) {
825 App::error_string.push_back("The value of the variable"
826 "buffer_index if greater than the number"
827 "of vertex buffers.");
828 return false;
829 }
830
831 return vbo[buffer_index]->replace_partial_data(offset, data);
832}
833
834
835template <>
836inline int Mesh::attach_index_buffer<unsigned char>(const std::vector<unsigned char>& indices) {
837 if(index_type && index_type != GL_UNSIGNED_BYTE)
838 return -1;
839
840 index_type = GL_UNSIGNED_BYTE;
841 std::unique_ptr<Buffer> index = std::make_unique<Buffer>(GL_ELEMENT_ARRAY_BUFFER);
842 index->load<unsigned char>(indices, GL_STATIC_DRAW);
843 ibo.push_back(std::move(index));
844 return ibo.size() - 1;
845}
846
847template <>
848inline int Mesh::attach_index_buffer<unsigned short>(const std::vector<unsigned short>& indices) {
849 if(index_type && index_type != GL_UNSIGNED_SHORT)
850 return -1;
851
852 index_type = GL_UNSIGNED_SHORT;
853 std::unique_ptr<Buffer> index = std::make_unique<Buffer>(GL_ELEMENT_ARRAY_BUFFER);
854 index->load<unsigned short>(indices, GL_STATIC_DRAW);
855 ibo.push_back(std::move(index));
856 return ibo.size() - 1;
857}
858
859template <>
860inline int Mesh::attach_index_buffer<unsigned int>(const std::vector<unsigned int>& indices) {
861 if(index_type && index_type != GL_UNSIGNED_INT)
862 return -1;
863
864 index_type = GL_UNSIGNED_INT;
865 std::unique_ptr<Buffer> index = std::make_unique<Buffer>(GL_ELEMENT_ARRAY_BUFFER);
866 index->load<unsigned int>(indices, GL_STATIC_DRAW);
867 ibo.push_back(std::move(index));
868 return ibo.size() - 1;
869}
870
871template <typename T>
872void Mesh::compute_bounding_box(const std::vector<T>& vertexdata, unsigned int pointer) {
873 glm::vec3 *pos = (glm::vec3 *)(&(vertexdata)[0] + pointer);
874 bounding_box[0] = *pos;
875 bounding_box[1] = *pos;
876 for(size_t i = 1; i < vertexdata.size(); i++) {
877 pos = (glm::vec3 *)(&(vertexdata)[i] + pointer);
878 if(pos->x < bounding_box[0].x)
879 bounding_box[0].x = pos->x;
880 if(pos->y < bounding_box[0].y)
881 bounding_box[0].y = pos->y;
882 if(pos->z < bounding_box[0].z)
883 bounding_box[0].z = pos->z;
884 if(pos->x > bounding_box[1].x)
885 bounding_box[1].x = pos->x;
886 if(pos->y > bounding_box[1].y)
887 bounding_box[1].y = pos->y;
888 if(pos->z > bounding_box[1].z)
889 bounding_box[1].z = pos->z;
890 }
891}
892}
893
894#endif
static std::vector< std::string > error_string
A list of all error strings.
Definition app.h:151
Manages buffer objects.
Definition buffer.h:12
Manages cameras.
Definition camera.h:12
void set_projection_matrix_name(const std::string &name)
Sets the name of the projection matrix in the shader.
Definition mesh.cpp:84
void compute_bounding_box(const std::vector< T > &vertexdata, unsigned int pointer)
Computes the bounding box of the mesh.
Definition mesh.h:872
bool replace_partial_data(unsigned int buffer_index, unsigned int offset, const T *data, unsigned int number_elements)
Modifies the data in a vertex buffer.
Definition mesh.h:810
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.
Definition mesh.cpp:246
void attach_buffer(const sgltk::Buffer *buffer, GLuint target, unsigned int index=0)
Attaches a buffer that is automatically bound before each draw call.
Definition mesh.cpp:170
glm::vec4 color_diffuse
The diffuse color component of the material.
Definition mesh.h:246
std::string model_view_projection_matrix_name
The name of the model-view-projection matrix in the shader.
Definition mesh.h:213
std::string view_proj_matrix_name
The name of the view-projection matrix in the shader.
Definition mesh.h:209
bool twosided
Indicates that the mesh should not be drawn using back face culling.
Definition mesh.h:268
std::vector< std::tuple< std::string, const Texture &, unsigned int > > textures
Attached user textures.
Definition mesh.h:258
std::string ambient_color_name
The name of the ambient materiel component.
Definition mesh.h:172
std::string diffuse_color_name
The name of the diffuse materiel component.
Definition mesh.h:176
void set_shininess_name(const std::string &name)
Sets the name of the shininess of the material in the shader.
Definition mesh.cpp:145
void set_model_view_matrix_name(const std::string &name)
Sets the name of the model-view matrix in the shader.
Definition mesh.cpp:92
void set_normal_matrix_name(const std::string &name)
Sets the name of the normal matrix in the shader.
Definition mesh.cpp:116
std::string projection_matrix_name
The name of the projection matrix in the shader.
Definition mesh.h:201
unsigned int attach_vertex_buffer(const T *vertexdata, unsigned int number_elements, GLenum usage=GL_STATIC_DRAW)
Loads data into memory.
Definition mesh.h:765
std::vector< std::tuple< std::string, const Texture &, unsigned int > > auto_textures
Attached textures.
Definition mesh.h:254
int attach_index_buffer(const std::vector< T > &indices)
Attaches an index array to the mesh.
void attach_texture(const std::string &name, const sgltk::Texture &texture, unsigned int index=0)
Attaches a texture to the mesh.
Definition mesh.cpp:159
void draw_instanced(GLenum mode, unsigned int num_instances)
Renders the mesh multiple times.
Definition mesh.cpp:433
void draw(GLenum mode)
Renders the mesh using the first index buffer.
Definition mesh.cpp:334
int add_vertex_attribute(std::string attrib_name, GLint number_elements, GLenum type, const T *data, GLenum usage=GL_STATIC_DRAW)
This is a convenience function that combines attach_vertex_buffer and set_vertex_attribute.
Definition mesh.h:719
void set_ambient_color_name(const std::string &name)
Sets the name of the ambient color in the shader.
Definition mesh.cpp:124
std::string model_matrix_name
The name of the model matrix in the shader.
Definition mesh.h:193
void set_model_matrix_name(const std::string &name)
Sets the name of the model matrix in the shader.
Definition mesh.cpp:68
bool replace_buffer_data(unsigned int buffer_index, const T *data, unsigned int number_elements)
Overwrites all data in a vertex buffer.
Definition mesh.h:785
std::vector< glm::vec3 > bounding_box
The bounding box.
Definition mesh.h:226
std::string shininess_strength_name
The name of the specular exponent.
Definition mesh.h:188
void set_diffuse_color_name(const std::string &name)
Sets the name of the diffuse color in the shader.
Definition mesh.cpp:131
std::string normal_matrix_name
The name of the normal matrix in the shader.
Definition mesh.h:217
unsigned int num_col
Number of vertex colors.
Definition mesh.h:164
void set_shininess_strength_name(const std::string &name)
Sets the name of the shininess strength of the material in the shader.
Definition mesh.cpp:152
void setup_shader(Shader *shader)
Specifies the shader to use to render the mesh.
Definition mesh.cpp:45
std::string model_view_matrix_name
The name of the model-view matrix in the shader.
Definition mesh.h:205
Shader * shader
The shader being used to draw the mesh.
Definition mesh.h:222
float shininess_strength
The strength of the shininess of the material.
Definition mesh.h:238
glm::vec4 color_ambient
The ambient color component of the material.
Definition mesh.h:242
void set_specular_color_name(const std::string &name)
Sets the name of the specular color in the shader.
Definition mesh.cpp:138
void set_model_view_proj_name(const std::string &name)
Sets the name of the model-view-projection matrix in the shader.
Definition mesh.cpp:108
std::string view_matrix_name
The name of the view matrix in the shader.
Definition mesh.h:197
std::string shininess_name
The name of the specular factor.
Definition mesh.h:184
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.
Definition mesh.cpp:49
glm::vec4 color_specular
The specular color component of the material.
Definition mesh.h:250
unsigned int num_vertices
Number of vertices.
Definition mesh.h:168
void set_transform_feedback_mode(GLenum mode)
Sets the output type for transform feedback operations.
Definition mesh.cpp:166
glm::mat4 model_matrix
The model matrix.
Definition mesh.h:230
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.
Definition mesh.cpp:179
unsigned int num_uv
Number of texture coordinates.
Definition mesh.h:160
std::string specular_color_name
The name of the specular materiel component.
Definition mesh.h:180
void set_view_proj_matrix_name(const std::string &name)
Sets the name of the view-projection matrix in the shader.
Definition mesh.cpp:100
bool wireframe
Indicates that the mesh should be drawn as a wireframe.
Definition mesh.h:263
float shininess
The shininess of the material.
Definition mesh.h:234
void set_view_matrix_name(const std::string &name)
Sets the name of the view matrix in the shader.
Definition mesh.cpp:76
Manager shaders.
Definition shader.h:12
Manages textures.
Definition texture.h:13
Vertex(glm::vec3 p, glm::vec3 n)
Definition mesh.h:58
glm::vec3 normal
Vertex normal.
Definition mesh.h:24
glm::vec4 bitangent
Vertex bi-tangent.
Definition mesh.h:32
glm::vec4 color
Vertex color.
Definition mesh.h:36
glm::vec3 tex_coord
Vertex texture coordinates.
Definition mesh.h:40
Vertex(glm::vec3 p, glm::vec3 n, glm::vec3 t, glm::vec4 c, glm::vec3 tc)
Definition mesh.h:118
glm::vec4 tangent
Vertex tangent.
Definition mesh.h:28
glm::vec4 position
Vertex position.
Definition mesh.h:20
Vertex(glm::vec3 p, glm::vec3 n, glm::vec3 t, glm::vec3 tc)
Definition mesh.h:97
Vertex(glm::vec3 p, glm::vec3 n, glm::vec3 tc)
Definition mesh.h:77