216{
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();
223
224 Transform mv= m_view * m_model;
225 Transform mvp= m_projection * mv;
226
227 GLuint program= 0;
228 if(flag(DEBUG_TEXCOORDS))
229 {
231 if(program > 0)
232 {
233 use_color= false;
234 use_normal= false;
235 use_texcoord= true;
236 update(flags, USE_LIGHT | USE_TEXTURE | USE_ALPHATEST, false);
237
238 glUseProgram(program);
239
240 program_uniform(program, "mvpMatrix", mvp);
241 program_uniform(program, "mvMatrix", mv);
242
243 mesh.
draw(first, count, program);
244 return;
245 }
246 }
247
248
250 assert(program > 0);
251
252 glUseProgram(program);
253 if(!use_color)
254 program_uniform(program,
"mesh_color", mesh.
default_color());
255
256 program_uniform(program, "mvpMatrix", mvp);
257
258
259 if(use_texcoord && m_texture > 0)
261
262 if(flag(USE_ALPHATEST))
263 program_uniform(program, "alpha_min", m_alpha_min);
264
265 if(flag(USE_SHADING))
266 {
267 if(use_normal)
268 program_uniform(program,
"normalMatrix", mv.
normal());
269 if(!use_normal || flag(USE_LIGHT | USE_MATERIAL))
270 program_uniform(program, "mvMatrix", mv);
271
272 if(flag(USE_MATERIAL))
273 {
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);
277
278 if(use_texcoord)
279 {
280
281 static GLuint white_texture= 0;
282 if(white_texture == 0)
283 {
284 ImageData data(16, 16, 4);
285 for(unsigned y= 0; y < data.height; y++)
286 for(unsigned x= 0; x < data.width; x++)
287 {
288 unsigned char *pixel= data.pixels.data() + data.offset(x, y);
289 for(unsigned i= 0; i < data.channels; i++)
290 pixel[i]= 255;
291 }
292
294 }
295
296 if(m_material.diffuse_texture > 0)
298 else
300
301 if(m_material.specular_texture > 0)
303 else
305
306 if(m_material.specular_texture > 0)
308 else
310 }
311 }
312
313 if(flag(USE_LIGHT))
314 {
315 program_uniform(program, "light", m_view(m_light));
316 program_uniform(program, "light_color", m_light_color);
317 }
318 if(flag(USE_SUN))
319 {
320 program_uniform(program, "sun", m_view(m_sun));
321 program_uniform(program, "sun_color", m_sun_color);
322 }
323 if(flag(USE_SKY))
324 {
325 program_uniform(program, "sky", m_view(m_sky));
326 program_uniform(program, "sky_color", m_sky_color);
327 }
328 }
329
330 mesh.
draw(first, count, program);
331
332
333 if(flag(DEBUG_NORMALS))
334 {
336 if(program > 0)
337 {
338 use_color= false;
339 use_normal= true;
340 use_texcoord= false;
341 update(flags, USE_SHADING | USE_LIGHT | USE_TEXTURE | USE_ALPHATEST, false);
342
343 static float scale= 1;
345 scale+= 0.005f;
347 scale-= 0.005f;
348 if(scale < 0.1f)
349 scale= 0.1f;
350
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);
355
356 mesh.
draw(first, count, program);
357 }
358 }
359}
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.
GLuint create_debug_normals_program(const GLenum primitives, const unsigned flags)
construit un shader program / debug des normales.
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.
int key_state(const SDL_Keycode key)
renvoie l'etat d'une touche du clavier. cf la doc SDL2 pour les codes.
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,...