src/raytracer.cc
branchpyrit
changeset 84 6f7fe14782c2
parent 83 e3a2a5b26abb
child 86 ce6abe0aeeae
equal deleted inserted replaced
83:e3a2a5b26abb 84:6f7fe14782c2
    72 
    72 
    73 // ---- tyto dve funkce budou v budouci verzi metody objektu PhongShader
    73 // ---- tyto dve funkce budou v budouci verzi metody objektu PhongShader
    74 
    74 
    75 // calculate shader function
    75 // calculate shader function
    76 // P is point of intersection, N normal in this point
    76 // P is point of intersection, N normal in this point
    77 Colour PhongShader_ambient(Material &mat, Vector3 &P)
    77 Colour PhongShader_ambient(const Material &mat, const Vector3 &P)
    78 {
    78 {
    79 	Colour col;
    79 	Colour col;
    80 	if (mat.texture)
    80 	if (mat.texture)
    81 		col = mat.texture->evaluate(P);
    81 		col = mat.texture->evaluate(P);
    82 	else
    82 	else
    90  P is point of intersection,
    90  P is point of intersection,
    91  N normal in this point,
    91  N normal in this point,
    92  R direction of reflected ray,
    92  R direction of reflected ray,
    93  V direction to the viewer
    93  V direction to the viewer
    94 */
    94 */
    95 Colour PhongShader_calculate(Material &mat, Vector3 &P, Vector3 &N, Vector3 &R, Vector3 &V,
    95 Colour PhongShader_calculate(const Material &mat,
    96 	Light &light)
    96 	const Vector3 &P, const Vector3 &N, const Vector3 &R, const Vector3 &V,
       
    97 	const Light &light)
    97 {
    98 {
    98 	Colour I = Colour();
    99 	Colour I = Colour();
    99 	Vector3 L = light.pos - P;
   100 	Vector3 L = light.pos - P;
   100 	L.normalize();
   101 	L.normalize();
   101 	Float L_dot_N = dot(L, N);
   102 	Float L_dot_N = dot(L, N);
   114 	if (R_dot_V > 0)
   115 	if (R_dot_V > 0)
   115 		I += mat.specular * light.colour * powf(R_dot_V, mat.shininess);
   116 		I += mat.specular * light.colour * powf(R_dot_V, mat.shininess);
   116 	return I;
   117 	return I;
   117 }
   118 }
   118 
   119 
   119 Colour Raytracer::shader_evalulate(Ray &ray, int depth, Shape *origin_shape,
   120 Colour Raytracer::shader_evalulate(const Ray &ray, int depth, Shape *origin_shape,
   120 	Float nearest_distance, Shape *nearest_shape)
   121 	Float nearest_distance, Shape *nearest_shape)
   121 {
   122 {
   122 	Colour col = Colour();
   123 	Colour col = Colour();
   123 	Vector3 P = ray.o + ray.dir * nearest_distance; // point of intersection
   124 	Vector3 P = ray.o + ray.dir * nearest_distance; // point of intersection
   124 	Vector3 normal = nearest_shape->normal(P);
   125 	Vector3 normal = nearest_shape->normal(P);
   234 {
   235 {
   235 	Float nearest_distance = Inf;
   236 	Float nearest_distance = Inf;
   236 	Shape *nearest_shape = top->nearest_intersection(origin_shape, ray, nearest_distance);
   237 	Shape *nearest_shape = top->nearest_intersection(origin_shape, ray, nearest_distance);
   237 
   238 
   238 	if (nearest_shape == NULL)
   239 	if (nearest_shape == NULL)
   239 	{
       
   240 		return bg_colour;
   240 		return bg_colour;
   241 	}
       
   242 	else
   241 	else
   243 	{
       
   244 		return shader_evalulate(ray, depth, origin_shape, nearest_distance, nearest_shape);
   242 		return shader_evalulate(ray, depth, origin_shape, nearest_distance, nearest_shape);
   245 	}
   243 }
   246 }
   244 
   247 
   245 void Raytracer::raytracePacket(RayPacket &rays, Colour *results)
   248 void Raytracer::raytracePacket(Ray *rays, Colour *results)
       
   249 {
   246 {
   250 	Float nearest_distances[4] = {Inf,Inf,Inf,Inf};
   247 	Float nearest_distances[4] = {Inf,Inf,Inf,Inf};
   251 	Shape *nearest_shapes[4];
   248 	Shape *nearest_shapes[4];
   252 	static const Shape *origin_shapes[4] = {NULL, NULL, NULL, NULL};
   249 	static const Shape *origin_shapes[4] = {NULL, NULL, NULL, NULL};
   253 	top->packet_intersection(origin_shapes, rays, nearest_distances, nearest_shapes);
   250 	top->packet_intersection(origin_shapes, rays, nearest_distances, nearest_shapes);
       
   251 	Ray ray;
   254 
   252 
   255 	for (int i = 0; i < 4; i++)
   253 	for (int i = 0; i < 4; i++)
   256 		if (nearest_shapes[i] == NULL)
   254 		if (nearest_shapes[i] == NULL)
   257 			results[i] = bg_colour;
   255 			results[i] = bg_colour;
   258 		else
   256 		else
   265 	static const int my_queue_size = 256;
   263 	static const int my_queue_size = 256;
   266 	Raytracer *rt = (Raytracer*)d;
   264 	Raytracer *rt = (Raytracer*)d;
   267 	Sample my_queue[my_queue_size];
   265 	Sample my_queue[my_queue_size];
   268 	Colour my_colours[my_queue_size];
   266 	Colour my_colours[my_queue_size];
   269 	int my_count;
   267 	int my_count;
   270 	Ray ray, rays[4];
   268 	Ray ray;
       
   269 	RayPacket rays;
   271 	const bool can_use_packets = (rt->use_packets && rt->sampler->packetableSamples());
   270 	const bool can_use_packets = (rt->use_packets && rt->sampler->packetableSamples());
   272 	for (;;)
   271 	for (;;)
   273 	{
   272 	{
   274 		pthread_mutex_lock(&rt->sample_queue_mutex);
   273 		pthread_mutex_lock(&rt->sample_queue_mutex);
   275 		while (rt->sample_queue_count == 0)
   274 		while (rt->sample_queue_count == 0)