From 51a08fc0bb419e08c46746bc75992df104658eb2 Mon Sep 17 00:00:00 2001 From: MitchellHansen Date: Sat, 10 Dec 2016 01:12:30 -0800 Subject: [PATCH] Various tweaks and modifications Some preliminary testing of map --- CMakeLists.txt | 6 + include/Map.h | 10 ++ kernels/ray_caster_kernel.cl | 28 ++++- notes/notes.cpp | 11 +- src/Camera.cpp | 8 +- src/GL_Testing.cpp | 16 +-- src/Hardware_Caster.cpp | 11 +- src/Map.cpp | 214 ++++++++++++++++++++++++++++++++++- src/Old_map.cpp | 16 +-- src/Ray.cpp | 12 +- src/main.cpp | 12 +- 11 files changed, 296 insertions(+), 48 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 382c1bd..fe6405d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,10 @@ message(STATUS "OpenCL found: ${OPENCL_FOUND}") find_package(GLEW REQUIRED) message(STATUS "GLEW found: ${GLEW_FOUND}") +# Find Vulkan +#find_package(VULKAN REQUIRED) +#message(STATUS "VULKAN found: ${VULKAN_FOUND}") + # Find OpenGL find_package( OpenGL REQUIRED) message(STATUS "OpenGL found: ${OPENGL_FOUND}") @@ -30,6 +34,7 @@ message(STATUS "OpenGL found: ${OPENGL_FOUND}") include_directories(${SFML_INCLUDE_DIR}) include_directories(${OpenCL_INCLUDE_DIRS}) include_directories(${OpenGL_INCLUDE_DIRS}) +#include_directories(${Vulkan_INCLUDE_DIRS}) include_directories(include) # Glob all thr sources into their values @@ -53,6 +58,7 @@ target_link_libraries (${PNAME} ${SFML_LIBRARIES} ${SFML_DEPENDENCIES}) target_link_libraries (${PNAME} ${OpenCL_LIBRARY}) target_link_libraries (${PNAME} ${OPENGL_LIBRARIES}) target_link_libraries (${PNAME} ${GLEW_LIBRARIES}) +#target_link_libraries (${PNAME} ${Vulkan_LIBRARIES}) # Setup to use C++11 set_property(TARGET ${PNAME} PROPERTY CXX_STANDARD 11) # Use C++11 diff --git a/include/Map.h b/include/Map.h index c2615ea..0e06477 100644 --- a/include/Map.h +++ b/include/Map.h @@ -16,6 +16,7 @@ #include #define CHUNK_DIM 32 +#define OCT_DIM 64 struct KeyHasher { std::size_t operator()(const sf::Vector3i& k) const { @@ -57,6 +58,15 @@ protected: private: + int64_t generate_children(sf::Vector3i pos, int dim); + + + int64_t block[1024]; + int stack_position = 0; + char getVoxel(sf::Vector3i pos); + char* voxel_data = new char[OCT_DIM * OCT_DIM * OCT_DIM]; + + std::unordered_map chunk_map; double* height_map; diff --git a/kernels/ray_caster_kernel.cl b/kernels/ray_caster_kernel.cl index 1e2cbbb..1c01ca0 100644 --- a/kernels/ray_caster_kernel.cl +++ b/kernels/ray_caster_kernel.cl @@ -75,6 +75,17 @@ float4 cast_light_rays( // if it does } +int rand(int* seed) // 1 <= *seed < m +{ + int const a = 16807; //ie 7**5 + int const m = 2147483647; //ie 2**31-1 + + *seed = ((*seed) * a) % m; + return(*seed); +} + + + __kernel void raycaster( global char* map, global int3* map_dim, @@ -85,9 +96,17 @@ __kernel void raycaster( global float* lights, global int* light_count, __write_only image2d_t image, - global int* seed + global int* seed_memory ){ + + int global_id = get_global_id(1) * get_global_size(0) + get_global_id(0); + int seed = seed_memory[global_id]; + int random_number = rand(&seed); + seed_memory[global_id] = seed; + + + size_t id = get_global_id(0); int2 pixel = {id % (*resolution).x, id / (*resolution).x}; float3 ray_dir = projection_matrix[pixel.x + (*resolution).x * pixel.y]; @@ -139,19 +158,18 @@ __kernel void raycaster( } // use a ghetto ass rng to give rays a "fog" appearance - int2 randoms = { seed, ray_dir.y }; + int2 randoms = { random_number, 14 }; uint tseed = randoms.x + id; uint t = tseed ^ (tseed << 11); uint result = randoms.y ^ (randoms.y >> 19) ^ (t ^ (t >> 8)); - *seed = result % 50; int max_dist = 800 + result % 100; int dist = 0; int3 mask = { 0, 0, 0 }; float4 color = { 0.73, 0.81, 0.89, 0.6 }; - float4 c = (float4)(0.40, 0.00, 0.40, 0.2); - c.x += ray_dir.y;// (result % 100) / 100; + float4 c = (float4)(0.60, 0.00, 0.40, 0.1); + c.x += (result % 100) / 10; // Andrew Woo's raycasting algo do { diff --git a/notes/notes.cpp b/notes/notes.cpp index 66c224d..395fdb4 100644 --- a/notes/notes.cpp +++ b/notes/notes.cpp @@ -8,7 +8,6 @@ OpenCL: - Separate out into a part of the rendering module Map: - - Reimplement the old map, put it into an old_map structure - Implement the new octree structure - storing the pre-octree volumetric data - determining when to load volumetric data into the in-memory structure @@ -28,5 +27,15 @@ Renderer: + + + + + +Build: + Z:\Cpp_Libs\SFML-Visual_Studio2015RCx64 + Z:/Cpp_Libs/glew-2.0.0/lib/Release/x64/glew32s.lib + Z:/Cpp_Libs/glew-2.0.0/include + */ diff --git a/src/Camera.cpp b/src/Camera.cpp index 55a7379..3be57ef 100644 --- a/src/Camera.cpp +++ b/src/Camera.cpp @@ -71,11 +71,11 @@ int Camera::update(double delta_time) { // have to do it component wise double multiplier = 40; - position.x += movement.x * delta_time * multiplier; - position.y += movement.y * delta_time * multiplier; - position.z += movement.z * delta_time * multiplier; + position.x += static_cast(movement.x * delta_time * multiplier); + position.y += static_cast(movement.y * delta_time * multiplier); + position.z += static_cast(movement.z * delta_time * multiplier); - movement *= (float)(friction_coefficient * delta_time * multiplier); + movement *= static_cast(friction_coefficient * delta_time * multiplier); return 1; } diff --git a/src/GL_Testing.cpp b/src/GL_Testing.cpp index 05ec91d..b4c26df 100644 --- a/src/GL_Testing.cpp +++ b/src/GL_Testing.cpp @@ -4,10 +4,10 @@ GL_Testing::GL_Testing() { GLfloat tmp[] = { - 1, 0, 0, 0, - 0, cos(1), sin(1), 0, - 0, -sin(1), cos(1), 0, - 0, 0, 0, 1 + 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 }; @@ -132,10 +132,10 @@ void GL_Testing::rotate(double delta) { GLfloat tmp[] = { - 1, 0, 0, 0, - 0, cos(counter), sin(counter), 0, - 0, -sin(counter), cos(counter), 0, - 0, 0, 0, 1 + 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 }; diff --git a/src/Hardware_Caster.cpp b/src/Hardware_Caster.cpp index 9238890..d67c036 100644 --- a/src/Hardware_Caster.cpp +++ b/src/Hardware_Caster.cpp @@ -37,9 +37,10 @@ int Hardware_Caster::init() { } srand(NULL); - int seed = rand(); - create_buffer("seed", sizeof(int), &seed); + int *seed_memory = new int[1920*1080]; + + create_buffer("seed", sizeof(int) * 1920 * 1080, seed_memory); return 1; @@ -87,7 +88,7 @@ void Hardware_Caster::validate() set_kernel_arg("raycaster", 8, "image"); set_kernel_arg("raycaster", 9, "seed"); - print_kernel_arguments(); + //print_kernel_arguments(); } @@ -180,7 +181,7 @@ void Hardware_Caster::assign_lights(std::vector lights) { this->lights = std::vector(lights); - light_count = lights.size(); + light_count = static_cast(lights.size()); create_buffer("lights", sizeof(float) * 10 * light_count, this->lights.data(), CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR); @@ -229,7 +230,7 @@ int Hardware_Caster::acquire_platform_and_device() { if (assert(error, "clGetDeviceIDs")) return OPENCL_ERROR; - for (int q = 0; q < deviceIdCount; q++) { + for (unsigned int q = 0; q < deviceIdCount; q++) { device d; diff --git a/src/Map.cpp b/src/Map.cpp index eb3e8a7..f649abc 100644 --- a/src/Map.cpp +++ b/src/Map.cpp @@ -1,8 +1,63 @@ #include "Map.h" + + + + +// root +// +// a1 +// a2 +// +// b1 +// b1 +// +// c1 +// c1 +// +// a2 +// a2 +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// + + + + + + + + + + + + + + + + + + + Map::Map(sf::Vector3i position) { load_unload(position); + + for (int i = 0; i < 8192; i++) { + block[i] = 0; + } } int BitCount(unsigned int u) { @@ -12,6 +67,30 @@ int BitCount(unsigned int u) { return ((uCount + (uCount >> 3)) & 030707070707) % 63; } +void SetBit(int position, char* c) { + *c |= 1 << position; +} + +void FlipBit(int position, char* c) { + *c ^= 1 << position; +} + +int GetBit(int position, char* c) { + return (*c >> position) & 1; +} + +void SetBit(int position, int64_t* c) { + *c |= 1 << position; +} + +void FlipBit(int position, int64_t* c) { + *c ^= 1 << position; +} + +int GetBit(int position, int64_t* c) { + return (*c >> position) & 1; +} + struct leaf { leaf *children; char leaf_mask; @@ -24,6 +103,103 @@ struct block { double* data = new double[1000]; }; +int64_t generate_children_at_raw() { + + int64_t t; + + // count the raw data and insert via bit masks or whatever into the valid field + + // Set the child pointer blank and the leaf mask blank as well + + // Return the single value + + return t; +} + +int64_t Map::generate_children(sf::Vector3i pos, int dim) { + + sf::Vector3i t1 = sf::Vector3i(pos.x, pos.y, pos.z); + sf::Vector3i t2 = sf::Vector3i(pos.x + dim, pos.y, pos.z); + sf::Vector3i t3 = sf::Vector3i(pos.x, pos.y + dim, pos.z); + sf::Vector3i t4 = sf::Vector3i(pos.x + dim, pos.y + dim, pos.z); + + sf::Vector3i t5 = sf::Vector3i(pos.x, pos.y, pos.z + dim); + sf::Vector3i t6 = sf::Vector3i(pos.x + dim, pos.y, pos.z + dim); + sf::Vector3i t7 = sf::Vector3i(pos.x, pos.y + dim, pos.z + dim); + sf::Vector3i t8 = sf::Vector3i(pos.x + dim, pos.y + dim, pos.z + dim); + + std::vector cps; + int64_t tmp = 0; + + if (dim == 1) { + + if (getVoxel(t1)) + SetBit(16, &tmp); + if (getVoxel(t2)) + SetBit(16, &tmp); + if (getVoxel(t3)) + SetBit(16, &tmp); + if (getVoxel(t4)) + SetBit(16, &tmp); + if (getVoxel(t5)) + SetBit(16, &tmp); + if (getVoxel(t6)) + SetBit(16, &tmp); + if (getVoxel(t7)) + SetBit(16, &tmp); + if (getVoxel(t8)) + SetBit(16, &tmp); + + cps.push_back(tmp); + + } + else { + + // Generate all 8 sub trees accounting for each of their unique positions + int curr_stack_pos = stack_position; + + + + tmp = generate_children(t1, dim / 2); + if (tmp != 0) + cps.push_back(tmp); + + tmp = generate_children(t2, dim / 2); + if (tmp != 0) + cps.push_back(tmp); + + tmp = generate_children(t3, dim / 2); + if (tmp != 0) + cps.push_back(tmp); + + tmp = generate_children(t4, dim / 2); + if (tmp != 0) + cps.push_back(tmp); + + tmp = generate_children(t5, dim / 2); + if (tmp != 0) + cps.push_back(tmp); + + tmp = generate_children(t6, dim / 2); + if (tmp != 0) + cps.push_back(tmp); + + tmp = generate_children(t7, dim / 2); + if (tmp != 0) + cps.push_back(tmp); + + tmp = generate_children(t8, dim / 2); + if (tmp != 0) + cps.push_back(tmp); + + } + + + memcpy(&block[stack_position], cps.data(), cps.size() * sizeof(int64_t)); + stack_position += cps.size(); + + return 0; +} void Map::generate_octree() { @@ -34,19 +210,40 @@ void Map::generate_octree() { int* dataset = new int[32 * 32 * 32]; for (int i = 0; i < 32 * 32 * 32; i++) { - dataset[0] = i; + dataset[0] = rand() % 2; } - int level = static_cast(log2(32)); + + + // levels defines how many levels to traverse before we hit raw data + // Will be the map width I presume. Will still need to handle how to swap in and out data. + // Possible have some upper static nodes that will stay full regardless of contents? + int levels = static_cast(log2(64)); + leaf top_node; - top_node.level = level; - for (int i = 0; i < 16 * 16 * 16; i++) { - for (int i = 0; i < 8 * 8 * 8; i++) { - for (int i = 0; i < 4 * 4 * 4; i++) { + int t_level = -1; + int b_level = 0; + + for (int i1 = 0; i1 < 2 * 2 * 2; i1++) { + int b_level = 1; + for (int i2 = 0; i2 < 2 * 2 * 2; i2++) { + int b_level = 2; + for (int i3 = 0; i3 < 2 * 2 * 2; i3++) { + int b_level = 3; + + leaf l1; + l1.children = nullptr; + l1.leaf_mask = 0; + l1.valid_mask = 0; + for (int i = 0; i < 2 * 2 * 2; i++) { + + //int x = + + //if (dataset[] } } } @@ -127,6 +324,11 @@ void Map::setVoxel(sf::Vector3i world_position, int val) { } +char Map::getVoxel(sf::Vector3i pos){ + + return voxel_data[pos.x + OCT_DIM * (pos.y + OCT_DIM * pos.z)]; +} + void Chunk::set(int type) { for (int i = 0; i < CHUNK_DIM * CHUNK_DIM * CHUNK_DIM; i++) { voxel_data[i] = 0; diff --git a/src/Old_map.cpp b/src/Old_map.cpp index f6526ea..e4a5d43 100644 --- a/src/Old_map.cpp +++ b/src/Old_map.cpp @@ -136,14 +136,14 @@ void Old_Map::generate_terrain() { } - for (int x = 0; x < dimensions.x / 10; x++) { - for (int y = 0; y < dimensions.y / 10; y++) { - for (int z = 0; z < dimensions.z; z++) { - if (rand() % 1000 < 1) - voxel_data[x + dimensions.x * (y + dimensions.z * z)] = rand() % 6; - } - } - } + //for (int x = 0; x < dimensions.x / 10; x++) { + // for (int y = 0; y < dimensions.y / 10; y++) { + // for (int z = 0; z < dimensions.z; z++) { + // if (rand() % 1000 < 1) + // voxel_data[x + dimensions.x * (y + dimensions.z * z)] = rand() % 6; + // } + // } + //} } diff --git a/src/Ray.cpp b/src/Ray.cpp index 70d62b4..a7ab0b2 100644 --- a/src/Ray.cpp +++ b/src/Ray.cpp @@ -130,20 +130,12 @@ sf::Color Ray::Cast() { alpha *= 162; switch (voxel_data) { - case 1: - // AngleBew0 - 1.57 * 162 = 0 - 255 - - return sf::Color(255, 0, 0, alpha); - case 2: - return sf::Color(255, 10, 0, alpha); - case 3: - return sf::Color(255, 0, 255, alpha); - case 4: - return sf::Color(80, 0, 150, alpha); case 5: return sf::Color(255, 120, 255, alpha); case 6: return sf::Color(150, 80, 220, alpha); + default: + return sf::Color(150, 80, 220, alpha); } dist++; diff --git a/src/main.cpp b/src/main.cpp index c1543dc..7d15bd8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,6 @@ #include "GL_Testing.h" - +#include +#include #ifdef linux #include #include @@ -66,6 +67,15 @@ sf::Texture window_texture; int main() { + + + + Map _map(sf::Vector3i(0, 0, 0)); + _map.generate_octree(); + + + glewInit(); + sf::RenderWindow window(sf::VideoMode(WINDOW_X, WINDOW_Y), "SFML"); GL_Testing t;