gKit2 light
ray_tuto2.cpp
1 
2 #include <cstdio>
3 #include <cmath>
4 #include <cfloat>
5 
6 #include "vec.h"
7 #include "mat.h"
8 #include "mesh.h"
9 #include "wavefront.h"
10 #include "image.h"
11 #include "image_io.h"
12 #include "orbiter.h"
13 
14 #include "bvh.h"
15 
16 
17 int main( int argc, char **argv )
18 {
19  const char *file= "geometry.obj";
20  if(argc > 1) file= argv[1];
21 
22  Mesh mesh= read_mesh(file);
23  if(mesh == Mesh::error())
24  return 1;
25 
26  Orbiter camera= read_orbiter("orbiter.txt");
27 
28  BVH bvh= make_bvh(mesh.positions);
29 
30  release_mesh(mesh);
31 
32  // go
33  Image image(1024, 640);
34 
35  Point d0;
36  Vector dx, dy;
37  Point o= orbiter_position(camera);
38  orbiter_image_frame(camera, image.width(), image.height(), 1, 45, d0, dx, dy);
39 
40 #pragma omp parallel for schedule(dynamic, 16)
41  for(int y= 0; y < image.height(); y++)
42  for(int x= 0; x < image.width(); x++)
43  {
44  Point e= d0 + (float) x * dx + (float) y * dy;
45  Ray ray= make_ray(o, e);
46 
47  Hit hit;
48  if(intersect(bvh, ray, FLT_MAX, hit))
49  {
50  Color color= make_white();
51  color= make_color( std::abs(ray.direction.x), std::abs(ray.direction.y), std::abs(ray.direction.z) );
52  float cos_theta= std::abs(dot(hit.n, normalize(make_vector(hit.p, o))) );
53 
54  Hit mhit;
55  Ray mray= reflect(ray, hit.p, hit.n);
56  //~ Ray tray;
57  //~ if(refract(ray, hit.p, hit.n, 1.5f, tray))
58 
59  //~ if(intersect(bvh, tray, FLT_MAX, mhit))
60  //~ if(intersect(bvh, mray, FLT_MAX, mhit))
61  //~ color= make_color( std::abs(mray.direction.x), std::abs(mray.direction.y), std::abs(mray.direction.z) );
62  //~ color= make_color( std::abs(mhit.n.x), std::abs(mhit.n.y), std::abs(mhit.n.z) ) * fresnel(mray, hit.p, hit.n, 1.5f);
63 
64  image(x, y)= Color(color * cos_theta, 1);
65  }
66  }
67 
68  printf("triangles %lu %f/ray, boxes %lu %f/ray\n", tri_n, (float) tri_n / (float) image.size(), box_n, (float) box_n / (float) image.size());
69 
70  write_image(image, "render.png");
71  return 0;
72 }
representation de la camera, type orbiter, placee sur une sphere autour du centre de l'objet...
Definition: orbiter.h:16
Vector normalize(const Vector &v)
renvoie un vecteur unitaire / longueur == 1.
Definition: vec.cpp:79
representation d'un objet / maillage.
Definition: mesh.h:88
representation d'une couleur (rgba) transparente ou opaque.
Definition: color.h:13
representation du bvh.
Definition: bvh.h:39
representation d'un vecteur 3d.
Definition: vec.h:42
float dot(const Vector &u, const Vector &v)
renvoie le produit scalaire de 2 vecteurs.
Definition: vec.cpp:93
static Mesh & error()
Definition: mesh.h:250
representation d'un point d'intersection.
Definition: ray.h:33
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
int write_image(const Image &image, const char *filename)
enregistre une image dans un fichier png.
Definition: image_io.cpp:88
representation d'un rayon.
Definition: ray.h:12
representation d'une image.
Definition: image.h:18
representation d'un point 3d.
Definition: vec.h:19
Mesh read_mesh(const char *filename)
charge un fichier wavefront .obj et renvoie un mesh compose de triangles non indexes. utiliser glDrawArrays pour l'afficher. a detruire avec Mesh::release( ).
Definition: wavefront.cpp:8