lots of little tweaks, the cl is still completely broken, need to revert the oct position calc

master
MitchellHansen 7 years ago
parent 8a1eb5e430
commit 20f36d4eb1

@ -42,10 +42,10 @@ class Application {
public: public:
// static const int WINDOW_X = 1366; // static const int WINDOW_X = 1366;
// static const int WINDOW_Y = 768; // static const int WINDOW_Y = 768;
static const int WINDOW_X = 1920; static const int WINDOW_X = 200;
static const int WINDOW_Y = 1080; static const int WINDOW_Y = 200;
static const int MAP_X; static const int MAP_X;
static const int MAP_Y; static const int MAP_Y;
static const int MAP_Z; static const int MAP_Z;

@ -124,7 +124,7 @@ struct TraversalState {
ulong current_descriptor_index; ulong current_descriptor_index;
int3 oct_pos; int3 oct_pos;
int oct_size;
// ====== DEBUG ======= // ====== DEBUG =======
char found; char found;
@ -144,12 +144,14 @@ struct TraversalState get_oct_vox(
ts.current_descriptor_index = setting(OCTREE_ROOT_INDEX); ts.current_descriptor_index = setting(OCTREE_ROOT_INDEX);
ts.current_descriptor = octree_descriptor_buffer[ts.current_descriptor_index]; ts.current_descriptor = octree_descriptor_buffer[ts.current_descriptor_index];
ts.scale = 0; ts.scale = 0;
ts.oct_size = 0;
ts.parent_stack_position = 0; ts.parent_stack_position = 0;
ts.found = false; ts.found = false;
ts.parent_stack[0] = ts.current_descriptor; 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 // Set our initial dimension and the position at the corner of the oct to keep track of our position
int dimension = setting(OCTDIM); int dimension = setting(OCTDIM);
ts.oct_size = dimension/2;
ts.oct_pos = zeroed_int3; ts.oct_pos = zeroed_int3;
// While we are not at the required resolution // 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]; int mask_index = ts.idx_stack[ts.scale];
// Check to see if we are on a valid oct // Check to see if we are on a valid oct
if ((ts.current_descriptor >> 16) & mask_8[mask_index]) { 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 // If it is, then we cannot traverse further as CP's won't have been generated
ts.found = true; ts.found = true;
return ts; 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 // If all went well and we found a valid non-leaf oct then we will traverse further down the hierarchy
ts.scale++; ts.scale++;
ts.parent_stack_position++; ts.parent_stack_position++;
dimension /= 2; 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 // 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 // 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 // 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!! // 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 // Parent stack is only populated up to the current descriptors parent.
// to focus on how to now take care of the end condition. // So that would be the current voxels grandparent
// Currently it adds the last parent on the second to lowest
// oct CP. Not sure if thats correct
ts.found = 0; ts.found = 0;
return ts; return ts;
//return ts.found;
} }
} }
ts.found = 1; ts.found = 1;
return ts; return ts;
//return ts.found;
} }
// ========================================================================= // =========================================================================
@ -300,25 +299,27 @@ __kernel void raycaster(
// Setup the voxel coords from the camera origin // Setup the voxel coords from the camera origin
// rtn = round towards negative // rtn = round towards negative
int3 voxel = convert_int3_rtn(*cam_pos); 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 // Delta T is the units a ray must travel along an axis in order to
// traverse an integer split // traverse an integer split
float3 delta_t = fabs(1.0f / ray_dir); float3 delta_t = fabs(1.0f / ray_dir);
// Intersection T is the collection of the next intersection points // Intersection T is the collection of the next intersection points
// for all 3 axis XYZ. We take the full negative cardinality when // for all 3 axis XYZ. We want to 'boost' the intersection_t start point up to
// subtracting the floor, so we must transfer the sign over from // the offset, so we get the -(difference) between the int voxel position and the
// the voxel step // float camera position.
float3 offset = delta_t * (floor(*cam_pos) - (*cam_pos)); float3 offset = delta_t * ((*cam_pos) - floor(*cam_pos));
float3 intersection_t = offset * convert_float3(voxel_step);
// Now we apply the inverse of the ray sign. This gives us a negative
// When we transfer the sign over, we get the correct direction of // offset for positive values and vis versa.
// the offset, but we merely transposed over the value instead of mirroring float3 intersection_t = offset * -convert_float3(voxel_step);
// 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 // For negative ray directions the positive value is the correct initial offset
// mirrors the offset // For positive rays we now just have to add the delta_t to the negative offset
intersection_t -= delta_t * convert_float3(isless(intersection_t, 0)); // 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 distance_traveled = 0;
int max_distance = 100; int max_distance = 100;
@ -344,7 +345,7 @@ __kernel void raycaster(
octree_attachment_buffer, octree_attachment_buffer,
settings_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; int prev_jump_power = jump_power;
int3 last_oct_pos = (0); int3 last_oct_pos = (0);
// TODO: DEBUG // TODO: DEBUG
@ -353,13 +354,13 @@ __kernel void raycaster(
// Andrew Woo's raycasting algo // Andrew Woo's raycasting algo
while (distance_traveled < max_distance && bounce_count < 2) { while (distance_traveled < max_distance && bounce_count < 2) {
if (jump_power == 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 = 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; // color_accumulator.w *= 4;
break; // break;
} // }
// If we hit a voxel // 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( traversal_state = get_oct_vox(
voxel, voxel,
@ -368,12 +369,16 @@ __kernel void raycaster(
octree_attachment_buffer, octree_attachment_buffer,
settings_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 // 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)); face_mask = -1 * (intersection_t.xyz <= min(intersection_t.yzx, intersection_t.zxy));
prev_jump_power = jump_power; 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 // Test for out of bounds contions, add fog
if (any(voxel >= *map_dim) || any(voxel < 0)){ if (any(voxel >= *map_dim) || any(voxel < 0)){
@ -418,9 +423,9 @@ __kernel void raycaster(
jump_power *= 2; jump_power *= 2;
// Keep track of the 0th edge of our current oct // 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.x -= jump_power/2;
traversal_state.oct_pos.y = floor((float)(voxel.y / 2)) * jump_power; traversal_state.oct_pos.y -= jump_power/2;
traversal_state.oct_pos.z = floor((float)(voxel.z / 2)) * jump_power; traversal_state.oct_pos.z -= jump_power/2;
// Clear and pop the idx stack // Clear and pop the idx stack
traversal_state.idx_stack[traversal_state.scale] = 0; 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 // 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 // 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 // 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 // Set the idx to represent the move
traversal_state.idx_stack[traversal_state.scale] |= idx_set_x_mask; 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; 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; 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 // Add the delta for the jump power and the traversed face
intersection_t += delta_t * jump_power * fabs(convert_float3(face_mask.xyz)); intersection_t += delta_t * jump_power * fabs(convert_float3(face_mask.xyz));
// Get the other faces // Get the other faces
int3 other_faces = select((int3)(1,1,1), (int3)(0,0,0), (int3)(face_mask == 1)); int3 other_faces = select((int3)(1,1,1), (int3)(0,0,0), (int3)(face_mask == 1));
@ -552,7 +556,12 @@ __kernel void raycaster(
break; break;
} }
//voxel_data = map[voxel.x + (*map_dim).x * (voxel.y + (*map_dim).z * (voxel.z))]; //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 // 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)); 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))]; voxel_data = map[voxel.x + (*map_dim).x * (voxel.y + (*map_dim).z * (voxel.z))];
} }
// =======================================================================
//
// =======================================================================
if (voxel_data == 5 || voxel_data == 6) { if (voxel_data == 5 || voxel_data == 6) {

@ -1,8 +1,8 @@
#include "Application.h" #include "Application.h"
const int Application::MAP_X = 32; const int Application::MAP_X = 16;
const int Application::MAP_Y = 32; const int Application::MAP_Y = 16;
const int Application::MAP_Z = 32; const int Application::MAP_Z = 16;
Application::Application() { Application::Application() {
@ -50,9 +50,10 @@ bool Application::init_clcaster() {
raycaster->assign_octree(map); raycaster->assign_octree(map);
raycaster->assign_map(map); raycaster->assign_map(map);
camera = std::make_shared<Camera>( camera = std::make_shared<Camera>(
sf::Vector3f(30.5f, 30.5f, 10.5f), // Starting position sf::Vector3f(3.8f, 3.4f, 2.6f), // Starting position
sf::Vector2f(1.50f, -2.0f), // Direction sf::Vector2f(1.56f, .81f), // Direction
window.get() window.get()
); );
raycaster->assign_camera(camera); 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 // Create a light prototype, send it to the controller, and get the handle back
LightPrototype prototype( 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::Vector3f(-1.0f, -1.0f, -1.5f),
sf::Vector4f(0.01f, 0.01f, 0.01f, 0.2f) sf::Vector4f(0.01f, 0.01f, 0.01f, 0.2f)
); );

@ -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 // And an array of vectors describing the way the "lens" of our
// camera works // camera works
// This could be modified to make some odd looking camera lenses
viewport_matrix = new sf::Vector4f[width * height * 4]; viewport_matrix = new sf::Vector4f[width * height * 4];
for (int y = -view_res.y / 2; y < view_res.y / 2; y++) { 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<float>(ray.z * cos(1.57) - ray.x * sin(1.57)) static_cast<float>(ray.z * cos(1.57) - ray.x * sin(1.57))
); );
ray.y += (rand() % 1000) / 100000.0; // ray.y += (rand() % 1000) / 100000.0;
ray.x += (rand() % 1000) / 100000.0; // ray.x += (rand() % 1000) / 100000.0;
ray.z += (rand() % 1000) / 100000.0; // ray.z += (rand() % 1000) / 100000.0;
ray = Normalize(ray); ray = Normalize(ray);
int index = (x + view_res.x / 2) + view_res.x * (y + view_res.y / 2); int index = (x + view_res.x / 2) + view_res.x * (y + view_res.y / 2);

@ -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 += SphereToCart(dir) * speed;
movement.z = val; //movement.z = val;
return 1; return 1;
} }
@ -125,14 +125,14 @@ int Camera::update(double delta_time) {
movement.x *= static_cast<float>(friction_coefficient * delta_time * multiplier); movement.x *= static_cast<float>(friction_coefficient * delta_time * multiplier);
movement.y *= static_cast<float>(friction_coefficient * delta_time * multiplier); movement.y *= static_cast<float>(friction_coefficient * delta_time * multiplier);
movement.z *= static_cast<float>(friction_coefficient * delta_time * multiplier);
if (position.z < 3.0f){ // if (position.z < 3.0f){
position.z = 3.0f; // position.z = 3.0f;
movement.z = -0.1; // movement.z = -0.1;
} else { // } else {
// gravity // // gravity
movement.z -= 0.7f * delta_time; // movement.z -= 0.7f * delta_time;
} // }
return 1; return 1;
} }

@ -16,19 +16,19 @@ ArrayMap::ArrayMap(sf::Vector3i dimensions) {
for (int x = 0; x < dimensions.x; x++) { for (int x = 0; x < dimensions.x; x++) {
for (int y = 0; y < dimensions.y; y++) { 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); setVoxel(sf::Vector3i(x, y, z), 5);
} }
} }
} }
for (int x = 0; x < dimensions.x/2-1; x++) { // for (int x = 0; x < dimensions.x/2-1; x++) {
for (int y = 0; y < dimensions.y/2-1; y++) { // for (int y = 0; y < dimensions.y/2-1; y++) {
for (int z = 0; z < dimensions.z/2-1; z++) { // for (int z = 0; z < dimensions.z/2-1; z++) {
setVoxel(sf::Vector3i(x, y, z), 5); // setVoxel(sf::Vector3i(x, y, z), 5);
} // }
} // }
} // }
} }

Loading…
Cancel
Save