/* Adds a triangle by interpolating over the given edges (which are expected to * contain an intersection). * The edges are specified by their vertices (u is the first edge, v the second, * etc.) */ void interpolate_edges(triangle *triangles, unsigned char isovalue, cell c, int u0, int u1, int v0, int v1, int w0, int w1) { vec3 p1, p2, p3; p1 = interpolate_points(isovalue, c.p[u0], c.p[u1], c.value[u0], c.value[u1]); p2 = interpolate_points(isovalue, c.p[v0], c.p[v1], c.value[v0], c.value[v1]); p3 = interpolate_points(isovalue, c.p[w0], c.p[w1], c.value[w0], c.value[w1]); triangles->p[0] = p1; triangles->p[1] = p2; triangles->p[2] = p3; // the surface-normal is the cross-product between 2 of the edges making up // the face vec3 normal = v3_crossprod(v3_subtract(p1, p2), v3_subtract(p3, p1)); normal = v3_normalize(normal); normal = v3_multiply(normal, -1); triangles->n[0] = normal; triangles->n[1] = normal; triangles->n[2] = normal; }
static void draw_bvh_inner_nodes(int level, bvh_node* node) { vec3 center, size; if (node->is_leaf) return; if (level == draw_bvh_mode) { center = v3_multiply(v3_add(node->bbox.min, node->bbox.max), 0.5); size = v3_subtract(node->bbox.max, node->bbox.min); glColor3f(1, 0, 0); glPushMatrix(); glTranslatef(center.x, center.y, center.z); glScalef(size.x, size.y, size.z); glutWireCube(1.0); glPopMatrix(); } else { draw_bvh_inner_nodes(level+1, node->u.inner.left_child); draw_bvh_inner_nodes(level+1, node->u.inner.right_child); } }
void *compute_next_dynamics(void *arg) { struct Work_Unit *chunk = (struct Work_Unit *)arg; // For each object... for( int object_i = chunk->start_index; object_i < chunk->stop_index; ++object_i ) { Vector3 total_force = { 0.0, 0.0, 0.0 }; // Consider interactions with all other objects... for( int object_j = 0; object_j < OBJECT_COUNT; ++object_j ) { if( object_i == object_j ) continue; Vector3 displacement = v3_subtract( current_dynamics[object_j].position, current_dynamics[object_i].position ); double distance_squared = magnitude_squared( displacement ); double distance = sqrt( distance_squared ); double force_magnitude = ( G * object_array[object_i].mass * object_array[object_j].mass ) / distance_squared; Vector3 force = v3_multiply( (force_magnitude / distance ), displacement ); total_force = v3_add( total_force, force ); } // Total force on object_i is now known. Compute acceleration, velocity and position. Vector3 acceleration = v3_divide( total_force, object_array[object_i].mass ); Vector3 delta_v = v3_multiply( TIME_STEP, acceleration ); Vector3 delta_position = v3_multiply( TIME_STEP, current_dynamics[object_i].velocity ); next_dynamics[object_i].velocity = v3_add( current_dynamics[object_i].velocity, delta_v ); next_dynamics[object_i].position = v3_add( current_dynamics[object_i].position, delta_position ); } return NULL; }
// Check if the given sphere is intersected by the given ray. // See Shirley et.al., section 10.3.1 // Returns 1 if there is an intersection (and sets the appropriate // fields of ip), or 0 otherwise. static int ray_intersects_sphere(intersection_point* ip, sphere sph, vec3 ray_origin, vec3 ray_direction) { float A, B, C, D; vec3 diff; float t_hit; A = v3_dotprod(ray_direction, ray_direction); diff = v3_subtract(ray_origin, sph.center); B = 2.0 * v3_dotprod(diff, ray_direction); C = v3_dotprod(diff, diff) - sph.radius * sph.radius; D = B*B - 4*A*C; if (D < 0.0) return 0; D = sqrt(D); // We're only interested in the first hit, i.e. the one with // the smallest t_hit, so we check -B-D first, followed by -B+D t_hit = (-B - D)/(2*A); if (t_hit < 0.0) { t_hit = (-B + D)/(2*A); if (t_hit < 0.0) return 0; } ip->t = t_hit; ip->p = v3_add(ray_origin, v3_multiply(ray_direction, t_hit)); ip->n = v3_normalize(v3_subtract(ip->p, sph.center)); ip->i = v3_normalize(v3_negate(ray_direction)); ip->material = sph.material; return 1; }
// Recursively subdivide triangles into two groups, by determining // a split plane and sorting the triangles into "left-of" and "right-of". static bvh_node* bvh_build_recursive(int depth, boundingbox bbox, triangle* triangles, int num_triangles) { vec3 bbox_size, bbox_center; int i, j, t; int sorted_dimensions[3]; int split_axis; float split_value; int num_triangles_left, num_triangles_right; boundingbox left_bbox, right_bbox; bvh_node *left_child, *right_child; if (depth == max_depth || num_triangles <= acceptable_leaf_size) { // Create a leaf node #ifdef VERBOSE if (num_triangles > acceptable_leaf_size) printf("[%d] Maximum depth reached, forced to create a leaf of %d triangles\n", depth, num_triangles); else printf("[%d] Creating a leaf node of %d triangles\n", depth, num_triangles); #endif return create_leaf_node(bbox, triangles, num_triangles); } // // Split the triangles into two groups, using a split plane based // on largest bbox side // bbox_size = v3_subtract(bbox.max, bbox.min); bbox_center = v3_multiply(v3_add(bbox.min, bbox.max), 0.5); // Sort bbox sides by size in descending order sorted_dimensions[0] = 0; sorted_dimensions[1] = 1; sorted_dimensions[2] = 2; // Good old bubble sort :) for (i = 0; i < 2; i++) { for (j = i+1; j < 3; j++) { if (v3_component(bbox_size, sorted_dimensions[i]) < v3_component(bbox_size, sorted_dimensions[j])) { t = sorted_dimensions[i]; sorted_dimensions[i] = sorted_dimensions[j]; sorted_dimensions[j] = t; } } } // Iterate over the three split dimensions in descending order of // bbox size on that dimension. When the triangles are unsplitable // (or not very favorably) continue to the next dimension. Create a // leaf with all triangles in case none of dimensions is a good // split candidate. for (i = 0; i < 3; i++) { // Partition the triangles on the chosen split axis, with the // split plane at the center of the bbox split_axis = sorted_dimensions[i]; split_value = v3_component(bbox_center, split_axis); #ifdef VERBOSE printf("[%d] Splitting on axis %d, value %.3f\n", depth, split_axis, split_value); #endif partition_on_split_value(&num_triangles_left, &num_triangles_right, triangles, num_triangles, split_axis, split_value); #ifdef VERBOSE printf("[%d] %d left, %d right\n", depth, num_triangles_left, num_triangles_right); #endif if (num_triangles_left == 0 || num_triangles_right == 0) continue; // Determine bboxes for the two new groups of triangles left_bbox = bound_triangles(triangles, num_triangles_left); if (v3_component(v3_subtract(left_bbox.max, left_bbox.min), split_axis) > 0.9 * v3_component(bbox_size, split_axis)) { //printf("[%d] skipping dimension (left)\n", split_axis); continue; } #ifdef VERBOSE printf("[%d] left bbox: %.3f, %.3f, %.3f .. %.3f, %.3f, %.3f\n", depth, left_bbox.min.x, left_bbox.min.y, left_bbox.min.z, left_bbox.max.x, left_bbox.max.y, left_bbox.max.z); #endif right_bbox = bound_triangles(triangles+num_triangles_left, num_triangles_right); if (v3_component(v3_subtract(right_bbox.max, right_bbox.min), split_axis) > 0.9 * v3_component(bbox_size, split_axis)) { //printf("[%d] skipping dimension (right)\n", split_axis); continue; } #ifdef VERBOSE printf("[%d] right bbox: %.3f, %.3f, %.3f .. %.3f, %.3f, %.3f\n", depth, right_bbox.min.x, right_bbox.min.y, right_bbox.min.z, right_bbox.max.x, right_bbox.max.y, right_bbox.max.z); #endif // Recurse using the two new groups left_child = bvh_build_recursive(depth+1, left_bbox, triangles, num_triangles_left); right_child = bvh_build_recursive(depth+1, right_bbox, triangles+num_triangles_left, num_triangles_right); // Create an inner code with two children return create_inner_node(left_child, right_child); } // Split dimensions exhausted, forced to create a leaf #ifdef VERBOSE printf("Split dimensions exhausted, creating a leaf of %d triangles\n", num_triangles); #endif return create_leaf_node(bbox, triangles, num_triangles); }
void ray_trace(void) { vec3 forward_vector, right_vector, up_vector; int i, j; float image_plane_width, image_plane_height; vec3 color; char buf[128]; struct timeval t0, t1; float time_taken; fprintf(stderr, "Ray tracing ..."); gettimeofday(&t0, NULL); num_rays_shot = num_shadow_rays_shot = num_triangles_tested = num_bboxes_tested = 0; // Compute camera coordinate system from camera position // and look-at point up_vector = v3_create(0, 0, 1); forward_vector = v3_normalize(v3_subtract(scene_camera_lookat, scene_camera_position)); right_vector = v3_normalize(v3_crossprod(forward_vector, up_vector)); up_vector = v3_crossprod(right_vector, forward_vector); // Compute size of image plane from the chosen field-of-view // and image aspect ratio. This is the size of the plane at distance // of one unit from the camera position. image_plane_height = 2.0 * tan(0.5*VFOV/180*M_PI); image_plane_width = image_plane_height * (1.0 * framebuffer_width / framebuffer_height); vec3 d, e, u, v, w, ud, vd; float l, r, b, t, nx, ny; // direction d, origin e // ONB u, v, w // image plane edges l, r, b, t // window size nx, ny e = scene_camera_position; u = right_vector, v = up_vector, w = v3_negate(forward_vector); l = -0.5 * image_plane_width, r = 0.5 * image_plane_width; b = 0.5 * image_plane_height, t = -0.5 * image_plane_height; nx = framebuffer_width, ny = framebuffer_height; // Loop over all pixels in the framebuffer for (j = 0; j < ny; j++) { for (i = 0; i < nx; i++) { ud = v3_multiply(u, (l + (r - l) * ((i + 0.5) / nx))); vd = v3_multiply(v, (b + (t - b) * ((j + 0.5) / ny))); d = v3_add(v3_add(ud, vd), v3_negate(w)); color = ray_color(0, e, d); put_pixel(i, j, color.x, color.y, color.z); } sprintf(buf, "Ray-tracing ::: %.0f%% done", 100.0*j/framebuffer_height); glutSetWindowTitle(buf); } // Done! gettimeofday(&t1, NULL); glutSetWindowTitle("Ray-tracing ::: done"); // Output some statistics time_taken = 1.0 * (t1.tv_sec - t0.tv_sec) + (t1.tv_usec - t0.tv_usec) / 1000000.0; fprintf(stderr, " done in %.1f seconds\n", time_taken); fprintf(stderr, "... %lld total rays shot, of which %d camera rays and " "%lld shadow rays\n", num_rays_shot, do_antialiasing ? 4*framebuffer_width*framebuffer_height : framebuffer_width*framebuffer_height, num_shadow_rays_shot); fprintf(stderr, "... %lld triangles intersection tested " "(avg %.1f tri/ray)\n", num_triangles_tested, 1.0*num_triangles_tested/num_rays_shot); fprintf(stderr, "... %lld bboxes intersection tested (avg %.1f bbox/ray)\n", num_bboxes_tested, 1.0*num_bboxes_tested/num_rays_shot); }
void ray_trace(void) { vec3 forward_vector, right_vector, up_vector; int i, j; float image_plane_width, image_plane_height; vec3 color; char buf[128]; struct timeval t0, t1; float time_taken; fprintf(stderr, "Ray tracing ..."); gettimeofday(&t0, NULL); num_rays_shot = num_shadow_rays_shot = num_triangles_tested = num_bboxes_tested = 0; // Compute camera coordinate system from camera position // and look-at point up_vector = v3_create(0, 0, 1); forward_vector = v3_normalize(v3_subtract(scene_camera_lookat, scene_camera_position)); right_vector = v3_normalize(v3_crossprod(forward_vector, up_vector)); up_vector = v3_crossprod(right_vector, forward_vector); // Compute size of image plane from the chosen field-of-view // and image aspect ratio. This is the size of the plane at distance // of one unit from the camera position. image_plane_height = 2.0 * tan(0.5*VFOV/180*M_PI); image_plane_width = image_plane_height * (1.0 * framebuffer_width / framebuffer_height); // vector points to the middle of the monitor vec3 plane_center = v3_add(scene_camera_position, forward_vector); // vector points to the left side of the monitor vec3 le_unnormalized = v3_multiply(right_vector, -(image_plane_width / 2.0)); // vector points to the up side of the monitor vec3 up_unnormalized = v3_multiply(up_vector, image_plane_height / 2.0); // vector points to the coordinates 0,0 (left up) of the monitor vec3 left_up = v3_add(plane_center, v3_add(le_unnormalized, up_unnormalized)); // vector points to right, and has length equal to width of a pixel vec3 pixel2pixel_x = v3_multiply(right_vector, (image_plane_width / framebuffer_width)); // vector points to down, and has length equal to height of a pixel vec3 pixel2pixel_y = v3_multiply(up_vector, -(image_plane_height / framebuffer_height)); // ANTI-ALIASING LOOP if (do_antialiasing) { // loop over all pixels in the framebuffer for (j = 0; j < framebuffer_height; j++) { for (i = 0; i < framebuffer_width; i++) { // for each pixel, shoot four rays vec3 ray_direction_00 = v3_add(v3_add(left_up, v3_multiply(pixel2pixel_y, j + 0.25)), v3_multiply(pixel2pixel_x, i + 0.25)); vec3 ray_direction_01 = v3_add(v3_add(left_up, v3_multiply(pixel2pixel_y, j + 0.75)), v3_multiply(pixel2pixel_x, i + 0.25)); vec3 ray_direction_10 = v3_add(v3_add(left_up, v3_multiply(pixel2pixel_y, j + 0.25)), v3_multiply(pixel2pixel_x, i + 0.75)); vec3 ray_direction_11 = v3_add(v3_add(left_up, v3_multiply(pixel2pixel_y, j + 0.75)), v3_multiply(pixel2pixel_x, i + 0.75)); // for each ray fired, get the difference ray_direction_00 = v3_subtract(ray_direction_00, scene_camera_position); ray_direction_01 = v3_subtract(ray_direction_01, scene_camera_position); ray_direction_10 = v3_subtract(ray_direction_10, scene_camera_position); ray_direction_11 = v3_subtract(ray_direction_11, scene_camera_position); // add the colors of the four rays, then divide by four color = ray_color(0, scene_camera_position, ray_direction_00); color = v3_add(color, ray_color(0, scene_camera_position, ray_direction_01)); color = v3_add(color, ray_color(0, scene_camera_position, ray_direction_10)); color = v3_add(color, ray_color(0, scene_camera_position, ray_direction_11)); color = v3_multiply(color, 0.25); // output pixel color put_pixel(i, j, color.x, color.y, color.z); } sprintf(buf, "Ray-tracing (AA enabled) ::: %.0f%% done", 100.0*j/framebuffer_height); glutSetWindowTitle(buf); } } // NON-ANTI-ALIASING LOOP else { // loop over all pixels in the framebuffer for (j = 0; j < framebuffer_height; j++) { for (i = 0; i < framebuffer_width; i++) { // select the currently relevant pixel vec3 ray_direction = v3_add(v3_add(left_up, v3_multiply(pixel2pixel_y, j + 0.5)), v3_multiply(pixel2pixel_x, i + 0.5)); // get diference between the two points ray_direction = v3_subtract(ray_direction, scene_camera_position); // set color color = ray_color(0, scene_camera_position, ray_direction); // output pixel color put_pixel(i, j, color.x, color.y, color.z); } sprintf(buf, "Ray-tracing ::: %.0f%% done", 100.0*j/framebuffer_height); glutSetWindowTitle(buf); } } // set a detailed title, useful for testing if (use_bvh && do_antialiasing) glutSetWindowTitle("Ray-tracing is done. AA=yes, BVH=yes"); else if (use_bvh && !do_antialiasing) glutSetWindowTitle("Ray-tracing is done. AA=no, BVH=yes"); else if (!use_bvh && do_antialiasing) glutSetWindowTitle("Ray-tracing is done. AA=yes, BVH=no"); else glutSetWindowTitle("Ray-tracing is done. AA=no, BVH=no"); // Done! gettimeofday(&t1, NULL); // Output some statistics time_taken = 1.0 * (t1.tv_sec - t0.tv_sec) + (t1.tv_usec - t0.tv_usec) / 1000000.0; fprintf(stderr, " done in %.1f seconds\n", time_taken); fprintf(stderr, "... %d total rays shot, of which %d camera rays and %d shadow rays\n", num_rays_shot, do_antialiasing ? 4*framebuffer_width*framebuffer_height : framebuffer_width*framebuffer_height, num_shadow_rays_shot); fprintf(stderr, "... %d triangles intersection tested (avg %.1f tri/ray)\n", num_triangles_tested, 1.0*num_triangles_tested/num_rays_shot); fprintf(stderr, "... %d bboxes intersection tested (avg %.1f bbox/ray)\n", num_bboxes_tested, 1.0*num_bboxes_tested/num_rays_shot); }
void ray_trace(void) { vec3 forward_vector, right_vector, up_vector; int i, j; float image_plane_width, image_plane_height; vec3 color; char buf[128]; struct timeval t0, t1; float time_taken; fprintf(stderr, "Ray tracing ..."); gettimeofday(&t0, NULL); num_rays_shot = num_shadow_rays_shot = num_triangles_tested = num_bboxes_tested = 0; // Compute camera coordinate system from camera position // and look-at point up_vector = v3_create(0, 0, 1); forward_vector = v3_normalize(v3_subtract(scene_camera_lookat, scene_camera_position)); right_vector = v3_normalize(v3_crossprod(forward_vector, up_vector)); up_vector = v3_crossprod(right_vector, forward_vector); // Compute size of image plane from the chosen field-of-view // and image aspect ratio. This is the size of the plane at distance // of one unit from the camera position. image_plane_height = 2.0 * tan(0.5*VFOV/180*M_PI); image_plane_width = image_plane_height * (1.0 * framebuffer_width / framebuffer_height); printf("imageplane: (%f, %f)\n", image_plane_width, image_plane_height); // the borders of the image plane in camera coordinates float left = -(image_plane_width / 2), right = image_plane_width / 2, bottom = (image_plane_height / 2), top = -image_plane_height / 2; // rays are expressed as a location vector and direction vector vec3 ray_origin = scene_camera_position; float u, v, w; // Loop over all pixels in the framebuffer for (j = 0; j < framebuffer_height; j++) { for (i = 0; i < framebuffer_width; i++) { vec3 directions[4]; int directions_i = 0; float offsets[2]; int offsets_n; // if anti-aliasing is activated, shoot 4 rays through each pixel // slightly offset from the center if (do_antialiasing) { offsets_n = 2; offsets[0] = 0.25; offsets[1] = 0.75; } // without anti-aliasing, just send a single ray through the // pixel-center else { offsets_n = 1; offsets[0] = 0.5; } // calculate the [u, v, w] components of the pixel's location // relative to the camera for (int u_offset = 0; u_offset < offsets_n; ++u_offset) { for (int v_offset = 0; v_offset < offsets_n; ++v_offset) { w = 1; u = left + (right - left) * (i + offsets[u_offset]) / framebuffer_width; v = bottom + (top - bottom) * (j + offsets[v_offset]) / framebuffer_height; // the direction of the ray is a a linear combination of the camera // basisvectors directions[directions_i] = v3_add3(v3_multiply(forward_vector, w), v3_multiply(right_vector, u), v3_multiply(up_vector, v)); directions_i += 1; } } // Output average pixel color color = v3_create(0.0, 0.0, 0.0); int num_directions = do_antialiasing ? 2 * offsets_n : 1; for (int dir = 0; dir < num_directions; ++dir) { color = v3_add(color, ray_color(0, ray_origin, directions[dir]) ); } color = v3_multiply(color, 1.0 / num_directions); put_pixel(i, j, color.x, color.y, color.z); } sprintf(buf, "Ray-tracing ::: %.0f%% done", 100.0*j/framebuffer_height); glutSetWindowTitle(buf); } // Done! gettimeofday(&t1, NULL); glutSetWindowTitle("Ray-tracing ::: done"); // Output some statistics time_taken = 1.0 * (t1.tv_sec - t0.tv_sec) + (t1.tv_usec - t0.tv_usec) / 1000000.0; fprintf(stderr, " done in %.1f seconds\n", time_taken); fprintf(stderr, "... %d total rays shot, of which %d camera rays and %d shadow rays\n", num_rays_shot, do_antialiasing ? 4*framebuffer_width*framebuffer_height : framebuffer_width*framebuffer_height, num_shadow_rays_shot); fprintf(stderr, "... %d triangles intersection tested (avg %.1f tri/ray)\n", num_triangles_tested, 1.0*num_triangles_tested/num_rays_shot); fprintf(stderr, "... %d bboxes intersection tested (avg %.1f bbox/ray)\n", num_bboxes_tested, 1.0*num_bboxes_tested/num_rays_shot); }
static int ray_intersects_triangle(intersection_point* ip, triangle tri, vec3 ray_origin, vec3 ray_direction) { vec3 edge1, edge2; vec3 tvec, pvec, qvec; double det, inv_det; double t, u, v; // u, v are barycentric coordinates // t is ray parameter num_triangles_tested++; edge1 = v3_subtract(scene_vertices[tri.v[1]], scene_vertices[tri.v[0]]); edge2 = v3_subtract(scene_vertices[tri.v[2]], scene_vertices[tri.v[0]]); pvec = v3_crossprod(ray_direction, edge2); det = v3_dotprod(edge1, pvec); if (det < 1.0e-6) return 0; tvec = v3_subtract(ray_origin, scene_vertices[tri.v[0]]); u = v3_dotprod(tvec, pvec); if (u < 0.0 || u > det) return 0; qvec = v3_crossprod(tvec, edge1); v = v3_dotprod(ray_direction, qvec); if (v < 0.0 || u+v > det) return 0; t = v3_dotprod(edge2, qvec); if (t < 0.0) return 0; inv_det = 1.0 / det; t *= inv_det; u *= inv_det; v *= inv_det; // We have a triangle intersection! // Return the relevant intersection values. // Compute the actual intersection point ip->t = t; ip->p = v3_add(ray_origin, v3_multiply(ray_direction, t)); // Compute an interpolated normal for this intersection point, i.e. // we use the barycentric coordinates as weights for the vertex normals ip->n = v3_normalize(v3_add( v3_add( v3_multiply(tri.vn[0], 1.0-u-v), v3_multiply(tri.vn[1], u) ), v3_multiply(tri.vn[2], v))); ip->i = v3_normalize(v3_negate(ray_direction)); ip->material = tri.material; return 1; }
void ray_trace(void) { vec3 forward_vector, right_vector, up_vector; int i, j; float image_plane_width, image_plane_height; vec3 color; char buf[128]; struct timeval t0, t1; float time_taken; fprintf(stderr, "Ray tracing ..."); gettimeofday(&t0, NULL); num_rays_shot = num_shadow_rays_shot = num_triangles_tested = num_bboxes_tested = 0; // Compute camera coordinate system from camera position // and look-at point up_vector = v3_create(0, 0, 1); forward_vector = v3_normalize(v3_subtract(scene_camera_lookat, scene_camera_position)); right_vector = v3_normalize(v3_crossprod(forward_vector, up_vector)); up_vector = v3_crossprod(right_vector, forward_vector); // Compute size of image plane from the chosen field-of-view // and image aspect ratio. This is the size of the plane at distance // of one unit from the camera position. image_plane_height = 2.0 * tan(0.5*VFOV/180*M_PI); image_plane_width = image_plane_height * (1.0 * framebuffer_width / framebuffer_height); float bottom, left, Us, Vs; left = -image_plane_width * 0.5; bottom = image_plane_height * 0.5; fprintf(stderr, "%d %d\r\n", framebuffer_height, framebuffer_width); // Loop over all pixels in the framebuffer for (j = 0; j < framebuffer_height; j++) { for (i = 0; i < framebuffer_width; i++) { if(!do_antialiasing){ /* With the formula "b + (t−b) * ((j+ 0.5) / ny)" the vector is calculated. * (t-b) equals the negative image_plane_height */ Us = bottom + (-image_plane_height*(j+0.5)/framebuffer_height); /* using the formula: "l + (r−l) * ((i+ 0.5) / nx)" to calculate the vector * (r-l) equals the image_plane_width. */ Vs = left + (image_plane_width*(i+0.5)/framebuffer_width); /* /* Calculate the vector through the pixel (for "Ray through pixel") * Using the formula "Us * U + Vs * v + n * w" */ vec3 UV = v3_add(v3_multiply(up_vector, Us), v3_multiply(right_vector, Vs)); vec3 ray = v3_add(forward_vector, UV); /* Fills the color */ color = ray_color(0, scene_camera_position, ray); } else { float Us1 = bottom + (-image_plane_height*(j+0.25)/framebuffer_height); float Us2 = bottom + (-image_plane_height*(j+0.75)/framebuffer_height); float Vs1 = left + (image_plane_width*(i+0.25)/framebuffer_width); float Vs2 = left + (image_plane_width*(i+0.75)/framebuffer_width); vec3 UV1 = v3_add(v3_multiply(up_vector, Us1), v3_multiply(right_vector, Vs1)); vec3 UV2 = v3_add(v3_multiply(up_vector, Us2), v3_multiply(right_vector, Vs1)); vec3 UV3 = v3_add(v3_multiply(up_vector, Us1), v3_multiply(right_vector, Vs2)); vec3 UV4 = v3_add(v3_multiply(up_vector, Us2), v3_multiply(right_vector, Vs2)); vec3 color1 = ray_color(0, scene_camera_position, v3_add(forward_vector, UV1)); vec3 color2 = ray_color(0, scene_camera_position, v3_add(forward_vector, UV2)); vec3 color3 = ray_color(0, scene_camera_position, v3_add(forward_vector, UV3)); vec3 color4 = ray_color(0, scene_camera_position, v3_add(forward_vector, UV4)); color = v3_multiply( v3_add(v3_add(color1, color2), v3_add(color3, color4)), 0.25 ); } /* Output pixel color */ put_pixel(i, j, color.x, color.y, color.z); } sprintf(buf, "Ray-tracing ::: %.0f%% done", 100.0*j/framebuffer_height); glutSetWindowTitle(buf); } // Done! gettimeofday(&t1, NULL); glutSetWindowTitle("Ray-tracing ::: done"); // Output some statistics time_taken = 1.0 * (t1.tv_sec - t0.tv_sec) + (t1.tv_usec - t0.tv_usec) / 1000000.0; fprintf(stderr, " done in %.1f seconds\n", time_taken); fprintf(stderr, "... %lld total rays shot, of which %d camera rays and " "%lld shadow rays\n", num_rays_shot, do_antialiasing ? 4*framebuffer_width*framebuffer_height : framebuffer_width*framebuffer_height, num_shadow_rays_shot); fprintf(stderr, "... %lld triangles intersection tested " "(avg %.1f tri/ray)\n", num_triangles_tested, 1.0*num_triangles_tested/num_rays_shot); fprintf(stderr, "... %lld bboxes intersection tested (avg %.1f bbox/ray)\n", num_bboxes_tested, 1.0*num_bboxes_tested/num_rays_shot); }