Assignment 5 Solution

$35.00 $30.80


For this project, you will write a program in C that implements a simple ray casting renderer and produces a 2D image of spheres. A file of sphere information will be provided. Your code should produce a PPM image of 1000×1000 pixels.


Your code must be modular and use functions, structures, and typedefs. We will discuss the algorithm and PPM files in class. Your solution should consist of four files with data types and functions listed below:




RAY_T with origin and  direction fields

COLOR_T with R, G, B fields


SPHERE_T with  center and  radius

OBJ_T with  sphere, color, and  next fields



void read_objs (OBJ_T **list)


reads spheres from stdin; creates linked list using dynamic memory


int intersect_sphere (RAY_T ray, SPHERE_T sphere, double *t)


computes intersection of ray and sphere; returns 1 if intersection, 0 otherwise; t is set to distance from camera to sphere intersection


COLOR_T cast (RAY_T ray, OBJ_T *list)


iterates through linked list; finds closest intersection using intersect_sphere; returns color of closest sphere




calls read_objs; initializes image file; loops through pixels: sets ray, calls cast, prints pixel; frees linked list




VEC_T with x, y, and z fields




VEC_T normalize (VEC_T v)


computes and returns normalized vector v


double dot (VEC_T v1, VEC_T v2)


computes and returns dot product of v1 and  v2


The program can be compiled with the command: gcc rc.c vec.c –lm –o rc


The program can be executed with the command:  ./rc < spheres.txt > img.ppm