gKit2 light
Loading...
Searching...
No Matches
tuto_rayons.cpp File Reference
#include <vector>
#include <cfloat>
#include <chrono>
#include "vec.h"
#include "mat.h"
#include "color.h"
#include "image.h"
#include "image_io.h"
#include "image_hdr.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  Triangle
 triangle pour le bvh, cf fonction bounds() et intersect(). More...

Functions

Vector normal (const Mesh &mesh, const Hit &hit)
int main (const int argc, const char **argv)

Function Documentation

◆ normal()

Vector normal ( const Mesh & mesh,
const Hit & hit )

Definition at line 86 of file tuto_rayons.cpp.

87{
88 // recuperer le triangle du mesh
89 const TriangleData& data= mesh.triangle(hit.triangle_id);
90
91 // interpoler la normale avec les coordonnées barycentriques du point d'intersection
92 float w= 1 - hit.u - hit.v;
93 Vector n= w * Vector(data.na) + hit.u * Vector(data.nb) + hit.v * Vector(data.nc);
94 return normalize(n);
95}
Vector normalize(const Vector &v)
renvoie un vecteur unitaire / longueur == 1.
Definition vec.cpp:167
representation d'un triangle.
Definition mesh.h:95
representation d'un vecteur 3d.
Definition vec.h:67

◆ main()

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

Definition at line 97 of file tuto_rayons.cpp.

98{
99 const char *mesh_filename= "data/cornell.obj";
100 if(argc > 1)
101 mesh_filename= argv[1];
102
103 const char *orbiter_filename= "data/cornell_orbiter.txt";
104 if(argc > 2)
105 orbiter_filename= argv[2];
106
107 Orbiter camera;
108 if(camera.read_orbiter(orbiter_filename) < 0)
109 return 1;
110
111 Mesh mesh= read_mesh(mesh_filename);
112
113 // recupere les triangles dans le mesh
114 std::vector<Triangle> triangles;
115 {
116 int n= mesh.triangle_count();
117 for(int i= 0; i < n; i++)
118 triangles.emplace_back(mesh.triangle(i), i);
119 }
120
121 //
122 Image image(1024, 768);
123
124 // recupere les transformations pour generer les rayons
125 camera.projection(image.width(), image.height(), 45);
126 Transform model= Identity();
127 Transform view= camera.view();
128 Transform projection= camera.projection();
129 Transform viewport= camera.viewport();
130 Transform inv= Inverse(viewport * projection * view * model);
131
132auto start= std::chrono::high_resolution_clock::now();
133
134 // parcours tous les pixels de l'image
135 for(unsigned y= 0; y < image.height(); y++)
136 for(unsigned x= 0; x < image.width(); x++)
137 {
138 // generer le rayon au centre du pixel
139 Point origine= inv(Point(x + float(0.5), y + float(0.5), 0));
140 Point extremite= inv(Point(x + float(0.5), y + float(0.5), 1));
141 Ray ray(origine, extremite);
142
143 // calculer les intersections avec tous les triangles
144 Hit hit; // proprietes de l'intersection
145 float tmax= ray.tmax; // extremite du rayon
146 for(int i= 0; i < int(triangles.size()); i++)
147 {
148 if(Hit h= triangles[i].intersect(ray, tmax))
149 {
150 // ne conserve que l'intersection *valide* la plus proche de l'origine du rayon
151 assert(h.t > 0);
152 hit= h;
153 tmax= h.t;
154 }
155 }
156
157 #if 0
158 if(hit)
159 // coordonnees barycentriques de l'intersection
160 image(x, y)= Color(1 - hit.u - hit.v, hit.u, hit.v);
161 #endif
162
163 #if 1
164 if(hit)
165 {
166 Vector n= normal(mesh, hit);
167 // normale interpolee a l'intersection
168 image(x, y)= Color(std::abs(n.x), std::abs(n.y), std::abs(n.z));
169 }
170 #endif
171 }
172
173auto stop= std::chrono::high_resolution_clock::now();
174 int cpu= std::chrono::duration_cast<std::chrono::milliseconds>(stop - start).count();
175 printf("%dms\n", cpu);
176
177 write_image(image, "render.png");
178 write_image_hdr(image, "render.hdr");
179 return 0;
180}
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 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
bool write_image_hdr(const Image &image, const char *filename, const bool flipY)
enregistre une image au format .hdr
Definition image_io.cpp:269
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 Identity()
construit la transformation identite.
Definition mat.cpp:187
Mesh read_mesh(const char *filename)
charge un fichier wavefront .obj et renvoie un mesh compose de triangles non indexes....
Definition wavefront.cpp:14
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