gKit2 light
Loading...
Searching...
No Matches
tuto_ray_gltf.cpp File Reference
#include <vector>
#include <cfloat>
#include <chrono>
#include "gltf.h"

Go to the source code of this file.

Classes

struct  Ray
 rayon. More...
struct  Hit
 intersection avec un triangle. More...
struct  Triangle
 triangle pour le bvh, cf fonction bounds() et intersect(). More...

Functions

int main (const int argc, const char **argv)

Function Documentation

◆ main()

int main ( const int argc,
const char ** argv )

Definition at line 88 of file tuto_ray_gltf.cpp.

89{
90 const char *filename= "data/cornell.glb";
91 if(argc > 1)
92 filename= argv[1];
93
94 // charge la scene
95 GLTFScene scene= read_gltf_scene(filename);
96
97 // recupere les triangles
98 std::vector<Triangle> triangles;
99 for(unsigned node_id= 0; node_id < scene.nodes.size(); node_id++)
100 {
101 const GLTFNode& node= scene.nodes[node_id];
102
103 const Transform& model= node.model;
104 int mesh_id= node.mesh_index;
105
106 const GLTFMesh& mesh= scene.meshes[mesh_id];
107 for(unsigned primitive_id= 0; primitive_id < mesh.primitives.size(); primitive_id++)
108 {
109 const GLTFPrimitives& primitives= mesh.primitives[primitive_id];
110 for(unsigned i= 0; i +2 < primitives.indices.size(); i+= 3)
111 {
112 // indice des sommets
113 int a= primitives.indices[i];
114 int b= primitives.indices[i+1];
115 int c= primitives.indices[i+2];
116
117 // position des sommets
118 Point pa= primitives.positions[a];
119 Point pb= primitives.positions[b];
120 Point pc= primitives.positions[c];
121
122 // transforme les sommets dans le repere de la scene
123 pa= model( pa );
124 pb= model( pb );
125 pc= model( pc );
126
127 triangles.push_back( Triangle(pa, pb, pc, mesh_id, primitive_id, i/3) );
128 }
129 }
130 }
131 assert(triangles.size());
132
133 // recupere les matrices de la camera gltf
134 assert(scene.cameras.size());
135 Transform view= scene.cameras[0].view;
136 Transform projection= scene.cameras[0].projection;
137
138 // cree l'image en respectant les proportions largeur/hauteur de la camera gltf
139 unsigned width= 1024;
140 unsigned height= width / scene.cameras[0].aspect;
141 Image image(width, height);
142
143 // transformations
144 Transform model= Identity();
145 Transform viewport= Viewport(image.width(), image.height());
146 Transform inv= Inverse(viewport * projection * view * model);
147
148 // c'est parti, parcours tous les pixels de l'image
149 for(unsigned y= 0; y < image.height(); y++)
150 for(unsigned x= 0; x < image.width(); x++)
151 {
152 // generer le rayon
153 Point origine= inv(Point(x, y, 0));
154 Point extremite= inv(Point(x, y, 1));
155 Ray ray(origine, extremite);
156
157 // calculer les intersections avec tous les triangles
158 Hit hit;
159 for(unsigned i= 0; i < triangles.size(); i++)
160 {
161 if(Hit h= triangles[i].intersect(ray, hit.t))
162 // ne conserve que l'intersection la plus proche de l'origine du rayon
163 // attention !! h.t <= hit.t !! sinon ca ne marche pas...
164 hit= h;
165 }
166
167 if(hit)
168 // coordonnees barycentriques de l'intersection
169 image(x, y)= Color(1 - hit.u - hit.v, hit.u, hit.v);
170 }
171
172 write_image(image, "render.png");
173 return 0;
174}
representation d'une image.
Definition image.h:21
int mesh_index
indice du maillage.
Definition gltf.h:131
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
bool write_image(const Image &image, const char *filename, const bool flipY)
enregistre une image au format .png
Definition image_io.cpp:245
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
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'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