gKit2 light
Loading...
Searching...
No Matches
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.
int render ()
 a deriver pour afficher les objets. renvoie 1 pour continuer, 0 pour fermer l'application.
int quit ()
 a deriver pour detruire les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon.
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.
virtual int update (const float time, const float delta)
 a deriver et redefinir pour animer les objets en fonction du temps.
int run ()
 execution de l'application.

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.

Constructor & Destructor Documentation

◆ IS()

IS::IS ( const char * filename)
inline

Definition at line 183 of file tuto_is.cpp.

183 : App(1024, 640)
184 {
185 m_mesh= read_mesh(filename);
186 if(m_mesh.vertex_count() == 0)
187 return;
188
189 build_sources();
190 build_triangles();
191
192 if(m_camera.read_orbiter("orbiter.txt") < 0)
193 {
194 Point pmin, pmax;
195 m_mesh.bounds(pmin, pmax);
196 m_camera.lookat(pmin, pmax);
197 }
198 }
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.
Definition app.cpp:11
Mesh read_mesh(const char *filename)
charge un fichier wavefront .obj et renvoie un mesh compose de triangles non indexes....
Definition wavefront.cpp:14

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.vertex_count() == 0)
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("gkit2_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 }
int window_height()
renvoie la hauteur de la fenetre de l'application.
Definition window.cpp:27
int window_width()
renvoie la largeur de la fenetre de l'application.
Definition window.cpp:23
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:10
GLuint read_program(const char *filename, const char *definitions)
Definition program.cpp:218
int program_print_errors(const GLuint program)
affiche les erreurs de compilation.
Definition program.cpp:446

◆ 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, "gkit2_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(unsigned y= 0; y < m_hitp.height(); y++)
301 for(unsigned 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 }
void clear_key_state(const SDL_Keycode key)
desactive une touche du clavier.
Definition window.cpp:46
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:40
Color Black()
utilitaire. renvoie une couleur noire.
Definition color.cpp:18
int screenshot(const char *filename)
enregistre le contenu de la fenetre dans un fichier. doit etre de type .png / .bmp
Definition texture.cpp:178

◆ 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 }

◆ build_sources()

int IS::build_sources ( )
inline

Definition at line 397 of file tuto_is.cpp.

398 {
399 for(int i= 0; i < m_mesh.triangle_count(); i++)
400 {
401 // recupere la matiere associee a chaque triangle de l'objet
402 Material material= m_mesh.triangle_material(i);
403
404 if((material.emission.r + material.emission.g + material.emission.b) > 0)
405 // inserer la source de lumiere dans l'ensemble.
406 m_sources.push_back( Source(m_mesh.triangle(i), material.emission) );
407 }
408
409 printf("%d sources.\n", (int) m_sources.size());
410 return (int) m_sources.size();
411 }
Color emission
pour une source de lumiere.
Definition materials.h:19

◆ direct()

bool IS::direct ( const Ray & ray)
inline

Definition at line 413 of file tuto_is.cpp.

414 {
415 for(size_t i= 0; i < m_sources.size(); i++)
416 {
417 float t, u, v;
418 if(m_sources[i].intersect(ray, ray.tmax, t, u, v))
419 return true;
420 }
421
422 return false;
423 }

◆ build_triangles()

int IS::build_triangles ( )
inline

Definition at line 427 of file tuto_is.cpp.

428 {
429 for(int i= 0; i < m_mesh.triangle_count(); i++)
430 m_triangles.push_back( Triangle(m_mesh.triangle(i)) );
431
432 printf("%d triangles.\n", (int) m_triangles.size());
433 return (int) m_triangles.size();
434 }

◆ intersect()

bool IS::intersect ( const Ray & ray,
Hit & hit )
inline

Definition at line 438 of file tuto_is.cpp.

439 {
440 hit.t= ray.tmax;
441 for(size_t i= 0; i < m_triangles.size(); i++)
442 {
443 float t, u, v;
444 if(m_triangles[i].intersect(ray, hit.t, t, u, v))
445 {
446 hit.t= t;
447 hit.u= u;
448 hit.v= v;
449
450 hit.p= ray(t); // evalue la positon du point d'intersection sur le rayon
451 hit.n= m_triangles[i].normal(u, v);
452
453 hit.object_id= i; // permet de retrouver toutes les infos associees au triangle
454 }
455 }
456
457 return (hit.object_id != -1);
458 }

Member Data Documentation

◆ m_mesh

Mesh IS::m_mesh
protected

Definition at line 461 of file tuto_is.cpp.

◆ m_camera

Orbiter IS::m_camera
protected

Definition at line 462 of file tuto_is.cpp.

◆ m_triangles

std::vector<Triangle> IS::m_triangles
protected

Definition at line 464 of file tuto_is.cpp.

◆ m_sources

std::vector<Source> IS::m_sources
protected

Definition at line 465 of file tuto_is.cpp.

◆ m_hitp

Image IS::m_hitp
protected

Definition at line 467 of file tuto_is.cpp.

◆ m_hitn

Image IS::m_hitn
protected

Definition at line 468 of file tuto_is.cpp.

◆ m_hitv

Image IS::m_hitv
protected

Definition at line 469 of file tuto_is.cpp.

◆ m_vao

GLuint IS::m_vao
protected

Definition at line 471 of file tuto_is.cpp.

◆ m_program

GLuint IS::m_program
protected

Definition at line 472 of file tuto_is.cpp.

◆ m_ptexture

GLuint IS::m_ptexture
protected

Definition at line 474 of file tuto_is.cpp.

◆ m_ntexture

GLuint IS::m_ntexture
protected

Definition at line 475 of file tuto_is.cpp.

◆ m_vtexture

GLuint IS::m_vtexture
protected

Definition at line 476 of file tuto_is.cpp.


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