|
|
|
@ -14,19 +14,25 @@ RayCaster::RayCaster(
|
|
|
|
|
//this.camera_direction = new Vector3<float> (1f, 0f, .8f);
|
|
|
|
|
//this.camera_position = new Vector3<float> (1, 10, 10);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this->map_dimensions = map_dimensions;
|
|
|
|
|
this->map = map;
|
|
|
|
|
|
|
|
|
|
resolution = viewport_resolution;
|
|
|
|
|
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() {
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
for (int y = 0; y < resolution.y; y++) {
|
|
|
|
|
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];
|
|
|
|
|
|
|
|
|
|
// Rotate it to the correct pitch and yaw
|
|
|
|
|
|
|
|
|
|
// Then rotate y axis, up down
|
|
|
|
|
// Y axis, pitch
|
|
|
|
|
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)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// // Rotate z axis, left to right.
|
|
|
|
|
// Z axis, yaw
|
|
|
|
|
ray = sf::Vector3f(
|
|
|
|
|
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.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
|
|
|
|
|
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;
|
|
|
|
|
// Cast it and assign its return value
|
|
|
|
|
image[x + resolution.x*y] = r.Cast();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delete view_plane_vectors;
|
|
|
|
|
|
|
|
|
|
return image;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|