26 draw(m, model, view, projection, texture);
31 draw(m,
Identity(), camera, texture);
50 draw(m, model, view, projection);
75 draw(group, m, model, view, projection);
100 draw(group, m, model, view, projection, texture);
105 draw(group, m,
Identity(), camera, texture);
117 std::string definitions;
119 if(flags & USE_TEXCOORD)
120 definitions.append(
"#define USE_TEXCOORD\n");
121 if(flags & USE_NORMAL)
122 definitions.append(
"#define USE_NORMAL\n");
123 if(flags & USE_COLOR)
124 definitions.append(
"#define USE_COLOR\n");
126 if((flags & USE_TEXCOORD) && (flags & USE_ALPHATEST))
127 definitions.append(
"#define USE_ALPHATEST\n");
129 if(flags & USE_SHADING)
131 definitions.append(
"#define USE_SHADING\n");
133 if(flags & USE_MATERIAL)
134 definitions.append(
"#define USE_MATERIAL\n");
135 if((flags & USE_LIGHT))
136 definitions.append(
"#define USE_LIGHT\n");
138 definitions.append(
"#define USE_SUN\n");
140 definitions.append(
"#define USE_SKY\n");
144 const char *filename=
smart_path(
"data/shaders/mesh.glsl");
145 bool use_mesh_color= (primitives == GL_POINTS || primitives == GL_LINES || primitives == GL_LINE_STRIP || primitives == GL_LINE_LOOP);
147 filename=
smart_path(
"data/shaders/mesh_color.glsl");
150 return cache->program;
155 const char *filename=
smart_path(
"data/shaders/normals.glsl");
156 bool use_mesh_color= (primitives == GL_POINTS || primitives == GL_LINES || primitives == GL_LINE_STRIP || primitives == GL_LINE_LOOP);
162 return cache->program;
167 const char *filename=
smart_path(
"data/shaders/texcoords.glsl");
169 return cache->program;
172unsigned DrawParam::create_flags(
const bool use_texcoord,
const bool use_normal,
const bool use_color )
174 unsigned options= flags;
182unsigned DrawParam::update(
unsigned &flags,
const unsigned option,
const bool use )
185 flags= flags | option;
187 flags= flags & ~option;
191bool DrawParam::flag(
const unsigned option )
193 return flags & option;
198 if(mesh.index_count())
199 draw(0, mesh.index_count(), mesh);
201 draw(0, mesh.vertex_count(), mesh);
217 bool use_texture= m_texture;
218 if(flag(USE_MATERIAL))
219 use_texture= use_texture || m_material.diffuse_texture || m_material.specular_texture || m_material.ns_texture;
220 bool use_texcoord= use_texture && mesh.has_texcoord();
221 bool use_normal= mesh.has_normal();
222 bool use_color= mesh.has_color();
224 Transform mv= m_view * m_model;
225 Transform mvp= m_projection * mv;
228 if(flag(DEBUG_TEXCOORDS))
236 update(flags, USE_LIGHT | USE_TEXTURE | USE_ALPHATEST,
false);
238 glUseProgram(program);
240 program_uniform(program,
"mvpMatrix", mvp);
241 program_uniform(program,
"mvMatrix", mv);
243 mesh.
draw(first, count, program);
252 glUseProgram(program);
254 program_uniform(program,
"mesh_color", mesh.
default_color());
256 program_uniform(program,
"mvpMatrix", mvp);
259 if(use_texcoord && m_texture > 0)
262 if(flag(USE_ALPHATEST))
263 program_uniform(program,
"alpha_min", m_alpha_min);
265 if(flag(USE_SHADING))
268 program_uniform(program,
"normalMatrix", mv.
normal());
269 if(!use_normal || flag(USE_LIGHT | USE_MATERIAL))
270 program_uniform(program,
"mvMatrix", mv);
272 if(flag(USE_MATERIAL))
274 program_uniform(program,
"diffuse_color", m_material.diffuse);
275 program_uniform(program,
"specular_color", m_material.specular);
276 program_uniform(program,
"ns", m_material.ns);
281 static GLuint white_texture= 0;
282 if(white_texture == 0)
284 ImageData data(16, 16, 4);
285 for(
unsigned y= 0; y < data.height; y++)
286 for(
unsigned x= 0; x < data.width; x++)
288 unsigned char *pixel= data.pixels.data() + data.offset(x, y);
289 for(
unsigned i= 0; i < data.channels; i++)
296 if(m_material.diffuse_texture > 0)
301 if(m_material.specular_texture > 0)
306 if(m_material.specular_texture > 0)
315 program_uniform(program,
"light", m_view(m_light));
316 program_uniform(program,
"light_color", m_light_color);
320 program_uniform(program,
"sun", m_view(m_sun));
321 program_uniform(program,
"sun_color", m_sun_color);
325 program_uniform(program,
"sky", m_view(m_sky));
326 program_uniform(program,
"sky_color", m_sky_color);
330 mesh.
draw(first, count, program);
333 if(flag(DEBUG_NORMALS))
341 update(flags, USE_SHADING | USE_LIGHT | USE_TEXTURE | USE_ALPHATEST,
false);
343 static float scale= 1;
351 glUseProgram(program);
352 program_uniform(program,
"mvpMatrix", mvp);
353 program_uniform(program,
"normalMatrix", mv.
normal());
354 program_uniform(program,
"scale", scale * m_normals_scale);
356 mesh.
draw(first, count, program);
GLuint create_program(const GLenum primitives, const unsigned flags)
construit un shader program configure pour l'ensemble d'options.
GLuint create_debug_texcoords_program(const GLenum primitives, const unsigned flags)
construit un shader program / debug des coordonnees de texture.
void draw(Mesh &mesh)
dessine l'objet avec l'ensemble des parametres definis.
GLuint create_debug_normals_program(const GLenum primitives, const unsigned flags)
construit un shader program / debug des normales.
DrawParam & model(const Transform &m)
modifie la transformation model utilisee pour afficher l'objet.
DrawParam & view(const Transform &m)
modifie la transformation view utilisee pour afficher l'objet.
DrawParam & projection(const Transform &m)
modifie la transformation projection utilisee pour afficher l'objet.
DrawParam & texture(const GLuint t)
plaque une texture opaque a la surface de l'objet.
representation d'un objet / maillage.
Color default_color() const
renvoie la couleur par defaut du mesh, utilisee si les sommets n'ont pas de couleur associee.
void draw(const GLuint program)
dessine l'objet avec un shader program.
GLenum primitives() const
renvoie le type de primitives.
representation de la camera, type orbiter, placee sur une sphere autour du centre de l'objet.
PipelineProgram * find(const char *filename, const char *definitions="")
renvoie un shader program compile. et l'ajoute dans le cache...
static PipelineCache & manager()
acces au singleton.
const char * smart_path(const char *filename)
renvoie le chemin(path) vers le fichier 'filename' apres l'avoir cherche dans un repertoire standard....
int window_height()
renvoie la hauteur de la fenetre de l'application.
int key_state(const SDL_Keycode key)
renvoie l'etat d'une touche du clavier. cf la doc SDL2 pour les codes.
int window_width()
renvoie la largeur de la fenetre de l'application.
Transform Identity()
construit la transformation identite.
int first
premier triangle du groupe
int n
nombre de triangles du groupe
@ USE_COLOR
inclut l'attribut couleur dans les buffers.
@ USE_TEXCOORD
inclut l'attribut coordonnees de texture dans les buffers.
@ USE_NORMAL
inclut l'attribut normale dans les buffers.
representation d'un ensemble de triangles de meme matiere.
GLuint make_texture(const int unit, const int width, const int height, const GLenum texel_type, const GLenum data_format, const GLenum data_type)
creation de textures filtrables / mipmaps
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 filtrage,...
description d'un shader program compile.