ccdemos/textures.cc
branchpyrit
changeset 93 96d65f841791
parent 92 9af5c039b678
child 100 c005054bf4c1
equal deleted inserted replaced
92:9af5c039b678 93:96d65f841791
     1 #include "raytracer.h"
     1 #include "raytracer.h"
     2 #include "kdtree.h"
     2 #include "kdtree.h"
     3 
     3 
     4 #include "common_sdl.h"
     4 #include "common_sdl.h"
     5 
     5 
     6 Camera cam(Vector(0.,6.,6.), Vector(0.,2.,-7.), Vector(0.,0.,-1.));
     6 Camera cam(Vector(0.0f,6.0f,6.0f), Vector(0.0f,2.0f,-7.0f), Vector(0.0f,0.0f,-1.0f));
     7 Light light(Vector(-2.0, 10.0, -2.0), Colour(0.9, 0.9, 0.9));
     7 Light light(Vector(-2.0f, 10.0f, -2.0f), Colour(0.9f, 0.9f, 0.9f));
     8 
     8 
     9 Float lx, ly, lz, cf;
     9 Float lx, ly, lz, cf;
    10 
    10 
    11 void update_callback(Float*)
    11 void update_callback(Float*)
    12 {
    12 {
    23 void key_callback(int key, int down)
    23 void key_callback(int key, int down)
    24 {
    24 {
    25 	switch (key)
    25 	switch (key)
    26 	{
    26 	{
    27 		case SDLK_r:
    27 		case SDLK_r:
    28 			lx = -0.1 * down;
    28 			lx = -0.1f * down;
    29 			break;
    29 			break;
    30 		case SDLK_t:
    30 		case SDLK_t:
    31 			lx = +0.1 * down;
    31 			lx = +0.1f * down;
    32 			break;
    32 			break;
    33 		case SDLK_f:
    33 		case SDLK_f:
    34 			ly = -0.1 * down;
    34 			ly = -0.1f * down;
    35 			break;
    35 			break;
    36 		case SDLK_g:
    36 		case SDLK_g:
    37 			ly = +0.1 * down;
    37 			ly = +0.1f * down;
    38 			break;
    38 			break;
    39 		case SDLK_v:
    39 		case SDLK_v:
    40 			lz = -0.1 * down;
    40 			lz = -0.1f * down;
    41 			break;
    41 			break;
    42 		case SDLK_b:
    42 		case SDLK_b:
    43 			lz = +0.1 * down;
    43 			lz = +0.1f * down;
    44 			break;
    44 			break;
    45 
    45 
    46 		case SDLK_z:
    46 		case SDLK_z:
    47 			cf = -0.02 * down;
    47 			cf = -0.02f * down;
    48 			break;
    48 			break;
    49 		case SDLK_x:
    49 		case SDLK_x:
    50 			cf = +0.02 * down;
    50 			cf = +0.02f * down;
    51 			break;
    51 			break;
    52 	}
    52 	}
    53 }
    53 }
    54 
    54 
    55 int main(int argc, char **argv)
    55 int main(int argc, char **argv)
    61 	rt.setTop(&top);
    61 	rt.setTop(&top);
    62 
    62 
    63 	rt.addLight(&light);
    63 	rt.addLight(&light);
    64 	light.castShadows(false);
    64 	light.castShadows(false);
    65 
    65 
    66 	Material mat0a(Colour(0.7, 0.7, 0.7));
    66 	const Colour c_white(1, 1, 1);
    67 	mat0a.setReflectivity(0.0);
    67 	const Colour c_black(0, 0, 0);
    68 	Box box(Vector(-12.0, -1.2, -20.0), Vector(12.0, -1.0, 0.0), &mat0a);
    68 
       
    69 	Material mat0a(Colour(0.7f, 0.7f, 0.7f));
       
    70 	mat0a.setReflectivity(0.0f);
       
    71 	Box box(Vector(-12.0f, -1.2f, -20.0f), Vector(12.0f, -1.0f, 0.0f), &mat0a);
    69 	rt.addShape(&box);
    72 	rt.addShape(&box);
    70 
    73 
    71 	Material mat0b(Colour(0.1, 0.7, 0.8));
    74 	Material mat0b(Colour(0.1f, 0.7f, 0.8f));
    72 	mat0b.setReflectivity(0.7);
    75 	mat0b.setReflectivity(0.7f);
    73 	Box box2(Vector(-12.0, -1.2, -10.0), Vector(12.0, 10.0, -10.2), &mat0b);
    76 	Box box2(Vector(-12.0f, -1.2f, -10.0f), Vector(12.0f, 10.0f, -10.2f), &mat0b);
    74 	rt.addShape(&box2);
    77 	rt.addShape(&box2);
    75 
    78 
    76 	Float bounds[] = {0.3, 0.6, 1.1};
    79 	Float bounds[] = {0.3f, 0.6f, 1.1f};
    77 	Colour colours[] = {Colour(0,0,0), Colour(1,1,1), Colour(0,0,0)};
    80 	Colour colours[] = {c_black, c_white, c_black};
    78 	BoundColourMap cmap(bounds, colours);
    81 	BoundColourMap cmap(bounds, colours);
    79 
    82 
    80 	// spheres
    83 	// spheres
    81 	Material mat1(Colour(1.0, 1.0, 1.0));
    84 	Material mat1(c_white);
    82 	mat1.texture = new CheckersTexture(new PlanarMap(Vector(-4.5, 2.0, -7.0), 0.48), &cmap);
    85 	mat1.texture = new CheckersTexture(new PlanarMap(Vector(-4.5f, 2.0f, -7.0f), 0.48f), &cmap);
    83 	Sphere sphere1(Vector(-4.5, 2.0, -7.0), 1.0, &mat1);
    86 	Sphere sphere1(Vector(-4.5f, 2.0f, -7.0f), 1, &mat1);
    84 	rt.addShape(&sphere1);
    87 	rt.addShape(&sphere1);
    85 
    88 
    86 	Material mat2(Colour(1.0, 1.0, 1.0));
    89 	Material mat2(c_white);
    87 	mat2.texture = new CheckersTexture(new CubicMap(Vector(-1.5, 2.0, -7.0), 0.48), &cmap);
    90 	mat2.texture = new CheckersTexture(new CubicMap(Vector(-1.5f, 2.0f, -7.0f), 0.48f), &cmap);
    88 	Sphere sphere2(Vector(-1.5, 2.0, -7.0), 1.0, &mat2);
    91 	Sphere sphere2(Vector(-1.5f, 2.0f, -7.0f), 1, &mat2);
    89 	rt.addShape(&sphere2);
    92 	rt.addShape(&sphere2);
    90 
    93 
    91 	Material mat3(Colour(1.0, 1.0, 1.0));
    94 	Material mat3(c_white);
    92 	mat3.texture = new CheckersTexture(new CylinderMap(Vector(1.5, 2.0, -7.0), 0.48), &cmap);
    95 	mat3.texture = new CheckersTexture(new CylinderMap(Vector(1.5f, 2.0f, -7.0f), 0.48f), &cmap);
    93 	Sphere sphere3(Vector(1.5, 2.0, -7.0), 1.0, &mat3);
    96 	Sphere sphere3(Vector(1.5f, 2.0f, -7.0f), 1, &mat3);
    94 	rt.addShape(&sphere3);
    97 	rt.addShape(&sphere3);
    95 
    98 
    96 	Material mat4(Colour(1.0, 1.0, 1.0));
    99 	Material mat4(c_white);
    97 	mat4.texture = new CheckersTexture(new SphereMap(Vector(4.5, 2.0, -7.0), 0.48), &cmap);
   100 	mat4.texture = new CheckersTexture(new SphereMap(Vector(4.5f, 2.0f, -7.0f), 0.48f), &cmap);
    98 	Sphere sphere4(Vector(4.5, 2.0, -7.0), 1.0, &mat4);
   101 	Sphere sphere4(Vector(4.5f, 2.0f, -7.0f), 1, &mat4);
    99 	rt.addShape(&sphere4);
   102 	rt.addShape(&sphere4);
   100 
   103 
   101 	// cubes
   104 	// cubes
   102 	Material mat5(Colour(1.0, 1.0, 1.0));
   105 	Material mat5(c_white);
   103 	mat5.texture = new CheckersTexture(new PlanarMap(Vector(-4.5, 0.0, -7.0), 0.48), &cmap);
   106 	const Vector cube1_base(-4.5f, 0.0f, -7.0f);
   104 	Box cube1(Vector(-4.5, 0.0, -7.0)-1.0, Vector(-4.5, 0.0, -7.0)+1.0, &mat5);
   107 	mat5.texture = new CheckersTexture(new PlanarMap(cube1_base, 0.48f), &cmap);
       
   108 	Box cube1(cube1_base - 1.0f, cube1_base + 1.0f, &mat5);
   105 	rt.addShape(&cube1);
   109 	rt.addShape(&cube1);
   106 
   110 
   107 	Material mat6(Colour(1.0, 1.0, 1.0));
   111 	Material mat6(c_white);
   108 	mat6.texture = new CheckersTexture(new CubicMap(Vector(-1.5, 0.0, -7.0), 0.48), &cmap);
   112 	const Vector cube2_base(-1.5f, 0.0f, -7.0f);
   109 	Box cube2(Vector(-1.5, 0.0, -7.0)-1.0, Vector(-1.5, 0.0, -7.0)+1.0, &mat6);
   113 	mat6.texture = new CheckersTexture(new CubicMap(cube2_base, 0.48f), &cmap);
       
   114 	Box cube2(cube2_base - 1.0f, cube2_base + 1.0f, &mat6);
   110 	rt.addShape(&cube2);
   115 	rt.addShape(&cube2);
   111 
   116 
   112 	Material mat7(Colour(1.0, 1.0, 1.0));
   117 	Material mat7(c_white);
   113 	mat7.texture = new CheckersTexture(new CylinderMap(Vector(1.5, 0.0, -7.0), 0.48), &cmap);
   118 	const Vector cube3_base(1.5f, 0.0f, -7.0f);
   114 	Box cube3(Vector(1.5, 0.0, -7.0)-1.0, Vector(1.5, 0.0, -7.0)+1.0, &mat7);
   119 	mat7.texture = new CheckersTexture(new CylinderMap(cube3_base, 0.48f), &cmap);
       
   120 	Box cube3(cube3_base - 1.0f, cube3_base + 1.0f, &mat7);
   115 	rt.addShape(&cube3);
   121 	rt.addShape(&cube3);
   116 
   122 
   117 	Material mat8(Colour(1.0, 1.0, 1.0));
   123 	Material mat8(c_white);
   118 	mat8.texture = new CheckersTexture(new SphereMap(Vector(4.5, 0.0, -7.0), 0.48), &cmap);
   124 	const Vector cube4_base(4.5f, 0.0f, -7.0f);
   119 	Box cube4(Vector(4.5, 0.0, -7.0)-1.0, Vector(4.5, 0.0, -7.0)+1.0, &mat8);
   125 	mat8.texture = new CheckersTexture(new SphereMap(cube4_base, 0.48f), &cmap);
       
   126 	Box cube4(cube4_base - 1.0f, cube4_base + 1.0f, &mat8);
   120 	rt.addShape(&cube4);
   127 	rt.addShape(&cube4);
   121 
   128 
   122 	mat1.setReflectivity(0);
   129 	mat1.setReflectivity(0);
   123 	mat2.setReflectivity(0);
   130 	mat2.setReflectivity(0);
   124 	mat3.setReflectivity(0);
   131 	mat3.setReflectivity(0);
   138 
   145 
   139 	/* render image */
   146 	/* render image */
   140 	if (argc == 2 && !strcmp(argv[1], "-r"))
   147 	if (argc == 2 && !strcmp(argv[1], "-r"))
   141 	{
   148 	{
   142 		pyrit_verbosity = 2;
   149 		pyrit_verbosity = 2;
   143 		rt.ambientOcclusion(300, 5.0, 0.5);
   150 		rt.ambientOcclusion(300, 5.0f, 0.5f);
   144 		DefaultSampler sampler(w, h);
   151 		DefaultSampler sampler(w, h);
   145 		sampler.setOversample(2);
   152 		sampler.setOversample(2);
   146 		sampler.setSubsample(1);
   153 		sampler.setSubsample(1);
   147 		rt.setSampler(&sampler);
   154 		rt.setSampler(&sampler);
   148 		rt.render();
   155 		rt.render();