From 37ef472f61d1ad3acf934ce037badaa2bb38f8ff Mon Sep 17 00:00:00 2001 From: MitchellHansen Date: Sun, 31 Jul 2016 00:01:50 -0700 Subject: [PATCH] More work done on the ray --- build/RayCaster.cpp | 84 +++++++++++++++++++++++++++++++++++++++++++++ build/RayCaster.h | 39 +++++++++++++++++++++ include/Ray.h | 50 +++++++++++++-------------- include/util.hpp | 25 ++++++++++++++ src/main.cpp | 6 ++++ 5 files changed, 179 insertions(+), 25 deletions(-) create mode 100644 build/RayCaster.cpp create mode 100644 build/RayCaster.h diff --git a/build/RayCaster.cpp b/build/RayCaster.cpp new file mode 100644 index 0000000..3835c92 --- /dev/null +++ b/build/RayCaster.cpp @@ -0,0 +1,84 @@ +#include "RayCaster.h" +#include +#include + + +RayCaster::RayCaster(Map *map, + sf::Vector3 map_dimensions, + sf::Vector2 viewport_resolution, + sf::Vector3 camera_direction, + sf::Vector3 camera_position) { + + // Override values + //this.map_dimensions = new Vector3 (50, 50, 50); + //this.resolution = new Vector2 (200, 200); + //this.camera_direction = new Vector3 (1f, 0f, .8f); + //this.camera_position = new Vector3 (1, 10, 10); + + + this->map_dimensions = map_dimensions; + map = map; + + resolution = viewport_resolution; + image = new sf::Uint8[resolution.x, resolution.y]; + + + this->camera_direction = camera_direction; + camera_direction_cartesian = Normalize(SphereToCart(camera_direction)); + + this->camera_position = camera_position; + +} + + +RayCaster::~RayCaster() { +} + + + +sf::Uint8* RayCaster::Cast() { + + // The radian increment each ray is spaced from one another + double y_increment_radians = DegreesToRadians(40.0 / resolution.y); + double x_increment_radians = DegreesToRadians(50.0 / resolution.x); + + + // A reference to the positive X axis as our base viewport point + sf::Vector3f base_direction(1, 0, 0); + + // Start the loop at the bottom left, scan right and work up + for (int x = 0; x < resolution.x / 2; x++) { + for (int y = -resolution.y / 2; y < resolution.y / 2; y++) { + + // The direction the final ray will point. + // First take a reference to the base direction to setup the viewport + //Vector3 ray_direction = new Vector3 (base_direction); + + // New method to cast rays using the original intended Spherical coords + // instead of that malarchy with converting them to cartesian from the formula + + + sf::Vector3f ray_direction( + camera_direction.x + 1.0f, + 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 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 + Ray r(map, resolution, sf::Vector2i(x, y), camera_position, ray_direction); + + // Cast it + ///image[x + 100, y + 100] = r.Cast(); + } + } + return image; + } diff --git a/build/RayCaster.h b/build/RayCaster.h new file mode 100644 index 0000000..47c175b --- /dev/null +++ b/build/RayCaster.h @@ -0,0 +1,39 @@ +#pragma once +#include +#include +#include + +class RayCaster { +public: + RayCaster(Map *map, + sf::Vector3 map_dimensions, + sf::Vector2 viewport_resolution, + sf::Vector3 camera_direction, + sf::Vector3 camera_position); + ~RayCaster(); + + + sf::Uint8* Cast(); + +private: + + sf::Vector3 map_dimensions; + Map *map; + + // The XY resolution of the viewport + sf::Vector2 resolution; + + // The pixel array, maybe do RBGA? Are there even 4 byte data types? + sf::Uint8 *image; + + // The direction of the camera in POLAR coordinates + sf::Vector3 camera_direction; + + + // Convert the polar coordinates to CARTESIAN + sf::Vector3 camera_direction_cartesian; + + // The world-space position of the camera + sf::Vector3 camera_position; +}; + diff --git a/include/Ray.h b/include/Ray.h index c22485f..03eb38d 100644 --- a/include/Ray.h +++ b/include/Ray.h @@ -5,39 +5,39 @@ class Ray { - private: + private: - // The Tail of the vector - sf::Vector3 origin; + // The Tail of the vector + sf::Vector3 origin; - // Direction / Length of the vector - sf::Vector3 direction; + // Direction / Length of the vector + sf::Vector3 direction; - // The incrementing points at which T intersects int(X, Y, Z) points - sf::Vector3 intersection_t; + // The incrementing points at which T intersects int(X, Y, Z) points + sf::Vector3 intersection_t; - // The speed at which the ray climbs. - // Take the slope of the line (1 / cartesian.x/y/z) = delta_t.x/y/z - sf::Vector3 delta_t; + // The speed at which the ray climbs. + // Take the slope of the line (1 / cartesian.x/y/z) = delta_t.x/y/z + sf::Vector3 delta_t; - // The 3d voxel position the ray is currently at - sf::Vector3 voxel; + // The 3d voxel position the ray is currently at + sf::Vector3 voxel; - // Reference to the voxel map - Map *map; + // Reference to the voxel map + Map *map; - // The dimensions of the voxel map - sf::Vector3 dimensions; + // The dimensions of the voxel map + sf::Vector3 dimensions; - public: + public: - Ray( - Map *m, - sf::Vector2 resolution, - sf::Vector2 pixel, - sf::Vector3 camera_position, - sf::Vector3 ray_direction - ); + Ray( + Map *m, + sf::Vector2 resolution, + sf::Vector2 pixel, + sf::Vector3 camera_position, + sf::Vector3 ray_direction + ); - sf::Color Cast(); + sf::Color Cast(); }; diff --git a/include/util.hpp b/include/util.hpp index 505c94e..2dc9133 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -1,5 +1,9 @@ #pragma once #include +#include + +const double PI = 3.141592653589793238463; +const float PI_F = 3.14159265358979f; struct fps_counter { public: @@ -49,3 +53,24 @@ inline sf::Vector3f CartToSphere(sf::Vector3f in) { ); return r; }; + + +inline sf::Vector3f Normalize(sf::Vector3f in) { + + float multiplier = sqrt(in.x * in.x + in.y * in.y + in.z * in.z); + auto r = sf::Vector3f( + in.x / multiplier, + in.y / multiplier, + in.z / multiplier + ); + return r; + +} + +inline float DegreesToRadians(float in) { + return in * PI / 180.0f; +} + +inline float RadiansToDegrees(float in) { + return in * 180.0f / PI; +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index f6a358d..39e85ac 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -23,6 +23,12 @@ float elap_time(){ } int main() { + + + sf::Uint8 c; + std::cout << sizeof(c); + + // Initialize the render window sf::RenderWindow window(sf::VideoMode(WINDOW_X, WINDOW_Y), "SFML");