Commit 1b4d9e25 authored by Sander Mathijs van Veen's avatar Sander Mathijs van Veen

Merge branch 'master' of ssh://vo20.nl/git/uva

parents aa4fd943 ec5c92d4
......@@ -217,8 +217,9 @@ intersect_node(bvh_node *node, intersection_point *ip,
}
else
{
// Leaf node, find shortest triangle and check if it is closer than the
// current closest intersection
// Leaf node, find the shortest triangle in the leaf and check if it is
// closer to the origin than the current closest intersection. If so,
// make it the new closest intersection.
triangle *triangles = leaf_node_triangles(node);
intersection_point ip2;
......@@ -253,13 +254,6 @@ find_first_intersected_bvh_triangle(intersection_point* ip,
ip->t = C_INFINITY;
// If the root has no intersection, none of the BVH nodes does
//
// Note that this step if mkes the program less efficient when the root's
// bounding box has a relatively large surface in the camera screen (has
// lot of ray intersections). This is because every ray intersecting a
// triangle is now tested one time more and every non-intersection ray is
// tested one time less. So, when raytracing large objects, omitting this
// step would be advisable.
if( !bbox_intersect(&t0, &t1, bvh_root->bbox, ray_origin,
ray_direction, 0.0, C_INFINITY) )
{
......
/* Computer Graphics, Assignment, Ray-tracing 2
*
* Student name ....
* Student email ...
* Collegekaart ....
* Date ............
* Comments ........
*
*
* (always fill in these fields before submitting!!)
* Student name .... Sander van Veen & Taddeus Kroes
* Student email ... sandervv@gmail.com & taddeuskroes@hotmail.com
* Collegekaart .... 6167969 & 6054129
* Date ............ 07.11.2010
*/
#include <sys/time.h>
......@@ -160,7 +156,7 @@ ray_trace(void)
vec3 forward_vector, right_vector, up_vector;
int i, j, k, l;
vec3 right_step, down_step;
float image_plane_width, image_plane_height, diff[2] = {0.25, 0.75};
float image_plane_width, image_plane_height, pixel_diff[2] = {0.25, 0.75};
vec3 image_plane_topleft, image_plane_point;
vec3 ray_direction;
vec3 color;
......@@ -203,26 +199,34 @@ ray_trace(void)
{
for (i = 0; i < framebuffer_width; i++)
{
if( do_antialiasing)
if( do_antialiasing )
{
color.x = color.y = color.z = 0;
// Anti-aliasing is on, shoot 4 rays through each point and use the average
color.x = color.y = color.z = 0.0;
for( k = 0; k < 2; k++ )
{
for( l = 0; l < 2; l++ )
{
// Compute corresponding point on image plane
image_plane_point = v3_add(image_plane_topleft, v3_multiply(right_step, i+diff[k]));
image_plane_point = v3_add(image_plane_point, v3_multiply(down_step, j+diff[l]));
// Compute corresponding point corner on image plane
image_plane_point = v3_add(
image_plane_topleft,
v3_multiply(right_step, i+pixel_diff[k])
);
image_plane_point = v3_add(
image_plane_point,
v3_multiply(down_step, j+pixel_diff[l])
);
// Compute direction for shooting the ray
ray_direction = v3_subtract(image_plane_point, scene_camera_position);
// Determine ray color
// Add ray color for this corner to the current color
color = v3_add(color, ray_color(0, scene_camera_position, ray_direction));
}
}
// Divide by 4 to get the average of all corners, this is the point color
color = v3_multiply(color, 0.25);
}
else
......
CC=gcc
WARNING_FLAGS=-Wall -Wextra -Werror-implicit-function-declaration -Wshadow -Wstrict-prototypes -pedantic-errors
CFLAGS=-g -O2 -std=c99 $(WARNING_FLAGS)
LDFLAGS=-g -lGL -lglut -lGLU
.c.o:
$(CC) -c $(CFLAGS) $<
all: main
main: main.o volume.o
$(CC) $(LDFLAGS) -o main main.o volume.o
clean:
rm -f *.o
rm -f main
volume.o : volume.h
main.o : volume.h main.c
volume.o : volume.h volume.c
We did the following measurements with out program.
+----------------+-----+-----------------------+---------------------+-------------+
| dataset | iso | framerate unoptimized | framerate optimized | improvement |
| | | (points/cubes) | (points/cubes) | ratio |
+----------------+-----+-----------------------+---------------------+-------------+
| frog.small.vtk | 78 | 80 / 19 | 670 / 235 | 8.4 / 12.4 |
+----------------+-----+-----------------------+---------------------+-------------+
| knee.vtk | 60 | 58 / 2 | 362 / 29 | 6.2 / 14.5 |
+----------------+-----+-----------------------+---------------------+-------------+
| mummy.128.vtk | 50 | 40 / 5 | 515 / 86 | 12.9 / 17.2 |
+----------------+-----+-----------------------+---------------------+-------------+
We can see that the improvement in performance is very large, especially for
the cubes. This makes sense, because when not using arrays, all of the cubes
have to be translated and transformed whereas the points only have to be
calculated. Still, the optimization certainly also pays off for the points.
This diff is collapsed.
#ifndef V3MATH_H
#define V3MATH_H
#include <math.h>
typedef struct
{
float x, y, z;
}
vec3;
// Create a new 3-vector of floats
static inline vec3
v3_create(float x, float y, float z)
{
vec3 res;
res.x = x;
res.y = y;
res.z = z;
return res;
}
// Return -a
static inline vec3
v3_negate(vec3 a)
{
vec3 res;
res.x = - a.x;
res.y = - a.y;
res.z = - a.z;
return res;
}
// Return a + b
static inline vec3
v3_add(vec3 a,vec3 b)
{
vec3 res;
res.x = a.x+b.x;
res.y = a.y+b.y;
res.z = a.z+b.z;
return res;
}
// Return a - b
static inline vec3
v3_subtract(vec3 a, vec3 b)
{
vec3 res;
res.x = a.x-b.x;
res.y = a.y-b.y;
res.z = a.z-b.z;
return res;
}
// Return a / |a|
static inline vec3
v3_normalize(vec3 a)
{
vec3 res;
double l = sqrt(a.x*a.x + a.y*a.y + a.z*a.z);
res = a;
res.x /= l;
res.y /= l;
res.z /= l;
return res;
}
// Return a ^ b
static inline vec3
v3_crossprod(vec3 a, vec3 b)
{
vec3 res;
res.x = a.y*b.z - a.z*b.y;
res.y = a.z*b.x - a.x*b.z;
res.z = a.x*b.y - a.y*b.x;
return res;
}
// Return a * b
static inline float
v3_dotprod(vec3 a, vec3 b)
{
return a.x*b.x + a.y*b.y + a.z*b.z;
}
// Return a*s
static inline vec3
v3_multiply(vec3 a, float s)
{
vec3 res;
res.x = a.x*s;
res.y = a.y*s;
res.z = a.z*s;
return res;
}
// Return |a|
static inline float
v3_length(vec3 a)
{
return sqrt(a.x*a.x + a.y*a.y + a.z*a.z);
}
// Return the i-th component of a, i.e. for i=0
// this returns a.x
static inline float
v3_component(vec3 a, int i)
{
if (i == 0)
return a.x;
else if (i == 1)
return a.y;
else
return a.z;
}
// Set the i-th component of a to v
static inline vec3
v3_set_component(vec3 a, int i, float v)
{
vec3 res = a;
if (i == 0)
res.x = v;
else if (i == 1)
res.y = v;
else
res.z = v;
return res;
}
#endif
/* Computer Graphics, Assignment, Volume rendering with cubes/points
*
* Description ..... Creates OpenGL window and draws the scene.
* Date ............ 29.10.2008
* Created by ...... Paul Melis
*
* Student name .... Sander van Veen & Taddeus Kroes
* Student email ... sandervv@gmail.com & taddeuskroes@hotmail.com
* Collegekaart .... 6167969 & 6054129
* Date ............ 07.11.2010
*/
#include <stdio.h>
#include <stdlib.h>
#include "volume.h"
/* The voxels of the volume dataset, stored as a one-dimensional array */
unsigned char *volume;
/* The dimensions of the volume dataset */
int nx, ny, nz;
/* The size of a voxel */
float sizex, sizey, sizez;
/* Utility function to convert the index of a voxel
into an index in the volume array above */
int
voxel2idx(int i, int j, int k)
{
return (k*ny + j)*nx + i;
}
/* Utility function to read a volume dataset from a VTK file.
This will store the data in the "volume" array and update the dimension
and size values. */
void
read_volume(const char *fname)
{
FILE *f;
char s[256];
unsigned int nvoxels;
printf("Reading %s\n", fname);
f = fopen(fname, "rb");
if (!f)
{
fprintf(stderr, "read_volume(): Could not open file '%s' for reading!\n", fname);
exit(-1);
}
// header line
fgets(s, 255, f);
// comment line
fgets(s, 255, f);
// BINARY
fgets(s, 255, f);
// DATASET STRUCTURED_POINTS
fgets(s, 255, f);
// DIMENSIONS %d %d %d
fscanf(f, "%s %d %d %d\n", s, &nx, &ny, &nz);
printf("%d x %d x %d voxels\n", nx, ny, nz);
// ASPECT_RATIO/SPACING %f %f %f
fscanf(f, "%s %f %f %f\n", s, &sizex, &sizey, &sizez);
printf("voxel sizes: %.3f, %.3f, %.3f\n", sizex, sizey, sizez);
// ORIGIN ...
fgets(s, 255, f);
// POINT_DATA ...
fgets(s, 255, f);
// SCALARS ...
fgets(s, 255, f);
// LOOKUP_TABLE ...
fgets(s, 255, f);
// allocate memory to hold the volume data and read it from file
nvoxels = nx * ny * nz;
volume = (unsigned char*)malloc(nvoxels);
if (fread(volume, 1, nvoxels, f) < nvoxels)
{
printf("WARNING: not all data could be read!\n");
}
fclose(f);
}
#ifndef VOLUME_H
#define VOLUME_H
/* The data points in the volume dataset, stored as a one-dimensional array */
extern unsigned char *volume;
/* The dimensions of the volume dataset in number of voxels in each
dimension*/
extern int nx, ny, nz;
/* The size of a voxel for each dimension */
extern float sizex, sizey, sizez;
/* Utility function to convert the index of a datapoint
into an index in the volume array above */
int voxel2idx(int i, int j, int k);
/* Utility function to read a volume dataset from a file.
This will store the data in the "volume" array and update the dimension
and size values. */
void read_volume(const char *fname);
#endif
......@@ -10,7 +10,6 @@
\usepackage{float}
\usepackage[dutch]{babel}
\usepackage{listings}
\lstset{
language=C,
......
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