221{
222 const char *mesh_filename= "data/cornell.obj";
223 if(argc > 1)
224 mesh_filename= argv[1];
225
226 const char *orbiter_filename= "data/cornell_orbiter.txt";
227 if(argc > 2)
228 orbiter_filename= argv[2];
229
231 if(camera.read_orbiter(orbiter_filename) < 0)
232 return 1;
233
237
238
239 std::vector<Triangle> triangles;
240 {
241 int n= mesh.triangle_count();
242 for(int i= 0; i < n; i++)
243 triangles.emplace_back(mesh.triangle(i), i);
244 }
245
246 Image image(1024, 768);
247
248
249 camera.projection(image.width(), image.height(), 45);
252 Transform projection= camera.projection();
255
256
257 std::vector<RayHit> rays;
258 for(unsigned y= 0; y < image.height(); y++)
259 for(unsigned x= 0; x < image.width(); x++)
260 {
261
262 Point origine= inv(
Point(x + .5f, y + .5f, 0));
263 Point extremite= inv(
Point(x + .5f, y + .5f, 1));
264
265 rays.emplace_back(origine, extremite, x, y);
266 }
267
268
269#if 0
270 {
271 auto start= std::chrono::high_resolution_clock::now();
272 direct(triangles, 0, int(triangles.size()), rays, 0, int(rays.size()));
273
274 auto stop= std::chrono::high_resolution_clock::now();
275 int cpu= std::chrono::duration_cast<std::chrono::milliseconds>(stop - start).count();
276 printf(
"direct %dms\n", cpu);
277 }
278#endif
279
280 {
281 auto start= std::chrono::high_resolution_clock::now();
282 divide(
bounds, triangles, 0,
int(triangles.size()), rays, 0,
int(rays.size()));
283
284 auto stop= std::chrono::high_resolution_clock::now();
285 int cpu= std::chrono::duration_cast<std::chrono::milliseconds>(stop - start).count();
286 printf(
"divide %dms\n", cpu);
287 }
288
289
290 for(unsigned i= 0; i < rays.size(); i++)
291 {
292 if(rays[i])
293 {
294 int x= rays[i].x;
295 int y= rays[i].y;
296 float u= rays[i].u;
297 float v= rays[i].v;
298 float w= 1 - u - v;
299 image(x, y)=
Color(w, u, v);
300 }
301 }
303
304
305 return 0;
306}
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(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.
representation d'un point 3d.