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

bvh 2 niveaux et instances, charge un fichier gltf... More...

#include <random>
#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 "gltf.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...
struct  Sampler
 generation de nombres aleatoires entre 0 et 1. 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, charge un fichier gltf...

Definition in file tuto_bvh2_gltf.cpp.

Typedef Documentation

◆ BVH

typedef BVHT<Triangle> BVH

Definition at line 306 of file tuto_bvh2_gltf.cpp.

◆ BLAS

typedef BVHT<Triangle> BLAS

Definition at line 307 of file tuto_bvh2_gltf.cpp.

◆ TLAS

typedef BVHT<Instance> TLAS

Definition at line 361 of file tuto_bvh2_gltf.cpp.

Function Documentation

◆ make_node()

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

Definition at line 114 of file tuto_bvh2_gltf.cpp.

115{
116 Node node { bounds, left, right };
117 assert(node.internal()); // verifie que c'est bien un noeud...
118 return node;
119}
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 122 of file tuto_bvh2_gltf.cpp.

123{
124 Node node { bounds, -begin, -end };
125 assert(node.leaf()); // verifie que c'est bien une feuille...
126 return node;
127}
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 378 of file tuto_bvh2_gltf.cpp.

379{
380 const char *mesh_filename= "data/robot.gltf";
381 const char *orbiter_filename= nullptr;
382
383 if(argc > 1) mesh_filename= argv[1];
384 if(argc > 2) orbiter_filename= argv[2];
385
386 GLTFScene scene= read_gltf_scene(mesh_filename);
387
388 // construit les bvh des objets de la scene, en parallele ! cf BLAS / bvh de triangles
389 std::vector<BVH *> bvhs(scene.meshes.size());
390 {
391 // parcourir les mesh
392 printf("%d meshes\n", int(scene.meshes.size()));
393
394 #pragma omp parallel for
395 for(unsigned mesh_id= 0; mesh_id < scene.meshes.size(); mesh_id++)
396 {
397 const GLTFMesh& mesh= scene.meshes[mesh_id];
398
399 // groupes de triangles du mesh
400 std::vector<Triangle> triangles;
401 for(unsigned primitive_id= 0; primitive_id < mesh.primitives.size(); primitive_id++)
402 {
403 const GLTFPrimitives& primitives= mesh.primitives[primitive_id];
404
405 for(unsigned i= 0; i +2 < primitives.indices.size(); i+= 3)
406 {
407 // extraire les positions des sommets du triangle
408 vec3 a= primitives.positions[primitives.indices[i]];
409 vec3 b= primitives.positions[primitives.indices[i+1]];
410 vec3 c= primitives.positions[primitives.indices[i+2]];
411 triangles.push_back( Triangle(a, b, c, mesh_id, primitive_id, i/3) );
412 // stocke aussi l'indice du triangle
413 }
414 }
415
416 BVH *bvh= new BVH;
417 bvh->build(triangles);
418 bvhs[mesh_id]= bvh;
419 }
420 }
421
422 // instancie les objets de la scene, cf TLAS / bvh d'instances
423 TLAS top_bvh;
424 {
425 printf("%d nodes\n", int(scene.nodes.size()));
426
427 // 1 instance par noeud de la scene gltf
428 std::vector<Instance> instances;
429 for(unsigned node_id= 0; node_id < scene.nodes.size(); node_id++)
430 {
431 const GLTFNode& node= scene.nodes[node_id];
432 const GLTFMesh& mesh= scene.meshes[node.mesh_index];
433
434 instances.push_back( Instance( BBox(mesh.pmin, mesh.pmax), node.model, bvhs[node.mesh_index], node_id ) );
435 }
436
437 top_bvh.build(instances);
438 printf("done. %d instances\n", int(instances.size()));
439 }
440
441 // recupere les matrices de la camera gltf
442 assert(scene.cameras.size());
443 Transform view= scene.cameras[0].view;
444 Transform projection= scene.cameras[0].projection;
445
446 // cree l'image en respectant les proportions largeur/hauteur de la camera gltf
447 int width= 1024;
448 int height= width / scene.cameras[0].aspect;
449 Image image(width, height);
450
451 // transformations
452 Transform model= Identity();
453 Transform viewport= Viewport(image.width(), image.height());
454 Transform inv= Inverse(viewport * projection * view * model);
455
456
457 // calcule l'image en parallele avec openMP
458#pragma omp parallel for
459 for(unsigned y= 0; y < image.height(); y++)
460 for(unsigned x= 0; x < image.width(); x++)
461 {
462 // genere le rayon pour le pixel x,y
463 Point o= inv( Point(x, y, 0) ); // origine
464 Point e= inv( Point(x, y, 1) ); // extremite
465 Ray ray(o, Vector(o, e));
466
467 // intersections !
468 if(Hit hit= top_bvh.intersect(ray))
469 {
470 assert(hit.instance_id != -1);
471 assert(hit.mesh_id != -1);
472 assert(hit.primitive_id != -1);
473 // retrouve le triangle dans le mesh et ses primitives
474 const GLTFMesh& mesh= scene.meshes[hit.mesh_id];
475 const GLTFPrimitives& primitives= mesh.primitives[hit.primitive_id];
476 // indices des sommets du triangle
477 unsigned a= primitives.indices[3*hit.triangle_id];
478 unsigned b= primitives.indices[3*hit.triangle_id+1];
479 unsigned c= primitives.indices[3*hit.triangle_id+2];
480 // c'est a ca que servent les indices ajoutes dans triangle, instance et hit !!
481
482 /* exemple : normale interpolee du point d'intersection
483 assert(primitives.normals.size());
484 Vector na= primitives.normals[a];
485 Vector nb= primitives.normals[b];
486 Vector nc= primitives.normals[c];
487
488 // normale interpolee au point d'intersection, cf coordonnees barycentriques dans le triangle
489 Vector n= (1 - hit.u - hit.v) * na + hit.u * nb + hit.v * nc;
490 // n est dans le repere local de l'objet...
491
492 // changement de repere vers la scene !
493 const Transform& model= scene.nodes[hit.instance_id].model; // recupere la transformation de l'instance...
494
495 // todo : ecrire une fonction utilitaire !!
496 */
497
498 // retrouve la couleur de base du triangle
499 Color diffuse= Yellow();
500 if(primitives.material_index != -1)
501 {
502 const GLTFMaterial& material= scene.materials[primitives.material_index];
503
504 // parametres du modele pbr / gltf : couleur / metal / rugosite
505 Color base= material.color;
506 diffuse= base;
507 }
508
509 image(x, y)= Color(diffuse, 1);
510 }
511 }
512 printf("\n");
513
514 write_image(image, "render.png");
515 return 0;
516}
representation d'une image.
Definition image.h:21
int mesh_index
indice du maillage.
Definition gltf.h:131
int material_index
indice de la matiere des primitives.
Definition gltf.h:102
Transform model
transformation model pour dessiner le maillage.
Definition gltf.h:130
description d'un maillage.
Definition gltf.h:115
position et orientation d'un maillage dans la scene.
Definition gltf.h:129
groupe de triangles d'un maillage. chaque groupe est associe a une matiere.
Definition gltf.h:99
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
Color Yellow()
utilitaire. renvoie une couleur jaune.
Definition color.cpp:43
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
boite englobante.
Definition tuto_bvh.cpp:47
representation d'une couleur (rgba) transparente ou opaque.
Definition color.h:14
Color color
base color.
Definition gltf.h:60
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
triangle pour le bvh, cf fonction bounds() et intersect().
Definition tuto_bvh.cpp:84
representation d'un vecteur 3d.
Definition vec.h:67
vecteur generique, utilitaire.
Definition vec.h:169