diff --git a/build/RayCaster.cpp b/build/RayCaster.cpp index 3835c92..d8642c3 100644 --- a/build/RayCaster.cpp +++ b/build/RayCaster.cpp @@ -3,11 +3,10 @@ #include -RayCaster::RayCaster(Map *map, - sf::Vector3 map_dimensions, - sf::Vector2 viewport_resolution, - sf::Vector3 camera_direction, - sf::Vector3 camera_position) { +RayCaster::RayCaster( + Map *map, + sf::Vector3 map_dimensions, + sf::Vector2 viewport_resolution ) { // Override values //this.map_dimensions = new Vector3 (50, 50, 50); @@ -17,16 +16,12 @@ RayCaster::RayCaster(Map *map, this->map_dimensions = map_dimensions; - map = map; + this->map = map; resolution = viewport_resolution; - image = new sf::Uint8[resolution.x, resolution.y]; + image = new sf::Color[resolution.x, resolution.y]; - this->camera_direction = camera_direction; - camera_direction_cartesian = Normalize(SphereToCart(camera_direction)); - - this->camera_position = camera_position; } @@ -36,7 +31,13 @@ RayCaster::~RayCaster() { -sf::Uint8* RayCaster::Cast() { +sf::Color* RayCaster::CastRays(sf::Vector3 camera_direction, sf::Vector3 camera_position) { + + // Setup the camera for this cast + this->camera_direction = camera_direction; + camera_direction_cartesian = Normalize(SphereToCart(camera_direction)); + this->camera_position = camera_position; + // The radian increment each ray is spaced from one another double y_increment_radians = DegreesToRadians(40.0 / resolution.y); diff --git a/build/RayCaster.h b/build/RayCaster.h index 47c175b..cf03c21 100644 --- a/build/RayCaster.h +++ b/build/RayCaster.h @@ -7,13 +7,11 @@ class RayCaster { public: RayCaster(Map *map, sf::Vector3 map_dimensions, - sf::Vector2 viewport_resolution, - sf::Vector3 camera_direction, - sf::Vector3 camera_position); + sf::Vector2 viewport_resolution); ~RayCaster(); - sf::Uint8* Cast(); + sf::Color* CastRays(sf::Vector3 camera_direction, sf::Vector3 camera_position); private: @@ -24,7 +22,7 @@ private: sf::Vector2 resolution; // The pixel array, maybe do RBGA? Are there even 4 byte data types? - sf::Uint8 *image; + sf::Color *image; // The direction of the camera in POLAR coordinates sf::Vector3 camera_direction; diff --git a/include/Map.h b/include/Map.h index 51e3c32..90902a6 100644 --- a/include/Map.h +++ b/include/Map.h @@ -6,14 +6,15 @@ class Map { public: Map(sf::Vector3i dim) { list = new char[dim.x * dim.y * dim.z]; + dimensions = dim; } ~Map() { } - sf::Vector3 getDimensions(); + sf::Vector3i getDimensions(); char *list; - + sf::Vector3i dimensions; protected: diff --git a/src/Map.cpp b/src/Map.cpp index 8fb9ee7..9a27686 100644 --- a/src/Map.cpp +++ b/src/Map.cpp @@ -3,6 +3,6 @@ #include #include -sf::Vector3 Map::getDimensions() { - throw std::logic_error("The method or operation is not implemented."); +sf::Vector3i Map::getDimensions() { + return dimensions; } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 39e85ac..7416f30 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,6 +3,7 @@ #include #include #include "util.hpp" +#include "../build/RayCaster.h" const int WINDOW_X = 600; const int WINDOW_Y = 800; @@ -22,12 +23,10 @@ float elap_time(){ return elapsed_time.count(); } -int main() { - - - sf::Uint8 c; - std::cout << sizeof(c); +sf::Sprite window_sprite; +sf::Texture window_texture; +int main() { // Initialize the render window sf::RenderWindow window(sf::VideoMode(WINDOW_X, WINDOW_Y), "SFML"); @@ -45,6 +44,25 @@ int main() { fps_counter fps; + // ============================= RAYCASTER SETUP ================================== + + // Setup the sprite and texture + window_texture.create(WINDOW_X, WINDOW_Y); + window_sprite.setPosition(0, 0); + + // State values + sf::Vector3i map_dim(100, 100, 100); + sf::Vector2i view_res(200, 200); + sf::Vector3f cam_dir(1.0f, 0.0f, 1.57f); + sf::Vector3f cam_pos(10, 10, 10); + + Map* map = new Map(map_dim); + RayCaster ray_caster(map, map_dim, view_res); + + + // =============================================================================== + + while (window.isOpen()) { // Poll for events from the user @@ -85,10 +103,21 @@ int main() { // Update(step_size); } - - // Rendering code goes here + window.clear(sf::Color::Black); + // Cast the rays and get the image + sf::Color* pixel_colors = ray_caster.CastRays(cam_dir, cam_pos); + + /*for (int i = 0; i < img_size; i++) { + pixel_colors[i] = sf::Color::Green; + }*/ + + auto out = (sf::Uint8*)pixel_colors; + window_texture.update(out); + window_sprite.setTexture(window_texture); + window.draw(window_sprite); + // Give the frame counter the frame time and draw the average frame time fps.frame(delta_time); fps.draw(&window);