gKit2 light
Public Member Functions | Protected Attributes | List of all members
IS Struct Reference
+ Inheritance diagram for IS:

Public Member Functions

 IS (const char *filename)
 
int init ()
 a deriver pour creer les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon. More...
 
int render ()
 a deriver pour afficher les objets. renvoie 1 pour continuer, 0 pour fermer l'application. More...
 
int quit ()
 a deriver pour detruire les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon. More...
 
int build_sources ()
 
bool direct (const Ray &ray)
 
int build_triangles ()
 
bool intersect (const Ray &ray, Hit &hit)
 
- Public Member Functions inherited from App
 App (const int width, const int height, const int major=3, const int minor=3, const int samples=0)
 constructeur, dimensions de la fenetre et version d'openGL. More...
 
virtual int update (const float time, const float delta)
 a deriver et redefinir pour animer les objets en fonction du temps. More...
 
int run ()
 execution de l'application. More...
 

Protected Attributes

Mesh m_mesh
 
Orbiter m_camera
 
std::vector< Trianglem_triangles
 
std::vector< Sourcem_sources
 
Image m_hitp
 
Image m_hitn
 
Image m_hitv
 
GLuint m_vao
 
GLuint m_program
 
GLuint m_ptexture
 
GLuint m_ntexture
 
GLuint m_vtexture
 
- Protected Attributes inherited from App
Window m_window
 
Context m_context
 
bool sync
 

Additional Inherited Members

- Protected Member Functions inherited from App
virtual int prerender ()
 
virtual int postrender ()
 
void vsync_off ()
 

Detailed Description

Definition at line 180 of file tuto_is.cpp.

Member Function Documentation

◆ init()

int IS::init ( )
inlinevirtual

a deriver pour creer les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon.

Implements App.

Definition at line 200 of file tuto_is.cpp.

201  {
202  if(m_mesh == Mesh::error())
203  return -1;
204 
205  m_ptexture= make_texture(0, window_width(), window_height());
206  m_ntexture= make_texture(1, window_width(), window_height());
207  m_vtexture= make_texture(2, window_width(), window_height());
208 
209  m_hitp= Image(window_width(), window_height());
210  m_hitn= Image(window_width(), window_height());
211  m_hitv= Image(window_width(), window_height());
212 
213  glGenVertexArrays(1, &m_vao);
214  glBindVertexArray(m_vao);
215 
216  m_program= read_program("tutos/M2/is.glsl");
217  program_print_errors(m_program);
218 
219  // etat openGL par defaut
220  glClearColor(0.2f, 0.2f, 0.2f, 1.f); // couleur par defaut de la fenetre
221 
222  glClearDepth(1.f); // profondeur par defaut
223  glDepthFunc(GL_LESS); // ztest, conserver l'intersection la plus proche de la camera
224  glEnable(GL_DEPTH_TEST); // activer le ztest
225 
226  return 0;
227  }
representation d'une image.
Definition: image.h:21
static Mesh & error()
Definition: mesh.h:348
int window_height()
renvoie la hauteur de la fenetre de l'application.
Definition: window.cpp:29
int window_width()
renvoie la largeur de la fenetre de l'application.
Definition: window.cpp:25
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
Definition: texture.cpp:25
GLuint read_program(const char *filename, const char *definitions)
Definition: program.cpp:204
int program_print_errors(const GLuint program)
affiche les erreurs de compilation.
Definition: program.cpp:432

◆ render()

int IS::render ( )
inlinevirtual

a deriver pour afficher les objets. renvoie 1 pour continuer, 0 pour fermer l'application.

Implements App.

Definition at line 229 of file tuto_is.cpp.

230  {
231  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
232 
233  if(key_state('r'))
234  {
235  clear_key_state('r');
236  reload_program(m_program, "tutos/M2/is.glsl");
237  program_print_errors(m_program);
238  }
239 
240  if(key_state('f'))
241  {
242  clear_key_state('f');
243  Point pmin, pmax;
244  m_mesh.bounds(pmin, pmax);
245  m_camera.lookat(pmin, pmax);
246  }
247 
248  // deplace la camera
249  int mx, my;
250  unsigned int mb= SDL_GetRelativeMouseState(&mx, &my);
251  if(mb & SDL_BUTTON(1)) // le bouton gauche est enfonce
252  m_camera.rotation(mx, my);
253  else if(mb & SDL_BUTTON(3)) // le bouton droit est enfonce
254  m_camera.move(mx);
255  else if(mb & SDL_BUTTON(2)) // le bouton du milieu est enfonce
256  m_camera.translation((float) mx / (float) window_width(), (float) my / (float) window_height());
257 
258  m_camera.projection(window_width(), window_height(), 45);
259 
260  static int mode= 0;
261  static vec2 texcoord= vec2(0, 0);
262  if(key_state(' '))
263  {
264  clear_key_state(' ');
265  mode= (mode +1) % 2;
266 
267  if(mode == 1)
268  {
269  int mx, my;
270  SDL_GetMouseState(&mx, &my);
271  vec2 pixel= vec2(mx, window_height() - my -1);
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  // go
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  // pixel
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  // frame
299  #pragma omp parallel for schedule(dynamic, 16)
300  for(int y= 0; y < m_hitp.height(); y++)
301  for(int x= 0; x < m_hitp.width(); x++)
302  {
303  // clear
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  // transferre les donnees
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  //
375  if(key_state('s'))
376  {
377  clear_key_state('s');
378 
379  static int n= 1;
380  char tmp[1024];
381  sprintf(tmp, "screenshot%02d.png", n++);
382  printf("writing %s...\n", tmp);
383  screenshot(tmp);
384  }
385 
386  return 1;
387  }
int height() const
renvoie la hauteur de l'image.
Definition: image.h:100
const void * data() const
renvoie un pointeur sur le stockage des couleurs des pixels.
Definition: image.h:84
int width() const
renvoie la largeur de l'image.
Definition: image.h:98
void bounds(Point &pmin, Point &pmax) const
renvoie min et max les coordonnees des extremites des positions des sommets de l'objet (boite engloba...
Definition: mesh.cpp:501
void lookat(const Point &center, const float size)
observe le point center a une distance size.
Definition: orbiter.cpp:7
void frame(const float z, Point &dO, Vector &dx, Vector &dy) const
Definition: orbiter.cpp:89
void move(const float z)
rapproche / eloigne la camera du centre.
Definition: orbiter.cpp:33
Transform projection(const int width, const int height, const float fov)
fixe la projection reglee pour une image d'aspect width / height, et une demi ouverture de fov degres...
Definition: orbiter.cpp:47
void translation(const float x, const float y)
deplace le centre / le point observe.
Definition: orbiter.cpp:27
void rotation(const float x, const float y)
change le point de vue / la direction d'observation.
Definition: orbiter.cpp:21
void clear_key_state(const SDL_Keycode key)
desactive une touche du clavier.
Definition: window.cpp:48
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().
Definition: text.cpp:140
int key_state(const SDL_Keycode key)
renvoie l'etat d'une touche du clavier. cf la doc SDL2 pour les codes.
Definition: window.cpp:42
Color Black()
utilitaire. renvoie une couleur noire.
Definition: color.cpp:31
int screenshot(const char *filename)
enregistre le contenu de la fenetre dans un fichier. doit etre de type .png / .bmp
Definition: texture.cpp:194
void program_uniform(const GLuint program, const char *uniform, const std::vector< unsigned > &v)
affecte un tableau de valeurs a un uniform du shader program.
Definition: uniforms.cpp:94
representation d'une couleur (rgba) transparente ou opaque.
Definition: color.h:14
intersection avec un triangle.
Definition: tuto_bvh2.cpp:33
representation d'un point 3d.
Definition: vec.h:21
rayon.
Definition: tuto_bvh2.cpp:20
representation d'un vecteur 3d.
Definition: vec.h:59
vecteur generique, utilitaire.
Definition: vec.h:131

◆ quit()

int IS::quit ( )
inlinevirtual

a deriver pour detruire les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon.

Implements App.

Definition at line 389 of file tuto_is.cpp.

390  {
391  m_mesh.release();
392  return 0;
393  }
void release()
detruit les objets openGL.
Definition: mesh.cpp:62

The documentation for this struct was generated from the following file: