@ -33,62 +33,83 @@ RayCaster::~RayCaster() {
sf : : Color * RayCaster : : CastRays ( sf : : Vector3 < float > camera_direction , sf : : Vector3 < float > camera_position ) {
sf : : Color * RayCaster : : CastRays ( sf : : Vector3 < float > camera_direction , sf : : Vector3 < float > camera_position ) {
// Setup the camera for this cast
// Setup the camera for this cast
this - > camera_direction = camera_direction ;
this - > camera_direction = camera_direction ;
camera_direction_cartesian = Normalize ( SphereToCart ( camera_direction ) ) ;
camera_direction_cartesian = Normalize ( SphereToCart ( camera_direction ) ) ;
this - > camera_position = camera_position ;
this - > camera_position = camera_position ;
// The radian increment each ray is spaced from one another
// The radian increment each ray is spaced from one another
double y_increment_radians = DegreesToRadians ( 60.0 / resolution . y ) ;
double y_increment_radians = DegreesToRadians ( 60.0 / resolution . y ) ;
double x_increment_radians = DegreesToRadians ( 80.0 / resolution . x ) ;
double x_increment_radians = DegreesToRadians ( 80.0 / resolution . x ) ;
// A reference to the positive X axis as our base viewport point
// A reference to the positive X axis as our base viewport point
sf : : Vector3f base_direction ( 1 , 0 , 0 ) ;
sf : : Vector3f base_direction ( 1 , 0 , 0 ) ;
//-resolution.y / 2
int view_plane_distance = 300 ;
// Start the loop at the top left, scan right and work down
sf : : Vector3f * view_plane_vectors = new sf : : Vector3f [ resolution . x * resolution . y ] ;
for ( int y = 0 ; y < resolution . y ; y + + ) {
for ( int x = 0 ; x < resolution . x ; x + + ) {
// The direction the final ray will point.
for ( int y = - resolution . y / 2 ; y < resolution . y / 2 ; y + + ) {
// First take a reference to the base direction to setup the viewport
for ( int x = - resolution . x / 2 ; x < resolution . x / 2 ; x + + ) {
//Vector3<float> ray_direction = new Vector3<float> (base_direction);
// New method to cast rays using the original intended Spherical coords
view_plane_vectors [ ( x + resolution . x / 2 ) + resolution . x * ( y + resolution . y / 2 ) ] = Normalize ( sf : : Vector3f ( view_plane_distance , x , y ) ) ;
// instead of that malarchy with converting them to cartesian from the formula
}
}
//-resolution.y / 2
// Start the loop at the top left, scan right and work down
for ( int y = 0 ; y < resolution . y ; y + + ) {
for ( int x = 0 ; x < resolution . x ; x + + ) {
sf : : Vector3f ray_direction (
camera_direction . x ,
camera_direction . y + ( float ) ( y_increment_radians * y ) ,
camera_direction . z + ( float ) ( x_increment_radians * x )
) ;
sf : : Vector3f ray_cartesian = Normalize ( SphereToCart ( ray_direction ) ) ;
sf : : Vector3f ray = view_plane_vectors [ x + resolution . x * y ] ;
sf : : Vector3f cam_cartesian = Normalize ( SphereToCart ( camera_direction ) ) ;
if ( ( y = = - 99 | | y = = 0 | | y = = 99 ) & & ( /*x == 99 || x == 0 || */ x = = - 99 ) ) {
std : : cout < < " X : " < < x < < " \n " ;
std : : cout < < " Y : " < < y < < " \n " ;
std : : cout < < ray_direction . x < < " : " < < ray_direction . y < < " : " < < ray_direction . z < < " \n " ;
}
// Setup the ray
// Then rotate y axis, up down
Ray r ( map , resolution , sf : : Vector2i ( x , y ) , camera_position , ray_direction ) ;
ray = sf : : Vector3f (
ray . z * sin ( camera_direction . y ) + ray . x * cos ( camera_direction . y ) ,
ray . y ,
ray . z * cos ( camera_direction . y ) - ray . x * sin ( camera_direction . y )
) ;
// Cast it
// // Rotate z axis, left to right.
sf : : Color c = r . Cast ( ) ;
ray = sf : : Vector3f (
if ( c . a = = 0 )
ray . x * cos ( camera_direction . z ) - ray . y * sin ( camera_direction . z ) ,
std : : cout < < " BLACK " ;
ray . x * sin ( camera_direction . z ) + ray . y * cos ( camera_direction . z ) ,
image [ x + resolution . x * y ] = c ;
ray . z
}
) ;
}
// sf::Vector3f ray_direction(
// camera_direction.x,
// camera_direction.y + (float)(y_increment_radians * y),
// camera_direction.z + (float)(x_increment_radians * x)
// );
return image ;
sf : : Vector3f ray_cartesian = Normalize ( SphereToCart ( ray ) ) ;
}
sf : : Vector3f cam_cartesian = Normalize ( SphereToCart ( camera_direction ) ) ;
if ( ( y = = - 99 | | y = = 0 | | y = = 99 ) & & ( /*x == 99 || x == 0 || */ x = = - 99 ) ) {
std : : cout < < " X : " < < x < < " \n " ;
std : : cout < < " Y : " < < y < < " \n " ;
std : : cout < < ray . x < < " : " < < ray . y < < " : " < < ray . z < < " \n " ;
}
// Setup the ray
Ray r ( map , resolution , sf : : Vector2i ( x , y ) , camera_position , ray ) ;
// Cast it
sf : : Color c = r . Cast ( ) ;
if ( c . a = = 0 )
std : : cout < < " BLACK " ;
image [ x + resolution . x * y ] = c ;
}
}
delete view_plane_vectors ;
return image ;
}
void RayCaster : : moveCamera ( sf : : Vector2f v ) {
void RayCaster : : moveCamera ( sf : : Vector2f v ) {
camera_direction . y + = v . x ;
camera_direction . y + = v . x ;