src/raytracer.cc
branchpyrit
changeset 11 4d192e13ee84
parent 10 f9fad94cd0cc
child 12 f4fcabf05785
equal deleted inserted replaced
10:f9fad94cd0cc 11:4d192e13ee84
    48 	x = xx*cos(q) - yy*sin(q);
    48 	x = xx*cos(q) - yy*sin(q);
    49 	y = xx*sin(q) + yy*cos(q);
    49 	y = xx*sin(q) + yy*cos(q);
    50 	z = zz;
    50 	z = zz;
    51 
    51 
    52 	return Vector3(x, y, z);
    52 	return Vector3(x, y, z);
    53 }
       
    54 
       
    55 inline Shape *Raytracer::nearest_intersection(const Shape *origin_shape, const Ray &ray,
       
    56 	float &nearest_distance)
       
    57 {
       
    58 	Shape *nearest_shape = NULL;
       
    59 	ShapeList::iterator shape;
       
    60 	for (shape = top->shapes.begin(); shape != top->shapes.end(); shape++)
       
    61 		if (*shape != origin_shape && (*shape)->intersect(ray, nearest_distance))
       
    62 			nearest_shape = *shape;
       
    63 	return nearest_shape;
       
    64 }
    53 }
    65 
    54 
    66 // ---- tyto dve funkce budou v budouci verzi metody objektu PhongShader
    55 // ---- tyto dve funkce budou v budouci verzi metody objektu PhongShader
    67 
    56 
    68 // calculate shader function
    57 // calculate shader function
   102 }
    91 }
   103 
    92 
   104 Colour Raytracer::raytrace(Ray &ray, int depth, Shape *origin_shape)
    93 Colour Raytracer::raytrace(Ray &ray, int depth, Shape *origin_shape)
   105 {
    94 {
   106 	float nearest_distance = FLT_MAX; //Infinity
    95 	float nearest_distance = FLT_MAX; //Infinity
   107 	Shape *nearest_shape = nearest_intersection(origin_shape, ray, nearest_distance);
    96 	Shape *nearest_shape = top->nearest_intersection(origin_shape, ray, nearest_distance);
   108 
    97 
   109 	if (nearest_shape == NULL) {
    98 	if (nearest_shape == NULL) {
   110 		return bg_colour;
    99 		return bg_colour;
   111 	} else {
   100 	} else {
   112 		Colour acc = Colour();
   101 		Colour acc = Colour();
   122 			if (L_dot_N > 0) {
   111 			if (L_dot_N > 0) {
   123 				// test if this light is occluded (sharp shadows)
   112 				// test if this light is occluded (sharp shadows)
   124 				if ((*light)->shadows) {
   113 				if ((*light)->shadows) {
   125 					Ray shadow_ray = Ray(P, L);
   114 					Ray shadow_ray = Ray(P, L);
   126 					float dist = FLT_MAX;
   115 					float dist = FLT_MAX;
   127 					if (nearest_intersection(nearest_shape, shadow_ray, dist))
   116 					if (top->nearest_intersection(nearest_shape, shadow_ray, dist))
   128 						continue;
   117 						continue;
   129 				}
   118 				}
   130 
   119 
   131 				// shading function
   120 				// shading function
   132 				Vector3 R = L - 2.0 * L_dot_N * normal;
   121 				Vector3 R = L - 2.0 * L_dot_N * normal;
   153 			float miss = 0;
   142 			float miss = 0;
   154 			for (int i = 0; i < ao_samples; i++) {
   143 			for (int i = 0; i < ao_samples; i++) {
   155 				Vector3 dir = SphereDistribute(i, ao_samples, ao_angle, normal);
   144 				Vector3 dir = SphereDistribute(i, ao_samples, ao_angle, normal);
   156 				Ray ao_ray = Ray(P, dir);
   145 				Ray ao_ray = Ray(P, dir);
   157 				float dist = ao_distance;
   146 				float dist = ao_distance;
   158 				Shape *shape_in_way = nearest_intersection(nearest_shape, ao_ray, dist);
   147 				Shape *shape_in_way = top->nearest_intersection(nearest_shape, ao_ray, dist);
   159 				if (shape_in_way == NULL)
   148 				if (shape_in_way == NULL)
   160 					miss += 1.0;
   149 					miss += 1.0;
   161 				else
   150 				else
   162 					miss += dist / ao_distance;
   151 					miss += dist / ao_distance;
   163 			}
   152 			}