ccdemos/spheres_shadow.cc
branchpyrit
changeset 40 929aad02c5f2
parent 39 7079dcc3bd74
child 47 320d5d466864
equal deleted inserted replaced
39:7079dcc3bd74 40:929aad02c5f2
     1 #include "raytracer.h"
     1 #include "raytracer.h"
     2 #include "octree.h"
     2 #include "octree.h"
     3 
     3 
     4 #include "image.h"
     4 #include "image.h"
     5 #include "common_sdl.h"
     5 #include "common_sdl.h"
       
     6 
       
     7 Camera cam;
       
     8 Light light(Vector3(-2.0, 10.0, -2.0), Colour(0.9, 0.9, 0.9));
       
     9 
       
    10 Float lx, ly, lz, cf;
       
    11 
       
    12 void update_callback()
       
    13 {
       
    14 	if (lx != 0.0)
       
    15 		light.pos.x += lx;
       
    16 	if (ly != 0.0)
       
    17 		light.pos.y += ly;
       
    18 	if (lz != 0.0)
       
    19 		light.pos.z += lz;
       
    20 	if (cf != 0.0)
       
    21 		cam.f += cf;
       
    22 }
       
    23 
       
    24 void key_callback(int key, int down)
       
    25 {
       
    26 	switch (key)
       
    27 	{
       
    28 		case SDLK_r:
       
    29 			lx = -0.1 * down;
       
    30 			break;
       
    31 		case SDLK_t:
       
    32 			lx = +0.1 * down;
       
    33 			break;
       
    34 		case SDLK_f:
       
    35 			ly = -0.1 * down;
       
    36 			break;
       
    37 		case SDLK_g:
       
    38 			ly = +0.1 * down;
       
    39 			break;
       
    40 		case SDLK_v:
       
    41 			lz = -0.1 * down;
       
    42 			break;
       
    43 		case SDLK_b:
       
    44 			lz = +0.1 * down;
       
    45 			break;
       
    46 
       
    47 		case SDLK_z:
       
    48 			cf = -0.02 * down;
       
    49 			break;
       
    50 		case SDLK_x:
       
    51 			cf = +0.02 * down;
       
    52 			break;
       
    53 	}
       
    54 }
     6 
    55 
     7 int main(int argc, char **argv)
    56 int main(int argc, char **argv)
     8 {
    57 {
     9 	Raytracer rt;
    58 	Raytracer rt;
    10 	rt.setOversample(0);
    59 	rt.setOversample(0);
    11 	rt.setSubsample(8);
    60 	rt.setSubsample(8);
    12 
    61 
    13 	Octree top;
    62 	Octree top;
    14 	rt.setTop(&top);
    63 	rt.setTop(&top);
    15 
    64 
    16 	Light light1(Vector3(0.0, 5.0, -5.0), Colour(0.7, 0.3, 0.6));
    65 	rt.addlight(&light);
    17 	rt.addlight(&light1);
       
    18 
    66 
    19 	Light light2(Vector3(-2.0, 10.0, -2.0), Colour(0.4, 0.6, 0.3));
    67 	//Light light2;
    20 	rt.addlight(&light2);
    68 	//light2.colour = Colour(0.7, 0.3, 0.6);
       
    69 	//rt.addlight(&light2);
    21 
    70 
    22 	Material mat0(Colour(0.7, 0.7, 0.7));
    71 	Material mat0a(Colour(0.7, 0.7, 0.7));
       
    72 	mat0a. setReflectivity(0.0);
       
    73 	Box box(Vector3(-10.0, -1.2, -20.0), Vector3(10.0, -1.0, 0.0), &mat0a);
       
    74 	rt.addshape(&box);
    23 
    75 
    24 	Box box(Vector3(-20.0, -1.2, -20.0), Vector3(20.0, -1.0, 20.0), &mat0);
    76 	Material mat0b(Colour(0.1, 0.7, 0.8));
    25 	rt.addshape(&box);
    77 	mat0b.setReflectivity(0.7);
       
    78 	Box box2(Vector3(-10.0, -1.2, -20.0), Vector3(10.0, 10.0, -20.2), &mat0b);
       
    79 	rt.addshape(&box2);
    26 
    80 
    27 	Material mat1(Colour(1.0, 0.0, 0.0));
    81 	Material mat1(Colour(1.0, 0.0, 0.0));
    28 	Sphere bigsphere(Vector3(3.0, 2.0, -7.0), 3.0, &mat1);
    82 	Sphere bigsphere(Vector3(3.0, 2.0, -7.0), 3.0, &mat1);
    29 	rt.addshape(&bigsphere);
    83 	rt.addshape(&bigsphere);
    30 
    84 
    31 	Material mat2(Colour(0.0, 1.0, 0.0));
    85 	Material mat2(Colour(0.0, 1.0, 0.0));
    32 	Sphere smallsphere(Vector3(-5.5, 1.5, -8.0), 2.0, &mat2);
    86 	Sphere smallsphere(Vector3(-5.5, 1.5, -8.0), 2.0, &mat2);
    33 	rt.addshape(&smallsphere);
    87 	rt.addshape(&smallsphere);
    34 
    88 
    35 	Material mat3(Colour(0.0, 0.0, 1.0));
    89 	Material mat3(Colour(0.0, 0.0, 1.0));
    36 	Sphere tinysphere(Vector3(-1.2, 0.0, -2.0), 0.5, &mat3);
    90 	mat3.setReflectivity(0.1);
       
    91 	mat3.setTransmissivity(0.8, 1.5);
       
    92 	Sphere tinysphere(Vector3(-1.2, 0.0, -2.0), 0.7, &mat3);
    37 	rt.addshape(&tinysphere);
    93 	rt.addshape(&tinysphere);
    38 
    94 
    39 	top.optimize();
    95 	top.optimize();
    40 
    96 
    41 	Camera cam;
    97 	cam.setEye(Vector3(-2.28908, 4.30992, 12.3051));
    42 	cam.setEye(Vector3(0,0,15));
    98 	cam.p = Vector3(0.0988566, -0.139543, -0.985269);
       
    99 	cam.u = Vector3(-0.995004, 0, -0.0998334);
       
   100 	cam.v = Vector3(0.0139311, 0.990216, -0.138846);
    43 	rt.setCamera(&cam);
   101 	rt.setCamera(&cam);
    44 
   102 
    45 	w = 800;
   103 	w = 800;
    46 	h = 600;
   104 	h = 600;
    47 
   105 
    48 	if (argc == 2 && !strcmp(argv[1], "-i"))
   106 	/* run interactive mode */
    49 		loop_sdl(rt, cam);
   107 	loop_sdl(rt, cam, update_callback, key_callback);
    50 	else
   108 
       
   109 	/* render image */
       
   110 	if (argc == 2 && !strcmp(argv[1], "-r"))
    51 	{
   111 	{
       
   112 		pyrit_verbosity = 2;
    52 		Float *fdata = (Float *) malloc(w*h*3*sizeof(Float));
   113 		Float *fdata = (Float *) malloc(w*h*3*sizeof(Float));
       
   114 		rt.setOversample(2);
    53 		rt.render(w, h, fdata);
   115 		rt.render(w, h, fdata);
    54 	
   116 
    55 		struct image *img;
   117 		struct image *img;
    56 		new_image(&img, w, h, 3);
   118 		new_image(&img, w, h, 3);
    57 	
   119 
    58 		Float *fd = fdata;
   120 		Float *fd = fdata;
    59 		for (char *cd = img->data; cd != img->data + w*h*3; cd++, fd++) {
   121 		for (char *cd = img->data; cd != img->data + w*h*3; cd++, fd++) {
    60 			if (*fd > 1.0)
   122 			if (*fd > 1.0)
    61 				*cd = 255;
   123 				*cd = 255;
    62 			else
   124 			else