From 20f36d4eb106a3f5a4ddda5fe5f5f1a01c87600f Mon Sep 17 00:00:00 2001 From: MitchellHansen Date: Wed, 21 Mar 2018 22:31:17 -0700 Subject: [PATCH] lots of little tweaks, the cl is still completely broken, need to revert the oct position calc --- include/Application.h | 8 +-- kernels/ray_caster_kernel.cl | 95 ++++++++++++++++++++---------------- src/Application.cpp | 13 ++--- src/CLCaster.cpp | 9 ++-- src/Camera.cpp | 20 ++++---- src/map/ArrayMap.cpp | 16 +++--- 6 files changed, 85 insertions(+), 76 deletions(-) diff --git a/include/Application.h b/include/Application.h index cfe57f6..d36b31c 100644 --- a/include/Application.h +++ b/include/Application.h @@ -42,10 +42,10 @@ class Application { public: -// 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 WINDOW_X = 1366; +// static const int WINDOW_Y = 768; + static const int WINDOW_X = 200; + static const int WINDOW_Y = 200; static const int MAP_X; static const int MAP_Y; static const int MAP_Z; diff --git a/kernels/ray_caster_kernel.cl b/kernels/ray_caster_kernel.cl index c3eed71..91fac82 100644 --- a/kernels/ray_caster_kernel.cl +++ b/kernels/ray_caster_kernel.cl @@ -124,7 +124,7 @@ struct TraversalState { ulong current_descriptor_index; int3 oct_pos; - + int oct_size; // ====== DEBUG ======= char found; @@ -144,12 +144,14 @@ struct TraversalState get_oct_vox( ts.current_descriptor_index = setting(OCTREE_ROOT_INDEX); ts.current_descriptor = octree_descriptor_buffer[ts.current_descriptor_index]; ts.scale = 0; + ts.oct_size = 0; ts.parent_stack_position = 0; ts.found = false; ts.parent_stack[0] = ts.current_descriptor; // Set our initial dimension and the position at the corner of the oct to keep track of our position int dimension = setting(OCTDIM); + ts.oct_size = dimension/2; ts.oct_pos = zeroed_int3; // While we are not at the required resolution @@ -193,6 +195,8 @@ struct TraversalState get_oct_vox( int mask_index = ts.idx_stack[ts.scale]; + + // Check to see if we are on a valid oct if ((ts.current_descriptor >> 16) & mask_8[mask_index]) { @@ -202,13 +206,13 @@ struct TraversalState get_oct_vox( // If it is, then we cannot traverse further as CP's won't have been generated ts.found = true; return ts; - //return ts.found; } // If all went well and we found a valid non-leaf oct then we will traverse further down the hierarchy ts.scale++; ts.parent_stack_position++; dimension /= 2; + ts.oct_size /= 2; // Count the number of valid octs that come before and add it to the index to get the position // Negate it by one as it counts itself @@ -235,19 +239,14 @@ struct TraversalState get_oct_vox( // If the oct was not valid, then no CP's exists any further // This implicitly says that if it's non-valid then it must be a leaf!! - // It appears that the traversal is now working but I need - // to focus on how to now take care of the end condition. - // Currently it adds the last parent on the second to lowest - // oct CP. Not sure if thats correct + // Parent stack is only populated up to the current descriptors parent. + // So that would be the current voxels grandparent ts.found = 0; return ts; - //return ts.found; } } - ts.found = 1; return ts; - //return ts.found; } // ========================================================================= @@ -300,25 +299,27 @@ __kernel void raycaster( // Setup the voxel coords from the camera origin // rtn = round towards negative int3 voxel = convert_int3_rtn(*cam_pos); + int3 prev_voxel = voxel; - //voxel = voxel + convert_int3(*cam_pos < 0.0f); // Delta T is the units a ray must travel along an axis in order to // traverse an integer split float3 delta_t = fabs(1.0f / ray_dir); // Intersection T is the collection of the next intersection points - // for all 3 axis XYZ. We take the full negative cardinality when - // subtracting the floor, so we must transfer the sign over from - // the voxel step - float3 offset = delta_t * (floor(*cam_pos) - (*cam_pos)); - float3 intersection_t = offset * convert_float3(voxel_step); - - // When we transfer the sign over, we get the correct direction of - // the offset, but we merely transposed over the value instead of mirroring - // it over the axis like we want. So here, isless returns a boolean if intersection_t - // is less than 0 which dictates whether or not we subtract the delta which in effect - // mirrors the offset - intersection_t -= delta_t * convert_float3(isless(intersection_t, 0)); + // for all 3 axis XYZ. We want to 'boost' the intersection_t start point up to + // the offset, so we get the -(difference) between the int voxel position and the + // float camera position. + float3 offset = delta_t * ((*cam_pos) - floor(*cam_pos)); + + // Now we apply the inverse of the ray sign. This gives us a negative + // offset for positive values and vis versa. + float3 intersection_t = offset * -convert_float3(voxel_step); + + // For negative ray directions the positive value is the correct initial offset + // For positive rays we now just have to add the delta_t to the negative offset + // and that will give us the correct positive intersection_t. Don't forget to + // correct the stupid -1==true + intersection_t += delta_t * -1 * convert_float3(isless(intersection_t, 0)); int distance_traveled = 0; int max_distance = 100; @@ -344,7 +345,7 @@ __kernel void raycaster( octree_attachment_buffer, settings_buffer); - int jump_power = (int)pow((float)2, log2((float)vox_dim) - (float)traversal_state.scale); + int jump_power = traversal_state.oct_size; int prev_jump_power = jump_power; int3 last_oct_pos = (0); // TODO: DEBUG @@ -353,13 +354,13 @@ __kernel void raycaster( // Andrew Woo's raycasting algo while (distance_traveled < max_distance && bounce_count < 2) { - if (jump_power == 2){ - color_accumulator = mix((1.0f, 1.0f, 1.0f, 1.0f), (1.0f, 1.0f, 1.0f, 1.0f), 1.0f - max(distance_traveled / 700.0f, 0.0f)); - color_accumulator.w *= 4; - break; - } + // if (jump_power == 2){ + // color_accumulator = mix((1.0f, 1.0f, 1.0f, 1.0f), (1.0f, 1.0f, 1.0f, 1.0f), 1.0f - max(distance_traveled / 700.0f, 0.0f)); + // color_accumulator.w *= 4; + // break; + // } // If we hit a voxel - if (setting(OCTENABLED) == 0 && voxel.x < (*map_dim).x/2 && voxel.y < (*map_dim).x/2 && voxel.z < (*map_dim).x) { + if (setting(OCTENABLED) == 0 && voxel.x < (*map_dim).x && voxel.y < (*map_dim).x && voxel.z < (*map_dim).x) { traversal_state = get_oct_vox( voxel, @@ -368,12 +369,16 @@ __kernel void raycaster( octree_attachment_buffer, settings_buffer); + intersection_t += + convert_float3((traversal_state.oct_pos - voxel.xyz) * traversal_state.oct_size/2 + traversal_state.oct_size/2); + // True will result in a -1, e.g (0, 0, -1) so negate it to positive face_mask = -1 * (intersection_t.xyz <= min(intersection_t.yzx, intersection_t.zxy)); prev_jump_power = jump_power; + prev_voxel = voxel; - voxel.xyz += voxel_step.xyz * jump_power * face_mask.xyz; + voxel.xyz += voxel_step.xyz * face_mask.xyz * convert_int3((traversal_state.oct_pos - voxel.xyz) + traversal_state.oct_size); // Test for out of bounds contions, add fog if (any(voxel >= *map_dim) || any(voxel < 0)){ @@ -418,9 +423,9 @@ __kernel void raycaster( jump_power *= 2; // Keep track of the 0th edge of our current oct - traversal_state.oct_pos.x = floor((float)(voxel.x / 2)) * jump_power; - traversal_state.oct_pos.y = floor((float)(voxel.y / 2)) * jump_power; - traversal_state.oct_pos.z = floor((float)(voxel.z / 2)) * jump_power; + traversal_state.oct_pos.x -= jump_power/2; + traversal_state.oct_pos.y -= jump_power/2; + traversal_state.oct_pos.z -= jump_power/2; // Clear and pop the idx stack traversal_state.idx_stack[traversal_state.scale] = 0; @@ -497,23 +502,23 @@ __kernel void raycaster( // Unlike the single shot DFS, it makes a bit more sense to have this at the tail of the while loop // Do the logic steps to find which sub oct we step down into - if (voxel.x >= (jump_power / 2) + traversal_state.oct_pos.x) { + if (voxel.x >= (jump_power * 2) + traversal_state.oct_pos.x) { // Set our voxel position to the (0,0) of the correct oct - traversal_state.oct_pos.x += (jump_power / 2); + traversal_state.oct_pos.x += (jump_power * 2); // Set the idx to represent the move traversal_state.idx_stack[traversal_state.scale] |= idx_set_x_mask; } - if (voxel.y >= (jump_power / 2) + traversal_state.oct_pos.y) { + if (voxel.y >= (jump_power * 2) + traversal_state.oct_pos.y) { - traversal_state.oct_pos.y += (jump_power / 2); + traversal_state.oct_pos.y += (jump_power * 2); traversal_state.idx_stack[traversal_state.scale] |= idx_set_y_mask; } - if (voxel.z >= (jump_power / 2) + traversal_state.oct_pos.z) { + if (voxel.z >= (jump_power * 2) + traversal_state.oct_pos.z) { - traversal_state.oct_pos.z += (jump_power / 2); + traversal_state.oct_pos.z += (jump_power * 2); traversal_state.idx_stack[traversal_state.scale] |= idx_set_z_mask; } @@ -529,7 +534,6 @@ __kernel void raycaster( // Add the delta for the jump power and the traversed face intersection_t += delta_t * jump_power * fabs(convert_float3(face_mask.xyz)); - // Get the other faces int3 other_faces = select((int3)(1,1,1), (int3)(0,0,0), (int3)(face_mask == 1)); @@ -552,7 +556,12 @@ __kernel void raycaster( break; } //voxel_data = map[voxel.x + (*map_dim).x * (voxel.y + (*map_dim).z * (voxel.z))]; - } else { + } + +// ======================================================================= +// +// ======================================================================= + else { // True will result in a -1, e.g (0, 0, -1) so negate it to positive face_mask = -1 * (intersection_t.xyz <= min(intersection_t.yzx, intersection_t.zxy)); @@ -568,7 +577,9 @@ __kernel void raycaster( } voxel_data = map[voxel.x + (*map_dim).x * (voxel.y + (*map_dim).z * (voxel.z))]; } - +// ======================================================================= +// +// ======================================================================= if (voxel_data == 5 || voxel_data == 6) { diff --git a/src/Application.cpp b/src/Application.cpp index eae5f3a..6699d1e 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -1,8 +1,8 @@ #include "Application.h" -const int Application::MAP_X = 32; -const int Application::MAP_Y = 32; -const int Application::MAP_Z = 32; +const int Application::MAP_X = 16; +const int Application::MAP_Y = 16; +const int Application::MAP_Z = 16; Application::Application() { @@ -50,9 +50,10 @@ bool Application::init_clcaster() { raycaster->assign_octree(map); raycaster->assign_map(map); + camera = std::make_shared( - sf::Vector3f(30.5f, 30.5f, 10.5f), // Starting position - sf::Vector2f(1.50f, -2.0f), // Direction + sf::Vector3f(3.8f, 3.4f, 2.6f), // Starting position + sf::Vector2f(1.56f, .81f), // Direction window.get() ); raycaster->assign_camera(camera); @@ -66,7 +67,7 @@ bool Application::init_clcaster() { // Create a light prototype, send it to the controller, and get the handle back LightPrototype prototype( - sf::Vector3f(30, 30.0f, 30.0f), + sf::Vector3f(10, 10.0f, 10.0f), sf::Vector3f(-1.0f, -1.0f, -1.5f), sf::Vector4f(0.01f, 0.01f, 0.01f, 0.2f) ); diff --git a/src/CLCaster.cpp b/src/CLCaster.cpp index dc334eb..80a8b86 100644 --- a/src/CLCaster.cpp +++ b/src/CLCaster.cpp @@ -239,9 +239,6 @@ bool CLCaster::create_viewport(int width, int height, float v_fov, float h_fov) // And an array of vectors describing the way the "lens" of our // camera works - - // This could be modified to make some odd looking camera lenses - viewport_matrix = new sf::Vector4f[width * height * 4]; for (int y = -view_res.y / 2; y < view_res.y / 2; y++) { @@ -258,9 +255,9 @@ bool CLCaster::create_viewport(int width, int height, float v_fov, float h_fov) static_cast(ray.z * cos(1.57) - ray.x * sin(1.57)) ); - ray.y += (rand() % 1000) / 100000.0; - ray.x += (rand() % 1000) / 100000.0; - ray.z += (rand() % 1000) / 100000.0; +// 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); diff --git a/src/Camera.cpp b/src/Camera.cpp index d82db07..db636f3 100644 --- a/src/Camera.cpp +++ b/src/Camera.cpp @@ -54,9 +54,9 @@ int Camera::add_relative_impulse(DIRECTION impulse_direction, float speed) { } - float val = movement.z; + //float val = movement.z; movement += SphereToCart(dir) * speed; - movement.z = val; + //movement.z = val; return 1; } @@ -125,14 +125,14 @@ int Camera::update(double delta_time) { movement.x *= static_cast(friction_coefficient * delta_time * multiplier); movement.y *= static_cast(friction_coefficient * delta_time * multiplier); - - if (position.z < 3.0f){ - position.z = 3.0f; - movement.z = -0.1; - } else { - // gravity - movement.z -= 0.7f * delta_time; - } + movement.z *= static_cast(friction_coefficient * delta_time * multiplier); +// if (position.z < 3.0f){ +// position.z = 3.0f; +// movement.z = -0.1; +// } else { +// // gravity +// movement.z -= 0.7f * delta_time; +// } return 1; } diff --git a/src/map/ArrayMap.cpp b/src/map/ArrayMap.cpp index eaef2aa..73b3265 100644 --- a/src/map/ArrayMap.cpp +++ b/src/map/ArrayMap.cpp @@ -16,19 +16,19 @@ ArrayMap::ArrayMap(sf::Vector3i dimensions) { for (int x = 0; x < dimensions.x; x++) { for (int y = 0; y < dimensions.y; y++) { - for (int z = 0; z < 2; z++) { + for (int z = 0; z < 1; z++) { setVoxel(sf::Vector3i(x, y, z), 5); } } } - for (int x = 0; x < dimensions.x/2-1; x++) { - for (int y = 0; y < dimensions.y/2-1; y++) { - for (int z = 0; z < dimensions.z/2-1; z++) { - setVoxel(sf::Vector3i(x, y, z), 5); - } - } - } +// for (int x = 0; x < dimensions.x/2-1; x++) { +// for (int y = 0; y < dimensions.y/2-1; y++) { +// for (int z = 0; z < dimensions.z/2-1; z++) { +// setVoxel(sf::Vector3i(x, y, z), 5); +// } +// } +// } }