From 6e0d5814e1cccd2bd9626497abe1b9b90b620c6c Mon Sep 17 00:00:00 2001 From: MitchellHansen Date: Sat, 11 Feb 2017 21:43:32 -0800 Subject: [PATCH] Added some documentation on the lights. Got most everything tidied up. Having some issues with std::moving unique ptrs. Going to change them to shared_ptr --- include/LightController.h | 13 +++++++++- include/LightHandle.h | 40 ++++++++++++++++++++++++------- include/raycaster/RayCaster.h | 2 +- src/LightController.cpp | 2 +- src/LightHandle.cpp | 8 ++++++- src/main.cpp | 17 +++++++++---- src/raycaster/Hardware_Caster.cpp | 6 ++--- 7 files changed, 68 insertions(+), 20 deletions(-) diff --git a/include/LightController.h b/include/LightController.h index 61e8942..d3e99f2 100644 --- a/include/LightController.h +++ b/include/LightController.h @@ -9,6 +9,16 @@ struct LightPrototype { + LightPrototype( + sf::Vector3f position, + sf::Vector3f direction_cartesian, + sf::Vector4f rgbi + ) : + position(position), + direction_cartesian(direction_cartesian), + rgbi(rgbi) { }; + + sf::Vector3f position; sf::Vector3f direction_cartesian; sf::Vector4f rgbi; @@ -25,9 +35,10 @@ struct PackedData { position(position), direction_cartesian(direction_cartesian), rgbi(rgbi) { } PackedData() {}; + sf::Vector4f rgbi; sf::Vector3f position; sf::Vector3f direction_cartesian; - sf::Vector4f rgbi; + }; class LightHandle; diff --git a/include/LightHandle.h b/include/LightHandle.h index faabc8e..2f2f370 100644 --- a/include/LightHandle.h +++ b/include/LightHandle.h @@ -4,14 +4,28 @@ #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 -// - Preserve the existence of lights upon object destruction -// - No need to keep track of an object only used upon special use cases -// - Maintain an X*N byte array, X = size of packed data, N = Light number -// - 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 +// Light Handle : +// - Contains data relating to movement, and a reference to the rbgi, direction, and position +// elements in the LightController. +// - Resultant of the use of LightController.create_light(LightPrototype). Cannot be self instantiated. +// - On deconstruction, light data is removed from the LightController and the light disappears + +// LightPrototype : +// - Contains the desired starting values for the light. The LightHandler object will then be +// instantiated using this data + +// PackedData : +// - We need to single out the data that the GPU needs into a single contiguous +// array. PackedData holds the values for position, direction, and rgbi + +// LightController : +// - Contains the PackedData array in a static sized array. +// Empty light slots are set to 0 and still sent over the line +// TODO: This introduces light limits and inefficiencies +// - Contains a factory that takes LightPrototypes and generates unique ptr LightHandles. +// Each light handle is given a light index enabling light removal. + + struct LightPrototype; class LightController; @@ -21,29 +35,37 @@ class LightHandle { public: + // Allow LightController to access this objects constructor for a factory style pattern friend class LightController; ~LightHandle(); + // Functions for movement void set_friction(float friction); void set_impulse(float impulse); void set_movement(sf::Vector3f movement); void add_movement(sf::Vector3f movement); + // Functions modifying the pointed to data void set_position(sf::Vector3f position); void set_direction(sf::Vector3f direction); void set_rgbi(sf::Vector4f rgbi); + // void update(double delta_time); + private: LightHandle(LightController *const light_controller, unsigned int light_id, LightPrototype light_prototype, std::unique_ptr data_reference); + // Reference to the LightController to handle deconstruction and removal using the light_id LightController *const light_controller_ref; - unsigned int light_id; + const unsigned int light_id; + // Movement values provided by the prototype float friction_coefficient = 0.1f; float default_impulse = 1.0f; sf::Vector3f movement; + // Reference to the packed data in the LightController std::unique_ptr data_reference; }; diff --git a/include/raycaster/RayCaster.h b/include/raycaster/RayCaster.h index ae66578..58bdfca 100644 --- a/include/raycaster/RayCaster.h +++ b/include/raycaster/RayCaster.h @@ -41,7 +41,7 @@ protected: Old_Map * map = nullptr; Camera *camera = nullptr; // std::vector *lights; - std::vector *lights; + std::vector *lights; int light_count = 0; sf::Uint8 *viewport_image = nullptr; sf::Vector4f *viewport_matrix = nullptr; diff --git a/src/LightController.cpp b/src/LightController.cpp index c976fbf..bb596ee 100644 --- a/src/LightController.cpp +++ b/src/LightController.cpp @@ -20,7 +20,7 @@ std::unique_ptr LightController::create_light(LightPrototype light_ unsigned int index = open_list.front(); open_list.pop_front(); - std::unique_ptr data(&packed_data_array.at(index)); + std::unique_ptr data(&packed_data_array.data()[index]); std::unique_ptr handle(new LightHandle(this, index, light_prototype, std::move(data))); diff --git a/src/LightHandle.cpp b/src/LightHandle.cpp index 552a126..d313b8f 100644 --- a/src/LightHandle.cpp +++ b/src/LightHandle.cpp @@ -3,12 +3,18 @@ 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)) { + light_controller_ref(light_controller), light_id(light_id) { + + data_reference = std::move(data_reference); friction_coefficient = light_prototype.friction; default_impulse = light_prototype.impulse; movement = light_prototype.movement; + data_reference->direction_cartesian = light_prototype.direction_cartesian; + data_reference->position = light_prototype.position; + data_reference->rgbi = light_prototype.rgbi; + } diff --git a/src/main.cpp b/src/main.cpp index 5782e2b..5fcbbce 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -22,6 +22,7 @@ #include #endif +#pragma once #include #include #include @@ -36,6 +37,7 @@ #include "Pub_Sub.h" #include "NetworkInput.h" #include "LightController.h" +#include "LightHandle.h" const int WINDOW_X = 1000; const int WINDOW_Y = 1000; @@ -91,8 +93,8 @@ int main() { // Start up the raycaster //Hardware_Caster *raycaster = new Hardware_Caster(); - 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(); @@ -123,10 +125,17 @@ int main() { float w = 60.0; float h = 90.0; - /*sf::Vector3f(256.0f, 256.0f, 256.0f), + + LightController light_controller(raycaster); + LightPrototype prototype( + 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) -*/ + ); + + std::unique_ptr handle = light_controller.create_light(prototype); + + // Light for the currently non functional Bling Phong shader //std::unique_ptr asdf(raycaster); diff --git a/src/raycaster/Hardware_Caster.cpp b/src/raycaster/Hardware_Caster.cpp index 2eac238..65a3c1b 100644 --- a/src/raycaster/Hardware_Caster.cpp +++ b/src/raycaster/Hardware_Caster.cpp @@ -213,15 +213,15 @@ void Hardware_Caster::create_viewport(int width, int height, float v_fov, float void Hardware_Caster::assign_lights(std::vector *data) { // Get a pointer to the packed light data -// this->lights = data; + this->lights = data; light_count = static_cast(lights->size()); - cl_uint packed_size = 0;// sizeof(PackedData); + cl_uint packed_size = sizeof(PackedData); create_buffer("lights", packed_size * light_count, lights->data(), CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR); - create_buffer("light_count", sizeof(int), &light_count); + create_buffer("light_count", 8, &light_count); }