gKit2 light
Loading...
Searching...
No Matches
tuto_bvh2.cpp File Reference

bvh 2 niveaux et instances More...

#include <algorithm>
#include <vector>
#include <cfloat>
#include "vec.h"
#include "mat.h"
#include "color.h"
#include "image.h"
#include "image_io.h"
#include "orbiter.h"
#include "mesh.h"
#include "wavefront.h"

Go to the source code of this file.

Classes

struct  Ray
 rayon. More...
struct  Hit
 intersection avec un triangle. More...
struct  BBoxHit
 intersection avec une boite / un englobant. More...
struct  BBox
 boite englobante. More...
struct  Node
 construction de l'arbre / BVH. More...
struct  BVHT< T >
 bvh parametre par le type des primitives, cf triangle et instance... More...
struct  Triangle
 triangle pour le bvh, cf fonction bounds() et intersect(). More...
struct  Instance
 instance pour le bvh, cf fonctions bounds() et intersect(). More...

Typedefs

typedef BVHT< TriangleBVH
typedef BVHT< TriangleBLAS
typedef BVHT< InstanceTLAS

Functions

Node make_node (const BBox &bounds, const int left, const int right)
Node make_leaf (const BBox &bounds, const int begin, const int end)
int main (int argc, char **argv)

Detailed Description

bvh 2 niveaux et instances

Definition in file tuto_bvh2.cpp.

Typedef Documentation

◆ BVH

typedef BVHT<Triangle> BVH

Definition at line 297 of file tuto_bvh2.cpp.

◆ BLAS

typedef BVHT<Triangle> BLAS

Definition at line 298 of file tuto_bvh2.cpp.

◆ TLAS

typedef BVHT<Instance> TLAS

Definition at line 352 of file tuto_bvh2.cpp.

Function Documentation

◆ make_node()

Node make_node ( const BBox & bounds,
const int left,
const int right )

Definition at line 109 of file tuto_bvh2.cpp.

110{
111 Node node { bounds, left, right };
112 assert(node.internal()); // verifie que c'est bien un noeud...
113 return node;
114}
void bounds(const MeshData &data, Point &pmin, Point &pmax)
renvoie l'englobant.
construction de l'arbre / BVH.
Definition tuto_bvh.cpp:133

◆ make_leaf()

Node make_leaf ( const BBox & bounds,
const int begin,
const int end )

Definition at line 117 of file tuto_bvh2.cpp.

118{
119 Node node { bounds, -begin, -end };
120 assert(node.leaf()); // verifie que c'est bien une feuille...
121 return node;
122}
void begin(Widgets &w)
debut de la description des elements de l'interface graphique.
Definition widgets.cpp:29

◆ main()

int main ( int argc,
char ** argv )

Definition at line 355 of file tuto_bvh2.cpp.

356{
357 const char *mesh_filename= "data/robot.obj";
358 const char *orbiter_filename= nullptr;
359
360 if(argc > 1) mesh_filename= argv[1];
361 if(argc > 2) orbiter_filename= argv[2];
362
363 Mesh mesh= read_mesh(mesh_filename);
364 if(mesh.triangle_count() == 0)
365 return 1;
366
367 BBox mesh_bounds;
368 mesh.bounds(mesh_bounds.pmin, mesh_bounds.pmax);
369
370 // construit le bvh de l'objet
371 BVH bvh;
372 {
373 // recupere les triangles du mesh
374 std::vector<Triangle> triangles;
375 for(int i= 0; i <mesh.triangle_count(); i++)
376 {
377 TriangleData data= mesh.triangle(i);
378 triangles.push_back( Triangle(data, triangles.size()) );
379 }
380
381 // construit le bvh...
382 bvh.build(triangles);
383 }
384
385 // instancie l'objet
386 TLAS top_bvh;
387 {
388 std::vector<Instance> instances;
389 for(int i= -2; i <= 2; i++)
390 {
391 Transform model= Translation(i*10, 0, 0);
392 instances.push_back( Instance(mesh_bounds, model, bvh, instances.size()) );
393 }
394
395 // construit le bvh...
396 top_bvh.build(instances);
397 }
398
399 // regle la camera
400 Orbiter camera;
401 if(orbiter_filename == nullptr || camera.read_orbiter(orbiter_filename) < 0)
402 // les objets sont instancies sur une ligne, agrandir la zone observee par la camera...
403 camera.lookat(mesh_bounds.pmin * 5, mesh_bounds.pmax * 5);
404
405 // image resultat
406 Image image(1024, 640);
407
408 // transformations
409 Transform model= Identity();
410 Transform view= camera.view();
411 Transform projection= camera.projection();
412 Transform viewport= Viewport(image.width(), image.height());
413 Transform inv= Inverse(viewport * projection * view * model);
414
415 // calcule l'image en parallele avec openMP
416#pragma omp parallel for
417 for(unsigned y= 0; y < image.height(); y++)
418 for(unsigned x= 0; x < image.width(); x++)
419 {
420 // genere le rayon pour le pixel x,y
421 Point o= inv( Point(x, y, 0) ); // origine
422 Point e= inv( Point(x, y, 1) ); // extremite
423 Ray ray(o, e);
424
425 // intersections !
426 if(Hit hit= top_bvh.intersect(ray))
427 image(x, y)= Red(); // touche !
428 }
429
430 write_image(image, "render.png");
431 return 0;
432}
representation d'une image.
Definition image.h:21
representation d'un objet / maillage.
Definition mesh.h:121
representation de la camera, type orbiter, placee sur une sphere autour du centre de l'objet.
Definition orbiter.h:17
void lookat(const Point &center, const float size)
observe le point center a une distance size.
Definition orbiter.cpp:7
Color Red()
utilitaire. renvoie une couleur rouge.
Definition color.cpp:28
bool write_image(const Image &image, const char *filename, const bool flipY)
enregistre une image au format .png
Definition image_io.cpp:225
Transform Inverse(const Transform &m)
renvoie l'inverse de la matrice.
Definition mat.cpp:197
Transform Viewport(const float width, const float height)
renvoie la matrice representant une transformation viewport.
Definition mat.cpp:357
Transform Identity()
construit la transformation identite.
Definition mat.cpp:187
Transform Translation(const Vector &v)
renvoie la matrice representant une translation par un vecteur.
Definition mat.cpp:216
Mesh read_mesh(const char *filename)
charge un fichier wavefront .obj et renvoie un mesh compose de triangles non indexes....
Definition wavefront.cpp:14
boite englobante.
Definition tuto_bvh.cpp:47
intersection avec un triangle.
Definition tuto_bvh2.cpp:33
instance pour le bvh, cf fonctions bounds() et intersect().
representation d'un point 3d.
Definition vec.h:21
rayon.
Definition tuto_bvh2.cpp:20
representation d'une transformation, une matrice 4x4, organisee par ligne / row major.
Definition mat.h:21
representation d'un triangle.
Definition mesh.h:95
triangle pour le bvh, cf fonction bounds() et intersect().
Definition tuto_bvh.cpp:84