From 1c750451f390cec28d90c63980bd6a45a6343221 Mon Sep 17 00:00:00 2001 From: MitchellHansen Date: Sun, 2 Oct 2016 23:42:13 -0700 Subject: [PATCH] Added a 1/4 working phong shading function, breaks when the light direction in in negative values. The normals of voxels also are symmetrical which needs to be fixed. --- include/Map.h | 96 ++++++++++++++++++------------------ kernels/ray_caster_kernel.cl | 64 ++++++++++++++++++++++-- src/main.cpp | 29 ++++++++++- 3 files changed, 135 insertions(+), 54 deletions(-) diff --git a/include/Map.h b/include/Map.h index 44b4f1c..e69b023 100644 --- a/include/Map.h +++ b/include/Map.h @@ -74,7 +74,9 @@ public: list[i] = 0; } - + for (int i = 0; i < dim.x * dim.y; i++) { + height_map[i] = 0; + } //for (int x = -dim.x / 2; x < dim.x/2; x++) { // for (int y = -dim.y / 2; y < dim.y/2; y++) { @@ -88,55 +90,55 @@ public: // } //} - int xx = 0; - int yy = 0; - for (int x = -dim.x / 2; x < dim.x / 2; x++) { - for (int y = -dim.y / 2; y < dim.y / 2; y++) { - - double z = 150; - //for (int x = 0; x < dim.x; x++) { - // for (int y = 0; y < dim.y; y++) { - double height = 0; - - z += -x*2 * std::sin(std::sqrt(abs(x*2 - y*2 - 47))) - - (y*2 + 47) * std::sin(std::sqrt(std::abs(y*2 + 47 + x*2 / 2))); - - - //z += x * std::sin(std::sqrt(std::abs(y - x + 1))) * - // std::cos(std::sqrt(std::abs(y + x + 1))) + - // (y + 1) * - // std::cos(std::sqrt(std::abs(y - x + 1))) * - // std::sin(std::sqrt(std::abs(y + x + 1))); - - // Pathological - //z += 0.5 + - // (std::pow(std::sin(std::sqrt(100 * std::pow(x/20, 2) + std::pow(y/20, 2))), 2) - 0.5) / - // (1 + 0.001 * std::pow(std::pow(x/20, 2) - 2 * x/20 * y/20 + std::pow(y/20, 2), 2)); - - // Ackleys - //z += 20 + M_E - - // (20 / (std::pow(M_E, 0.2) * std::sqrt((std::pow(x / 16.0, 2) + std::pow(y / 16.0, 2) + 1) / 2))) - - // std::pow(M_E, 0.5 * std::cos(2 * M_PI * x / 16.0) + cos(2 * M_PI * y / 16.0)); - - // - //z += -20 * std::pow(M_E, -0.2 * sqrt(0.5 * std::pow(x/64.0, 2) + std::pow(y/64.0, 2))) - std::pow(M_E, 0.5 * (cos(2 * M_PI * x/64.0) + (cos(2 * M_PI * y/64.0)))) + 20 + M_E; - - //list[x + dim.x * (y + dim.z * (int)height)] = 5; + //int xx = 0; + //int yy = 0; + //for (int x = -dim.x / 2; x < dim.x / 2; x++) { + // for (int y = -dim.y / 2; y < dim.y / 2; y++) { + + // double z = 150; + ////for (int x = 0; x < dim.x; x++) { + //// for (int y = 0; y < dim.y; y++) { + // double height = 0; + + // z += -x*2 * std::sin(std::sqrt(abs(x*2 - y*2 - 47))) - + // (y*2 + 47) * std::sin(std::sqrt(std::abs(y*2 + 47 + x*2 / 2))); + // + + // //z += x * std::sin(std::sqrt(std::abs(y - x + 1))) * + // // std::cos(std::sqrt(std::abs(y + x + 1))) + + // // (y + 1) * + // // std::cos(std::sqrt(std::abs(y - x + 1))) * + // // std::sin(std::sqrt(std::abs(y + x + 1))); + // + // // Pathological + // //z += 0.5 + + // // (std::pow(std::sin(std::sqrt(100 * std::pow(x/20, 2) + std::pow(y/20, 2))), 2) - 0.5) / + // // (1 + 0.001 * std::pow(std::pow(x/20, 2) - 2 * x/20 * y/20 + std::pow(y/20, 2), 2)); + + // // Ackleys + // //z += 20 + M_E - + // // (20 / (std::pow(M_E, 0.2) * std::sqrt((std::pow(x / 16.0, 2) + std::pow(y / 16.0, 2) + 1) / 2))) - + // // std::pow(M_E, 0.5 * std::cos(2 * M_PI * x / 16.0) + cos(2 * M_PI * y / 16.0)); + + // // + // //z += -20 * std::pow(M_E, -0.2 * sqrt(0.5 * std::pow(x/64.0, 2) + std::pow(y/64.0, 2))) - std::pow(M_E, 0.5 * (cos(2 * M_PI * x/64.0) + (cos(2 * M_PI * y/64.0)))) + 20 + M_E; + // + // //list[x + dim.x * (y + dim.z * (int)height)] = 5; - double m = 0.7; - while ((z*m) > 0){ - list[xx + dim.x * (yy + dim.z * (int)(z*m))] = 5; - z -= 1/m; - } - yy++; + // double m = 0.2; + // while ((z*m) > 0){ + // list[xx + dim.x * (yy + dim.z * (int)(z*m))] = 5; + // z -= 1/m; + // } + // yy++; - } - yy = 0; - xx++; - } - + // } + // yy = 0; + // xx++; + //} + // - return; + //return; diff --git a/kernels/ray_caster_kernel.cl b/kernels/ray_caster_kernel.cl index ac4d14e..aa33ac4 100644 --- a/kernels/ray_caster_kernel.cl +++ b/kernels/ray_caster_kernel.cl @@ -13,11 +13,45 @@ float4 white_light(float4 input, float3 light, int3 mask) { } +// 0 1 2 3 4 5 6 7 8 9 // {r, g, b, i, x, y, z, x', y', z'} -float4 cast_light_rays(float3 ray_origin, global float* lights, global int* light_count) { +float4 cast_light_rays(float3 eye_direction, float3 ray_origin, float4 voxel_color, float3 voxel_normal, global float* lights, global int* light_count) { + // set the ray origin to be where the initial ray intersected the voxel + // which side z, and the x and y position + + float ambient_constant = 0.5; + float intensity = 1.2; + for (int i = 0; i < *light_count; i++) { + + float3 light_direction = (lights[10 * i + 7], lights[10 * i + 8], lights[10 * i + 9]); + float c = 1.5; + + if (dot(light_direction, voxel_normal) > 0.0) { + float3 halfwayVector = normalize(light_direction + eye_direction); + float dot_prod = dot(voxel_normal, halfwayVector); + float specTmp = max((float)dot_prod, 0.0f); + intensity += pow(specTmp, c); + } + } + + /*if (get_global_id(0) == 0) + printf("%f", intensity);*/ + return voxel_color * (intensity) + ambient_constant; + + // for every light + // + // check if the light is within falloff distance + // every unit, light halfs + // + // if it is, cast a ray to that light and check for collisions. + // if ray exits voxel volume, assume unobstructed + // + // if ray intersects a voxel, dont influence the voxel color + // + // if it does } __kernel void min_kern( @@ -61,7 +95,8 @@ __kernel void min_kern( // offset is how far we are into a voxel, enables sub voxel movement float3 offset = ((*cam_pos) - floor(*cam_pos)) * convert_float3(voxel_step); - + + //offset.x += delta_t.x * convert_float((voxel_step.x < 0)); //offset -= delta_t * floor(offset / delta_t); @@ -81,6 +116,7 @@ __kernel void min_kern( intersection_t.z += delta_t.z; } + // use a ghetto ass rng to give rays a "fog" appearance int2 randoms = { 3, 14 }; uint seed = randoms.x + id; uint t = seed ^ (seed << 11); @@ -132,10 +168,28 @@ __kernel void min_kern( write_imagef(image, pixel, (float4)(.25, .00, .25, 1.00)); return; case 5: - //write_imagef(image, pixel, (float4)(.25, .00, .25, 1.00)); - write_imagef(image, pixel, white_light((float4)(.25, .32, .14, 0.2), (float3)(lights[7], lights[8], lights[9]), mask)); - //cast_light_rays(voxel, lights, light_count) + { + //write_imagef(image, pixel, (float4)(.00, .00, + 0.5, 1.00)); + //write_imagef(image, pixel, white_light((float4)(.35, .00, ((1.0 - 0) / (128 - 0) * (voxel.z - 128)) + 1, 0.2), (float3)(lights[7], lights[8], lights[9]), mask)); + + + float3 vox = convert_float3(voxel); + float3 norm = normalize(convert_float3(mask)); + float4 color = (float4)(0.25, 0.00, 0.25, 1.00); + + + write_imagef(image, pixel, + cast_light_rays( + ray_dir, + vox, + color, + norm , + lights, + light_count + )); + return; + } case 6: write_imagef(image, pixel, (float4)(.30, .80, .10, 1.00)); return; diff --git a/src/main.cpp b/src/main.cpp index 471a1ff..a05db0a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -143,8 +143,9 @@ int main() { c.create_buffer("cam_pos_buffer", sizeof(float) * 4, (void*)camera.get_position_pointer(), CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR); // {r, g, b, i, x, y, z, x', y', z'} - float light[] = { 0.4, 0.8, 0.1, 1, 50, 50, 50, 1.1, 0.4, 0.7}; - c.create_buffer("light_buffer", sizeof(float) * 10, light); + sf::Vector3f v = Normalize(sf::Vector3f(1.0, 1.0, 0.0)); + float light[] = { 0.4, 0.8, 0.1, 1, 50, 50, 50, v.x, v.y, v.z}; + c.create_buffer("light_buffer", sizeof(float) * 10, light, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR); int light_count = 1; c.create_buffer("light_count_buffer", sizeof(int), &light_count); @@ -222,6 +223,10 @@ int main() { debug_text cam_text_mov_y(5, 30, &mp->y, "Y: "); debug_text cam_text_mov_z(6, 30, &mp->y, "Z: "); //debug_text cam_text_z(3, 30, &p->z); + + debug_text light_x (7, 30, &light[7], "X: "); + debug_text light_y(8, 30, &light[8], "Y: "); + debug_text light_z(9, 30, &light[9], "Z: "); // =============================================================================== // Mouse capture @@ -304,6 +309,22 @@ int main() { // ==== DELTA TIME LOCKED ==== } + float l[] = { + light[9] * sin(delta_time) + light[7] * cos(delta_time), + light[8], + light[9] * cos(delta_time) - light[7] * sin(delta_time) + }; + + float l2[] = { + l[0] * cos(delta_time) - l[2] * sin(delta_time), + l[0] * sin(delta_time) + l[2] * cos(delta_time), + l[2] + }; + + light[7] = l[0]; + light[8] = l[1]; + light[9] = l[2]; + // ==== FPS LOCKED ==== camera.update(delta_time); @@ -326,6 +347,10 @@ int main() { cam_text_mov_x.draw(&window); cam_text_mov_y.draw(&window); cam_text_mov_z.draw(&window); + + light_x.draw(&window); + light_y.draw(&window); + light_z.draw(&window); window.display();