From cc7a4a2efbc196ce7d3ac141d08e95a0b41719e1 Mon Sep 17 00:00:00 2001 From: MitchellHansen Date: Sun, 15 Jan 2017 23:23:43 -0800 Subject: [PATCH] Shadows now working, though they are still voxel based. Need to calculate where on the surface the ray hits, and then send the ray out from that point. --- kernels/ray_caster_kernel.cl | 88 ++++++++++++++---------------------- src/Old_Map.cpp | 2 +- src/main.cpp | 4 +- 3 files changed, 36 insertions(+), 58 deletions(-) diff --git a/kernels/ray_caster_kernel.cl b/kernels/ray_caster_kernel.cl index 43a0564..e09edd4 100644 --- a/kernels/ray_caster_kernel.cl +++ b/kernels/ray_caster_kernel.cl @@ -39,7 +39,7 @@ bool cast_light_intersection_ray( // Intersection T is the collection of the next intersection points // for all 3 axis XYZ. - float3 intersection_t = delta_t * offset; + float3 intersection_t = delta_t *offset; // for negative values, wrap around the delta_t, rather not do this // component wise, but it doesn't appear to want to work @@ -109,7 +109,7 @@ float4 view_light(float4 in_color, float3 light, float3 view, int3 mask) { //float3 halfwayDir = normalize(normalize(view) + normalize(light)); //float spec = pow(max(dot(normalize(convert_float3(mask)), halfwayDir), 0.0f), 32.0f); - in_color.w += 0.2; + in_color += 0.02; return in_color; } @@ -274,7 +274,7 @@ __kernel void raycaster( int3 face_mask = { 0, 0, 0 }; float4 fog_color = { 0.73, 0.81, 0.89, 0.8 }; - float4 voxel_color = (float4)(0.25, 0.52, 0.30, 0.1); + float4 voxel_color = (float4)(0.50, 0.0, 0.50, 0.1); float4 overshoot_color = { 0.25, 0.48, 0.52, 0.8 }; @@ -305,66 +305,44 @@ __kernel void raycaster( if (voxel_data != 0) { - switch (voxel_data) { - - case 5: - - // write_imagef(image, pixel, (float4)(0.90, 0.00, 0.40, 0.9)); - - if (!cast_light_intersection_ray( - map, - map_dim, - (float3)(lights[4], lights[5], lights[6]) - (convert_float3(voxel) + offset), - (convert_float3(voxel) + offset + convert_float3(face_mask)/10.0f), - lights, - light_count - )) { + // write_imagef(image, pixel, (float4)(0.90, 0.00, 0.40, 0.9)); - write_imagef(image, pixel, (float4)(0.90, 0.00, 0.40, 0.9)); - return; - } + if (voxel_data == 6) { + voxel_color = (float4)(0.0, 0.239, 0.419, 0.3); + } + else if (voxel_data == 5) { + voxel_color = (float4)(0.25, 0.52, 0.30, 0.1); + } - write_imagef( - image, - pixel, - view_light( - voxel_color, - (convert_float3(voxel) + offset) - (float3)(lights[4], lights[5], lights[6]), - (convert_float3(voxel) + offset) - (*cam_pos), - face_mask * voxel_step - ) - ); + if (cast_light_intersection_ray( + map, + map_dim, + (float3)(lights[4], lights[5], lights[6]) - (convert_float3(voxel)), + (convert_float3(voxel) - convert_float3(face_mask * voxel_step)), + lights, + light_count + )) { + + write_imagef(image, pixel, voxel_color); + //write_imagef(image, pixel, voxel_color); return; + } - float3 vox = convert_float3(voxel); - float3 norm = normalize(convert_float3(face_mask) * convert_float3(voxel_step)); - float4 color = (float4)(0.95, 0.00, 0.25, 1.00); - - - write_imagef(image, pixel, - cast_light_rays( - ray_dir, - vox, - color, - norm , - lights, - light_count - )); - - return; + write_imagef( + image, + pixel, + view_light( + voxel_color, + (convert_float3(voxel) + offset) - (float3)(lights[4], lights[5], lights[6]), + (convert_float3(voxel) + offset) - (*cam_pos), + face_mask * voxel_step + ) + ); - + return; - case 6: - write_imagef(image, pixel, view_light((float4)(0.0, 0.239, 0.419, 0.3), (convert_float3(voxel) + offset) - (float3)(lights[4], lights[5], lights[6]), (convert_float3(voxel) + offset) - (*cam_pos), face_mask * voxel_step)); - //write_imagef(image, pixel, white_light(mix((float4)(0.73, 0.81, 0.89, 0.6), (float4)(0.0, 0.239, 0.419, 0.3), 1.0 - max((dist / 700.0f) - 0.3f, (float)0)), (float3)(lights[7], lights[8], lights[9]), face_mask)); - return; - default: - write_imagef(image, pixel, (float4)(.30, .10, .10, 1.00)); - continue; - } } dist++; diff --git a/src/Old_Map.cpp b/src/Old_Map.cpp index 8ce46f3..b19d52d 100644 --- a/src/Old_Map.cpp +++ b/src/Old_Map.cpp @@ -37,7 +37,7 @@ void Old_Map::generate_terrain() { int DATA_SIZE = dimensions.x + 1; //an initial seed value for the corners of the data //srand(f_rand()); - double SEED = rand() % 25 + 55; + double SEED = rand() % 30 + 55; //seed the data set_sample(0, 0, SEED); diff --git a/src/main.cpp b/src/main.cpp index 4e60361..904ee26 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -116,7 +116,7 @@ int main() { // Light for the currently non functional Bling Phong shader Light l; l.direction_cartesian = sf::Vector3f(-0.2f, -0.2f, -1.5f); - l.position = sf::Vector3f(100.0f, 100.0f, 500.0f); + l.position = sf::Vector3f(100.0f, 500.0f, 100.0f); l.rgbi = sf::Vector4f(0.3f, 0.4f, 0.3f, 1.0f); std::vector light_vec; @@ -180,7 +180,7 @@ int main() { raycaster->test_edit_viewport(WINDOW_X, WINDOW_Y, w -= 5, h -= 5); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::L)) { - light_vec.at(0).position.y += 0.5; + light_vec.at(0).position = camera->get_position(); } // Time keeping