There we go! The raycaster now has a camera that renders without distortion and can be pitched, yawed, and moved on the XYZ axis. I'm not sure now if I want to: Add lighting, improve performance, or start working on porting it to OpenCL
This commit is contained in:
@@ -10,7 +10,7 @@ public:
|
|||||||
list[i] = 0;
|
list[i] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 50; i < 52; i++) {
|
for (int i = 51; i < 52; i++) {
|
||||||
list[55 + dim.x * (55 + dim.z * i)] = 1;
|
list[55 + dim.x * (55 + dim.z * i)] = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ private:
|
|||||||
sf::Vector3<float> camera_position;
|
sf::Vector3<float> camera_position;
|
||||||
|
|
||||||
// The distance in units the view plane is from the iris point
|
// The distance in units the view plane is from the iris point
|
||||||
int view_plane_distance = 300;
|
int view_plane_distance = 200;
|
||||||
|
|
||||||
// Precalculated values for the view plane rays
|
// Precalculated values for the view plane rays
|
||||||
sf::Vector3f *view_plane_vectors;
|
sf::Vector3f *view_plane_vectors;
|
||||||
|
|||||||
15
src/Ray.cpp
15
src/Ray.cpp
@@ -46,9 +46,9 @@ sf::Color Ray::Cast() {
|
|||||||
// 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>(
|
||||||
delta_t.x + origin.x,
|
delta_t.x,
|
||||||
delta_t.y + origin.y,
|
delta_t.y,
|
||||||
delta_t.z + origin.z
|
delta_t.z
|
||||||
);
|
);
|
||||||
|
|
||||||
int dist = 0;
|
int dist = 0;
|
||||||
@@ -99,7 +99,8 @@ sf::Color Ray::Cast() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If we hit a voxel
|
// If we hit a voxel
|
||||||
switch (map->list[voxel.x + dimensions.x * (voxel.y + dimensions.z * voxel.z)]) {
|
int index = voxel.x + dimensions.x * (voxel.y + dimensions.z * voxel.z);
|
||||||
|
switch (map->list[index]) {
|
||||||
case 1:
|
case 1:
|
||||||
return sf::Color::Red;
|
return sf::Color::Red;
|
||||||
case 2:
|
case 2:
|
||||||
@@ -107,11 +108,11 @@ sf::Color Ray::Cast() {
|
|||||||
case 3:
|
case 3:
|
||||||
return sf::Color::Yellow;
|
return sf::Color::Yellow;
|
||||||
case 4:
|
case 4:
|
||||||
return sf::Color(40, 230, 96, 200);
|
return sf::Color(80, 0, 150, 255);
|
||||||
case 5:
|
case 5:
|
||||||
return sf::Color(80, 120, 96, 100);
|
return sf::Color(255, 120, 255, 255);
|
||||||
case 6:
|
case 6:
|
||||||
return sf::Color(150, 80, 220, 200);
|
return sf::Color(150, 80, 220, 255);
|
||||||
}
|
}
|
||||||
|
|
||||||
dist++;
|
dist++;
|
||||||
|
|||||||
@@ -20,23 +20,48 @@ RayCaster::RayCaster(
|
|||||||
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
|
// Calculate the view plane vectors
|
||||||
|
// Because casting to individual pixels causes barrel distortion,
|
||||||
|
// Get the radian increments
|
||||||
|
// Set the camera origin
|
||||||
|
// Rotate the ray by the specified pixel * increment
|
||||||
|
|
||||||
|
double y_increment_radians = DegreesToRadians(50.0 / resolution.y);
|
||||||
|
double x_increment_radians = DegreesToRadians(80.0 / resolution.x);
|
||||||
|
|
||||||
view_plane_vectors = new sf::Vector3f[resolution.x * resolution.y];
|
view_plane_vectors = new sf::Vector3f[resolution.x * resolution.y];
|
||||||
for (int y = -resolution.y / 2 ; y < resolution.y / 2; y++) {
|
for (int y = -resolution.y / 2 ; y < resolution.y / 2; y++) {
|
||||||
for (int x = -resolution.x / 2; x < resolution.x / 2; x++) {
|
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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// The base ray direction to slew from
|
||||||
|
sf::Vector3f ray(1, 0, 0);
|
||||||
|
|
||||||
|
// Y axis, pitch
|
||||||
|
ray = sf::Vector3f(
|
||||||
|
ray.z * sin(y_increment_radians * y) + ray.x * cos(y_increment_radians * y),
|
||||||
|
ray.y,
|
||||||
|
ray.z * cos(y_increment_radians * y) - ray.x * sin(y_increment_radians * y)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Z axis, yaw
|
||||||
|
ray = sf::Vector3f(
|
||||||
|
ray.x * cos(x_increment_radians * x) - ray.y * sin(x_increment_radians * x),
|
||||||
|
ray.x * sin(x_increment_radians * x) + ray.y * cos(x_increment_radians * x),
|
||||||
|
ray.z
|
||||||
|
);
|
||||||
|
|
||||||
|
int index = (x + resolution.x / 2) + resolution.x * (y + resolution.y / 2);
|
||||||
|
view_plane_vectors[index] = Normalize(ray);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
RayCaster::~RayCaster() {
|
RayCaster::~RayCaster() {
|
||||||
delete image;
|
delete image;
|
||||||
delete view_plane_vectors;
|
delete view_plane_vectors;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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
|
||||||
@@ -44,7 +69,6 @@ sf::Color* RayCaster::CastRays(sf::Vector3<float> camera_direction, sf::Vector3<
|
|||||||
camera_direction_cartesian = Normalize(SphereToCart(camera_direction));
|
camera_direction_cartesian = Normalize(SphereToCart(camera_direction));
|
||||||
this->camera_position = camera_position;
|
this->camera_position = camera_position;
|
||||||
|
|
||||||
|
|
||||||
// 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++) {
|
||||||
|
|||||||
@@ -6,8 +6,8 @@
|
|||||||
#include "RayCaster.h"
|
#include "RayCaster.h"
|
||||||
#include <Map.h>
|
#include <Map.h>
|
||||||
|
|
||||||
const int WINDOW_X = 400;
|
const int WINDOW_X = 600;
|
||||||
const int WINDOW_Y = 400;
|
const int WINDOW_Y = 600;
|
||||||
|
|
||||||
|
|
||||||
float elap_time(){
|
float elap_time(){
|
||||||
@@ -52,7 +52,7 @@ int main() {
|
|||||||
window_sprite.setPosition(0, 0);
|
window_sprite.setPosition(0, 0);
|
||||||
|
|
||||||
// State values
|
// State values
|
||||||
sf::Vector3i map_dim(100, 100, 100);
|
sf::Vector3i map_dim(200, 200, 200);
|
||||||
sf::Vector2i view_res(WINDOW_X, WINDOW_Y);
|
sf::Vector2i view_res(WINDOW_X, WINDOW_Y);
|
||||||
sf::Vector3f cam_dir(1.0f, 0.0f, 1.57f);
|
sf::Vector3f cam_dir(1.0f, 0.0f, 1.57f);
|
||||||
sf::Vector3f cam_pos(50, 50, 50);
|
sf::Vector3f cam_pos(50, 50, 50);
|
||||||
|
|||||||
Reference in New Issue
Block a user