From e45df185f76295b718990b8c5c4ddf2368963eaf Mon Sep 17 00:00:00 2001 From: MitchellHansen Date: Wed, 22 Mar 2017 22:50:17 -0700 Subject: [PATCH] Cleaned up Map and the Octree. Did some testing and refactoring of generation code. Interleaved data is now good, also changed the block stack dealio to just a blob of uint64_t data. Used a GCC and by extension MSVC extension which speeds up count_bits by a good bit. After all optimizations, getVoxel is now around 10-15 times faster. --- include/Map.h | 243 +++-------------------------- include/util.hpp | 35 +++-- src/Map.cpp | 395 +++++++++++++++++++++++++++++++---------------- src/Ray.cpp | 14 +- src/main.cpp | 4 +- 5 files changed, 317 insertions(+), 374 deletions(-) diff --git a/include/Map.h b/include/Map.h index 445414e..5626be2 100644 --- a/include/Map.h +++ b/include/Map.h @@ -19,7 +19,7 @@ #include #define CHUNK_DIM 32 -#define OCT_DIM 8 +#define OCT_DIM 32 struct XYZHasher { std::size_t operator()(const sf::Vector3i& k) const { @@ -31,228 +31,40 @@ struct XYZHasher { class Octree { public: - Octree() { - - // initialize the first stack block - block_stack.push_back(new uint64_t[0x8000]); - for (int i = 0; i < 0x8000; i++) { - block_stack.back()[i] = 0; - } - - - }; - + Octree(); ~Octree() {}; - std::list block_stack; + uint64_t *blob = new uint64_t[100000]; + uint64_t stack_pos = 0x8000; uint64_t global_pos = 0; - uint64_t copy_to_stack(std::vector children) { - - // Check for the 15 bit boundry - if (stack_pos - children.size() > stack_pos) { - global_pos = stack_pos; - stack_pos = 0x8000; - } - else { - stack_pos -= children.size(); - } - - // Check for the far bit - - memcpy(&block_stack.front()[stack_pos + global_pos], children.data(), children.size() * sizeof(uint64_t)); - - // Return the bitmask encoding the index of that value - // If we tripped the far bit, allocate a far index to the stack and place - // it one above preferably. - // And then shift the far bit to 1 - - // If not, shift the index to its correct place - return stack_pos; - }; + uint64_t copy_to_stack(std::vector children); + // With a position and the head of the stack. Traverse down the voxel hierarchy to find + // the IDX and stack position of the highest resolution (maybe set resolution?) oct + bool get_voxel(sf::Vector3i position); - int get_idx(sf::Vector3i voxel_pos) { - - return 1; - - } + void print_block(int block_pos); + +private: // (X, Y, Z) mask for the idx - uint8_t idx_set_x_mask = 0x1; - uint8_t idx_set_y_mask = 0x2; - uint8_t idx_set_z_mask = 0x4; + const uint8_t idx_set_x_mask = 0x1; + const uint8_t idx_set_y_mask = 0x2; + const uint8_t idx_set_z_mask = 0x4; // Mask for - uint8_t mask_8[8] = { + const uint8_t mask_8[8] = { 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80 }; - uint8_t count_mask_8[8]{ + const uint8_t count_mask_8[8]{ 0x1, 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF }; - //uint8_t count_mask_8[8]{ - // 0xFF, 0x7F, 0x3F, 0x1F, - // 0xF, 0x7, 0x3, 0x1 - //}; - - - // With a position and the head of the stack. Traverse down the voxel hierarchy to find - // the IDX and stack position of the highest resolution (maybe set resolution?) oct - bool get_voxel(sf::Vector3i position) { - - // 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 (DEBUG) - std::vector> scale_stack(static_cast(log2(OCT_DIM))); - - // 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 - // array index that holds our masks as we build the idx. - // Adding 1 for X, 2 for Y, and 4 for Z - int mask_index = 0; - - - // Do the logic steps to find which sub oct we step down into - if (position.x >= (dimension / 2) + quad_position.x) { - - // Set our voxel position to the (0,0) of the correct oct - quad_position.x += (dimension / 2); - - // increment the mask index and mentioned above - mask_index += 1; - - // Set the idx to represent the move - idx_stack[scale] |= idx_set_x_mask; - - // Debug - scale_stack.at(static_cast(log2(OCT_DIM) - log2(dimension))).set(0); - - } - if (position.y >= (dimension / 2) + quad_position.y) { - - quad_position.y |= (dimension / 2); - - mask_index += 2; - - idx_stack[scale] ^= idx_set_y_mask; - scale_stack.at(static_cast(log2(OCT_DIM) - log2(dimension))).set(1); - } - if (position.z >= (dimension / 2) + quad_position.z) { - - 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]) { - - // Check to see if it is a leaf - 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; - } - - // If all went well and we found a valid non-leaf oct then we will traverse further down the hierarchy - scale++; - dimension /= 2; - - // 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 index to get the position - int i1 = count_bits((uint8_t)(head >> 16) & count_mask_8[0]); - int i2 = count_bits((uint8_t)(head >> 16) & count_mask_8[1]); - int i3 = count_bits((uint8_t)(head >> 16) & count_mask_8[2]); - int i4 = count_bits((uint8_t)(head >> 16) & count_mask_8[3]); - int i5 = count_bits((uint8_t)(head >> 16) & count_mask_8[4]); - int i6 = count_bits((uint8_t)(head >> 16) & count_mask_8[5]); - int i7 = count_bits((uint8_t)(head >> 16) & count_mask_8[6]); - int i8 = count_bits((uint8_t)(head >> 16) & count_mask_8[7]); - - int count = count_bits((uint8_t)(head >> 16) & count_mask_8[mask_index]); - - // 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]; - - } else { - // 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 - return false; - break; - } - } - - - std::bitset<64> t(index); - auto val = t.count(); - - return true; - } - - - void print_block(int block_pos) { - std::stringstream sss; - for (int i = 0; i < (int)pow(2, 15); i++) { - PrettyPrintUINT64(block_stack.front()[i], &sss); - sss << "\n"; - } - DumpLog(&sss, "raw_data.txt"); - } - -private: - const uint64_t child_pointer_mask = 0x0000000000007fff; const uint64_t far_bit_mask = 0x8000; const uint64_t valid_mask = 0xFF0000; @@ -266,16 +78,9 @@ private: class Map { public: - Map(sf::Vector3i position); - void generate_octree(); - - void load_unload(sf::Vector3i world_position); - void load_single(sf::Vector3i world_position); - sf::Vector3i getDimensions(); - char *list; - //sf::Vector3i dimensions; + void generate_octree(); void setVoxel(sf::Vector3i position, int val); @@ -284,28 +89,20 @@ public: bool getVoxel(sf::Vector3i pos); Octree a; - sf::Vector3f global_light; - void test_map(); -protected: - private: - // DEBUG + // ======= DEBUG =========== int counter = 0; std::stringstream output_stream; - - // !DEBUG + // ========================= uint64_t generate_children(sf::Vector3i pos, int dim); - char* voxel_data = new char[OCT_DIM * OCT_DIM * OCT_DIM]; - //std::unordered_map chunk_map; - double* height_map; // 2^k @@ -316,7 +113,7 @@ private: world_coords.x / CHUNK_DIM + 1, world_coords.y / CHUNK_DIM + 1, world_coords.z / CHUNK_DIM + 1 - ); + ); } }; diff --git a/include/util.hpp b/include/util.hpp index 0892b9f..946751c 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -13,6 +13,7 @@ #include #include + const double PI = 3.141592653589793238463; const float PI_F = 3.14159265358979f; struct fps_counter { @@ -262,25 +263,35 @@ inline std::vector sfml_get_float_input(sf::RenderWindow *window) { } +#ifdef _MSC_VER +# include +# define __builtin_popcount _mm_popcnt_u32 +# define __builtin_popcountll _mm_popcnt_u64 +#endif + inline int count_bits(int32_t v) { - v = v - ((v >> 1) & 0x55555555); // reuse input as temporary - v = (v & 0x33333333) + ((v >> 2) & 0x33333333); // temp - return (((v + (v >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24; // count + return static_cast(__builtin_popcount(v)); + + //v = v - ((v >> 1) & 0x55555555); // reuse input as temporary + //v = (v & 0x33333333) + ((v >> 2) & 0x33333333); // temp + //return (((v + (v >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24; // count } inline int count_bits(int64_t v) { - int32_t left = (int32_t)(v); - int32_t right = (int32_t)(v >> 32); + return static_cast(__builtin_popcountll(v)); + + //int32_t left = (int32_t)(v); + //int32_t right = (int32_t)(v >> 32); - left = left - ((left >> 1) & 0x55555555); // reuse input as temporary - left = (left & 0x33333333) + ((left >> 2) & 0x33333333); // temp - left = ((left + (left >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; // count + //left = left - ((left >> 1) & 0x55555555); // reuse input as temporary + //left = (left & 0x33333333) + ((left >> 2) & 0x33333333); // temp + //left = ((left + (left >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; // count - right = right - ((right >> 1) & 0x55555555); // reuse input as temporary - right = (right & 0x33333333) + ((right >> 2) & 0x33333333); // temp - right = ((right + (right >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; // count + //right = right - ((right >> 1) & 0x55555555); // reuse input as temporary + //right = (right & 0x33333333) + ((right >> 2) & 0x33333333); // temp + //right = ((right + (right >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; // count - return left + right; + //return left + right; } \ No newline at end of file diff --git a/src/Map.cpp b/src/Map.cpp index f4260f6..28e9552 100644 --- a/src/Map.cpp +++ b/src/Map.cpp @@ -68,28 +68,15 @@ bool IsLeaf(const uint64_t descriptor) { Map::Map(sf::Vector3i position) { - //srand(time(NULL)); - - //load_unload(position); + srand(time(NULL)); for (int i = 0; i < OCT_DIM * OCT_DIM * OCT_DIM; i++) { - if (rand() % 25 > 1) + if (rand() % 25 < 2) voxel_data[i] = 1; else - voxel_data[i] = 1; + voxel_data[i] = 0; } - - //voxel_data[1 + OCT_DIM * (0 + OCT_DIM * 0)] = 0; - //voxel_data[1 + OCT_DIM * (1 + OCT_DIM * 0)] = 0; - //voxel_data[1 + OCT_DIM * (0 + OCT_DIM * 1)] = 0; - //voxel_data[1 + OCT_DIM * (1 + OCT_DIM * 1)] = 0; - - //voxel_data[0 + OCT_DIM * (0 + OCT_DIM * 0)] = 0; - //voxel_data[0 + OCT_DIM * (1 + OCT_DIM * 0)] = 0; - //voxel_data[0 + OCT_DIM * (0 + OCT_DIM * 1)] = 0; - //voxel_data[0 + OCT_DIM * (1 + OCT_DIM * 1)] = 0; - } uint64_t Map::generate_children(sf::Vector3i pos, int voxel_scale) { @@ -108,72 +95,73 @@ uint64_t Map::generate_children(sf::Vector3i pos, int voxel_scale) { sf::Vector3i(pos.x + voxel_scale, pos.y + voxel_scale, pos.z + voxel_scale) }; + // If we hit the 1th voxel scale then we need to query the 3D grid + // and get the voxel at that position. I assume in the future when I + // want to do chunking / loading of raw data I can edit the voxel access if (voxel_scale == 1) { - // Return the base 2x2 leaf node - uint64_t tmp = 0; + // + uint64_t child_descriptor = 0; - // These don't bound check, should they? // Setting the individual valid mask bits + // These don't bound check, should they? for (int i = 0; i < v.size(); i++) { if (getVoxel(v.at(i))) - SetBit(i + 16, &tmp); + SetBit(i + 16, &child_descriptor); } - // Set the leaf mask to full - tmp |= 0xFF000000; + // We are querying leafs, so we need to fill the leaf mask + child_descriptor |= 0xFF000000; + // This is where contours // The CP will be left blank, contours will be added maybe - return tmp; + return child_descriptor; } - else { - uint64_t tmp = 0; - uint64_t child = 0; - - std::vector children; + // Init a blank child descriptor for this node + uint64_t child_descriptor = 0; + + std::vector descriptor_array; - // Generate down the recursion, returning the descriptor of the current node - for (int i = 0; i < v.size(); i++) { + // Generate down the recursion, returning the descriptor of the current node + 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), voxel_scale / 2); + uint64_t child = 0; - // - PrettyPrintUINT64(child, &output_stream); - output_stream << " " << voxel_scale << " " << counter++ << std::endl; + // Get the child descriptor from the i'th to 8th subvoxel + child = generate_children(v.at(i), voxel_scale / 2); - if (IsLeaf(child)) { - if (CheckLeafSign(child)) { - SetBit(i + 16, &tmp); - children.push_back(child); - } else { - SetBit(i + 16 + 8, &tmp); - } - } + // =========== Debug =========== + PrettyPrintUINT64(child, &output_stream); + output_stream << " " << voxel_scale << " " << counter++ << std::endl; + // ============================= - else { - SetBit(i + 16, &tmp); - children.push_back(child); - } + // If the child is a leaf (contiguous) of non-valid values + if (IsLeaf(child) && !CheckLeafSign(child)) { + // Leave the valid mask 0, set leaf mask to 1 + SetBit(i + 16 + 8, &child_descriptor); } - // Now put those values onto the block stack, it returns the - // 16 bit topmost pointer to the block. The 16th bit being - // a switch to jump to a far pointer. - int y = 0; - tmp |= a.copy_to_stack(children); + // If the child is valid and not a leaf + else { - if ((tmp & 0xFFFFFFFF00000000) != 0) { - abort(); + // Set the valid mask, and add it to the descriptor array + SetBit(i + 16, &child_descriptor); + descriptor_array.push_back(child); } - - return tmp; - } - return 0; + // Any free space between the child descriptors must be added here in order to + // interlace them and allow the memory handler to work correctly. + + // Copy the children to the stack and set the child_descriptors pointer to the correct value + child_descriptor |= a.copy_to_stack(descriptor_array); + + // Free space may also be allocated here as well + + // Return the node up the stack + return child_descriptor; } void Map::generate_octree() { @@ -183,77 +171,20 @@ 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, &output_stream); - output_stream << " " << OCT_DIM << " " << counter++ << std::endl; - - if (IsLeaf(root_node)) { - if (CheckLeafSign(root_node)) - SetBit(0 + 16, &tmp); - - SetBit(0 + 16 + 8, &tmp); - } - - else { - SetBit(0 + 16, &tmp); - - } - - tmp |= a.copy_to_stack(std::vector{root_node}); - - DumpLog(&output_stream, "raw_output.txt"); - - a.print_block(0); + // ========= DEBUG ============== + // PrettyPrintUINT64(root_node, &output_stream); + // output_stream << " " << OCT_DIM << " " << counter++ << std::endl; + // ============================== -} - -void Map::load_unload(sf::Vector3i world_position) { - - //sf::Vector3i chunk_pos(world_to_chunk(world_position)); - // - ////Don't forget the middle chunk - //if (chunk_map.find(chunk_pos) == chunk_map.end()) { - // chunk_map[chunk_pos] = Chunk(5); - //} - - //for (int x = chunk_pos.x - chunk_radius / 2; x < chunk_pos.x + chunk_radius / 2; x++) { - // for (int y = chunk_pos.y - chunk_radius / 2; y < chunk_pos.y + chunk_radius / 2; y++) { - // for (int z = chunk_pos.z - chunk_radius / 2; z < chunk_pos.z + chunk_radius / 2; z++) { - - // if (chunk_map.find(sf::Vector3i(x, y, z)) == chunk_map.end()) { - // chunk_map.emplace(sf::Vector3i(x, y, z), Chunk(rand() % 6)); - // //chunk_map[sf::Vector3i(x, y, z)] = Chunk(rand() % 6); - // } - // } - // } - //} -} + int position = a.copy_to_stack(std::vector{root_node}); -void Map::load_single(sf::Vector3i world_position) { - //sf::Vector3i chunk_pos(world_to_chunk(world_position)); + // Dump the debug log + // DumpLog(&output_stream, "raw_output.txt"); - ////Don't forget the middle chunk - //if (chunk_map.find(chunk_pos) == chunk_map.end()) { - // chunk_map[chunk_pos] = Chunk(0); - //} -} - -sf::Vector3i Map::getDimensions() { - return sf::Vector3i(0, 0, 0); } void Map::setVoxel(sf::Vector3i world_position, int val) { - //load_single(world_position); - //sf::Vector3i chunk_pos(world_to_chunk(world_position)); - //sf::Vector3i in_chunk_pos( - // world_position.x % CHUNK_DIM, - // world_position.y % CHUNK_DIM, - // world_position.z % CHUNK_DIM - //); - - //chunk_map.at(chunk_pos).voxel_data[in_chunk_pos.x + CHUNK_DIM * (in_chunk_pos.y + CHUNK_DIM * in_chunk_pos.z)] - // = val; - } char Map::getVoxelFromOctree(sf::Vector3i position) @@ -272,24 +203,224 @@ bool Map::getVoxel(sf::Vector3i pos){ 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++) { + std::cout << "Validating map..." << std::endl; + + 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); + bool arr2 = getVoxelFromOctree(pos); if (arr1 != arr2) { - std::cout << "MISMATCH" << std::endl; + std::cout << "X: " << pos.x << "Y: " << pos.y << "Z: " << pos.z << std::endl; } - - } - } + + } + } + } + + std::cout << "Done" << std::endl; + + sf::Clock timer; + + timer.restart(); + + 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 arr2 = getVoxelFromOctree(pos); + } + } + } + + std::cout << "Octree linear xyz access : "; + std::cout << timer.restart().asMicroseconds() << " microseconds" << std::endl; + + 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); + } + } + } + + std::cout << "Array linear xyz access : "; + std::cout << timer.restart().asMicroseconds() << " microseconds" << std::endl; + +} + +Octree::Octree() { + + // initialize the first stack block + + for (int i = 0; i < 0x8000; i++) { + blob[i] = 0; + } +} + +uint64_t Octree::copy_to_stack(std::vector children) { + + // Check for the 15 bit boundry + if (stack_pos - children.size() > stack_pos) { + global_pos = stack_pos; + stack_pos = 0x8000; + } + else { + stack_pos -= children.size(); + } + + // Check for the far bit + + memcpy(&blob[stack_pos + global_pos], children.data(), children.size() * sizeof(uint64_t)); + + // Return the bitmask encoding the index of that value + // If we tripped the far bit, allocate a far index to the stack and place + // it at the bottom of the child_descriptor node level array + // And then shift the far bit to 1 + + // If not, shift the index to its correct place + return stack_pos; +} + +bool Octree::get_voxel(sf::Vector3i position) { + + // Init the parent stack + int parent_stack_position = 0; + uint64_t parent_stack[32] = { 0 }; + + // and push the head node + uint64_t head = blob[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 (DEBUG) + //std::vector> scale_stack(static_cast(log2(OCT_DIM))); + + // 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 + // array index that holds our masks as we build the idx. + // Adding 1 for X, 2 for Y, and 4 for Z + int mask_index = 0; + + + // Do the logic steps to find which sub oct we step down into + if (position.x >= (dimension / 2) + quad_position.x) { + + // Set our voxel position to the (0,0) of the correct oct + quad_position.x += (dimension / 2); + + // increment the mask index and mentioned above + mask_index += 1; + + // Set the idx to represent the move + idx_stack[scale] |= idx_set_x_mask; + + // Debug + //scale_stack.at(static_cast(log2(OCT_DIM) - log2(dimension))).set(0); + + } + if (position.y >= (dimension / 2) + quad_position.y) { + + quad_position.y |= (dimension / 2); + + mask_index += 2; + + idx_stack[scale] ^= idx_set_y_mask; + //scale_stack.at(static_cast(log2(OCT_DIM) - log2(dimension))).set(1); + } + if (position.z >= (dimension / 2) + quad_position.z) { + + 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]) { + + // Check to see if it is a leaf + 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; + } + + // If all went well and we found a valid non-leaf oct then we will traverse further down the hierarchy + scale++; + dimension /= 2; + + // Count the number of valid octs that come before and add it to the index to get the position + int count = count_bits((uint8_t)(head >> 16) & count_mask_8[mask_index]); + + // 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 = blob[index]; + + // Increment the parent stack position and put the new oct node as the parent + parent_stack_position++; + parent_stack[parent_stack_position] = head; + + } + else { + // 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 + return false; + } } + return true; +} - std::cout << "\nGOOD" << std::endl; +void Octree::print_block(int block_pos) { + std::stringstream sss; + for (int i = block_pos; i < (int)pow(2, 15); i++) { + PrettyPrintUINT64(blob[i], &sss); + sss << "\n"; + } + DumpLog(&sss, "raw_data.txt"); + } + diff --git a/src/Ray.cpp b/src/Ray.cpp index 1709da7..e1daad7 100644 --- a/src/Ray.cpp +++ b/src/Ray.cpp @@ -15,7 +15,9 @@ Ray::Ray( this->map = map; origin = camera_position; direction = ray_direction; - dimensions = map->getDimensions(); + + // TODO: Had to break this while refactoring map + dimensions = sf::Vector3i(0, 0, 0); // map->getDimensions(); } sf::Color Ray::Cast() { @@ -105,23 +107,25 @@ sf::Color Ray::Cast() { // If we hit a voxel int index = voxel.x + dimensions.x * (voxel.y + dimensions.z * voxel.z); - int voxel_data = map->list[index]; + + // TODO: Had to break this while refactoring map + int voxel_data = 0; // map->list[index]; float alpha = 0; if (face == 0) { - alpha = AngleBetweenVectors(sf::Vector3f(1, 0, 0), map->global_light); + //alpha = AngleBetweenVectors(sf::Vector3f(1, 0, 0), map->global_light); alpha = static_cast(fmod(alpha, 0.785) * 2); } else if (face == 1) { - alpha = AngleBetweenVectors(sf::Vector3f(0, 1, 0), map->global_light); + //alpha = AngleBetweenVectors(sf::Vector3f(0, 1, 0), map->global_light); alpha = static_cast(fmod(alpha, 0.785) * 2); } else if (face == 2){ //alpha = 1.57 / 2; - alpha = AngleBetweenVectors(sf::Vector3f(0, 0, 1), map->global_light); + //alpha = AngleBetweenVectors(sf::Vector3f(0, 0, 1), map->global_light); alpha = static_cast(fmod(alpha, 0.785) * 2); } diff --git a/src/main.cpp b/src/main.cpp index 58e77e7..54b607b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -85,6 +85,7 @@ int main() { #elif defined _WIN32 glewInit(); #elif defined TARGET_OS_MAC + // Do nothing, extension wrangling handled by macOS #endif // The socket listener for interacting with the TCP streaming android controller @@ -95,8 +96,7 @@ int main() { // ============================= Map _map(sf::Vector3i(0, 0, 0)); _map.generate_octree(); - std::cout << _map.a.get_voxel(sf::Vector3i(1, 1, 0)); - std::cout << _map.getVoxel(sf::Vector3i(1, 1, 0)); + _map.a.print_block(0); _map.test_map(); std::cin.get(); return 0;