Commit f5c7e003 authored by Taddeüs Kroes's avatar Taddeüs Kroes

- Added graphics assignment 8.

 - Added last modifications to graphics assignment 7.
parent 20795120
...@@ -217,8 +217,9 @@ intersect_node(bvh_node *node, intersection_point *ip, ...@@ -217,8 +217,9 @@ intersect_node(bvh_node *node, intersection_point *ip,
} }
else else
{ {
// Leaf node, find shortest triangle and check if it is closer than the // Leaf node, find the shortest triangle in the leaf and check if it is
// current closest intersection // closer to the origin than the current closest intersection. If so,
// make it the new closest intersection.
triangle *triangles = leaf_node_triangles(node); triangle *triangles = leaf_node_triangles(node);
intersection_point ip2; intersection_point ip2;
...@@ -253,13 +254,6 @@ find_first_intersected_bvh_triangle(intersection_point* ip, ...@@ -253,13 +254,6 @@ find_first_intersected_bvh_triangle(intersection_point* ip,
ip->t = C_INFINITY; ip->t = C_INFINITY;
// If the root has no intersection, none of the BVH nodes does // 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, if( !bbox_intersect(&t0, &t1, bvh_root->bbox, ray_origin,
ray_direction, 0.0, C_INFINITY) ) ray_direction, 0.0, C_INFINITY) )
{ {
......
/* Computer Graphics, Assignment, Ray-tracing 2 /* Computer Graphics, Assignment, Ray-tracing 2
* *
* Student name .... * Student name .... Sander van Veen & Taddeus Kroes
* Student email ... * Student email ... sandervv@gmail.com & taddeuskroes@hotmail.com
* Collegekaart .... * Collegekaart .... 6167969 & 6054129
* Date ............ * Date ............ 07.11.2010
* Comments ........
*
*
* (always fill in these fields before submitting!!)
*/ */
#include <sys/time.h> #include <sys/time.h>
...@@ -160,7 +156,7 @@ ray_trace(void) ...@@ -160,7 +156,7 @@ ray_trace(void)
vec3 forward_vector, right_vector, up_vector; vec3 forward_vector, right_vector, up_vector;
int i, j, k, l; int i, j, k, l;
vec3 right_step, down_step; 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 image_plane_topleft, image_plane_point;
vec3 ray_direction; vec3 ray_direction;
vec3 color; vec3 color;
...@@ -203,26 +199,34 @@ ray_trace(void) ...@@ -203,26 +199,34 @@ ray_trace(void)
{ {
for (i = 0; i < framebuffer_width; i++) 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( k = 0; k < 2; k++ )
{ {
for( l = 0; l < 2; l++ ) for( l = 0; l < 2; l++ )
{ {
// Compute corresponding point on image plane // Compute corresponding point corner 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_add(image_plane_point, v3_multiply(down_step, j+diff[l])); 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 // Compute direction for shooting the ray
ray_direction = v3_subtract(image_plane_point, scene_camera_position); 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)); 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); color = v3_multiply(color, 0.25);
} }
else 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
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 ....
* Student email ...
* Collegekaart ....
* Date ............
* Comments ........
*
*
* (always fill in these fields before submitting!!)
*/
#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 @@ ...@@ -10,7 +10,6 @@
\usepackage{float} \usepackage{float}
\usepackage[dutch]{babel} \usepackage[dutch]{babel}
\usepackage{listings}
\lstset{ \lstset{
language=C, 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