From bfb6d922a3bd9b42b337b2cdd3f0732d58fcb4b0 Mon Sep 17 00:00:00 2001 From: MitchellHansen Date: Wed, 8 Feb 2017 23:17:10 -0800 Subject: [PATCH] Working on lights, I'm conceeding and just using a fixed array. I think it will be faster in the long run, as I won't have to rebind the lights when I add/remove one. Also wrestling with really lame compiler erros with these templated pointers --- include/LightController.h | 69 ++++++++++++++--------------- include/LightHandle.h | 27 ++++++++--- include/raycaster/Hardware_Caster.h | 4 +- include/raycaster/RayCaster.h | 5 ++- include/raycaster/Software_Caster.h | 7 ++- src/LightController.cpp | 63 +++++++++----------------- src/LightHandle.cpp | 58 ++++++++++++++++++++---- src/NetworkInput.cpp | 34 +------------- src/Old_Map.cpp | 4 +- src/main.cpp | 8 ++-- src/raycaster/Hardware_Caster.cpp | 9 ++-- src/raycaster/Software_Caster.cpp | 4 +- 12 files changed, 151 insertions(+), 141 deletions(-) diff --git a/include/LightController.h b/include/LightController.h index 5d96b44..61e8942 100644 --- a/include/LightController.h +++ b/include/LightController.h @@ -4,56 +4,55 @@ #include "util.hpp" #include "Pub_Sub.h" #include "raycaster/RayCaster.h" -#include "LightHandle.h" +#include +#include "raycaster/Hardware_Caster.h" +struct LightPrototype { + + sf::Vector3f position; + sf::Vector3f direction_cartesian; + sf::Vector4f rgbi; -class LightController : public VrEventSubscriber { -public: + sf::Vector3f movement; + float impulse = 1.0f; + float friction = 1.0f; - enum DIRECTION { FORWARD, REARWARD, LEFT, RIGHT, UP, DOWN }; - - // 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) { - } - PackedData() {}; - sf::Vector3f position; - sf::Vector3f direction_cartesian; - sf::Vector4f rgbi; - }; - -// LightController(std::shared_ptr raycaster); - LightController(); - ~LightController(); +}; - //void create_light(LightController::PackedData light_data, std::string light_name); -// LightHandle get_light_handle(std::string light_name); +// 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) { + } + PackedData() {}; + sf::Vector3f position; + sf::Vector3f direction_cartesian; + sf::Vector4f rgbi; +}; - void set_position(sf::Vector3f position); +class LightHandle; +class Hardware_Caster; +class LightController : public VrEventSubscriber { +public: - int update(double delta_time); + LightController(std::shared_ptr raycaster); + ~LightController(); - void look_at_center(); + std::unique_ptr create_light(LightPrototype light_prototype); + void remove_light(unsigned int light_index); void recieve_event(VrEventPublisher* p, std::unique_ptr event) override; - void erase_light(); - //std::vector* get_lights(); private: + // Set the static arrays size + int reserved_count = 8; - //// Need to allow N byte light class to be packed into 10 byte packets - //int packed_size = sizeof(PackedData); - + // Indices available in the light array + std::list open_list; + std::vector packed_data_array; - // - // - std::unordered_map light_map; - // - //std::shared_ptr raycaster; - }; diff --git a/include/LightHandle.h b/include/LightHandle.h index 72a461c..faabc8e 100644 --- a/include/LightHandle.h +++ b/include/LightHandle.h @@ -3,6 +3,7 @@ #include #include + // We need to be able to : // - Allow lights to exist outside the context of a LightController // - i.e we can have a prototype light or similar @@ -12,21 +13,37 @@ // - But still allow classes of size Y bytes // - Preserve X packed bytes in an array which are pointed to by an array of shared pointers +struct LightPrototype; +class LightController; +struct PackedData; class LightHandle { public: - LightHandle(); - //LightHandle(LightController light_controller, std::string light_name); + + friend class LightController; + ~LightHandle(); + void set_friction(float friction); + void set_impulse(float impulse); + void set_movement(sf::Vector3f movement); + void add_movement(sf::Vector3f movement); + + void set_position(sf::Vector3f position); + void set_direction(sf::Vector3f direction); + void set_rgbi(sf::Vector4f rgbi); + private: + LightHandle(LightController *const light_controller, unsigned int light_id, LightPrototype light_prototype, std::unique_ptr data_reference); + + LightController *const light_controller_ref; + unsigned int light_id; + float friction_coefficient = 0.1f; float default_impulse = 1.0f; sf::Vector3f movement; - std::shared_ptr position; - std::shared_ptr direction_cartesian; - std::shared_ptr rgbi; + std::unique_ptr data_reference; }; diff --git a/include/raycaster/Hardware_Caster.h b/include/raycaster/Hardware_Caster.h index 9b663a4..0c7b202 100644 --- a/include/raycaster/Hardware_Caster.h +++ b/include/raycaster/Hardware_Caster.h @@ -36,6 +36,8 @@ struct device { cl_uint comp_units; }; +struct PackedData; + class Hardware_Caster : public RayCaster { public: @@ -50,7 +52,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 *data) 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/include/raycaster/RayCaster.h b/include/raycaster/RayCaster.h index a9c5d9e..ae66578 100644 --- a/include/raycaster/RayCaster.h +++ b/include/raycaster/RayCaster.h @@ -4,7 +4,8 @@ #include #include "Old_Map.h" #include "Camera.h" -#include "LightController.h" + +struct PackedData; class RayCaster { public: @@ -24,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 *data) = 0; + virtual void assign_lights(std::vector *data) = 0; virtual void validate() = 0; // draw will abstract the gl sharing and software rendering diff --git a/include/raycaster/Software_Caster.h b/include/raycaster/Software_Caster.h index f2c28d4..ea53272 100644 --- a/include/raycaster/Software_Caster.h +++ b/include/raycaster/Software_Caster.h @@ -1,6 +1,9 @@ -#include "RayCaster.h" +#pragma once +#include "raycaster/RayCaster.h" #include +struct PackedData; + class Software_Caster : public RayCaster { public: @@ -15,7 +18,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 *data) 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/LightController.cpp b/src/LightController.cpp index 881b9d6..c976fbf 100644 --- a/src/LightController.cpp +++ b/src/LightController.cpp @@ -1,48 +1,39 @@ #include "LightController.h" +#include "LightHandle.h" +#include -//LightController::LightController(std::shared_ptr raycaster) { -// //:raycaster(raycaster) { -// -// -// -// //packed_index = packed_data.size() / packed_size; -//} -LightController::~LightController() { -} -//void LightController::create_light(LightController::PackedData light_data, std::string light_name) { -// -// //if (light_map.count(light_name) == 1) { -// // // light already exists, TODO: error out -// // return; -// //} -// -// -//} +LightController::LightController(std::shared_ptr raycaster) : packed_data_array(reserved_count), open_list(reserved_count) { -//LightHandle LightController::get_light_handle(std::string light_name) { + std::iota(open_list.begin(), open_list.end(), 0); -//} - -void LightController::set_position(sf::Vector3f position) { + raycaster->assign_lights(&packed_data_array); +} +LightController::~LightController() { + } +std::unique_ptr LightController::create_light(LightPrototype light_prototype) { + unsigned int index = open_list.front(); + open_list.pop_front(); + + std::unique_ptr data(&packed_data_array.at(index)); + std::unique_ptr handle(new LightHandle(this, index, light_prototype, std::move(data))); + + return handle; -int LightController::update(double delta_time) { +} - double multiplier = 40; - //position.x += static_cast(movement.x * delta_time * multiplier); - //position.y += static_cast(movement.y * delta_time * multiplier); - //position.z += static_cast(movement.z * delta_time * multiplier); +void LightController::remove_light(unsigned int light_index) { - //movement *= static_cast(1.0f * delta_time * multiplier); + // Sanitization is currently handled by the light handler + open_list.push_front(light_index); - return 1; } void LightController::recieve_event(VrEventPublisher* publisher, std::unique_ptr event) { @@ -69,17 +60,3 @@ void LightController::recieve_event(VrEventPublisher* publisher, std::unique_ptr } } - -void LightController::erase_light() { - //packed_data.emplace_back(PackedData(position, direction, rgbi)); -} - -//std::vector* LightController::get_lights() { -// return &packed_data_array; -//} - -void LightController::look_at_center() { - - //direction_cartesian = CartToNormalizedSphere(sf::Vector3f(75, 75, 75) - position); -} - diff --git a/src/LightHandle.cpp b/src/LightHandle.cpp index 8b8e0f6..552a126 100644 --- a/src/LightHandle.cpp +++ b/src/LightHandle.cpp @@ -1,18 +1,60 @@ #include "LightHandle.h" #include "LightController.h" -LightHandle::LightHandle() { - // init an empty light - LightController::PackedData data; - data.direction_cartesian = sf::Vector3f(0, 0, 0); - data.position = sf::Vector3f(0, 0, 0); - data.rgbi = sf::Vector4f(0, 0, 0, 0); - - //light_controller.create_light(data, light_name); +LightHandle::LightHandle(LightController *const light_controller, unsigned int light_id, LightPrototype light_prototype, std::unique_ptr data_reference) : + light_controller_ref(light_controller), data_reference(std::move(data_reference)) { + + friction_coefficient = light_prototype.friction; + default_impulse = light_prototype.impulse; + movement = light_prototype.movement; + } + LightHandle::~LightHandle() { + // Sanitize data here, or in the controller? + data_reference->direction_cartesian = sf::Vector3f(0, 0, 0); + data_reference->position = sf::Vector3f(0, 0, 0); + data_reference->rgbi = sf::Vector4f(0, 0, 0, 0); + + light_controller_ref->remove_light(light_id); + +} + +void LightHandle::set_friction(float friction) +{ + +} + +void LightHandle::set_impulse(float impulse) +{ + +} + +void LightHandle::set_movement(sf::Vector3f movement) +{ + +} + +void LightHandle::add_movement(sf::Vector3f movement) +{ + +} + +void LightHandle::set_position(sf::Vector3f position) +{ + +} + +void LightHandle::set_direction(sf::Vector3f direction) +{ + +} + +void LightHandle::set_rgbi(sf::Vector4f rgbi) +{ + } diff --git a/src/NetworkInput.cpp b/src/NetworkInput.cpp index ad61178..050addb 100644 --- a/src/NetworkInput.cpp +++ b/src/NetworkInput.cpp @@ -18,39 +18,7 @@ void NetworkInput::stop_listening_for_clients() { void NetworkInput::recieve_from_clients() { - //// Receive a message from the client - //char buffer[1024]; - //std::vector packets; - - //sf::TcpSocket::Status status; - - //do { - - // std::size_t received = 0; - // status = socket.receive(buffer, 1024, received); - - // while (received < 12) { - // std::size_t tack_on; - // status = socket.receive(&buffer[received], 1024 - received, tack_on); - // received += tack_on; - // } - - - // int position = 0; - // while (position < received) { - // CustomPacket p; - // memcpy(p.data, &buffer[position], p.size); - // packets.push_back(p); - // position += p.size; - // } - - // std::cout << "packet_count = " << packets.size() << std::endl; - - // int left_over = 12 - (position - received); - // memcpy(buffer, &buffer[received - left_over], left_over); - - //} while (status != sf::TcpSocket::Status::Disconnected); } void NetworkInput::dispatch_events() @@ -128,7 +96,7 @@ void NetworkInput::threaded_client_listener(int port) { std::cout << "packet_count = " << packets.size() << std::endl; - int left_over = 12 - (position - received); + int left_over = 12 - static_cast(position - received); std::memcpy(buffer, &buffer[received - left_over], left_over); } while (status != sf::TcpSocket::Status::Done); diff --git a/src/Old_Map.cpp b/src/Old_Map.cpp index f2201d3..a24265b 100644 --- a/src/Old_Map.cpp +++ b/src/Old_Map.cpp @@ -15,8 +15,8 @@ Old_Map::~Old_Map() { void generate_at(int x, int y, std::vector> *grid) { - int x_bound = grid->size(); - int y_bound = grid->at(0).size(); + size_t x_bound = grid->size(); + size_t y_bound = grid->at(0).size(); // N S E W std::vector t = { 1, 2, 3, 4 }; diff --git a/src/main.cpp b/src/main.cpp index 68007a6..5782e2b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -32,7 +32,6 @@ #include "raycaster/Hardware_Caster.h" #include "Vector4.hpp" #include "Camera.h" -#include "raycaster/Software_Caster.h" #include "Input.h" #include "Pub_Sub.h" #include "NetworkInput.h" @@ -92,7 +91,8 @@ int main() { // Start up the raycaster //Hardware_Caster *raycaster = new Hardware_Caster(); - std::shared_ptr raycaster(new Hardware_Caster()); + Hardware_Caster *raycaster = new Hardware_Caster(); + //std::shared_ptr raycaster(new Hardware_Caster()); if (raycaster->init() != 1) { abort(); @@ -129,8 +129,8 @@ int main() { */ // Light for the currently non functional Bling Phong shader - std::shared_ptr asdf; - //LightController l(asdf); + //std::unique_ptr asdf(raycaster); + //LightController light_controller(std::move(raycaster)); // *links* the lights to the GPU //raycaster->assign_lights(); diff --git a/src/raycaster/Hardware_Caster.cpp b/src/raycaster/Hardware_Caster.cpp index bc18eaa..2eac238 100644 --- a/src/raycaster/Hardware_Caster.cpp +++ b/src/raycaster/Hardware_Caster.cpp @@ -1,5 +1,6 @@ #include "raycaster/Hardware_Caster.h" - +#include +#include "LightController.h" Hardware_Caster::Hardware_Caster() { @@ -35,7 +36,7 @@ int Hardware_Caster::init() { return error; } - srand(time(NULL)); + srand(time(nullptr)); int *seed_memory = new int[1920*1080]; @@ -209,14 +210,14 @@ void Hardware_Caster::create_viewport(int width, int height, float v_fov, float // //} -void Hardware_Caster::assign_lights(std::vector *data) { +void Hardware_Caster::assign_lights(std::vector *data) { // Get a pointer to the packed light data // this->lights = data; light_count = static_cast(lights->size()); - size_t packed_size = sizeof(LightController::PackedData); + cl_uint packed_size = 0;// sizeof(PackedData); create_buffer("lights", packed_size * light_count, lights->data(), CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR); diff --git a/src/raycaster/Software_Caster.cpp b/src/raycaster/Software_Caster.cpp index befb337..13d4a6f 100644 --- a/src/raycaster/Software_Caster.cpp +++ b/src/raycaster/Software_Caster.cpp @@ -82,11 +82,11 @@ void Software_Caster::create_viewport(int width, int height, float v_fov, float } -void Software_Caster::assign_lights(std::vector *data) { +void Software_Caster::assign_lights(std::vector *data) { // this->lights = data; - int light_count = static_cast(data->size()); +// int light_count = static_cast(data->size()); } void Software_Caster::assign_map(Old_Map * map) {