Completed graphics assignment 6: ray tracing 1.

parent 481a8b48
/* Computer Graphics, Assignment, Ray-tracing 1
*
* Student name .... Sander van Veen, Taddeüs Kroes
* Student email ... sander@vo20.nl, taddeuskroes@gmail.com
* Collegekaart .... 6167969,
* Student email ... sander@vo20.nl , taddeuskroes@gmail.com
* Collegekaart .... 6167969 , 6054129
* Date ............
* Comments ........
*/
......@@ -176,65 +176,42 @@ ray_trace(void)
image_plane_width = image_plane_height * (1.0 * framebuffer_width
/ framebuffer_height);
float l = image_plane_width * .5f, // measured from e along right_vector
r = -l,
b = image_plane_height * .5f, // measured from e along up_vector
t = -b;
float scale = image_plane_width / framebuffer_width;
float u, v;
vec3 scaled_right = v3_multiply(right_vector, scale),
origin = scene_camera_position,
next_line_diff = v3_subtract(
v3_add(v3_multiply(up_vector, scale),
v3_multiply(right_vector, image_plane_width)),
origin
);
float frame_height_div = 1.0 / framebuffer_height,
frame_width_div = 1.0 / framebuffer_width;
// Find midpoint and point 0,0
vec3 p = v3_subtract(
v3_add(v3_add(origin, forward_vector),
v3_multiply(up_vector, 0.5 * (image_plane_height - 0.5 * scale))),
v3_multiply(right_vector, 0.5 * (image_plane_width - 0.5 * scale))
);
up_vector = v3_normalize(up_vector);
vec3 ray_o = scene_camera_position,
ray_d = forward_vector;
// e = scene_camera_position;
// e = v3_subtract(scene_camera_lookat, scene_camera_position);
// Loop over all pixels in the framebuffer
for( j = 0; j < framebuffer_height; j++ )
{
for( i = 0; i < framebuffer_width; i++ )
p = v3_subtract(p, origin);
for (i = 0; i < framebuffer_width; i++)
{
// compute viewing ray
//u = l + (r - l) * (i + 0.5) * frame_width_div;
//v = b + (t - b) * (j + 0.5) * frame_height_div;
// One unit distance, adjust to plane center and normalize
ray_d = v3_normalize(v3_create(
((i + .5f) * frame_width_div) + r,
((framebuffer_height - j + .5f) * frame_height_div) + t,
1.f
));
ray_d = v3_create(
right_vector.x * ray_d.x + up_vector.x * ray_d.y
+ forward_vector.x * ray_d.z,
right_vector.y * ray_d.x + up_vector.y * ray_d.y
+ forward_vector.y * ray_d.z,
right_vector.z * ray_d.x + up_vector.z * ray_d.y
+ forward_vector.z * ray_d.z
);
//ray_d = v3_normalize(v3_add(
// ray_o,
// v3_add(v3_multiply(right_vector, u), v3_multiply(up_vector, v))
//));
//if( i == 0 && j < 20 )
// printf("d: [%f, %f, %f]\n", ray_d.x, ray_d.y, ray_d.z);
//ray_origin = v3_add(e, v3_add(v3_multiply(right_vector, u),
// v3_multiply(up_vector, v)));
color = ray_color(0, ray_o, ray_d);
// Calculate point on the image plane that is center of the pixel
p = v3_add(p, scaled_right);
color = ray_color(0, origin, p);
// Output pixel color
put_pixel(i, j, color.x, color.y, color.z);
}
sprintf(buf, "Ray-tracing ::: %.0f%% done",
// Reset for the next line of pixels
p = v3_subtract(p, next_line_diff);
sprintf(buf, "Ray-tracing ::: %.0f%% done",
100.0 * j / framebuffer_height);
glutSetWindowTitle(buf);
}
......
/* Computer Graphics, Assignment, Ray-tracing 1
*
* Student name ....
* Student email ...
* Collegekaart ....
* Student name .... Sander van Veen, Taddeüs Kroes
* Student email ... sander@vo20.nl , taddeuskroes@gmail.com
* Collegekaart .... 6167969 , 6054129
* Date ............
* Comments ........
*
*
* (always fill in these fields before submitting!!)
*/
#include <math.h>
......@@ -34,19 +31,84 @@ shade_constant(intersection_point ip)
vec3
shade_matte(intersection_point ip)
{
return v3_create(1, 0, 0);
int i;
float c = .0f,
dot;
vec3 shade;
for( i = 0; i < scene_num_lights; i++ )
{
shade = v3_normalize(v3_subtract(scene_lights[i].position, ip.p));
dot = v3_dotprod(ip.n, shade);
if( dot > .0f
&& !shadow_check(v3_add(ip.p, v3_multiply(shade, .001f)), shade) )
{
c += scene_lights[i].intensity * dot;
}
}
c += scene_ambient_light;
// Make sure c is in range [0, 1]
if( c > 1.f )
c = 1.f;
return v3_create(c, c, c);
}
vec3
shade_blinn_phong(intersection_point ip)
{
return v3_create(1, 0, 0);
int i, alpha = 50;
float c = .0f, dot, spec = .0f;
vec3 shade;
// Compute diffuse and specular illumination part
for (i = 0; i < scene_num_lights; i++)
{
shade = v3_normalize(v3_subtract(scene_lights[i].position, ip.p));
dot = v3_dotprod(ip.n, shade);
if( dot > .0f
&& !shadow_check(v3_add(ip.p, v3_multiply(shade, .001f)), shade) )
{
c += scene_lights[i].intensity * dot;
}
// Specular (color of a highlight)
spec += scene_lights[i].intensity *
pow(v3_dotprod(ip.n, v3_normalize(v3_add(ip.i, shade))), alpha);
}
// Apply k_d and k_s
c *= .8f;
spec *= .5f;
c += scene_ambient_light;
// Make sure color components are in [0, 1]
spec = fmin(spec, 1.f);
c = fmin(c + spec, 1.f);
return v3_create(c, spec, spec);
}
vec3
shade_reflection(intersection_point ip)
{
return v3_create(1, 0, 0);
// Compute reflection vector also to prevent self-reflection. Add a
// small bias to the surface point in the direction of the normal.
vec3 r = v3_normalize(
v3_subtract(v3_multiply(ip.n, 2.f * v3_dotprod(ip.i, ip.n)), ip.i)
);
r = ray_color(ip.ray_level + 1, v3_add(ip.p, v3_multiply(ip.n, .001f)), r);
// Mix it with 75% matte shade
return v3_add(v3_multiply(shade_matte(ip), .75f), v3_multiply(r, .25f));
}
// Returns the shaded color for the given point to shade.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment