From 30959128e46f253df63f042e7678bc4949a7d4dc Mon Sep 17 00:00:00 2001 From: MitchellHansen Date: Tue, 21 Mar 2017 22:05:23 -0700 Subject: [PATCH] Hunting down some bugs and verfiying correct oct-tree traversal, not quite there yet but close --- include/Map.h | 59 ++++++++++++++--------- include/raycaster/Hardware_Caster.h | 1 + notes/notes.cpp | 35 +------------- src/Map.cpp | 72 +++++++++++++++++++++-------- src/main.cpp | 11 +++-- src/raycaster/Hardware_Caster.cpp | 3 +- 6 files changed, 102 insertions(+), 79 deletions(-) diff --git a/include/Map.h b/include/Map.h index 6035cdd..6f9ceae 100644 --- a/include/Map.h +++ b/include/Map.h @@ -84,9 +84,10 @@ public: uint8_t idx_set_y_mask = 0x2; uint8_t idx_set_z_mask = 0x4; + // Mask for uint8_t mask_8[8] = { - 0x0, 0x1, 0x2, 0x3, - 0x4, 0x5, 0x6, 0x7 + 0x1, 0x2, 0x4, 0x8, + 0x10, 0x20, 0x40, 0x80 }; uint8_t count_mask_8[8]{ @@ -98,29 +99,38 @@ public: // the IDX and stack position of the highest resolution (maybe set resolution?) oct bool get_voxel(sf::Vector3i position) { - // Init the parent stack and push the head node - //std::queue parent_stack; - + // Init the parent stack int parent_stack_position = 0; uint64_t parent_stack[32] = {0}; + // and push the head node uint64_t head = block_stack.front()[stack_pos]; parent_stack[parent_stack_position] = head; - // Get the index of the first child of the head node uint64_t index = head & child_pointer_mask; + // Init the idx stack uint8_t scale = 0; uint8_t idx_stack[32] = {0}; - // Init the idx stack + // Init the idx stack (DEBUG) std::vector> scale_stack(static_cast(log2(OCT_DIM))); - // Set our initial dimension and the position we use to keep track what oct were in + // Set our initial dimension and the position at the corner of the oct to keep track of our position int dimension = OCT_DIM; sf::Vector3i quad_position(0, 0, 0); + // While we are not at the required resolution + // Traverse down by setting the valid/leaf mask to the subvoxel + // Check to see if it is valid + // Yes? + // Check to see if it is a leaf + // No? Break + // Yes? Scale down to the next hierarchy, push the parent to the stack + // + // No? + // Break while (dimension > 1) { // So we can be a little bit tricky here and increment our @@ -159,11 +169,14 @@ public: quad_position.z += (dimension / 2); mask_index += 4; - + idx_stack[scale] |= idx_set_z_mask; scale_stack.at(static_cast(log2(OCT_DIM) - log2(dimension))).set(2); } + uint64_t out1 = (head >> 16) & mask_8[mask_index]; + uint64_t out2 = (head >> 24) & mask_8[mask_index]; + // Check to see if we are on a valid oct if ((head >> 16) & mask_8[mask_index]) { @@ -171,6 +184,7 @@ public: if ((head >> 24) & mask_8[mask_index]) { // If it is, then we cannot traverse further as CP's won't have been generated + return true; break; } @@ -180,10 +194,14 @@ public: // We also need to traverse to the correct child pointer - // Count the number of non-leaf octs that come before and add it to the current parent stack position + // Count the number of non-leaf octs that come before and add it to the index to get the position int count = count_bits((uint8_t)(head >> 24) ^ count_mask_8[mask_index]); - int index = static_cast((parent_stack[parent_stack_position] & child_pointer_mask) + count); - + + // Because we are getting the position at the first child we need to back up one + // Or maybe it's because my count bits function is wrong... + index = (head & child_pointer_mask) + count - 1; + head = block_stack.front()[index]; + // Increment the parent stack position and put the new oct node as the parent parent_stack_position++; parent_stack[parent_stack_position] = block_stack.front()[index]; @@ -196,6 +214,7 @@ public: // 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 + return false; break; } } @@ -226,17 +245,14 @@ private: const uint64_t contour_pointer_mask = 0xFFFFFF00000000; const uint64_t contour_mask = 0xFF00000000000000; - - - }; class Map { public: - - Map(sf::Vector3i dim); + + Map(sf::Vector3i position); void generate_octree(); void load_unload(sf::Vector3i world_position); @@ -250,10 +266,12 @@ public: char getVoxelFromOctree(sf::Vector3i position); - void moveLight(sf::Vector2f in); + bool getVoxel(sf::Vector3i pos); + Octree a; + sf::Vector3f global_light; - Octree a; + void test_map(); protected: @@ -261,7 +279,7 @@ private: // DEBUG int counter = 0; - std::stringstream ss; + std::stringstream output_stream; // !DEBUG @@ -269,7 +287,6 @@ private: uint64_t generate_children(sf::Vector3i pos, int dim); - char getVoxel(sf::Vector3i pos); char* voxel_data = new char[OCT_DIM * OCT_DIM * OCT_DIM]; //std::unordered_map chunk_map; diff --git a/include/raycaster/Hardware_Caster.h b/include/raycaster/Hardware_Caster.h index e3073fe..e0c96c6 100644 --- a/include/raycaster/Hardware_Caster.h +++ b/include/raycaster/Hardware_Caster.h @@ -38,6 +38,7 @@ struct device { cl_uint comp_units; char extensions[1024]; char name[256]; + cl_bool is_little_endian = false; bool cl_gl_sharing = false; }; diff --git a/notes/notes.cpp b/notes/notes.cpp index 395fdb4..1e41569 100644 --- a/notes/notes.cpp +++ b/notes/notes.cpp @@ -2,40 +2,9 @@ /* -OpenCL: - - Add phong lighting / fix the current implementation - - Switch to switch lighting models - - Separate out into a part of the rendering module - -Map: - - Implement the new octree structure - - storing the pre-octree volumetric data - - determining when to load volumetric data into the in-memory structure - - building the octree from that raw volumetric data - - combining with other octree nodes to allow streaming of leafs - - passing that data into the renderer - - renderer needs to then traverse the octree - - Terrain generation for real this time - - Loader of 3rd party voxel data - -Renderer: - - Determine when to switch between the cpu and gpu rendering - - call to the map to make sure that the gpu/cpu has an up to date copy - of the volumetric data - - - - - - - - - - Build: - Z:\Cpp_Libs\SFML-Visual_Studio2015RCx64 + Z:\Cpp_Libs\SFML-2.4.2 Z:/Cpp_Libs/glew-2.0.0/lib/Release/x64/glew32s.lib Z:/Cpp_Libs/glew-2.0.0/include - -*/ +*/ diff --git a/src/Map.cpp b/src/Map.cpp index 4d2ed07..26e70b5 100644 --- a/src/Map.cpp +++ b/src/Map.cpp @@ -1,7 +1,5 @@ #include "Map.h" - - void SetBit(int position, char* c) { *c |= (uint64_t)1 << position; } @@ -70,33 +68,38 @@ bool IsLeaf(const uint64_t descriptor) { Map::Map(sf::Vector3i position) { + srand(time(NULL)); + load_unload(position); for (int i = 0; i < OCT_DIM * OCT_DIM * OCT_DIM; i++) { - if (rand() % 8 > 2) + if (rand() % 2 == 1) voxel_data[i] = 0; else voxel_data[i] = 1; } + + voxel_data[0 + OCT_DIM * (0 + OCT_DIM * 0)] = 1; + } -uint64_t Map::generate_children(sf::Vector3i pos, int dim) { +uint64_t Map::generate_children(sf::Vector3i pos, int voxel_scale) { // The 8 subvoxel coords starting from the 1th direction, the direction of the origin of the 3d grid // XY, Z++, XY std::vector v = { sf::Vector3i(pos.x , pos.y , pos.z), - sf::Vector3i(pos.x + dim, pos.y , pos.z), - sf::Vector3i(pos.x , pos.y + dim, pos.z), - sf::Vector3i(pos.x + dim, pos.y + dim, pos.z), - sf::Vector3i(pos.x , pos.y , pos.z + dim), - sf::Vector3i(pos.x + dim, pos.y , pos.z + dim), - sf::Vector3i(pos.x , pos.y + dim, pos.z + dim), - sf::Vector3i(pos.x + dim, pos.y + dim, pos.z + dim) + sf::Vector3i(pos.x + voxel_scale, pos.y , pos.z), + sf::Vector3i(pos.x , pos.y + voxel_scale, pos.z), + sf::Vector3i(pos.x + voxel_scale, pos.y + voxel_scale, pos.z), + sf::Vector3i(pos.x , pos.y , pos.z + voxel_scale), + sf::Vector3i(pos.x + voxel_scale, pos.y , pos.z + voxel_scale), + sf::Vector3i(pos.x , pos.y + voxel_scale, pos.z + voxel_scale), + sf::Vector3i(pos.x + voxel_scale, pos.y + voxel_scale, pos.z + voxel_scale) }; - if (dim == 1) { + if (voxel_scale == 1) { // Return the base 2x2 leaf node uint64_t tmp = 0; @@ -126,10 +129,11 @@ uint64_t Map::generate_children(sf::Vector3i pos, int dim) { for (int i = 0; i < v.size(); i++) { // Get the child descriptor from the i'th to 8th subvoxel - child = generate_children(v.at(i), dim / 2); + child = generate_children(v.at(i), voxel_scale / 2); - PrettyPrintUINT64(child, &ss); - ss << " " << dim << " " << counter++ << std::endl; + // + PrettyPrintUINT64(child, &output_stream); + output_stream << " " << voxel_scale << " " << counter++ << std::endl; if (IsLeaf(child)) { if (CheckLeafSign(child)) @@ -168,8 +172,8 @@ void Map::generate_octree() { uint64_t root_node = generate_children(sf::Vector3i(0, 0, 0), OCT_DIM/2); uint64_t tmp = 0; - PrettyPrintUINT64(root_node, &ss); - ss << " " << OCT_DIM << " " << counter++ << std::endl; + PrettyPrintUINT64(root_node, &output_stream); + output_stream << " " << OCT_DIM << " " << counter++ << std::endl; if (IsLeaf(root_node)) { if (CheckLeafSign(root_node)) @@ -185,7 +189,7 @@ void Map::generate_octree() { tmp |= a.copy_to_stack(std::vector{root_node}); - DumpLog(&ss, "raw_output.txt"); + DumpLog(&output_stream, "raw_output.txt"); a.print_block(0); @@ -246,7 +250,35 @@ char Map::getVoxelFromOctree(sf::Vector3i position) return a.get_voxel(position); } -char Map::getVoxel(sf::Vector3i pos){ +bool Map::getVoxel(sf::Vector3i pos){ + + if (voxel_data[pos.x + OCT_DIM * (pos.y + OCT_DIM * pos.z)]) { + return true; + } else { + return false; + } +} + +void Map::test_map() { + + for (int x = 0; x < OCT_DIM; x++) { + for (int y = 0; y < OCT_DIM; y++) { + for (int z = 0; z < OCT_DIM; z++) { + + sf::Vector3i pos(x, y, z); + + bool arr1 = getVoxel(pos); + bool arr2 = getVoxelFromOctree(pos); + + if (arr1 != arr2) { + std::cout << "MISMATCH" << std::endl; + } + + } + } + } + + + std::cout << "\nGOOD" << std::endl; - return voxel_data[pos.x + OCT_DIM * (pos.y + OCT_DIM * pos.z)]; } diff --git a/src/main.cpp b/src/main.cpp index 115df99..fc451fb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -93,10 +93,13 @@ int main() { // ni.stop_listening_for_clients(); // ============================= - // Map _map(sf::Vector3i(0, 0, 0)); - // _map.generate_octree(); - // _map.a.get_voxel(sf::Vector3i(5, 5, 0)); - // return 0; + Map _map(sf::Vector3i(0, 0, 0)); + _map.generate_octree(); + std::cout << _map.a.get_voxel(sf::Vector3i(5, 5, 0)); + std::cout << _map.getVoxel(sf::Vector3i(5, 5, 0)); + _map.test_map(); + std::cin.get(); + return 0; // ============================= sf::RenderWindow window(sf::VideoMode(WINDOW_X, WINDOW_Y), "SFML"); diff --git a/src/raycaster/Hardware_Caster.cpp b/src/raycaster/Hardware_Caster.cpp index 81e997e..51d0538 100644 --- a/src/raycaster/Hardware_Caster.cpp +++ b/src/raycaster/Hardware_Caster.cpp @@ -11,7 +11,6 @@ Hardware_Caster::~Hardware_Caster() { int Hardware_Caster::init() { // Initialize opencl up to the point where we start assigning buffers - error = acquire_platform_and_device(); if(vr_assert(error, "aquire_platform_and_device")) return error; @@ -316,6 +315,7 @@ int Hardware_Caster::acquire_platform_and_device() { clGetDeviceInfo(d.id, CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(cl_uint), &d.comp_units, NULL); clGetDeviceInfo(d.id, CL_DEVICE_EXTENSIONS, 1024, &d.extensions, NULL); clGetDeviceInfo(d.id, CL_DEVICE_NAME, 256, &d.name, NULL); + clGetDeviceInfo(d.id, CL_DEVICE_ENDIAN_LITTLE, sizeof(cl_bool), &d.is_little_endian, NULL); std::cout << "Device: " << q << std::endl; std::cout << "Device Name : " << d.name << std::endl; @@ -335,6 +335,7 @@ int Hardware_Caster::acquire_platform_and_device() { std::cout << "Max clock frequency : " << d.clock_frequency << std::endl; std::cout << "Max compute units : " << d.comp_units << std::endl; + std::cout << "Is little endian : " << std::boolalpha << static_cast(d.is_little_endian) << std::endl; std::cout << "cl_khr_gl_sharing supported: "; if (std::string(d.extensions).find("cl_khr_gl_sharing") == std::string::npos &&