Still wrestling with a good way to hide the packing
This commit is contained in:
@@ -50,7 +50,7 @@ public:
|
|||||||
// Both will create the view matrix, view res buffer
|
// Both will create the view matrix, view res buffer
|
||||||
void create_viewport(int width, int height, float v_fov, float h_fov) override;
|
void create_viewport(int width, int height, float v_fov, float h_fov) override;
|
||||||
|
|
||||||
void assign_lights(std::vector<LightController> *lights) override;
|
void assign_lights(std::vector<LightController::PackedData> *data) override;
|
||||||
void assign_lights();
|
void assign_lights();
|
||||||
void assign_map(Old_Map *map) override;
|
void assign_map(Old_Map *map) override;
|
||||||
void assign_camera(Camera *camera) override;
|
void assign_camera(Camera *camera) override;
|
||||||
|
|||||||
@@ -3,24 +3,35 @@
|
|||||||
#include <SFML/System/Vector2.hpp>
|
#include <SFML/System/Vector2.hpp>
|
||||||
#include "util.hpp"
|
#include "util.hpp"
|
||||||
#include "Pub_Sub.h"
|
#include "Pub_Sub.h"
|
||||||
|
#include "RayCaster.h"
|
||||||
|
|
||||||
class LightController : public VrEventSubscriber {
|
class LightController : public VrEventSubscriber {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
enum DIRECTION { FORWARD, REARWARD, LEFT, RIGHT, UP, DOWN };
|
enum DIRECTION { FORWARD, REARWARD, LEFT, RIGHT, UP, DOWN };
|
||||||
|
|
||||||
struct Light {
|
// Packed data structure for passing raw light data to the caster
|
||||||
Light(sf::Vector3f position, sf::Vector3f direction_cartesian, sf::Vector4f rgbi) :
|
struct PackedData {
|
||||||
|
PackedData(sf::Vector3f position, sf::Vector3f direction_cartesian, sf::Vector4f rgbi) :
|
||||||
position(position), direction_cartesian(direction_cartesian), rgbi(rgbi) {
|
position(position), direction_cartesian(direction_cartesian), rgbi(rgbi) {
|
||||||
}
|
}
|
||||||
Light();
|
PackedData();
|
||||||
sf::Vector3f position;
|
sf::Vector3f position;
|
||||||
sf::Vector3f direction_cartesian;
|
sf::Vector3f direction_cartesian;
|
||||||
sf::Vector4f rgbi;
|
sf::Vector4f rgbi;
|
||||||
};
|
};
|
||||||
|
|
||||||
LightController();
|
// Data that enables "camera" style movement. I can't really use inheritance easily because
|
||||||
LightController(sf::Vector3f position, sf::Vector3f direction, sf::Vector4f rgbi);
|
// 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> raycaster);
|
||||||
~LightController();
|
~LightController();
|
||||||
|
|
||||||
void set_position(sf::Vector3f position);
|
void set_position(sf::Vector3f position);
|
||||||
@@ -34,17 +45,21 @@ public:
|
|||||||
|
|
||||||
void recieve_event(VrEventPublisher* p, std::unique_ptr<vr::Event> event) override;
|
void recieve_event(VrEventPublisher* p, std::unique_ptr<vr::Event> event) override;
|
||||||
|
|
||||||
static void erase_light();
|
void erase_light();
|
||||||
std::vector<LightController::Light>* get_lights();
|
std::vector<LightController::PackedData>* get_lights();
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
|
||||||
// Need to allow N byte light class to be packed into 10 byte packets
|
// 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
|
// Index that this light is in the packed data
|
||||||
int packed_index;
|
int packed_index;
|
||||||
|
|
||||||
std::vector<LightController::Light> packed_data;
|
std::vector<PackedData> packed_data_array;
|
||||||
|
std::unordered_map<std::string, Light> light_map;
|
||||||
|
std::shared_ptr<RayCaster> raycaster;
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ public:
|
|||||||
virtual void assign_map(Old_Map *map) = 0;
|
virtual void assign_map(Old_Map *map) = 0;
|
||||||
virtual void assign_camera(Camera *camera) = 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 create_viewport(int width, int height, float v_fov, float h_fov) = 0;
|
||||||
virtual void assign_lights(std::vector<LightController> *lights) = 0;
|
virtual void assign_lights(std::vector<LightController::PackedData> *data) = 0;
|
||||||
virtual void validate() = 0;
|
virtual void validate() = 0;
|
||||||
|
|
||||||
// draw will abstract the gl sharing and software rendering
|
// draw will abstract the gl sharing and software rendering
|
||||||
@@ -40,7 +40,7 @@ protected:
|
|||||||
|
|
||||||
Old_Map * map = nullptr;
|
Old_Map * map = nullptr;
|
||||||
Camera *camera = nullptr;
|
Camera *camera = nullptr;
|
||||||
std::vector<LightController::Light> *lights;
|
std::vector<LightController::PackedData> *lights;
|
||||||
int light_count = 0;
|
int light_count = 0;
|
||||||
sf::Uint8 *viewport_image = nullptr;
|
sf::Uint8 *viewport_image = nullptr;
|
||||||
sf::Vector4f *viewport_matrix = nullptr;
|
sf::Vector4f *viewport_matrix = nullptr;
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ public:
|
|||||||
// Both will create the view matrix, view res buffer
|
// Both will create the view matrix, view res buffer
|
||||||
void create_viewport(int width, int height, float v_fov, float h_fov) override;
|
void create_viewport(int width, int height, float v_fov, float h_fov) override;
|
||||||
|
|
||||||
void assign_lights(std::vector<LightController> *lights) override;
|
void assign_lights(std::vector<LightController::PackedData> *data) override;
|
||||||
void assign_map(Old_Map *map) override;
|
void assign_map(Old_Map *map) override;
|
||||||
void assign_camera(Camera *camera) override;
|
void assign_camera(Camera *camera) override;
|
||||||
void validate() override;
|
void validate() override;
|
||||||
|
|||||||
@@ -196,28 +196,28 @@ void Hardware_Caster::create_viewport(int width, int height, float v_fov, float
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Hardware_Caster::assign_lights(std::vector<LightController> *lights) {
|
//void Hardware_Caster::assign_lights(std::vector<LightController> *lights) {
|
||||||
|
//
|
||||||
|
// //this->lights = ;
|
||||||
|
//
|
||||||
|
// std::cout << sizeof(LightController);
|
||||||
|
// std::cout << sizeof(float);
|
||||||
|
// light_count = static_cast<int>(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);
|
||||||
|
//
|
||||||
|
//}
|
||||||
|
|
||||||
//this->lights = ;
|
void Hardware_Caster::assign_lights(std::vector<LightController::PackedData> *data) {
|
||||||
|
|
||||||
std::cout << sizeof(LightController);
|
|
||||||
std::cout << sizeof(float);
|
|
||||||
light_count = static_cast<int>(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() {
|
|
||||||
|
|
||||||
// Get a pointer to the packed light data
|
// Get a pointer to the packed light data
|
||||||
this->lights = LightController::get_lights();
|
this->lights = data;
|
||||||
|
|
||||||
light_count = static_cast<int>(lights->size());
|
light_count = static_cast<int>(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);
|
create_buffer("lights", packed_size * light_count, lights->data(), CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR);
|
||||||
|
|
||||||
|
|||||||
@@ -2,14 +2,12 @@
|
|||||||
#include "LightController.h"
|
#include "LightController.h"
|
||||||
#include "Pub_Sub.h"
|
#include "Pub_Sub.h"
|
||||||
|
|
||||||
|
LightController::LightController(std::shared_ptr<RayCaster> raycaster) :
|
||||||
LightController::LightController() {
|
raycaster(raycaster) {
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
LightController::LightController(sf::Vector3f position, sf::Vector3f direction, sf::Vector4f rgbi) {
|
|
||||||
packed_index = packed_data.size() / packed_size;
|
//packed_index = packed_data.size() / packed_size;
|
||||||
packed_data.emplace_back(Light(position, direction, rgbi));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
LightController::~LightController() {
|
LightController::~LightController() {
|
||||||
@@ -32,22 +30,22 @@ int LightController::add_relative_impulse(DIRECTION impulse_direction, float spe
|
|||||||
switch (impulse_direction) {
|
switch (impulse_direction) {
|
||||||
|
|
||||||
case DIRECTION::FORWARD:
|
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;
|
break;
|
||||||
case DIRECTION::REARWARD:
|
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;
|
break;
|
||||||
case DIRECTION::LEFT:
|
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;
|
break;
|
||||||
case DIRECTION::RIGHT:
|
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;
|
break;
|
||||||
case DIRECTION::UP:
|
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;
|
break;
|
||||||
case DIRECTION::DOWN:
|
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;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -97,7 +95,11 @@ void LightController::recieve_event(VrEventPublisher* publisher, std::unique_ptr
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<LightController::Light>* LightController::get_lights() {
|
void LightController::erase_light() {
|
||||||
|
//packed_data.emplace_back(PackedData(position, direction, rgbi));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<LightController::PackedData>* LightController::get_lights() {
|
||||||
return &packed_data;
|
return &packed_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,107 +5,3 @@ RayCaster::RayCaster() {
|
|||||||
|
|
||||||
RayCaster::~RayCaster() {
|
RayCaster::~RayCaster() {
|
||||||
}
|
}
|
||||||
|
|
||||||
//void RayCaster::assign_map(Old_Map* map) {
|
|
||||||
// this->map = map;
|
|
||||||
//}
|
|
||||||
// // Override values
|
|
||||||
// //this.map_dimensions = new Vector3<int> (50, 50, 50);
|
|
||||||
// //this.resolution = new Vector2<int> (200, 200);
|
|
||||||
// //this.camera_direction = new Vector3<float> (1f, 0f, .8f);
|
|
||||||
// //this.camera_position = new Vector3<float> (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<float> camera_direction, sf::Vector3<float> 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;
|
|
||||||
//}
|
|
||||||
|
|||||||
12
src/main.cpp
12
src/main.cpp
@@ -92,7 +92,8 @@ int main() {
|
|||||||
t.create_buffers();*/
|
t.create_buffers();*/
|
||||||
|
|
||||||
// Start up the raycaster
|
// Start up the raycaster
|
||||||
Hardware_Caster *raycaster = new Hardware_Caster();
|
//Hardware_Caster *raycaster = new Hardware_Caster();
|
||||||
|
std::shared_ptr<Hardware_Caster> raycaster(new Hardware_Caster());
|
||||||
|
|
||||||
if (raycaster->init() != 1) {
|
if (raycaster->init() != 1) {
|
||||||
abort();
|
abort();
|
||||||
@@ -123,12 +124,13 @@ int main() {
|
|||||||
float w = 60.0;
|
float w = 60.0;
|
||||||
float h = 90.0;
|
float h = 90.0;
|
||||||
|
|
||||||
// Light for the currently non functional Bling Phong shader
|
/*sf::Vector3f(256.0f, 256.0f, 256.0f),
|
||||||
LightController l(
|
|
||||||
sf::Vector3f(256.0f, 256.0f, 256.0f),
|
|
||||||
sf::Vector3f(-1.0f, -1.0f, -1.5f),
|
sf::Vector3f(-1.0f, -1.0f, -1.5f),
|
||||||
sf::Vector4f(1.0f, 1.0f, 1.0f, 1.0f)
|
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
|
// *links* the lights to the GPU
|
||||||
raycaster->assign_lights();
|
raycaster->assign_lights();
|
||||||
|
|||||||
Reference in New Issue
Block a user