From 6a3eaa04f7791dd73afc43f3580d8f4803190506 Mon Sep 17 00:00:00 2001 From: MitchellHansen Date: Sat, 18 Mar 2017 20:08:38 -0700 Subject: [PATCH] Added color to lights, added a gui slider to control the single light color --- include/raycaster/Hardware_Caster.h | 5 ++ kernels/ray_caster_kernel.cl | 85 +++++++++++++++-------------- src/LightHandle.cpp | 4 +- src/Old_Map.cpp | 2 +- src/main.cpp | 15 ++++- src/raycaster/Hardware_Caster.cpp | 13 ++++- 6 files changed, 77 insertions(+), 47 deletions(-) diff --git a/include/raycaster/Hardware_Caster.h b/include/raycaster/Hardware_Caster.h index d04d4eb..d960c6b 100644 --- a/include/raycaster/Hardware_Caster.h +++ b/include/raycaster/Hardware_Caster.h @@ -41,6 +41,11 @@ struct device { bool cl_gl_sharing = false; }; +struct raycaster_settings { + +}; + + struct PackedData; class Hardware_Caster : public RayCaster diff --git a/kernels/ray_caster_kernel.cl b/kernels/ray_caster_kernel.cl index f8bd989..bb4638c 100644 --- a/kernels/ray_caster_kernel.cl +++ b/kernels/ray_caster_kernel.cl @@ -20,16 +20,16 @@ float4 white_light(float4 input, float3 light, int3 mask) { // {r, g, b, i, x, y, z, x', y', z'} -float4 view_light(float4 in_color, float3 light, float3 view, int3 mask) { +float4 view_light(float4 in_color, float3 light, float4 light_color, float3 view, int3 mask) { float diffuse = max(dot(normalize(convert_float3(mask)), normalize(light)), 0.0f); - in_color += diffuse * 0.2; + in_color += diffuse * 0.2f * light_color; //(float4)(1.0f, 1.0f, 0.0f, 1.0f); - if (dot(light, normalize(convert_float3(mask))) > 0.0) + if (dot(light, normalize(convert_float3(mask))) > 0.0f) { float3 halfwayVector = normalize(normalize(light) + normalize(view)); float specTmp = max(dot(normalize(convert_float3(mask)), halfwayVector), 0.0f); - in_color += pow(specTmp, 1.0f) * 0.5; + in_color += pow(specTmp, 1.0f) * 0.5f * light_color;//(float4)(1.0f, 1.0f, 0.0f, 0.0f); } //in_color += 0.02; @@ -59,8 +59,8 @@ bool cast_light_intersection_ray( float3 ray_dir, float3 ray_pos, global float* lights, - global int* light_count - + global int* light_count + ){ float distance_to_light = DistanceBetweenPoints(ray_pos, (float3)(lights[4], lights[5], lights[6])); @@ -75,7 +75,7 @@ bool cast_light_intersection_ray( // Delta T is the units a ray must travel along an axis in order to // traverse an integer split float3 delta_t = fabs(1.0f / ray_dir); - + // offset is how far we are into a voxel, enables sub voxel movement float3 offset = ((ray_pos)-floor(ray_pos)) * convert_float3(voxel_step); @@ -114,9 +114,9 @@ bool cast_light_intersection_ray( //} while (any(isless(intersection_t, (float3)(distance_to_light - 1)))); } while (intersection_t.x < distance_to_light - 1 || - intersection_t.y < distance_to_light - 1 || + intersection_t.y < distance_to_light - 1 || intersection_t.z < distance_to_light - 1 ); - + return false; } @@ -125,23 +125,23 @@ bool cast_light_intersection_ray( // ================================================================================================== __kernel void raycaster( - global char* map, - global int3* map_dim, - global int2* resolution, - global float3* projection_matrix, - global float2* cam_dir, - global float3* cam_pos, - global float* lights, - global int* light_count, - __write_only image2d_t image, - global int* seed_memory, - __read_only image2d_t texture_atlas, - global int2 *atlas_dim, - global int2 *tile_dim + global char* map, + global int3* map_dim, + global int2* resolution, + global float3* projection_matrix, + global float2* cam_dir, + global float3* cam_pos, + global float* lights, + global int* light_count, + __write_only image2d_t image, + global int* seed_memory, + __read_only image2d_t texture_atlas, + global int2 *atlas_dim, + global int2 *tile_dim ){ int global_id = get_global_id(0); - + // Get and set the random seed from seed memory int seed = seed_memory[global_id]; int random_number = rand(&seed); @@ -170,7 +170,7 @@ __kernel void raycaster( ray_dir.z ); - + // Setup the voxel step based on what direction the ray is pointing int3 voxel_step = {1, 1, 1}; voxel_step *= (ray_dir > 0) - (ray_dir < 0); @@ -184,7 +184,7 @@ __kernel void raycaster( // offset is how far we are into a voxel, enables sub voxel movement float3 offset = ((*cam_pos) - floor(*cam_pos)) * convert_float3(voxel_step); - + // Intersection T is the collection of the next intersection points // for all 3 axis XYZ. @@ -192,19 +192,19 @@ __kernel void raycaster( // for negative values, wrap around the delta_t intersection_t += delta_t * -convert_float3(isless(intersection_t, 0)); - + // Hard cut-off for how far the ray can travel int max_dist = 800; int dist = 0; - + int3 face_mask = { 0, 0, 0 }; - float4 fog_color = { 0.73, 0.81, 0.89, 0.8 }; - float4 voxel_color = (float4)(0.50, 0.0, 0.50, 0.1); - float4 overshoot_color = { 0.25, 0.48, 0.52, 0.8 }; - float4 overshoot_color_2 = { 0.25, 0.1, 0.52, 0.8 }; - + float4 fog_color = { 0.73f, 0.81f, 0.89f, 0.8f }; + float4 voxel_color = (float4)(0.50f, 0.0f, 0.50f, 0.1f); + float4 overshoot_color = { 0.25f, 0.48f, 0.52f, 0.8f }; + float4 overshoot_color_2 = { 0.25f, 0.1f, 0.52f, 0.8f }; + // Andrew Woo's raycasting algo do { @@ -226,7 +226,7 @@ __kernel void raycaster( write_imagef(image, pixel, white_light(mix(fog_color, overshoot_color_2, 1.0 - max(dist / 700.0f, (float)0)), (float3)(lights[7], lights[8], lights[9]), face_mask)); return; } - + // If we hit a voxel int index = voxel.x + (*map_dim).x * (voxel.y + (*map_dim).z * (voxel.z)); int voxel_data = map[index]; @@ -264,7 +264,7 @@ __kernel void raycaster( float x_percent = (intersection_t.x - (intersection_t.z - delta_t.z)) / delta_t.x; float y_percent = (intersection_t.y - (intersection_t.z - delta_t.z)) / delta_t.y; - + face_position = (float3)(x_percent, y_percent, 1.001f); tile_face_position = (float2)(x_percent, y_percent); @@ -307,28 +307,32 @@ __kernel void raycaster( // just a plain color for the voxel color if (voxel_data == 6) { - voxel_color = (float4)(0.0, 0.239, 0.419, 0.3); + voxel_color = (float4)(0.0f, 0.239f, 0.419f, 0.3f); } else if (voxel_data == 5) { float2 tile_size = convert_float2(*atlas_dim / *tile_dim); voxel_color = read_imagef(texture_atlas, convert_int2(tile_face_position * tile_size) + convert_int2((float2)(3, 0) * tile_size)); + voxel_color.w = 0.3f; //voxel_color = (float4)(0.25, 0.52, 0.30, 0.1); } else if (voxel_data == 1) { - voxel_color = (float4)(0.929, 0.957, 0.027, 0.7); + voxel_color = (float4)(0.929f, 0.957f, 0.027f, 0.3f); } - - // + //else { + // voxel_color = (float4)(1.0f, 0.0f, 0.0f, 0.0f); + //} + // if (cast_light_intersection_ray( map, map_dim, normalize((float3)(lights[4], lights[5], lights[6]) - (convert_float3(voxel) + face_position)), (convert_float3(voxel) + face_position), - lights, + lights, light_count )) { + // If the light ray intersected an object on the way to the light point float4 ambient_color = white_light(voxel_color, (float3)(lights[4], lights[5], lights[6]), face_mask); write_imagef(image, pixel, ambient_color); return; @@ -343,6 +347,7 @@ __kernel void raycaster( view_light( voxel_color, (convert_float3(voxel) + face_position) - (float3)(lights[4], lights[5], lights[6]), + (float4)(lights[0], lights[1], lights[2], lights[3]), (convert_float3(voxel) + face_position) - (*cam_pos), face_mask * voxel_step ) @@ -360,4 +365,4 @@ __kernel void raycaster( write_imagef(image, pixel, white_light(mix(fog_color, (float4)(0.40, 0.00, 0.40, 0.2), 1.0 - max(dist / 700.0f, (float)0)), (float3)(lights[7], lights[8], lights[9]), face_mask)); return; -} \ No newline at end of file +} diff --git a/src/LightHandle.cpp b/src/LightHandle.cpp index 3bc9653..0fedbdd 100644 --- a/src/LightHandle.cpp +++ b/src/LightHandle.cpp @@ -50,7 +50,7 @@ void LightHandle::add_movement(sf::Vector3f movement) void LightHandle::set_position(sf::Vector3f position) { - + data_reference->position = position; } void LightHandle::set_direction(sf::Vector3f direction) @@ -60,7 +60,7 @@ void LightHandle::set_direction(sf::Vector3f direction) void LightHandle::set_rgbi(sf::Vector4f rgbi) { - + data_reference->rgbi = rgbi; } void LightHandle::recieve_event(VrEventPublisher* publisher, std::unique_ptr event) diff --git a/src/Old_Map.cpp b/src/Old_Map.cpp index 4d383a5..d4398d4 100644 --- a/src/Old_Map.cpp +++ b/src/Old_Map.cpp @@ -203,7 +203,7 @@ void Old_Map::generate_terrain() { int z = static_cast(height_map[x + y * dimensions.x]); - while (z > 0) { + while (z > 0 && z < dimensions.z) { voxel_data[x + dimensions.x * (y + dimensions.z * z)] = 5; z--; } diff --git a/src/main.cpp b/src/main.cpp index 669e0cb..a963f65 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -143,7 +143,7 @@ int main() { LightPrototype prototype( sf::Vector3f(100.0f, 100.0f, 30.0f), sf::Vector3f(-1.0f, -1.0f, -1.5f), - sf::Vector4f(1.0f, 1.0f, 1.0f, 1.0f) + sf::Vector4f(0.2f, 0.9f, 0.0f, 1.0f) ); std::shared_ptr handle(light_controller.create_light(prototype)); @@ -178,6 +178,8 @@ int main() { sf::Clock sf_delta_clock; fps_counter fps; + float light_color[4] = { 0, 0, 0, 0 }; + while (window.isOpen()) { input_handler.consume_sf_events(&window); @@ -254,6 +256,17 @@ int main() { ImGui::End(); + ImGui::Begin("Lights"); + + if (ImGui::SliderFloat4("Color", light_color, 0, 1)) { + sf::Vector4f light(light_color[0], light_color[1], light_color[2], light_color[3]); + handle->set_rgbi(light); + } + + + ImGui::End(); + + ImGui::Render(); diff --git a/src/raycaster/Hardware_Caster.cpp b/src/raycaster/Hardware_Caster.cpp index 2a709bb..d473f06 100644 --- a/src/raycaster/Hardware_Caster.cpp +++ b/src/raycaster/Hardware_Caster.cpp @@ -216,7 +216,7 @@ int Hardware_Caster::debug_quick_recompile() } validate(); - return 1; + return 0; } void Hardware_Caster::test_edit_viewport(int width, int height, float v_fov, float h_fov) @@ -377,6 +377,11 @@ int Hardware_Caster::acquire_platform_and_device() { current_best_device = device; } + //if (device.type == CL_DEVICE_TYPE_CPU && + // current_best_device.type != CL_DEVICE_TYPE_CPU) { + // current_best_device = device; + //} + // Get the unit with the higher compute units if (device.comp_units > current_best_device.comp_units) { current_best_device = device; @@ -390,6 +395,7 @@ int Hardware_Caster::acquire_platform_and_device() { if (current_best_device.cl_gl_sharing == false && device.cl_gl_sharing == true) { current_best_device = device; } + } } @@ -400,7 +406,8 @@ int Hardware_Caster::acquire_platform_and_device() { std::cout << "Selected Platform : " << platform_id << std::endl; std::cout << "Selected Device : " << device_id << std::endl; std::cout << "Selected Name : " << current_best_device.name << std::endl; - + std::cout << "Selected Version : " << current_best_device.version << std::endl; + if (current_best_device.cl_gl_sharing == false) { std::cout << "This device does not support the cl_khr_gl_sharing extension" << std::endl; return RayCaster::SHARING_NOT_SUPPORTED; @@ -538,7 +545,7 @@ int Hardware_Caster::compile_kernel(std::string kernel_source, bool is_path, std kernel_map[kernel_name] = kernel; //kernel_map.emplace(std::make_pair(kernel_name, kernel)); - return 1; + return 0; } int Hardware_Caster::set_kernel_arg(