From a01b089d1245f20bdd33ff281ed93506d8ad0b64 Mon Sep 17 00:00:00 2001 From: MitchellHansen Date: Fri, 3 Feb 2017 18:06:29 -0800 Subject: [PATCH] Still wrestling with a good way to hide the packing --- include/Hardware_Caster.h | 2 +- include/LightController.h | 35 +++++++++---- include/RayCaster.h | 4 +- include/Software_Caster.h | 2 +- src/Hardware_Caster.cpp | 34 ++++++------ src/LightController.cpp | 32 ++++++------ src/RayCaster.cpp | 106 +------------------------------------- src/main.cpp | 14 ++--- 8 files changed, 72 insertions(+), 157 deletions(-) diff --git a/include/Hardware_Caster.h b/include/Hardware_Caster.h index 4229b54..55bd42f 100644 --- a/include/Hardware_Caster.h +++ b/include/Hardware_Caster.h @@ -50,7 +50,7 @@ public: // Both will create the view matrix, view res buffer void create_viewport(int width, int height, float v_fov, float h_fov) override; - void assign_lights(std::vector *lights) override; + void assign_lights(std::vector *data) override; void assign_lights(); void assign_map(Old_Map *map) override; void assign_camera(Camera *camera) override; diff --git a/include/LightController.h b/include/LightController.h index 4eebe91..d0a394f 100644 --- a/include/LightController.h +++ b/include/LightController.h @@ -3,24 +3,35 @@ #include #include "util.hpp" #include "Pub_Sub.h" +#include "RayCaster.h" class LightController : public VrEventSubscriber { public: enum DIRECTION { FORWARD, REARWARD, LEFT, RIGHT, UP, DOWN }; - struct Light { - Light(sf::Vector3f position, sf::Vector3f direction_cartesian, sf::Vector4f rgbi) : - position(position), direction_cartesian(direction_cartesian), rgbi(rgbi) { + // Packed data structure for passing raw light data to the caster + struct PackedData { + PackedData(sf::Vector3f position, sf::Vector3f direction_cartesian, sf::Vector4f rgbi) : + position(position), direction_cartesian(direction_cartesian), rgbi(rgbi) { } - Light(); + PackedData(); sf::Vector3f position; sf::Vector3f direction_cartesian; sf::Vector4f rgbi; }; - LightController(); - LightController(sf::Vector3f position, sf::Vector3f direction, sf::Vector4f rgbi); + // Data that enables "camera" style movement. I can't really use inheritance easily because + // of the data packing requirements + struct Light { + Light(); + int packed_index; + float friction_coefficient = 0.1f; + float default_impulse = 1.0f; + sf::Vector3f movement; + }; + + LightController(std::shared_ptr raycaster); ~LightController(); void set_position(sf::Vector3f position); @@ -34,17 +45,21 @@ public: void recieve_event(VrEventPublisher* p, std::unique_ptr event) override; - static void erase_light(); - std::vector* get_lights(); + void erase_light(); + std::vector* get_lights(); private: + // Need to allow N byte light class to be packed into 10 byte packets - int packed_size = sizeof(Light); + int packed_size = sizeof(PackedData); // Index that this light is in the packed data int packed_index; - std::vector packed_data; + std::vector packed_data_array; + std::unordered_map light_map; + std::shared_ptr raycaster; + }; diff --git a/include/RayCaster.h b/include/RayCaster.h index 3bfebd3..a84c6c5 100644 --- a/include/RayCaster.h +++ b/include/RayCaster.h @@ -25,7 +25,7 @@ public: virtual void assign_map(Old_Map *map) = 0; virtual void assign_camera(Camera *camera) = 0; virtual void create_viewport(int width, int height, float v_fov, float h_fov) = 0; - virtual void assign_lights(std::vector *lights) = 0; + virtual void assign_lights(std::vector *data) = 0; virtual void validate() = 0; // draw will abstract the gl sharing and software rendering @@ -40,7 +40,7 @@ protected: Old_Map * map = nullptr; Camera *camera = nullptr; - std::vector *lights; + std::vector *lights; int light_count = 0; sf::Uint8 *viewport_image = nullptr; sf::Vector4f *viewport_matrix = nullptr; diff --git a/include/Software_Caster.h b/include/Software_Caster.h index 46f7683..3b8ca65 100644 --- a/include/Software_Caster.h +++ b/include/Software_Caster.h @@ -15,7 +15,7 @@ public: // Both will create the view matrix, view res buffer void create_viewport(int width, int height, float v_fov, float h_fov) override; - void assign_lights(std::vector *lights) override; + void assign_lights(std::vector *data) override; void assign_map(Old_Map *map) override; void assign_camera(Camera *camera) override; void validate() override; diff --git a/src/Hardware_Caster.cpp b/src/Hardware_Caster.cpp index ed982a1..06cfa04 100644 --- a/src/Hardware_Caster.cpp +++ b/src/Hardware_Caster.cpp @@ -196,28 +196,28 @@ void Hardware_Caster::create_viewport(int width, int height, float v_fov, float } -void Hardware_Caster::assign_lights(std::vector *lights) { - - //this->lights = ; - - std::cout << sizeof(LightController); - std::cout << sizeof(float); - light_count = static_cast(lights->size()); - - //create_buffer("lights", sizeof(float) * 10 * light_count, this->lights->data(), CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR); - - create_buffer("light_count", sizeof(int), &light_count); - -} - -void Hardware_Caster::assign_lights() { +//void Hardware_Caster::assign_lights(std::vector *lights) { +// +// //this->lights = ; +// +// std::cout << sizeof(LightController); +// std::cout << sizeof(float); +// light_count = static_cast(lights->size()); +// +// //create_buffer("lights", sizeof(float) * 10 * light_count, this->lights->data(), CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR); +// +// create_buffer("light_count", sizeof(int), &light_count); +// +//} + +void Hardware_Caster::assign_lights(std::vector *data) { // Get a pointer to the packed light data - this->lights = LightController::get_lights(); + this->lights = data; light_count = static_cast(lights->size()); - size_t packed_size = sizeof(LightController::Light); + size_t packed_size = sizeof(LightController::PackedData); create_buffer("lights", packed_size * light_count, lights->data(), CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR); diff --git a/src/LightController.cpp b/src/LightController.cpp index 8f64c00..d1cb811 100644 --- a/src/LightController.cpp +++ b/src/LightController.cpp @@ -2,14 +2,12 @@ #include "LightController.h" #include "Pub_Sub.h" - -LightController::LightController() { -} - - -LightController::LightController(sf::Vector3f position, sf::Vector3f direction, sf::Vector4f rgbi) { - packed_index = packed_data.size() / packed_size; - packed_data.emplace_back(Light(position, direction, rgbi)); +LightController::LightController(std::shared_ptr raycaster) : + raycaster(raycaster) { + + + + //packed_index = packed_data.size() / packed_size; } LightController::~LightController() { @@ -32,22 +30,22 @@ int LightController::add_relative_impulse(DIRECTION impulse_direction, float spe switch (impulse_direction) { case DIRECTION::FORWARD: - dir = sf::Vector2f(packed_data.at(packed_index).direction_cartesian.y, packed_data.at(packed_index).direction_cartesian.x); + dir = sf::Vector2f(packed_data_array.at(packed_index).direction_cartesian.y, packed_data_array.at(packed_index).direction_cartesian.x); break; case DIRECTION::REARWARD: - dir = sf::Vector2f(packed_data.at(packed_index).direction_cartesian.y, packed_data.at(packed_index).direction_cartesian.x + PI_F); + dir = sf::Vector2f(packed_data_array.at(packed_index).direction_cartesian.y, packed_data_array.at(packed_index).direction_cartesian.x + PI_F); break; case DIRECTION::LEFT: - dir = sf::Vector2f(packed_data.at(packed_index).direction_cartesian.y + PI_F + PI_F / 2, PI_F / 2); + dir = sf::Vector2f(packed_data_array.at(packed_index).direction_cartesian.y + PI_F + PI_F / 2, PI_F / 2); break; case DIRECTION::RIGHT: - dir = sf::Vector2f(packed_data.at(packed_index).direction_cartesian.y + PI_F / 2, PI_F / 2); + dir = sf::Vector2f(packed_data_array.at(packed_index).direction_cartesian.y + PI_F / 2, PI_F / 2); break; case DIRECTION::UP: - dir = sf::Vector2f(packed_data.at(packed_index).direction_cartesian.y, packed_data.at(packed_index).direction_cartesian.x + PI_F / 2); + dir = sf::Vector2f(packed_data_array.at(packed_index).direction_cartesian.y, packed_data_array.at(packed_index).direction_cartesian.x + PI_F / 2); break; case DIRECTION::DOWN: - dir = sf::Vector2f(packed_data.at(packed_index).direction_cartesian.y + PI_F, (packed_data.at(packed_index).direction_cartesian.x * -1) + PI_F / 2); + dir = sf::Vector2f(packed_data_array.at(packed_index).direction_cartesian.y + PI_F, (packed_data_array.at(packed_index).direction_cartesian.x * -1) + PI_F / 2); break; } @@ -97,7 +95,11 @@ void LightController::recieve_event(VrEventPublisher* publisher, std::unique_ptr } -std::vector* LightController::get_lights() { +void LightController::erase_light() { + //packed_data.emplace_back(PackedData(position, direction, rgbi)); +} + +std::vector* LightController::get_lights() { return &packed_data; } diff --git a/src/RayCaster.cpp b/src/RayCaster.cpp index e3a061d..9ea8088 100644 --- a/src/RayCaster.cpp +++ b/src/RayCaster.cpp @@ -4,108 +4,4 @@ RayCaster::RayCaster() { } RayCaster::~RayCaster() { -} - -//void RayCaster::assign_map(Old_Map* map) { -// this->map = map; -//} -// // 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; -// this->map = map; -// -// resolution = viewport_resolution; -// image = new sf::Color[resolution.x * resolution.y]; -// -// -// // 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]; -// for (int y = -resolution.y / 2 ; y < resolution.y / 2; y++) { -// for (int x = -resolution.x / 2; x < resolution.x / 2; x++) { -// -// // 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() { -// delete image; -// delete view_plane_vectors; -//} -// -//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; -// -// // 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 -// -// // 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) -// ); -// -// // 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 -// ); -// -// -// // Setup the ray -// Ray r(map, resolution, sf::Vector2i(x, y), camera_position, ray); -// -// // Cast it and assign its return value -// image[x + resolution.x * y] = r.Cast(); -// } -// } -// -// return image; -//} -// -//void RayCaster::moveCamera(sf::Vector2f v) { -// camera_direction.y += v.x; -// camera_direction.z += v.y; -//} +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index c78521e..802e308 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -92,8 +92,9 @@ int main() { t.create_buffers();*/ // Start up the raycaster - Hardware_Caster *raycaster = new Hardware_Caster(); - + //Hardware_Caster *raycaster = new Hardware_Caster(); + std::shared_ptr raycaster(new Hardware_Caster()); + if (raycaster->init() != 1) { abort(); } @@ -123,12 +124,13 @@ int main() { float w = 60.0; float h = 90.0; - // Light for the currently non functional Bling Phong shader - LightController l( - sf::Vector3f(256.0f, 256.0f, 256.0f), + /*sf::Vector3f(256.0f, 256.0f, 256.0f), sf::Vector3f(-1.0f, -1.0f, -1.5f), sf::Vector4f(1.0f, 1.0f, 1.0f, 1.0f) - ); +*/ + + // Light for the currently non functional Bling Phong shader + LightController l(raycaster); // *links* the lights to the GPU raycaster->assign_lights();