From 5e9401cd27e1ff182a6dd8ca0bf01944751aea5d Mon Sep 17 00:00:00 2001 From: MitchellHansen Date: Sun, 2 Jul 2017 12:36:25 -0700 Subject: [PATCH] Linux build working again, removed the GL_Testing stuff, I'm going to move to Vulkan eventually. Got voxel search working mostly with the new octree changes. Issue with mirroring of voxel data currently --- include/Camera.h | 1 + include/GL_Testing.h | 43 ------ include/map/Map.h | 7 +- include/map/Octree.h | 5 +- include/raycaster/Hardware_Caster.h | 2 +- include/util.hpp | 4 +- src/GL_Testing.cpp | 199 ---------------------------- src/main.cpp | 28 +--- src/map/Map.cpp | 27 ++-- src/map/Octree.cpp | 55 ++++++-- src/raycaster/Hardware_Caster.cpp | 4 +- 11 files changed, 72 insertions(+), 303 deletions(-) delete mode 100644 include/GL_Testing.h delete mode 100644 src/GL_Testing.cpp diff --git a/include/Camera.h b/include/Camera.h index a360172..571c534 100644 --- a/include/Camera.h +++ b/include/Camera.h @@ -3,6 +3,7 @@ #include #include "util.hpp" #include "Pub_Sub.h" +#include class Camera : public VrEventSubscriber{ public: diff --git a/include/GL_Testing.h b/include/GL_Testing.h deleted file mode 100644 index 5aeb8aa..0000000 --- a/include/GL_Testing.h +++ /dev/null @@ -1,43 +0,0 @@ -#pragma once -#include -#include -#include - -#ifdef _WIN32 -#define GLEW_STATIC -#include - -#elif defined TARGET_OS_MAC -#include - -#endif - -class GL_Testing -{ -public: - GL_Testing(); - ~GL_Testing(){}; - - - enum Shader_Type {VERTEX, FRAGMENT}; - void compile_shader(std::string file_path, Shader_Type t); - void create_program(); - void create_buffers(); - void transform(); - void rotate(double delta); - void draw(); - -private: - - GLuint VBO; //raw points - GLuint EBO; //link triangles - GLuint VAO; - GLuint vertex_shader; - GLuint fragment_shader; - GLuint shader_program; - - GLfloat *matrix; - - double counter = 0; -}; - diff --git a/include/map/Map.h b/include/map/Map.h index aafaee1..00747e0 100644 --- a/include/map/Map.h +++ b/include/map/Map.h @@ -25,11 +25,10 @@ public: Map(uint32_t dimensions); void setVoxel(sf::Vector3i position, int val); - bool getVoxelFromOctree(sf::Vector3i position); - bool getVoxel(sf::Vector3i pos); - Octree octree; + + Octree octree; bool test(); @@ -40,8 +39,6 @@ private: std::stringstream output_stream; // ========================= - void generate_octree(unsigned int dimensions); - char* voxel_data; }; diff --git a/include/map/Octree.h b/include/map/Octree.h index afa6041..815fc57 100644 --- a/include/map/Octree.h +++ b/include/map/Octree.h @@ -24,7 +24,6 @@ public: static const int buffer_size = 100000; - Octree(); ~Octree() {}; @@ -56,10 +55,12 @@ public: // 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); + bool GetVoxel(sf::Vector3i position); void print_block(int block_pos); + bool Validate(char* data, sf::Vector3i dimensions); + private: std::tuple GenerationRecursion( diff --git a/include/raycaster/Hardware_Caster.h b/include/raycaster/Hardware_Caster.h index 3a2c577..ecb2672 100644 --- a/include/raycaster/Hardware_Caster.h +++ b/include/raycaster/Hardware_Caster.h @@ -85,7 +85,7 @@ struct device_info { cl_uint cl_device_preferred_vector_width_double; char cl_device_profile[256]; size_t cl_device_profiling_timer_resolution; - cl_device_type cl_device_type; + cl_device_type device_type; char cl_device_vendor[128]; cl_uint cl_device_vendor_id; char cl_device_version[128]; diff --git a/include/util.hpp b/include/util.hpp index 881a139..f62c46a 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -7,7 +7,7 @@ #include #include #include - +#include const double PI = 3.141592653589793238463; const float PI_F = 3.14159265358979f; @@ -268,4 +268,4 @@ inline bool IsLeaf(const uint64_t descriptor) { } return false; -} \ No newline at end of file +} diff --git a/src/GL_Testing.cpp b/src/GL_Testing.cpp deleted file mode 100644 index 6e20e39..0000000 --- a/src/GL_Testing.cpp +++ /dev/null @@ -1,199 +0,0 @@ -#include "GL_Testing.h" - -GL_Testing::GL_Testing() { - - GLfloat tmp[] = { - - 1.0f, 0.0f, 0.0f, 0.0f, - 0.0f, static_cast(cos(1.0f)), static_cast(sin(1.0f)), 0.0f, - 0.0f, static_cast(-sin(1.0f)), static_cast(cos(1.0f)), 0.0f, - 0.0f, 0.0f, 0.0f, 1.0f - - }; - - matrix = new GLfloat[16]; - memcpy(matrix, tmp, sizeof(GLfloat) * 16); - - #ifdef linux - GLint err = glewInit(); - #elif _WIN32 - GLint err = glewInit(); - #elif TARGET_OS_MAC - GLint err = 0; - #endif - - if (err) { - std::cout << "error initializing glew" << std::endl; - } - -} - -void GL_Testing::compile_shader(std::string file_path, Shader_Type t) { - - - // Load in the source and cstring it - const char* source; - std::string tmp; - - tmp = read_file(file_path); - source = tmp.c_str(); - - GLint success; - GLchar log[512]; - - if (t == Shader_Type::VERTEX) { - - vertex_shader = glCreateShader(GL_VERTEX_SHADER); - glShaderSource(vertex_shader, 1, &source, NULL); - glCompileShader(vertex_shader); - - glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &success); - - if (!success) { - glGetShaderInfoLog(vertex_shader, 512, NULL, log); - std::cout << "Vertex shader failed compilation: " << log << std::endl; - } - - } else if (t == Shader_Type::FRAGMENT) { - - fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); - glShaderSource(fragment_shader, 1, &source, NULL); - glCompileShader(fragment_shader); - - glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &success); - - if (!success) { - glGetShaderInfoLog(fragment_shader, 512, NULL, log); - std::cout << "Vertex shader failed compilation: " << log << std::endl; - } - } -} - -void GL_Testing::create_program() { - - GLint success; - GLchar log[512]; - - shader_program = glCreateProgram(); - glAttachShader(shader_program, vertex_shader); - glAttachShader(shader_program, fragment_shader); - glLinkProgram(shader_program); - - - glGetProgramiv(shader_program, GL_LINK_STATUS, &success); - - if (!success) { - glGetProgramInfoLog(shader_program, 512, NULL, log); - std::cout << "Failed to link shaders into program: " << log << std::endl; - } - - glDeleteShader(vertex_shader); - glDeleteShader(fragment_shader); -} - -void GL_Testing::create_buffers() { - - GLfloat vertices[] = { - 0.5f, 0.5f, 0.0f, // Top Right - 0.5f, -0.5f, 0.0f, // Bottom Right - -0.5f, -0.5f, 0.0f, // Bottom Left - -0.5f, 0.5f, 0.0f // Top Left - }; - GLuint indices[] = { // Note that we start from 0! - 0, 1, 3 // First Triangle - // Second Triangle - }; - - #ifdef linux - glGenVertexArrays(1, &VAO); - #elif defined _WIN32 - glGenVertexArrays(1, &VAO); - #elif defined TARGET_OS_MAC - glGenVertexArraysAPPLE(1, &VAO); - #endif - - glGenBuffers(1, &VBO); - glGenBuffers(1, &EBO); - // Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s). - - #ifdef linux - glBindVertexArray(VAO); - #elif defined _WIN32 - glBindVertexArray(VAO); - #elif defined TARGET_OS_MAC - glBindVertexArrayAPPLE(VAO); - #endif - - - glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); - - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0); - glEnableVertexAttribArray(0); - - glBindBuffer(GL_ARRAY_BUFFER, 0); - - #ifdef linux - glbindvertexarray(0); - #elif defined _win32 - glbindvertexarray(0); - #elif defined target_os_mac - glbindvertexarrayapple(0); - #endif -} - -void GL_Testing::transform() -{ - GLuint transformLoc = glGetUniformLocation(shader_program, "transform"); - - glUseProgram(shader_program); - glUniformMatrix4fv(transformLoc, 1, GL_FALSE, matrix); -} - -void GL_Testing::rotate(double delta) { - - counter += delta; - - GLfloat tmp[] = { - - 1.0f, 0.0f, 0.0f, 0.0f, - 0.0f, static_cast(cos(counter)), static_cast(sin(counter)), 0.0f, - 0.0f, static_cast(-sin(counter)), static_cast(cos(counter)), 0.0f, - 0.0f, 0.0f, 0.0f, 1.0f - - }; - - memcpy(matrix, tmp, sizeof(GLfloat) * 16); - -} - -void GL_Testing::draw() { - - glUseProgram(shader_program); - - #ifdef linux - glBindVertexArray(VAO); - #elif defined _WIN32 - glBindVertexArray(VAO); - #elif defined TARGET_OS_MAC - glBindVertexArrayAPPLE(VAO); - #endif - - - - //glDrawArrays(GL_TRIANGLES, 0, 6); - glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0); - - #ifdef linux - glbindVertexArray(0); - #elif defined _win32 - glbindVertexArray(0); - #elif defined target_os_mac - glbindVertexArrayAPPLE(0); - #endif - -} -// diff --git a/src/main.cpp b/src/main.cpp index 0db92c4..dcd4863 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,6 @@  // This has to be up here or else glew will complain -#include "GL_Testing.h" +//#include "GL_Testing.h" #ifdef linux #include @@ -16,8 +16,8 @@ #elif defined TARGET_OS_MAC #include -# include -# include +#include +#include #include #include #endif @@ -65,31 +65,14 @@ float elap_time(){ sf::Sprite window_sprite; sf::Texture window_texture; -// Y: -1.57 is straight up -// Y: 1.57 is straight down - - // TODO: -// - Texture axis sign flipping issue -// - Diffuse fog hard cut off -// - Infinite light distance, no inverse square // - Inconsistent lighting constants. GUI manipulation -// - Far pointers, attachment lookup and aux buffer, contour lookup & masking +// Ancilary settings buffer and memory controller +// - Attachment lookup and aux buffer, contour lookup & masking int main() { - // Keep at this at the top of main. I think it has to do with it and - // sf::RenderWindow stepping on each others feet - #ifdef linux - glewInit(); - #elif defined _WIN32 - //glewInit(); - #elif defined TARGET_OS_MAC - // Do nothing, extension wrangling handled by macOS - #endif - - // ============================= Map _map(32); //_map.test(); @@ -113,7 +96,6 @@ int main() { abort(); } - // Create and generate the old 3d array style map Old_Map* map = new Old_Map(sf::Vector3i(MAP_X, MAP_Y, MAP_Z)); map->generate_terrain(); diff --git a/src/map/Map.cpp b/src/map/Map.cpp index 4d9de8c..bbdec5c 100644 --- a/src/map/Map.cpp +++ b/src/map/Map.cpp @@ -5,31 +5,34 @@ Map::Map(uint32_t dimensions) { srand(time(nullptr)); - voxel_data = new char[dimensions * dimensions * dimensions]; + voxel_data = new char[dimensions * dimensions * dimensions](); for (uint64_t i = 0; i < dimensions * dimensions * dimensions; i++) { - if (rand() % 25 < 2) - voxel_data[i] = 1; - else - voxel_data[i] = 0; - } - generate_octree(dimensions); -} + voxel_data[i] = 1; + } +// for (uint64_t i = 0; i < dimensions * dimensions * dimensions; i++) { +// if (rand() % 25 < 2) +// voxel_data[i] = 1; +// else +// voxel_data[i] = 0; +// } + + setVoxel(sf::Vector3i(1, 1, 1), 0); -void Map::generate_octree(unsigned int dimensions) { - octree.Generate(voxel_data, sf::Vector3i(dimensions, dimensions, dimensions)); + octree.Validate(voxel_data, sf::Vector3i(dimensions, dimensions, dimensions)); } -void Map::setVoxel(sf::Vector3i world_position, int val) { +void Map::setVoxel(sf::Vector3i pos, int val) { + voxel_data[pos.x + OCT_DIM * (pos.y + OCT_DIM * pos.z)] = val; } bool Map::getVoxelFromOctree(sf::Vector3i position) { - return octree.get_voxel(position); + return 0; } bool Map::getVoxel(sf::Vector3i pos){ diff --git a/src/map/Octree.cpp b/src/map/Octree.cpp index d6dfee0..cba6fdb 100644 --- a/src/map/Octree.cpp +++ b/src/map/Octree.cpp @@ -21,18 +21,13 @@ void Octree::Generate(char* data, sf::Vector3i dimensions) { PrettyPrintUINT64(std::get<0>(root_node), &output_stream); output_stream << " " << OCT_DIM << " " << counter++ << std::endl; // ============================== - - // ============= TEMP!!! =================== - if (stack_pos - 1 > stack_pos) { - global_pos -= stack_pos; - stack_pos = 0x8000; - } - else { - stack_pos -= 1; - } + std::get<0>(root_node) |= 1; memcpy(&descriptor_buffer[descriptor_buffer_position], &std::get<0>(root_node), sizeof(uint64_t)); - descriptor_buffer_position--; + + root_index = descriptor_buffer_position; + descriptor_buffer_position--; + // ======================================== @@ -48,13 +43,15 @@ void Octree::Generate(char* data, sf::Vector3i dimensions) { } -bool Octree::get_voxel(sf::Vector3i position) { +bool Octree::GetVoxel(sf::Vector3i position) { // Struct that holds the state necessary to continue the traversal from the found voxel oct_state state; // push the root node to the parent stack - uint64_t head = descriptor_buffer[root_index]; + uint64_t current_index = root_index; + uint64_t head = descriptor_buffer[current_index]; +// PrettyPrintUINT64(head); state.parent_stack[state.parent_stack_position] = head; // Set our initial dimension and the position at the corner of the oct to keep track of our position @@ -98,6 +95,7 @@ bool Octree::get_voxel(sf::Vector3i position) { mask_index += 2; + // What is up with the binary operator on this one? TODO state.idx_stack[state.scale] ^= idx_set_y_mask; } @@ -131,7 +129,8 @@ bool Octree::get_voxel(sf::Vector3i position) { // access the element at which head points to and then add the specified number of indices // to get to the correct child descriptor - head = descriptor_buffer[(head & child_pointer_mask) + count]; + current_index = current_index + (head & child_pointer_mask) + count; + head = descriptor_buffer[current_index]; // Increment the parent stack position and put the new oct node as the parent state.parent_stack_position++; @@ -313,7 +312,35 @@ std::tuple Octree::GenerationRecursion(char* data, sf::Vecto } char Octree::get1DIndexedVoxel(char* data, sf::Vector3i dimensions, sf::Vector3i position) { - return data[position.x + OCT_DIM * (position.y + OCT_DIM * position.z)]; + return data[position.x + dimensions.x * (position.y + dimensions.y * position.z)]; } +bool Octree::Validate(char* data, sf::Vector3i dimensions){ + +// std::cout << (int)get1DIndexedVoxel(data, dimensions, sf::Vector3i(16, 16, 16)) << std::endl; +// std::cout << (int)GetVoxel(sf::Vector3i(16, 16, 16)) << std::endl; + + 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); + char arr_val = get1DIndexedVoxel(data, dimensions, pos); + char oct_val = GetVoxel(pos); + + if (arr_val != oct_val) { + std::cout << "X: " << pos.x << " Y: " << pos.y << " Z: " << pos.z << " "; + std::cout << (int)arr_val << " : " << (int)oct_val << std::endl; + } + + } + } + } + + std::cout << "Done" << std::endl; + + +} diff --git a/src/raycaster/Hardware_Caster.cpp b/src/raycaster/Hardware_Caster.cpp index f09edf1..cbbf279 100644 --- a/src/raycaster/Hardware_Caster.cpp +++ b/src/raycaster/Hardware_Caster.cpp @@ -478,7 +478,7 @@ int Hardware_Caster::query_hardware() { clGetDeviceInfo(id, CL_DEVICE_PREFERRED_VECTOR_WIDTH_DOUBLE, sizeof(cl_uint), &d.cl_device_preferred_vector_width_double, NULL); clGetDeviceInfo(id, CL_DEVICE_PROFILE, sizeof(char) * 256, &d.cl_device_profile, NULL); clGetDeviceInfo(id, CL_DEVICE_PROFILING_TIMER_RESOLUTION, sizeof(size_t), &d.cl_device_profiling_timer_resolution, NULL); - clGetDeviceInfo(id, CL_DEVICE_TYPE, sizeof(cl_device_type), &d.cl_device_type, NULL); + clGetDeviceInfo(id, CL_DEVICE_TYPE, sizeof(cl_device_type), &d.device_type, NULL); clGetDeviceInfo(id, CL_DEVICE_VENDOR, sizeof(char)*128, &d.cl_device_vendor, NULL); clGetDeviceInfo(id, CL_DEVICE_VENDOR_ID, sizeof(cl_uint), &d.cl_device_vendor_id, NULL); clGetDeviceInfo(id, CL_DEVICE_VERSION, sizeof(char)*128, &d.cl_device_version, NULL); @@ -1094,4 +1094,4 @@ void Hardware_Caster::device::print(std::ostream& stream) const { void Hardware_Caster::device::print_packed_data(std::ostream& stream) { stream.write(reinterpret_cast(&data), sizeof(data)); -} \ No newline at end of file +}