15 m_primitives= primitives;
25 for(std::unordered_map<unsigned int, GLuint>::iterator it= m_state_map.begin(); it != m_state_map.end(); ++it)
39 m_colors.push_back(color);
45 m_normals.push_back(normal);
51 m_texcoords.push_back(uv);
58 m_positions.push_back(position);
61 if(m_texcoords.size() > 0 && m_texcoords.size() != m_positions.size())
62 m_texcoords.push_back(m_texcoords.back());
63 if(m_normals.size() > 0 && m_normals.size() != m_positions.size())
64 m_normals.push_back(m_normals.back());
65 if(m_colors.size() > 0 && m_colors.size() != m_positions.size())
66 m_colors.push_back(m_colors.back());
68 unsigned int index= (
unsigned int) m_positions.size() -1;
74 case GL_TRIANGLE_STRIP:
76 m_indices.push_back(index);
89 assert(
id < m_colors.size());
90 m_update_buffers=
true;
97 assert(
id < m_normals.size());
98 m_update_buffers=
true;
105 assert(
id < m_texcoords.size());
106 m_update_buffers=
true;
113 assert(
id < m_positions.size());
114 m_update_buffers=
true;
121 assert(a < m_positions.size());
122 assert(b < m_positions.size());
123 assert(c < m_positions.size());
124 m_indices.push_back(a);
125 m_indices.push_back(b);
126 m_indices.push_back(c);
135 m_indices.push_back((
int) m_positions.size() + a);
136 m_indices.push_back((
int) m_positions.size() + b);
137 m_indices.push_back((
int) m_positions.size() + c);
143 m_indices.push_back(~0u);
145 glPrimitiveRestartIndex(~0u);
146 glEnable(GL_PRIMITIVE_RESTART);
148 glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX);
155 if(m_positions.size() < 1)
158 pmin=
Point(m_positions[0]);
161 for(
unsigned int i= 1; i < (
unsigned int) m_positions.size(); i++)
163 vec3 p= m_positions[i];
164 pmin=
Point( std::min(pmin.x, p.x), std::min(pmin.y, p.y), std::min(pmin.z, p.z) );
165 pmax=
Point( std::max(pmax.x, p.x), std::max(pmax.y, p.y), std::max(pmax.z, p.z) );
178 default:
return nullptr;
198 if(m_positions.size() == 0)
205 if(m_indices.size() > 0)
209 if(m_texcoords.size() > 0 && m_texcoords.size() < m_positions.size() && use_texcoord)
210 printf(
"[error] invalid texcoords array...\n");
211 if(m_normals.size() > 0 && m_normals.size() < m_positions.size() && use_normal)
212 printf(
"[error] invalid normals array...\n");
213 if(m_colors.size() > 0 && m_colors.size() < m_positions.size() && use_color)
214 printf(
"[error] invalid colors array...\n");
217 if(m_texcoords.size() == m_positions.size() && use_texcoord)
219 if(m_normals.size() == m_positions.size() && use_normal)
221 if(m_colors.size() == m_positions.size() && use_color)
224 m_update_buffers=
false;
228 int Mesh::update_buffers(
const bool use_texcoord,
const bool use_normal,
const bool use_color )
231 if(!m_update_buffers)
234 glBindVertexArray(m_vao);
238 if(m_texcoords.size() == m_positions.size() && use_texcoord)
240 if(m_normals.size() == m_positions.size() && use_normal)
242 if(m_colors.size() == m_positions.size() && use_color)
245 m_update_buffers=
false;
250 GLuint
Mesh::create_program(
const bool use_texcoord,
const bool use_normal,
const bool use_color,
const bool use_light )
252 std::string definitions;
254 if(m_texcoords.size() == m_positions.size() && use_texcoord)
255 definitions.append(
"#define USE_TEXCOORD\n");
256 if(m_normals.size() == m_positions.size() && use_normal)
257 definitions.append(
"#define USE_NORMAL\n");
258 if(m_colors.size() == m_positions.size() && use_color)
259 definitions.append(
"#define USE_COLOR\n");
261 definitions.append(
"#define USE_LIGHT\n");
263 bool use_mesh_color= (m_primitives == GL_POINTS || m_primitives == GL_LINES || m_primitives == GL_LINE_STRIP || m_primitives == GL_LINE_LOOP);
265 return read_program(
"data/shaders/mesh.glsl", definitions.c_str());
267 return read_program(
"data/shaders/mesh_color.glsl", definitions.c_str());
272 const bool use_light,
const Point& light,
const Color& light_color,
273 const bool use_texture,
const GLuint texture )
275 bool use_texcoord= (m_texcoords.size() == m_positions.size() && texture > 0);
276 bool use_normal= (m_normals.size() == m_positions.size());
277 bool use_color= (m_colors.size() == m_positions.size());
283 update_buffers(
true,
true,
true);
286 if(use_texcoord) key= key | 1;
287 if(use_normal) key= key | 2;
288 if(use_color) key= key | 4;
289 if(use_texture) key= key | 8;
290 if(use_light) key= key | 16;
294 m_program= m_state_map[key];
299 m_program=
create_program(use_texcoord, use_normal, use_color, use_light);
303 m_state_map[key]= m_program;
309 glBindVertexArray(m_vao);
310 glUseProgram(m_program);
315 Transform mvp= projection * view * model;
322 if(texture && use_texcoord && use_texture)
331 if(m_indices.size() > 0)
332 glDrawElements(m_primitives, (GLsizei) m_indices.size(), GL_UNSIGNED_INT, 0);
334 glDrawArrays(m_primitives, 0, (GLsizei) m_positions.size());
Mesh & restart_strip()
demarre un nouveau strip. a utiliser avec un objet composes de GL_TRIANGLE_STRIP, doit aussi fonction...
const float * normal_buffer() const
renvoie l'adresse de la normale du premier sommet. par convention, la normale est un vec3...
std::size_t index_buffer_size() const
renvoie la taille (en octets) de l'index buffer.
const float * vertex_buffer() const
renvoie l'adresse de la position du premier sommet. permet de construire les vertex buffers openGL...
void bounds(Point &pmin, Point &pmax)
renvoie min et max les coordonnees des extremites des positions des sommets de l'objet (boite engloba...
vecteur generique, utilitaire.
bool update_vertex_buffer(const GLuint vao, const GLint attribute, const size_t data_size, const void *data)
modifie le contenu d'un vertex buffer associe au vertex format, vao.
void draw(const Transform &model, const Transform &view, const Transform &projection, const bool use_light, const Point &light, const Color &light_color, const bool use_texture, const GLuint texture)
vecteur generique, utilitaire.
std::size_t normal_buffer_size() const
renvoie la longueur (en octets) du normal buffer.
Mesh & color(const vec4 &c)
definit la couleur du prochain sommet.
representation d'un objet / maillage.
void release_vertex_format(const GLuint vao)
detruit le vertex array vao et les buffers associes, crees par make_vertex_buffer() et make_index_buf...
representation d'une couleur (rgba) transparente ou opaque.
std::size_t vertex_buffer_size() const
renvoie la longueur (en octets) du vertex buffer.
Mesh & triangle(const unsigned int a, const unsigned int b, const unsigned int c)
GLuint create_program(const bool use_texcoord=true, const bool use_normal=true, const bool use_color=true, const bool use_light=false)
construit un shader program configure.
GLuint create_buffers(const bool use_texcoord=true, const bool use_normal=true, const bool use_color=true)
const float * texcoord_buffer() const
renvoie l'adresse des coordonnees de textures du premier sommet. par convention, c'est un vec2...
unsigned int vertex(const vec3 &p)
insere un sommet de position p, et ses attributs (s'ils sont definis par color(), texcoord()...
vecteur generique 4d, ou 3d homogene, utilitaire.
std::size_t attribute_buffer_size(const unsigned int id) const
renvoie la taille (en octets) d'un attribut.
void program_uniform(const GLuint program, const char *uniform, const unsigned int v)
affecte une valeur a un uniform du shader program. uint.
void program_use_texture(const GLuint program, const char *uniform, const int unit, const GLuint texture, const GLuint sampler)
configure le pipeline et le shader program pour utiliser une texture, et des parametres de filtrages...
GLuint create_vertex_format()
cree un vertex array. a detruire avec release_vertex_format( ).
const void * attribute_buffer(const unsigned int id) const
GLuint make_index_buffer(const GLuint vao, const size_t data_size, const void *data)
cree un index buffer et configure le vertex array vao. detruit par release_vertex_format( )...
int create(const GLenum primitives)
construit les objets openGL.
std::size_t color_buffer_size() const
renvoie la taille (en octets) du color buffer.
void release()
detruit les objets openGL.
int program_print_errors(const GLuint program)
affiche les erreurs de compilation.
const void * index_buffer() const
renvoie l'adresse du premier indice du premier triangle. par convention c'est un uint, 1, GL_UNSIGNED_INT.
GLuint read_program(const char *filename, const char *definitions)
std::size_t texcoord_buffer_size() const
renvoie la taille (en octets) du texcoord buffer.
Mesh & triangle_last(const int a, const int b, const int c)
void printf(Text &text, const int px, const int py, const char *format,...)
affiche un texte a la position x, y. meme utilisation que printf().
int release_program(const GLuint program)
detruit les shaders et le program.
representation d'un point 3d.
Mesh & normal(const vec3 &n)
definit la normale du prochain sommet.
GLuint make_vertex_buffer(const GLuint vao, const GLint attribute, const int size, const GLenum type, const size_t data_size, const void *data)
cree un vertex buffer et configure le vertex array vao. detruit par release_vertex_format( )...
Mesh & texcoord(const vec2 &uv)
definit les coordonnees de texture du prochain sommet.
const float * color_buffer() const
renvoie l'adresse de la couleur du premier sommet. par convention, la couleur est un vec4...
Color default_color() const
renvoie la couleur par defaut du mesh, utilisee si les sommets n'ont pas de couleur associee...