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
389 std::vector<BVH *> bvhs(scene.meshes.size());
390 {
391
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
400 std::vector<Triangle> triangles;
401 for(unsigned primitive_id= 0; primitive_id < mesh.primitives.size(); primitive_id++)
402 {
404
405 for(unsigned i= 0; i +2 < primitives.indices.size(); i+= 3)
406 {
407
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
413 }
414 }
415
417 bvh->build(triangles);
418 bvhs[mesh_id]= bvh;
419 }
420 }
421
422
423 TLAS top_bvh;
424 {
425 printf(
"%d nodes\n",
int(scene.nodes.size()));
426
427
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];
433
435 }
436
437 top_bvh.build(instances);
438 printf(
"done. %d instances\n",
int(instances.size()));
439 }
440
441
442 assert(scene.cameras.size());
444 Transform projection= scene.cameras[0].projection;
445
446
447 int width= 1024;
448 int height= width / scene.cameras[0].aspect;
449 Image image(width, height);
450
451
455
456
457
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
466
467
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
474 const GLTFMesh& mesh= scene.meshes[hit.mesh_id];
475 const GLTFPrimitives& primitives= mesh.primitives[hit.primitive_id];
476
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
501 {
503
504
506 diffuse= base;
507 }
508
509 image(x, y)=
Color(diffuse, 1);
510 }
511 }
513
515 return 0;
516}
representation d'une image.
int mesh_index
indice du maillage.
int material_index
indice de la matiere des primitives.
Transform model
transformation model pour dessiner le maillage.
description d'un maillage.
position et orientation d'un maillage dans la scene.
groupe de triangles d'un maillage. chaque groupe est associe a une matiere.
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().
Color Yellow()
utilitaire. renvoie une couleur jaune.
bool write_image(const Image &image, const char *filename, const bool flipY)
enregistre une image au format .png
Transform Inverse(const Transform &m)
renvoie l'inverse de la matrice.
Transform Viewport(const float width, const float height)
renvoie la matrice representant une transformation viewport.
Transform Identity()
construit la transformation identite.
representation d'une couleur (rgba) transparente ou opaque.
intersection avec un triangle.
instance pour le bvh, cf fonctions bounds() et intersect().
representation d'un point 3d.
triangle pour le bvh, cf fonction bounds() et intersect().
representation d'un vecteur 3d.
vecteur generique, utilitaire.