/* listing1.c: This file holds some simple pseudo-code that can be used to calculate the focal-distance and the angle between subsequent rays for various projection-plane dimensions. AUTHOR: Peter Restall for my racasting tutorial */ /* Include header files */ #include /* For the 'tan()' function */ /* Make sure that 'PI' is defined (some implementations don't supply it) */ #ifndef PI /* No 'PI' constant */ #ifdef M_PI /* Check for an 'M_PI' definition */ #define PI M_PI /* Use the one supplied in 'M_PI' */ #else /* No definition of pi found */ #define PI 3.1415926535 #endif #endif /* The trig functions usually take angles in radians, so this macro converts degrees to radians */ #define deg2rad(x) ((x) * (PI / 180.0)) /* Define the two variables to take the computations (they're global because they're widely used) */ float focal_distance, /* The distance to the view plane */ ray_increment; /* Angle between subsequent rays */ /* void calculate_focal_distance(int width, int fov): This function takes 'width' and 'fov' as arguments, which are the projection plane's width and field-of-view, respectively. It then calculates the distance to the projection plane (put into 'focal_distance') and the angle between subsequent rays (which is put into 'ray_increment'). */ void calculate_focal_distance(int width, int fov) { focal_distance = (width / 2) / tan(deg2rad(fov / 2)); ray_increment = fov / width; }