From e13280bb07f1796919f828a2da271f3c714c85b5 Mon Sep 17 00:00:00 2001 From: MitchellHansen Date: Thu, 22 Sep 2016 21:48:38 -0700 Subject: [PATCH] added relative camera controls, now handles like an actual fly cam. --- include/Camera.h | 7 ++-- include/Map.h | 79 +++++++++++++++++++++++++++++++++++++++++----- include/util.hpp | 26 +++++++++++++++ src/CL_Wrapper.cpp | 7 ++-- src/Camera.cpp | 51 ++++++++++++++++++++++++++---- src/Map.cpp | 3 -- src/main.cpp | 47 ++++++++++++++++++++------- 7 files changed, 185 insertions(+), 35 deletions(-) diff --git a/include/Camera.h b/include/Camera.h index 201262f..2e600e9 100644 --- a/include/Camera.h +++ b/include/Camera.h @@ -19,10 +19,11 @@ public: int slew_camera(sf::Vector2f input); - int update(); + int update(double delta_time); - void* get_direction_pointer(); - void* get_position_pointer(); + sf::Vector2f* get_direction_pointer(); + sf::Vector3f* get_position_pointer(); + sf::Vector3f* get_movement_pointer(); sf::Vector3f get_movement(); sf::Vector3f get_position(); diff --git a/include/Map.h b/include/Map.h index 14491b8..0efcef2 100644 --- a/include/Map.h +++ b/include/Map.h @@ -6,10 +6,68 @@ #include #include #include +#include class Map { public: + + // In addition to traversing the voxel hierarchy, we must also be able to + // tell which block a given voxel resides in.This is accomplished us - + // ing 32 - bit page headers spread amongst the child descriptors.Page + // headers are placed at every 8 kilobyte boundary, and each contains + // a relative pointer to the block info section.By placing the begin - + // ning of the child descriptor array at such a boundary, we can always + // find a page header by simply clearing the lowest bits of any child + // descriptor pointer. + struct page_header { + int bitmask; + }; + + struct leaf_node { + long bitmask; + }; + + + short scale; + + void generate_octree() { + + uint64_t *octree = new uint64_t[200]; + + + long tree_node = 0; + + std::vector page_array; + + // Page placed every 8 kilobytes + // contains a relative pointer to the block info section + uint32_t page = 255; + + // Child pointer points to the first non-leaf child of this node + uint16_t child_pointer = 20; + + + uint32_t pointer = page | child_pointer; + + + }; + Map(sf::Vector3i dim) { + + //generate_octree(); + //return; + + + + + + + + + + + + dimensions = dim; std::mt19937 gen; std::uniform_real_distribution dis(-1.0, 1.0); @@ -147,14 +205,14 @@ public: } - // for (int x = 0; x < dim.x / 10; x++) { - // for (int y = 0; y < dim.y / 10; y++) { - // for (int z = 0; z < dim.z; z++) { - // if (rand() % 1000 < 1) - // list[x + dim.x * (y + dim.z * z)] = rand() % 6; - // } - // } - // } + for (int x = 0; x < dim.x / 10; x++) { + for (int y = 0; y < dim.y / 10; y++) { + for (int z = 0; z < dim.z; z++) { + if (rand() % 1000 < 1) + list[x + dim.x * (y + dim.z * z)] = rand() % 6; + } + } + } } @@ -162,6 +220,11 @@ public: ~Map() { } + + + + + sf::Vector3i getDimensions(); char *list; sf::Vector3i dimensions; diff --git a/include/util.hpp b/include/util.hpp index 4b693f3..e5bba3b 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -39,6 +39,32 @@ private: double fps_average = 0; }; +struct debug_text { +public: + debug_text(int slot, int pixel_spacing, void* data_, std::string prefix_) : data(data_), prefix(prefix_) { + if (!f.loadFromFile("../assets/fonts/Arial.ttf")) { + std::cout << "couldn't find the fall back Arial font in ../assets/fonts/" << std::endl; + } + else { + t.setFont(f); + t.setCharacterSize(20); + t.setPosition(20, slot * pixel_spacing); + } + + } + + void draw(sf::RenderWindow *r) { + t.setString(prefix + std::to_string(*(float*)data)); + r->draw(t); + } + +private: + void* data; + std::string prefix; + sf::Font f; + sf::Text t; + +}; inline sf::Vector3f SphereToCart(sf::Vector2f i) { diff --git a/src/CL_Wrapper.cpp b/src/CL_Wrapper.cpp index 1118a36..4881c96 100644 --- a/src/CL_Wrapper.cpp +++ b/src/CL_Wrapper.cpp @@ -59,7 +59,9 @@ int CL_Wrapper::acquire_platform_and_device(){ // falling back to the cpu with the fastest clock if we weren't able to find one device current_best_device; - current_best_device.type = -1; // Set this to -1 so the first run always selects a new device + current_best_device.type = 0; // Set this to 0 so the first run always selects a new device + current_best_device.clock_frequency = 0; + current_best_device.comp_units = 0; for (auto kvp: plt_ids){ @@ -86,7 +88,7 @@ int CL_Wrapper::acquire_platform_and_device(){ platform_id = current_best_device.platform; device_id = current_best_device.id; - return 0; + return 1; }; int CL_Wrapper::create_shared_context() { @@ -272,7 +274,6 @@ int CL_Wrapper::store_buffer(cl_mem buffer, std::string buffer_name){ int CL_Wrapper::run_kernel(std::string kernel_name, const int work_size){ - const int WORKER_SIZE = 10; size_t global_work_size[1] = { static_cast(work_size) }; cl_kernel kernel = kernel_map.at(kernel_name); diff --git a/src/Camera.cpp b/src/Camera.cpp index e87209f..06419a0 100644 --- a/src/Camera.cpp +++ b/src/Camera.cpp @@ -19,15 +19,42 @@ int Camera::set_position(sf::Vector3f position) { this->position = position; return 1; } - + int Camera::add_static_impulse(sf::Vector3f impulse) { movement += impulse; return 1; } int Camera::add_relative_impulse(DIRECTION impulse_direction) { + + // No sense in doing fancy dot products, adding Pi's will suffice + // Always add PI/2 to X initially to avoid negative case + sf::Vector2f dir; - SphereToCart(direction); + switch (impulse_direction) { + + case DIRECTION::UP: + dir = sf::Vector2f(direction.y, direction.x + PI); + break; + case DIRECTION::DOWN: + dir = sf::Vector2f(direction.y, direction.x); + break; + case DIRECTION::LEFT: + dir = sf::Vector2f(direction.y + PI + PI / 2, PI / 2); + break; + case DIRECTION::RIGHT: + dir = sf::Vector2f(direction.y + PI / 2, PI / 2); + break; + case DIRECTION::FORWARD: + dir = sf::Vector2f(direction.y, direction.x + PI / 2); + break; + case DIRECTION::REARWARD: + dir = sf::Vector2f(direction.y + PI, (direction.x * -1) + PI / 2 ); + break; + + } + + movement += SphereToCart(dir); return 1; } @@ -37,20 +64,30 @@ int Camera::slew_camera(sf::Vector2f input) { return 1; } -int Camera::update() { +int Camera::update(double delta_time) { - position += movement; + // so vector multiplication broke? + // have to do it component wise + double multiplier = 50; + + position.x += movement.x * delta_time * multiplier; + position.y += movement.y * delta_time * multiplier; + position.z += movement.z * delta_time * multiplier; - movement *= friction_coefficient; + movement *= (float)(friction_coefficient * delta_time * multiplier); return 1; } -void* Camera::get_direction_pointer() { +sf::Vector2f* Camera::get_direction_pointer() { return &direction; } -void* Camera::get_position_pointer() { +sf::Vector3f* Camera::get_movement_pointer() { + return &movement; +} + +sf::Vector3f* Camera::get_position_pointer() { return &position; } diff --git a/src/Map.cpp b/src/Map.cpp index 3d478cf..b821059 100644 --- a/src/Map.cpp +++ b/src/Map.cpp @@ -22,9 +22,6 @@ void Map::moveLight(sf::Vector2f in) { } -//void Map::GenerateFloor(){ -//} - diff --git a/src/main.cpp b/src/main.cpp index 28c7ea6..505d368 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -65,6 +65,12 @@ sf::Texture window_texture; int main() { + //Map m(sf::Vector3i (50, 50, 50)); + //return 1; + + + + sf::RenderWindow window(sf::VideoMode(WINDOW_X, WINDOW_Y), "SFML"); @@ -137,8 +143,8 @@ int main() { sf::Vector2f(0.0f, 1.00f) ); - c.create_buffer("cam_dir_buffer", sizeof(float) * 4, camera.get_direction_pointer(), CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR); - c.create_buffer("cam_pos_buffer", sizeof(float) * 4, camera.get_position_pointer(), CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR); + c.create_buffer("cam_dir_buffer", sizeof(float) * 4, (void*)camera.get_direction_pointer(), CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR); + c.create_buffer("cam_pos_buffer", sizeof(float) * 4, (void*)camera.get_position_pointer(), CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR); // {r, g, b, i, x, y, z, x', y', z'} float light[] = { 0.4, 0.8, 0.1, 1, 50, 50, 50, 1.1, 0.4, 0.7}; @@ -211,7 +217,15 @@ int main() { RayCaster ray_caster(map, map_dim, view_res); + sf::Vector2f *dp = camera.get_direction_pointer(); + debug_text cam_text_x(1, 30, &dp->x, "X: "); + debug_text cam_text_y(2, 30, &dp->y, "Y: "); + sf::Vector3f *mp = camera.get_movement_pointer(); + debug_text cam_text_mov_x(4, 30, &mp->x, "X: "); + debug_text cam_text_mov_y(5, 30, &mp->y, "Y: "); + debug_text cam_text_mov_z(6, 30, &mp->y, "Z: "); + //debug_text cam_text_z(3, 30, &p->z); // =============================================================================== // Mouse capture @@ -245,22 +259,25 @@ int main() { cam_vec.z = 0; if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q)) { - cam_vec.z = 1; + camera.add_relative_impulse(Camera::DIRECTION::DOWN); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::E)) { - cam_vec.z = -1; + camera.add_relative_impulse(Camera::DIRECTION::UP); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) { - cam_vec.y = 1; + camera.add_relative_impulse(Camera::DIRECTION::FORWARD); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) { - cam_vec.y = -1; + camera.add_relative_impulse(Camera::DIRECTION::REARWARD); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) { - cam_vec.x = 1; + camera.add_relative_impulse(Camera::DIRECTION::LEFT); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) { - cam_vec.x = -1; + camera.add_relative_impulse(Camera::DIRECTION::RIGHT); + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::T)) { + camera.set_position(sf::Vector3f(20, 20, 20)); } camera.add_static_impulse(cam_vec); @@ -288,13 +305,12 @@ int main() { while ((accumulator_time - step_size) >= step_size) { accumulator_time -= step_size; - // ==== DELTA TIME LOCKED ==== - camera.update(); - } // ==== FPS LOCKED ==== + camera.update(delta_time); + // Run the raycast error = clEnqueueAcquireGLObjects(c.getCommandQueue(), 1, &image_buff, 0, 0, 0); @@ -320,6 +336,15 @@ int main() { fps.frame(delta_time); fps.draw(&window); + cam_text_x.draw(&window); + cam_text_y.draw(&window); + + cam_text_mov_x.draw(&window); + cam_text_mov_y.draw(&window); + cam_text_mov_z.draw(&window); + + //cam_text_z.draw(&window); + window.display(); }