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
108 if(camera.read_orbiter(orbiter_filename) < 0)
109 return 1;
110
112
113
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
125 camera.projection(image.width(), image.height(), 45);
128 Transform projection= camera.projection();
131
132auto start= std::chrono::high_resolution_clock::now();
133
134
135 for(unsigned y= 0; y < image.height(); y++)
136 for(unsigned x= 0; x < image.width(); x++)
137 {
138
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
145 float tmax= ray.tmax;
146 for(int i= 0; i < int(triangles.size()); i++)
147 {
148 if(
Hit h= triangles[i].intersect(ray, tmax))
149 {
150
151 assert(h.t > 0);
152 hit= h;
153 tmax= h.t;
154 }
155 }
156
157 #if 0
158 if(hit)
159
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
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();
176
179 return 0;
180}
representation d'une image.
representation d'un objet / maillage.
representation de la camera, type orbiter, placee sur une sphere autour du centre de l'objet.
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().
bool write_image_hdr(const Image &image, const char *filename, const bool flipY)
enregistre une image au format .hdr
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 Identity()
construit la transformation identite.
Mesh read_mesh(const char *filename)
charge un fichier wavefront .obj et renvoie un mesh compose de triangles non indexes....
representation d'une couleur (rgba) transparente ou opaque.
intersection avec un triangle.
representation d'un point 3d.