a deriver pour afficher les objets. renvoie 1 pour continuer, 0 pour fermer l'application.
230 {
231 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
232
234 {
236 reload_program(m_program, "gkit2_tutos/M2/is.glsl");
238 }
239
241 {
243 Point pmin, pmax;
244 m_mesh.bounds(pmin, pmax);
245 m_camera.lookat(pmin, pmax);
246 }
247
248
249 int mx, my;
250 unsigned int mb= SDL_GetRelativeMouseState(&mx, &my);
251 if(mb & SDL_BUTTON(1))
252 m_camera.rotation(mx, my);
253 else if(mb & SDL_BUTTON(3))
254 m_camera.move(mx);
255 else if(mb & SDL_BUTTON(2))
257
259
260 static int mode= 0;
261 static vec2 texcoord= vec2(0, 0);
263 {
265 mode= (mode +1) % 2;
266
267 if(mode == 1)
268 {
269 int mx, my;
270 SDL_GetMouseState(&mx, &my);
272 texcoord= vec2(pixel.x / m_hitp.width(), pixel.y / m_hitp.height());
273 printf(
"pixel %f %f\n", pixel.x, pixel.y);
274
275
276 Point d0;
277 Vector dx0, dy0;
278 m_camera.frame(0, d0, dx0, dy0);
279
280 Point d1;
281 Vector dx1, dy1;
282 m_camera.frame(1, d1, dx1, dy1);
283
284
285 Point o= d0 + pixel.x*dx0 + pixel.y*dy0;
286 Point e= d1 + pixel.x*dx1 + pixel.y*dy1;
287
288 Point point;
289 Vector normal;
290
291 Ray ray(o, e);
292 Hit hit;
293 if(intersect(ray, hit))
294 {
295 point= hit.p;
296 normal= hit.n;
297
298
299 #pragma omp parallel for schedule(dynamic, 16)
300 for(unsigned y= 0; y < m_hitp.height(); y++)
301 for(unsigned x= 0; x < m_hitp.width(); x++)
302 {
303
304 m_hitp(x, y)=
Black();
305 m_hitn(x, y)=
Black();
306 m_hitv(x, y)=
Black();
307
308 Point o= d0 + x*dx0 + y*dy0;
309 Point e= d1 + x*dx1 + y*dy1;
310
311 Ray ray(o, e);
312 Hit hit;
313 if(intersect(ray, hit))
314 {
315 m_hitp(x, y)= Color(hit.p.x, hit.p.y, hit.p.z);
316 m_hitn(x, y)= Color(hit.n.x, hit.n.y, hit.n.z);
317
318 Ray shadow(hit.p + hit.n * 0.001f, point + normal * 0.001f);
319 Hit shadow_hit;
320 int v= 1;
321 if(intersect(shadow, shadow_hit))
322 v= 0;
323
324 m_hitv(x, y)= Color(v, v, v);
325 }
326 }
327
328
329 glActiveTexture(GL_TEXTURE0);
330 glBindTexture(GL_TEXTURE_2D, m_ptexture);
331 glTexSubImage2D(GL_TEXTURE_2D, 0,
332 0, 0, m_hitp.width(), m_hitp.height(),
333 GL_RGBA, GL_FLOAT, m_hitp.data());
334
335 glActiveTexture(GL_TEXTURE0 +1);
336 glBindTexture(GL_TEXTURE_2D, m_ntexture);
337 glTexSubImage2D(GL_TEXTURE_2D, 0,
338 0, 0, m_hitn.width(), m_hitn.height(),
339 GL_RGBA, GL_FLOAT, m_hitn.data());
340
341 glActiveTexture(GL_TEXTURE0 +2);
342 glBindTexture(GL_TEXTURE_2D, m_vtexture);
343 glTexSubImage2D(GL_TEXTURE_2D, 0,
344 0, 0, m_hitv.width(), m_hitv.height(),
345 GL_RGBA, GL_FLOAT, m_hitv.data());
346 }
347 }
348 }
349
350 if(mode == 1)
351 {
352 glBindVertexArray(m_vao);
353 glUseProgram(m_program);
354
355 program_uniform(m_program, "ptexture", 0);
356 program_uniform(m_program, "ntexture", 1);
357 program_uniform(m_program, "vtexture", 2);
358 program_uniform(m_program, "pixel", texcoord);
359 program_uniform(m_program, "mode", mode);
360
361 glActiveTexture(GL_TEXTURE0);
362 glBindTexture(GL_TEXTURE_2D, m_ptexture);
363 glActiveTexture(GL_TEXTURE0 +1);
364 glBindTexture(GL_TEXTURE_2D, m_ntexture);
365 glActiveTexture(GL_TEXTURE0 +2);
366 glBindTexture(GL_TEXTURE_2D, m_vtexture);
367
368 glDrawArrays(GL_TRIANGLES, 0, 3);
369 }
370
371 else if(mode == 0)
372 draw(m_mesh, m_camera);
373
374
376 {
378
379 static int n= 1;
380 char tmp[1024];
381 sprintf(tmp, "screenshot%02d.png", n++);
382 printf(
"writing %s...\n", tmp);
384 }
385
386 return 1;
387 }
void clear_key_state(const SDL_Keycode key)
desactive une touche du clavier.
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 key_state(const SDL_Keycode key)
renvoie l'etat d'une touche du clavier. cf la doc SDL2 pour les codes.
Color Black()
utilitaire. renvoie une couleur noire.
int screenshot(const char *filename)
enregistre le contenu de la fenetre dans un fichier. doit etre de type .png / .bmp