From 51be54c96445e3466b219b3566937d34892c3ca1 Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Sat, 17 Feb 2018 01:08:31 -0800 Subject: [PATCH] partway through some documentation and bug fixing --- include/CLCaster.h | 12 +++++++++--- include/Camera.h | 22 ++++++++++++++++++++-- include/Gui.h | 13 +++++++++++++ include/Input.h | 22 ++++++++++++++++++---- include/NetworkInput.h | 11 +++++++++++ include/Pub_Sub.h | 11 ++++++++--- src/Application.cpp | 15 +++++++-------- src/CLCaster.cpp | 23 ++++++++++++++--------- src/Camera.cpp | 11 +++++------ src/Input.cpp | 4 +--- src/Pub_Sub.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 11 files changed, 146 insertions(+), 38 deletions(-) diff --git a/include/CLCaster.h b/include/CLCaster.h index 92c38ad..f193449 100644 --- a/include/CLCaster.h +++ b/include/CLCaster.h @@ -137,19 +137,24 @@ public: bool create_texture_atlas(sf::Texture *t, sf::Vector2i tile_dim); // Check to make sure that the buffers have been initiated and set them as kernel args - bool validate() ; + bool validate(); // Aquires the GL objects, runs the kernel, releases back the GL objects - bool compute() ; + bool compute(); // Take the viewport sprite and draw it to the screen - void draw(sf::RenderWindow* window) ; + void draw(sf::RenderWindow* window); // Load the saved device config from a file bool load_config(); // Save the chosen device config to a file void save_config(); + + // Set a + void + + // ================================== DEBUG ======================================= // Re compile the kernel and revalidate the args @@ -282,6 +287,7 @@ private: // Containers holding the kernels and buffers std::map kernel_map; std::map buffer_map; + std::vector> settings_index_map; std::unordered_map>> image_map; // Hardware caster holds and renders its own textures diff --git a/include/Camera.h b/include/Camera.h index ef69179..f70f5c5 100644 --- a/include/Camera.h +++ b/include/Camera.h @@ -6,28 +6,46 @@ #include #include "Gui.h" + + +/** + * + * Camera + * + * Camera provides a convenient way to create 3d vectors and positions which represent a camera. It provides physics + * to move the camera around in 3d space as well as impulse and friction control to alter its movement characteristics + * + */ + + class Camera : public VrEventSubscriber, private Gui{ + public: enum DIRECTION { FORWARD, REARWARD, LEFT, RIGHT, UP, DOWN }; Camera(); + // TODO: Remove dependency on getting a window ptr. Instead provide window interface to get and set mouse position Camera(sf::Vector3f position, sf::Vector2f direction, sf::RenderWindow *window); ~Camera(); int set_position(sf::Vector3f position); + // Apply an incoming impule to the velocity vector int add_static_impulse(sf::Vector3f impulse); + + // Apply an impulse in one of the 6 relative directions with a certain magnitude int add_relative_impulse(DIRECTION direction, float speed); int slew_camera(sf::Vector2f input); - void set_camera(sf::Vector2f input); - void set_camera(sf::Vector3f input); + void set_camera_direction(sf::Vector2f input); + void set_camera_direction(sf::Vector3f input); int update(double delta_time); void look_at_center(); + // TODO: Raw ptr's SHARED_PTR with CL, bad idea sf::Vector2f* get_direction_pointer(); sf::Vector3f* get_position_pointer(); sf::Vector3f* get_movement_pointer(); diff --git a/include/Gui.h b/include/Gui.h index 1c266de..1cd5ef6 100644 --- a/include/Gui.h +++ b/include/Gui.h @@ -3,6 +3,19 @@ #include #include +/** + * + * GUI + * + * Any class that wants to have an interactive GUI rendered to the window may + * inherit GUI and override the render_gui() and update_gui() methods + * + * ImGui operations must be completely wrapped in Begins and Ends + * + * You may enable and disable rendering by setting the 'rendering' flag to true or false + * + */ + class Gui { public: diff --git a/include/Input.h b/include/Input.h index 883a96b..e21d733 100644 --- a/include/Input.h +++ b/include/Input.h @@ -7,6 +7,22 @@ #include "Gui.h" #include +/** + * + * Input + * + * For each frame the Application must call + * + * consume_sf_events(*window) + * handle_held_keys() + * dispatch_events() + * + * which will pull all the events from the sfml event queue, transpose them over + * to vr:events, compare to the last frame and create held key events for keys held + * for longer than one frame, and finally dispatch the events to the relevent VrEventListener's + * + */ + class Input : public VrEventPublisher, private Gui{ public: @@ -14,9 +30,6 @@ public: Input(); ~Input(); - // Keep track of keys that are not released - // Keep track of mouse up and downs in conjunction with dragging - // Keep track of joystick buttons void consume_sf_events(sf::RenderWindow *window); void consume_vr_events(); @@ -33,7 +46,8 @@ private: std::vector held_keys; std::vector held_mouse_buttons; - + + // TODO: What the hell was I using these for? std::vector keyboard_flags; std::vector mouse_flags; diff --git a/include/NetworkInput.h b/include/NetworkInput.h index dd1b017..cf66e15 100644 --- a/include/NetworkInput.h +++ b/include/NetworkInput.h @@ -4,6 +4,17 @@ #include "Pub_Sub.h" #include +/** + * + * NetworkInput + * + * Prototype network joystick, listens to clients connecting to a certain port and reads + * packets pertaining to, in the case of the lights, xyz movement. This could in theory + * provide a very generic way to listen to network input and generate events from received + * packets. + * + */ + struct CustomPacket { char data[1024]; diff --git a/include/Pub_Sub.h b/include/Pub_Sub.h index 7ea3aaf..7b486c9 100644 --- a/include/Pub_Sub.h +++ b/include/Pub_Sub.h @@ -9,19 +9,24 @@ class VrEventPublisher; class VrEventSubscriber { public: - virtual ~VrEventSubscriber() {}; + virtual ~VrEventSubscriber(); virtual void recieve_event(VrEventPublisher* publisher, std::unique_ptr event) = 0; void subscribe_to_publisher(VrEventPublisher* publisher, vr::Event::EventType type); void subscribe_to_publisher(VrEventPublisher* publisher, std::vector type); + void unsubscribe(VrEventPublisher* publisher, vr::Event::EventType type); protected: - std::vector subscribed_event_types; + + // When we destroy a subscriber we need to be able to notify the publishers + // We have to keep track of every EventType because of the way EventTypes + // are mapped to subscribers in the publisher + std::map> subscriptions; }; class VrEventPublisher { public: - virtual ~VrEventPublisher() {}; + virtual ~VrEventPublisher(); virtual void subscribe(VrEventSubscriber *subscriber, vr::Event::EventType type); virtual void subscribe(VrEventSubscriber *subscriber, std::vector type); virtual void unsubscribe(VrEventSubscriber *s, vr::Event::EventType c); diff --git a/src/Application.cpp b/src/Application.cpp index 2d84745..60029d5 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -30,22 +30,22 @@ bool Application::init_clcaster() { abort(); map = std::make_shared(MAP_X); - sf::Image bitmap = map->GenerateHeightBitmap(sf::Vector3i(MAP_X, MAP_Y, MAP_Z)); + + // TODO: Implement this + sf::Image bitmap = map->GenerateHeightBitmap(sf::Vector3i(MAP_X, MAP_Y, MAP_Z)); map->ApplyHeightmap(bitmap); - map->octree.CastRayOctree(sf::Vector2f(1.57f, 0.0001f), sf::Vector3f(0.5f, 0.5f, 0.5f)); + //map->octree.CastRayOctree(sf::Vector2f(1.57f, 0.0001f), sf::Vector3f(0.5f, 0.5f, 0.5f)); + // TODO: Consolidate this to one call raycaster->assign_octree(map); raycaster->assign_map(map); - // Create a new camera with (starting position, direction) camera = std::make_shared( - sf::Vector3f(3.5f, 3.5f, 3.5f), - sf::Vector2f(1.57f, 0.0f), + sf::Vector3f(3.5f, 3.5f, 3.5f), // Starting position + sf::Vector2f(1.57f, 0.0f), // Direction window.get() ); - - // *link* the camera to the GPU raycaster->assign_camera(camera); // Generate and send the viewport to the GPU. Also creates the viewport texture @@ -63,7 +63,6 @@ bool Application::init_clcaster() { light_handle = light_controller->create_light(prototype); // Load in the spritesheet texture - if (!spritesheet.loadFromFile("../assets/textures/minecraft_tiles.png")) Logger::log("Failed to load spritesheet from file", Logger::LogLevel::WARN); raycaster->create_texture_atlas(&spritesheet, sf::Vector2i(16, 16)); diff --git a/src/CLCaster.cpp b/src/CLCaster.cpp index fce3204..89d48f4 100644 --- a/src/CLCaster.cpp +++ b/src/CLCaster.cpp @@ -8,11 +8,10 @@ CLCaster::~CLCaster() { //release_camera(); //release_octree(); //clReleaseKernel(kernel_map.at("raycaster")); - // clReleaseProgram() + //clReleaseProgram() //release_viewport(); delete[] viewport_matrix; - delete[] viewport_image; delete[] viewport_image; camera.reset(); @@ -720,13 +719,19 @@ bool CLCaster::compile_kernel(std::string kernel_source, bool is_path, std::stri return false; } - // Try and build the program - // "-cl-finite-math-only -cl-fast-relaxed-math -cl-unsafe-math-optimizations" - - // need a ref to the oct dimensions - //std::string oct_dimensions = std::to_string(map->getDimensions().x); - - std::string build_string = "-DOCTDIM=" + std::to_string(Application::MAP_X) + " -cl-finite-math-only -cl-fast-relaxed-math -cl-unsafe-math-optimizations"; + + std::stringstream build_string_stream; + + // walk the settings index's and add them to the defines + for (auto i: settings_index_map){ + build_string_stream << " -D" << i.first << "=" << std::to_string(i.second); + } + + build_string_stream << "-DOCTDIM=" << std::to_string(Application::MAP_X); + build_string_stream << " -cl-finite-math-only -cl-fast-relaxed-math -cl-unsafe-math-optimizations"; + + std::string build_string = build_string_stream.str(); + error = clBuildProgram(program, 1, &device_id, build_string.c_str(), NULL, NULL); // Check to see if it error'd out diff --git a/src/Camera.cpp b/src/Camera.cpp index 1784061..1f3dcf5 100644 --- a/src/Camera.cpp +++ b/src/Camera.cpp @@ -65,11 +65,11 @@ int Camera::slew_camera(sf::Vector2f input) { return 1; } -void Camera::set_camera(sf::Vector2f input) { +void Camera::set_camera_direction(sf::Vector2f input) { direction = input; } -void Camera::set_camera(sf::Vector3f input) { +void Camera::set_camera_direction(sf::Vector3f input) { direction = CartToNormalizedSphere(input); } @@ -101,10 +101,10 @@ void Camera::recieve_event(VrEventPublisher* publisher, std::unique_ptr(event.get()); if (held_event->code == sf::Keyboard::LShift) { - default_impulse = 0.01f; + setSpeed(0.01f); } else if (held_event->code == sf::Keyboard::RShift) { - default_impulse = 1.0f; + setSpeed(0.3f); } else if (held_event->code == sf::Keyboard::C) { look_at_center(); @@ -151,7 +151,6 @@ void Camera::recieve_event(VrEventPublisher* publisher, std::unique_ptr(event.get()); - //deltas = fixed - sf::Mouse::getPosition(); deltas = fixed - sf::Vector2i(mouse_event->x, mouse_event->y); if (deltas != sf::Vector2i(0, 0) && mouse_enabled == true) { @@ -271,7 +270,7 @@ sf::Vector2f Camera::get_direction() { } void Camera::setSpeed(float speed) { - default_impulse = speed;; + default_impulse = speed; } float Camera::getSpeed() { diff --git a/src/Input.cpp b/src/Input.cpp index 4c8fbee..8791bd9 100644 --- a/src/Input.cpp +++ b/src/Input.cpp @@ -147,7 +147,6 @@ void Input::render_gui() { for (int i = 0; i < 3; i++) { - float offset = ImGui::GetColumnWidth(i); draw_list->AddLine(ImVec2(p.x + 0 + offset * i, p.y + 50), ImVec2(p.x + 100 + offset * i, p.y + 50), col32, 1.0); @@ -284,8 +283,7 @@ void Input::transpose_sf_events(std::list sf_event_queue) { break; }; default: { - std::cout << "Event not recognized"; - abort(); + Logger::log("Event not recognized", Logger::LogLevel::WARN); break; } } diff --git a/src/Pub_Sub.cpp b/src/Pub_Sub.cpp index 2a9f376..d69cf5a 100644 --- a/src/Pub_Sub.cpp +++ b/src/Pub_Sub.cpp @@ -3,14 +3,53 @@ #include "Pub_Sub.h" +/** + * Subscriber + */ + +VrEventSubscriber::~VrEventSubscriber() { + + // Cycles through the publishers we're subscribed to + for (auto const& publisher : subscriptions) { + + // And one by one remove the EventTypes we're subscribed to + for (auto event_type: publisher.second) { + publisher.first->unsubscribe(this, event_type); + } + } +} + void VrEventSubscriber::subscribe_to_publisher(VrEventPublisher* publisher, vr::Event::EventType type) { publisher->subscribe(this, type); + + subscriptions[publisher].push_back(type); } void VrEventSubscriber::subscribe_to_publisher(VrEventPublisher* publisher, std::vector type) { publisher->subscribe(this, type); + + subscriptions[publisher].insert(subscriptions[publisher].end(), type.begin(), type.end()); +} + +void VrEventSubscriber::unsubscribe(VrEventPublisher* publisher, vr::Event::EventType type){ + +} + +/** + * Publisher + */ +VrEventPublisher::~VrEventPublisher() { + + // Cycle through the subscribers that are listening to us + for (auto const& subscriber_bucket : subscribers) { + + // And one by one remove the + for (auto subscriber: subscriber_bucket.second){ + subscriber. + } + } } void VrEventPublisher::subscribe(VrEventSubscriber *subscriber, vr::Event::EventType type) { @@ -42,3 +81,4 @@ void VrEventPublisher::notify_subscribers(std::unique_ptr event) { } } +