Moved the view plane calc to the constructor, cleaned up old code. Added / removed relevant comments

master
mitchellhansen 8 years ago
parent 1de9c6dd35
commit 2b7dceee1b

@ -10,6 +10,8 @@ public:
sf::Vector2<int> viewport_resolution); sf::Vector2<int> viewport_resolution);
~RayCaster(); ~RayCaster();
void setFOV(float fov);
void setResolution(sf::Vector2<int> resolution);
sf::Color* CastRays(sf::Vector3<float> camera_direction, sf::Vector3<float> camera_position); sf::Color* CastRays(sf::Vector3<float> camera_direction, sf::Vector3<float> camera_position);
void moveCamera(sf::Vector2f v); void moveCamera(sf::Vector2f v);
@ -33,5 +35,12 @@ private:
// The world-space position of the camera // The world-space position of the camera
sf::Vector3<float> camera_position; sf::Vector3<float> camera_position;
// The distance in units the view plane is from the iris point
int view_plane_distance = 300;
// Precalculated values for the view plane rays
sf::Vector3f *view_plane_vectors;
}; };

@ -17,48 +17,32 @@ Ray::Ray(
this->map = map; this->map = map;
origin = camera_position; origin = camera_position;
direction = ray_direction; direction = ray_direction;
dimensions = map->getDimensions(); dimensions = map->getDimensions();
} }
sf::Color Ray::Cast() { sf::Color Ray::Cast() {
// Get the cartesian direction for computing
sf::Vector3<float> cartesian = direction;//SphereToCart(direction);
// Setup the voxel step based on what direction the ray is pointing // Setup the voxel step based on what direction the ray is pointing
sf::Vector3<int> voxel_step(1, 1, 1); sf::Vector3<int> voxel_step(1, 1, 1);
voxel_step.x *= (cartesian.x > 0) - (cartesian.x < 0); voxel_step.x *= (direction.x > 0) - (direction.x < 0);
voxel_step.y *= (cartesian.y > 0) - (cartesian.y < 0); voxel_step.y *= (direction.y > 0) - (direction.y < 0);
voxel_step.z *= (cartesian.z > 0) - (cartesian.z < 0); voxel_step.z *= (direction.z > 0) - (direction.z < 0);
// Setup the voxel coords from the camera origin // Setup the voxel coords from the camera origin
voxel = sf::Vector3<int>( voxel = sf::Vector3<int>(
(int) origin.x, floorf(origin.x),
(int) origin.y, floorf(origin.y),
(int) origin.z floorf(origin.z)
); );
// Delta T is the units a ray must travel along an axis in order to // Delta T is the units a ray must travel along an axis in order to
// traverse an integer split // traverse an integer split
delta_t = sf::Vector3<float>( delta_t = sf::Vector3<float>(
fabsf((float) (1.0 / cartesian.x)), fabsf(1.0f / direction.x),
fabsf((float) (1.0 / cartesian.y)), fabsf(1.0f / direction.y),
fabsf((float) (1.0 / cartesian.z)) fabsf(1.0f / direction.z)
); );
// So the way I need to do the camera is this.
// 1.) Setup the viewplane and then store the values
// - Camera origin
// - Resolution of the view plane X, Y
// - Focal length to determine FOV
//
// 2.) For each draw. Get a copy of the view plane
// 3.) Rotate around the X axis first, left and right
// 4.) Then rotate alond the Y axis, up and down.
// 5.) Make sure to limit the camera Y Rotation to 180 and -180 degrees
// - Rays will still go pas 180 for the amount of FOV the camera has!
// Intersection T is the collection of the next intersection points // Intersection T is the collection of the next intersection points
// for all 3 axis XYZ. // for all 3 axis XYZ.
intersection_t = sf::Vector3<float>( intersection_t = sf::Vector3<float>(
@ -67,13 +51,9 @@ sf::Color Ray::Cast() {
delta_t.z + origin.z delta_t.z + origin.z
); );
if (pixel.y == 200){
int i = 0;
i++;
}
int dist = 0; int dist = 0;
// Andrew Woo's raycasting algo
do { do {
if ((intersection_t.x) < (intersection_t.y)) { if ((intersection_t.x) < (intersection_t.y)) {
if ((intersection_t.x) < (intersection_t.z)) { if ((intersection_t.x) < (intersection_t.z)) {
@ -139,6 +119,7 @@ sf::Color Ray::Cast() {
} while (dist < 200); } while (dist < 200);
// Ray timeout color
return sf::Color::Cyan; return sf::Color::Cyan;
} }

@ -14,19 +14,25 @@ RayCaster::RayCaster(
//this.camera_direction = new Vector3<float> (1f, 0f, .8f); //this.camera_direction = new Vector3<float> (1f, 0f, .8f);
//this.camera_position = new Vector3<float> (1, 10, 10); //this.camera_position = new Vector3<float> (1, 10, 10);
this->map_dimensions = map_dimensions; this->map_dimensions = map_dimensions;
this->map = map; this->map = map;
resolution = viewport_resolution; resolution = viewport_resolution;
image = new sf::Color[resolution.x * resolution.y]; image = new sf::Color[resolution.x * resolution.y];
// Calculate the view plane vectors
view_plane_vectors = new sf::Vector3f[resolution.x * resolution.y];
for (int y = -resolution.y / 2 ; y < resolution.y / 2; y++) {
for (int x = -resolution.x / 2; x < resolution.x / 2; x++) {
view_plane_vectors[(x + resolution.x / 2) + resolution.x * (y + resolution.y / 2)] = Normalize(sf::Vector3f(view_plane_distance, x, y));
}
}
} }
RayCaster::~RayCaster() { RayCaster::~RayCaster() {
delete image;
delete view_plane_vectors;
} }
@ -39,75 +45,38 @@ sf::Color* RayCaster::CastRays(sf::Vector3<float> camera_direction, sf::Vector3<
this->camera_position = camera_position; this->camera_position = camera_position;
// The radian increment each ray is spaced from one another
double y_increment_radians = DegreesToRadians(60.0 / resolution.y);
double x_increment_radians = DegreesToRadians(80.0 / resolution.x);
// A reference to the positive X axis as our base viewport point
sf::Vector3f base_direction(1, 0, 0);
int view_plane_distance = 300;
sf::Vector3f *view_plane_vectors = new sf::Vector3f[resolution.x * resolution.y];
for (int y = -resolution.y / 2 ; y < resolution.y / 2; y++) {
for (int x = -resolution.x / 2; x < resolution.x / 2; x++) {
view_plane_vectors[(x + resolution.x / 2) + resolution.x * (y + resolution.y / 2)] = Normalize(sf::Vector3f(view_plane_distance, x, y));
}
}
//-resolution.y / 2
// Start the loop at the top left, scan right and work down // Start the loop at the top left, scan right and work down
for (int y = 0; y < resolution.y; y++) { for (int y = 0; y < resolution.y; y++) {
for (int x = 0; x < resolution.x; x++) { for (int x = 0; x < resolution.x; x++) {
// Get the ray at the base direction
sf::Vector3f ray = view_plane_vectors[x + resolution.x * y]; sf::Vector3f ray = view_plane_vectors[x + resolution.x * y];
// Rotate it to the correct pitch and yaw
// Then rotate y axis, up down // Y axis, pitch
ray = sf::Vector3f( ray = sf::Vector3f(
ray.z * sin(camera_direction.y) + ray.x * cos(camera_direction.y), ray.z * sin(camera_direction.y) + ray.x * cos(camera_direction.y),
ray.y, ray.y,
ray.z * cos(camera_direction.y) - ray.x * sin(camera_direction.y) ray.z * cos(camera_direction.y) - ray.x * sin(camera_direction.y)
); );
// // Rotate z axis, left to right. // Z axis, yaw
ray = sf::Vector3f( ray = sf::Vector3f(
ray.x * cos(camera_direction.z) - ray.y * sin(camera_direction.z), ray.x * cos(camera_direction.z) - ray.y * sin(camera_direction.z),
ray.x * sin(camera_direction.z) + ray.y * cos(camera_direction.z), ray.x * sin(camera_direction.z) + ray.y * cos(camera_direction.z),
ray.z 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)
// );
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 // Setup the ray
Ray r(map, resolution, sf::Vector2i(x, y), camera_position, ray); Ray r(map, resolution, sf::Vector2i(x, y), camera_position, ray);
// Cast it // Cast it and assign its return value
sf::Color c = r.Cast(); image[x + resolution.x*y] = r.Cast();
if (c.a == 0)
std::cout << "BLACK";
image[x + resolution.x*y] = c;
} }
} }
delete view_plane_vectors;
return image; return image;
} }

@ -157,11 +157,9 @@ int main() {
// Cast the rays and get the image // Cast the rays and get the image
sf::Color* pixel_colors = ray_caster.CastRays(cam_dir, cam_pos); sf::Color* pixel_colors = ray_caster.CastRays(cam_dir, cam_pos);
/*for (int i = 0; i < img_size; i++) { // Cast it to an array of Uint8's
pixel_colors[i] = sf::Color::Green;
}*/
auto out = (sf::Uint8*)pixel_colors; auto out = (sf::Uint8*)pixel_colors;
window_texture.update(out); window_texture.update(out);
window_sprite.setTexture(window_texture); window_sprite.setTexture(window_texture);
window.draw(window_sprite); window.draw(window_sprite);

Loading…
Cancel
Save