From c698711fdf3f448516b1082dc437cb0bd0ce14ac Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Wed, 7 Feb 2018 23:58:48 -0800 Subject: [PATCH] Finally converted the camera lens to a proper frustrum, no more fish eye --- include/Application.h | 7 ++-- include/CLCaster.h | 4 ++- include/util.hpp | 2 +- kernels/ray_caster_kernel.cl | 21 ++++------- src/Application.cpp | 4 +-- src/CLCaster.cpp | 67 +++++++++++++++++++++--------------- src/map/ArrayMap.cpp | 2 +- src/map/Octree.cpp | 4 ++- 8 files changed, 59 insertions(+), 52 deletions(-) diff --git a/include/Application.h b/include/Application.h index ffa0841..93be54e 100644 --- a/include/Application.h +++ b/include/Application.h @@ -41,9 +41,10 @@ class CLCaster; class Application { public: - static const int WINDOW_X = 1600; - static const int WINDOW_Y = 900; - + static const int WINDOW_X = 1366; + static const int WINDOW_Y = 768; +// static const int WINDOW_X = 1920; +// static const int WINDOW_Y = 1080; static const int MAP_X; static const int MAP_Y; static const int MAP_Z; diff --git a/include/CLCaster.h b/include/CLCaster.h index cefa050..92c38ad 100644 --- a/include/CLCaster.h +++ b/include/CLCaster.h @@ -113,11 +113,13 @@ public: // Creates a texture to send to the GPU via height and width // Creates a viewport vector array via vertical and horizontal fov bool create_viewport(int width, int height, float v_fov, float h_fov) ; - + bool release_viewport(); + // Light controllers own the copy of the PackedData array. // We receive a pointer to the array and USE_HOST_POINTER to map the memory to the GPU bool assign_lights(std::vector *data) ; + // TODO: Double maps?? // We take a ptr to the map and create the map, and map_dimensions buffer for the GPU bool assign_map(std::shared_ptr map); bool release_map(); diff --git a/include/util.hpp b/include/util.hpp index ee15d67..18fc2a2 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -11,6 +11,7 @@ #include #include #include +#include "Vector4.hpp" const double PI = 3.141592653589793238463; const float PI_F = 3.14159265358979f; @@ -108,7 +109,6 @@ inline sf::Vector3f Normalize(sf::Vector3f in) { return r; } - inline float DotProduct(sf::Vector3f a, sf::Vector3f b){ return a.x * b.x + a.y * b.y + a.z * b.z; } diff --git a/kernels/ray_caster_kernel.cl b/kernels/ray_caster_kernel.cl index c5ef6ee..1a5fdda 100644 --- a/kernels/ray_caster_kernel.cl +++ b/kernels/ray_caster_kernel.cl @@ -16,7 +16,7 @@ __constant int2 zeroed_int2 = {0, 0}; __constant const uchar idx_set_x_mask = 0x1; __constant const uchar idx_set_y_mask = 0x2; __constant const uchar idx_set_z_mask = 0x4; -__constant const uchar idx_set_mask = {0x1, 0x2, 0x4}; +__constant const uchar3 idx_set_mask = {0x1, 0x2, 0x4}; __constant const uchar mask_8[8] = { 0x1, 0x2, 0x4, 0x8, @@ -50,15 +50,6 @@ constant float4 overshoot_color_2 = { 0.00f, 0.00f, 0.00f, 0.00f }; // ========================================================================= // ========================= HELPER FUNCTIONS ============================== -float DistanceBetweenPoints(float3 a, float3 b) { - return fast_distance(a, b); -} - -float Distance(float3 a) { - return fast_length(a); -} - - // Phong + diffuse lighting function for g // 0 1 2 3 4 5 6 7 8 9 // {r, g, b, i, x, y, z, x', y', z'} @@ -68,7 +59,7 @@ float4 view_light(float4 in_color, float3 light, float4 light_color, float3 view if (all(light == zeroed_float3)) return zeroed_float4; - float d = Distance(light) / 120.0f; + float d = fast_length(light) * 0.01f; d *= d; float diffuse = max(dot(normalize(convert_float3(mask)), normalize(light)), 0.1f); @@ -474,8 +465,8 @@ __kernel void raycaster( constant int vox_dim = OCTDIM; - // If we hit a voxel - if (voxel.x < vox_dim && voxel.y < vox_dim && voxel.z < vox_dim){ +// If we hit a voxel + if (voxel.x < (*map_dim).x && voxel.y < (*map_dim).x && voxel.z < (*map_dim).x){ if (get_oct_vox( voxel, octree_descriptor_buffer, @@ -570,7 +561,7 @@ __kernel void raycaster( voxel_color.xyz += (float3)read_imagef( texture_atlas, convert_int2(tile_face_position * convert_float2(*atlas_dim / *tile_dim)) + - convert_int2((float2)(3, 0) * convert_float2(*atlas_dim / *tile_dim)) + convert_int2((float2)(5, 0) * convert_float2(*atlas_dim / *tile_dim)) ).xyz/2; color_accumulator = view_light( @@ -582,7 +573,7 @@ __kernel void raycaster( ); fog_distance = distance_traveled; - max_distance = distance_traveled + DistanceBetweenPoints(convert_float3(voxel), (float3)(lights[4], lights[5], lights[6])); + max_distance = distance_traveled + fast_distance(convert_float3(voxel), (float3)(lights[4], lights[5], lights[6])); float3 hit_pos = convert_float3(voxel) + face_position; ray_dir = normalize((float3)(lights[4], lights[5], lights[6]) - hit_pos); diff --git a/src/Application.cpp b/src/Application.cpp index 18a742b..2d84745 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -40,8 +40,8 @@ bool Application::init_clcaster() { // Create a new camera with (starting position, direction) camera = std::make_shared( - sf::Vector3f(0.5f, 0.5f, 0.5f), - sf::Vector2f(1.45f, 0.3f), + sf::Vector3f(3.5f, 3.5f, 3.5f), + sf::Vector2f(1.57f, 0.0f), window.get() ); diff --git a/src/CLCaster.cpp b/src/CLCaster.cpp index a5d16b8..72e623f 100644 --- a/src/CLCaster.cpp +++ b/src/CLCaster.cpp @@ -2,8 +2,18 @@ CLCaster::CLCaster() {} CLCaster::~CLCaster() { + + // Causes sigabrt?? + //release_map(); + //release_camera(); + //release_octree(); + release_viewport(); + delete[] viewport_matrix; delete[] viewport_image; + + camera.reset(); + map.reset(); } bool CLCaster::init() { @@ -173,7 +183,7 @@ bool CLCaster::validate() { Logger::log("Raycaster.validate() failed, viewport_matrix not initialized", Logger::LogLevel::WARN); return false; } - + // Set all the kernel args set_kernel_arg("raycaster", 0, "map"); set_kernel_arg("raycaster", 1, "map_dimensions"); @@ -222,7 +232,6 @@ bool CLCaster::compute() { // There is a possibility that I would want to move this over to be all inside it's own // container to make it so it can be changed via CL_MEM_USE_HOST_PTR. But I doubt it // would ever be called enough to warrant that -// TODO : Move CL interaction into the CLCaster? bool CLCaster::create_viewport(int width, int height, float v_fov, float h_fov) { // CL needs the screen resolution @@ -235,46 +244,34 @@ bool CLCaster::create_viewport(int width, int height, float v_fov, float h_fov) // This could be modified to make some odd looking camera lenses - double y_increment_radians = DegreesToRadians(v_fov / view_res.y); - double x_increment_radians = DegreesToRadians(h_fov / view_res.x); - viewport_matrix = new sf::Vector4f[width * height * 4]; for (int y = -view_res.y / 2; y < view_res.y / 2; y++) { + for (int x = -view_res.x / 2; x < view_res.x / 2; x++) { // The base ray direction to slew from - sf::Vector3f ray(1, 0, 0); - - // Y axis, pitch - ray = sf::Vector3f( - static_cast(ray.z * sin(y_increment_radians * y) + ray.x * cos(y_increment_radians * y)), - static_cast(ray.y), - static_cast(ray.z * cos(y_increment_radians * y) - ray.x * sin(y_increment_radians * y)) - ); - - // Z axis, yaw - ray = sf::Vector3f( - static_cast(ray.x * cos(x_increment_radians * x) - ray.y * sin(x_increment_radians * x)), - static_cast(ray.x * sin(x_increment_radians * x) + ray.y * cos(x_increment_radians * x)), - static_cast(ray.z) - ); + sf::Vector3f ray(-800, x, y); // correct for the base ray pointing to (1, 0, 0) as (0, 0). Should equal (1.57, 0) ray = sf::Vector3f( - static_cast(ray.z * sin(-1.57) + ray.x * cos(-1.57)), + static_cast(ray.z * sin(1.57) + ray.x * cos(1.57)), static_cast(ray.y), - static_cast(ray.z * cos(-1.57) - ray.x * sin(-1.57)) + static_cast(ray.z * cos(1.57) - ray.x * sin(1.57)) ); - int index = (x + view_res.x / 2) + view_res.x * (y + view_res.y / 2); + ray.y += (rand() % 1000) / 100000.0; + ray.x += (rand() % 1000) / 100000.0; + ray.z += (rand() % 1000) / 100000.0; + ray = Normalize(ray); + int index = (x + view_res.x / 2) + view_res.x * (y + view_res.y / 2); viewport_matrix[index] = sf::Vector4f( - ray.x, - ray.y, - ray.z, - 0 + ray.x, + ray.y, + ray.z, + 0 ); } } @@ -306,6 +303,18 @@ bool CLCaster::create_viewport(int width, int height, float v_fov, float h_fov) } +bool CLCaster::release_viewport() { + + bool success = true; + if (!release_buffer("viewport_resolution")) + success = false; + if (!release_buffer("viewport_matrix")) + success = false; + if (!release_buffer("image")) + success = false; + return success; +} + bool CLCaster::assign_lights(std::vector *data) { // Get a pointer to the packed light data @@ -854,7 +863,8 @@ bool CLCaster::create_buffer(std::string buffer_name, cl_uint size, void* data) bool CLCaster::release_buffer(std::string buffer_name) { if (buffer_map.count(buffer_name) > 0) { - + + clFinish(command_queue); int error = clReleaseMemObject(buffer_map.at(buffer_name)); if (cl_assert(error)) { @@ -1160,6 +1170,7 @@ std::string CLCaster::cl_err_lookup(int error_code) { } + CLCaster::device::device(cl_device_id device_id, cl_platform_id platform_id) { this->device_id = device_id; diff --git a/src/map/ArrayMap.cpp b/src/map/ArrayMap.cpp index ba0374b..2755112 100644 --- a/src/map/ArrayMap.cpp +++ b/src/map/ArrayMap.cpp @@ -16,7 +16,7 @@ ArrayMap::ArrayMap(sf::Vector3i dimensions) { for (int x = 0; x < dimensions.x; x++) { for (int y = 0; y < dimensions.y; y++) { - setVoxel(sf::Vector3i(x, y, 0), 1); + setVoxel(sf::Vector3i(x, y, 0), 5); } } } diff --git a/src/map/Octree.cpp b/src/map/Octree.cpp index 857403c..302571d 100644 --- a/src/map/Octree.cpp +++ b/src/map/Octree.cpp @@ -336,11 +336,13 @@ bool Octree::Validate(char* data, sf::Vector3i dimensions){ char arr_val = get1DIndexedVoxel(data, dimensions, pos); char oct_val = GetVoxel(pos).found; - if (arr_val != oct_val) { + + if (arr_val != oct_val && (oct_val == 0 || arr_val == 0)) { std::cout << "X: " << pos.x << " Y: " << pos.y << " Z: " << pos.z << " "; std::cout << (int)arr_val << " : " << (int)oct_val << std::endl; //return false; } + } } }