grid based
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / scene_vertex_blend.h
index 028392d04379bf0e70740f7d92779ffa9786625e..60945cd4d59730a7bf3fcdef07dba12a37c5105a 100644 (file)
@@ -83,12 +83,16 @@ static struct vg_shader _shader_scene_vertex_blend = {
 "flat in ivec4 light_indices;\n"
 "\n"
 "uniform samplerBuffer uLightsArray;\n"
+"uniform usampler3D uLightsIndex;\n"
 "\n"
 "#line       1        1 \n"
 "layout (location = 0) out vec4 oColour;\n"
 "\n"
 "layout (std140) uniform ub_world_lighting\n"
 "{\n"
+"   vec4 g_cube_min;\n"
+"   vec4 g_cube_inv_range;\n"
+"\n"
 "   vec4 g_light_colours[3];\n"
 "   vec4 g_light_directions[3];\n"
 "   vec4 g_ambient_colour;\n"
@@ -101,6 +105,9 @@ static struct vg_shader _shader_scene_vertex_blend = {
 "   int g_light_preview;\n"
 "   int g_shadow_samples;\n"
 "\n"
+"   int g_debug_indices;\n"
+"   int g_debug_complexity;\n"
+"\n"
 "   // g_time ?\n"
 "\n"
 "   //vec4 g_point_light_positions[32];\n"
@@ -201,16 +208,19 @@ static struct vg_shader _shader_scene_vertex_blend = {
 "   return pow(max(dot( halfview, specdir ), 0.0), exponent);\n"
 "}\n"
 "\n"
-"vec3 newlight_compute_quadratic( vec3 wnormal, vec3 halfview, \n"
+"vec3 newlight_compute_quadratic( vec3 wnormal, float max_dist,\n"
 "                                 vec3 light_colour, vec3 light_pos )\n"
 "{\n"
-"   vec3 light_delta = (light_pos-aWorldCo) * 10.0;\n"
+"   vec3 light_delta = light_pos-aWorldCo;\n"
 "\n"
-"   float quadratic = dot(light_delta,light_delta);\n"
-"   float attenuation = 1.0f/( 1.0f + quadratic );\n"
-"   attenuation *= max( 0.0, dot( normalize(light_delta), wnormal ) );\n"
+"   float dist2 = dot(light_delta,light_delta);\n"
+"\n"
+"   float quadratic = dist2*100.0;\n"
+"   float attenuation  = 1.0f/( 1.0f + quadratic );\n"
+"         attenuation *= max( dot( normalize(light_delta), wnormal ), 0.0 );\n"
 "\n"
-"   return light_colour*attenuation;\n"
+"   float falloff = max( 0.0, 1.0-(dist2*max_dist) );\n"
+"   return light_colour * attenuation * falloff;\n"
 "}\n"
 "\n"
 "vec3 newlight_compute_spot( vec3 wnormal, vec3 halfview, \n"
@@ -231,7 +241,7 @@ static struct vg_shader _shader_scene_vertex_blend = {
 "   return light_colour*attenuation*falloff;\n"
 "}\n"
 "\n"
-"#line     12        0 \n"
+"#line     13        0 \n"
 "#line       1        2 \n"
 "const vec3  DAYSKY_COLOUR   = vec3( 0.37, 0.54, 0.97 );\n"
 "const vec3  NIGHTSKY_COLOUR = vec3( 0.03, 0.05, 0.20 );\n"
@@ -378,7 +388,7 @@ static struct vg_shader _shader_scene_vertex_blend = {
 "}\n"
 "\n"
 "\n"
-"#line     13        0 \n"
+"#line     14        0 \n"
 "\n"
 "float sdLine( vec3 p, vec3 a, vec3 b )\n"
 "{\n"
@@ -405,6 +415,69 @@ static struct vg_shader _shader_scene_vertex_blend = {
 "   return mix( vfrag, colour, min( 1.0, dist ) );\n"
 "}\n"
 "\n"
+"vec3 rand33(vec3 p3)\n"
+"{\n"
+"      p3 = fract(p3 * vec3(.1031, .1030, .0973));\n"
+"   p3 += dot(p3, p3.yxz+33.33);\n"
+"   return fract((p3.xxy + p3.yxx)*p3.zyx);\n"
+"}\n"
+"\n"
+"vec3 scene_calculate_light( int light_index, \n"
+"                            vec3 halfview, vec3 co, vec3 normal )\n"
+"{\n"
+"   vec4 light_colour = texelFetch( uLightsArray, light_index+0 );\n"
+"   vec4 light_co     = texelFetch( uLightsArray, light_index+1 );\n"
+"   vec4 light_dir    = texelFetch( uLightsArray, light_index+2 );\n"
+"\n"
+"   vec3 light_delta = light_co.xyz-co;\n"
+"   float dist2 = dot(light_delta,light_delta);\n"
+"\n"
+"   light_delta = normalize( light_delta );\n"
+"\n"
+"   float quadratic = dist2*100.0;\n"
+"   float attenuation  = 1.0f/( 1.0f + quadratic );\n"
+"         attenuation *= max( dot( light_delta, normal ), 0.0 );\n"
+"\n"
+"   float falloff = max( 0.0, 1.0-(dist2*light_co.w) );\n"
+"\n"
+"   if( light_dir.w < 0.999999 )\n"
+"   {\n"
+"      float spot_theta = max( 0.0, dot( light_delta, -light_dir.xyz ) );\n"
+"      falloff *= max( 0.0, (spot_theta - light_dir.w) / (1.0-light_dir.w) );\n"
+"   }\n"
+"\n"
+"   return light_colour.rgb * attenuation * falloff;\n"
+"}\n"
+"\n"
+"vec3 scene_calculate_packed_light_patch( uint packed_index, \n"
+"                                         vec3 halfview, vec3 co, vec3 normal )\n"
+"{\n"
+"   uint light_count = packed_index & 0x3u;\n"
+"\n"
+"   vec3 l = vec3(0.0);\n"
+"\n"
+"   if( light_count >= 1u )\n"
+"   {\n"
+"      int index_0 = int( ((packed_index >>  2u) & 0x3ffu) * 3u );\n"
+"      int index_1 = int( ((packed_index >> 12u) & 0x3ffu) * 3u );\n"
+"      int index_2 = int( ((packed_index >> 22u) & 0x3ffu) * 3u );\n"
+"\n"
+"      l += scene_calculate_light( index_0, halfview, co, normal );\n"
+"\n"
+"      if( light_count >= 2u )\n"
+"      {\n"
+"         l += scene_calculate_light( index_1, halfview, co, normal );\n"
+"\n"
+"         if( light_count >= 3u )\n"
+"         {\n"
+"            l += scene_calculate_light( index_2, halfview, co, normal );\n"
+"         }\n"
+"      }\n"
+"   }\n"
+"\n"
+"   return l;\n"
+"}\n"
+"\n"
 "vec3 scene_do_lighting( vec3 diffuse, vec3 wnormal )\n"
 "{\n"
 "   world_info world;\n"
@@ -416,63 +489,45 @@ static struct vg_shader _shader_scene_vertex_blend = {
 "   halfview /= fdist;\n"
 "\n"
 "   vec3 total_light = vec3(0.0);\n"
-"\n"
-"\n"
 "   float world_shadow = newlight_compute_sun_shadow( world.sun_dir \n"
-"                                          * (1.0/(max(world.sun_dir.y,0.0)+0.2)) );\n"
+"                                    * (1.0/(max(world.sun_dir.y,0.0)+0.2)) );\n"
 "   float board_shadow = compute_board_shadow();\n"
 "\n"
 "   total_light += scene_lighting( wnormal, min( board_shadow, world_shadow ), \n"
 "                                  halfview, world );\n"
 "\n"
-"   //total_light += scene_lighting_old( wnormal, world );\n"
-"\n"
-"   // Compute the other lights that exist in the map, not effected by the sun\n"
-"   // shadow\n"
-"\n"
-"   // read lights\n"
-"   vec4 light_colour_0 = texelFetch( uLightsArray, light_indices.x*3+0 );\n"
-"   vec4 light_colour_1 = texelFetch( uLightsArray, light_indices.y*3+0 );\n"
-"   vec4 light_colour_2 = texelFetch( uLightsArray, light_indices.z*3+0 );\n"
-"   vec4 light_co_0     = texelFetch( uLightsArray, light_indices.x*3+1 );\n"
-"   vec4 light_co_1     = texelFetch( uLightsArray, light_indices.y*3+1 );\n"
-"   vec4 light_co_2     = texelFetch( uLightsArray, light_indices.z*3+1 );\n"
-"   vec4 light_dir_0    = texelFetch( uLightsArray, light_indices.x*3+2 );\n"
-"   vec4 light_dir_1    = texelFetch( uLightsArray, light_indices.y*3+2 );\n"
-"   vec4 light_dir_2    = texelFetch( uLightsArray, light_indices.z*3+2 );\n"
-"\n"
-"   //return vec3(fract(distance(light_co_0.xyz,aWorldCo)),\n"
-"   //            fract(distance(light_co_1.xyz,aWorldCo)),\n"
-"   //            fract(distance(light_co_2.xyz,aWorldCo)));\n"
-"\n"
-"  // return vec3(fract(light_indices.x * 0.125), fract(light_indices.y*0.125),\n"
-"  //             fract(light_indices.z * 0.125 ));\n"
-"\n"
-"   total_light += newlight_compute_spot\n"
-"                  ( \n"
-"                     wnormal, halfview,\n"
-"                     light_colour_0.rgb,\n"
-"                     light_co_0.xyz,\n"
-"                     light_dir_0\n"
-"                  ) * board_shadow;\n"
-"\n"
-"   total_light += newlight_compute_spot\n"
-"                  ( \n"
-"                     wnormal, halfview,\n"
-"                     light_colour_1.rgb,\n"
-"                     light_co_1.xyz,\n"
-"                     light_dir_1\n"
-"                  ) * board_shadow;\n"
-"   total_light += newlight_compute_spot\n"
-"                  ( \n"
-"                     wnormal, halfview,\n"
-"                     light_colour_2.rgb,\n"
-"                     light_co_2.xyz,\n"
-"                     light_dir_2\n"
-"                  ) * board_shadow;\n"
+"   vec3 cube_coord = (aWorldCo - g_cube_min.xyz) * g_cube_inv_range.xyz;\n"
+"        cube_coord = floor( cube_coord );\n"
 "\n"
-"   vec3 fog_colour = scene_sky( -halfview, world );\n"
+"   if( g_debug_indices == 1 )\n"
+"   {\n"
+"      return rand33(cube_coord);\n"
+"   }\n"
+"\n"
+"   if( g_debug_complexity == 1 )\n"
+"   {\n"
+"      ivec3 coord = ivec3( cube_coord );\n"
+"      uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 );\n"
+"\n"
+"      uint light_count = (index_sample.x & 0x3u) + (index_sample.y & 0x3u);\n"
+"      return vec3( float(light_count)*(1.0/6.0), 0.0, 0.5 );\n"
+"   }\n"
+"\n"
+"   // FIXME: this should absolutely must be clamped!\n"
 "   \n"
+"   ivec3 coord = ivec3( cube_coord );\n"
+"   uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 );\n"
+"\n"
+"   total_light += \n"
+"      scene_calculate_packed_light_patch( index_sample.x,\n"
+"                                          halfview, aWorldCo, wnormal ) \n"
+"                                          * board_shadow;\n"
+"   total_light += \n"
+"      scene_calculate_packed_light_patch( index_sample.y,\n"
+"                                          halfview, aWorldCo, wnormal )\n"
+"                                          * board_shadow;\n"
+"\n"
+"   vec3 fog_colour = scene_sky( -halfview, world );\n"
 "   return scene_apply_fog( diffuse * total_light, fog_colour, fdist );\n"
 "}\n"
 "\n"
@@ -546,6 +601,7 @@ static GLuint _uniform_scene_vertex_blend_uCamera;
 static GLuint _uniform_scene_vertex_blend_uBoard0;
 static GLuint _uniform_scene_vertex_blend_uBoard1;
 static GLuint _uniform_scene_vertex_blend_uLightsArray;
+static GLuint _uniform_scene_vertex_blend_uLightsIndex;
 static GLuint _uniform_scene_vertex_blend_g_world_depth;
 static void shader_scene_vertex_blend_uMdl(m4x3f m){
    glUniformMatrix4x3fv(_uniform_scene_vertex_blend_uMdl,1,GL_FALSE,(float*)m);
@@ -588,6 +644,7 @@ static void shader_scene_vertex_blend_link(void){
    _uniform_scene_vertex_blend_uBoard0 = glGetUniformLocation( _shader_scene_vertex_blend.id, "uBoard0" );
    _uniform_scene_vertex_blend_uBoard1 = glGetUniformLocation( _shader_scene_vertex_blend.id, "uBoard1" );
    _uniform_scene_vertex_blend_uLightsArray = glGetUniformLocation( _shader_scene_vertex_blend.id, "uLightsArray" );
+   _uniform_scene_vertex_blend_uLightsIndex = glGetUniformLocation( _shader_scene_vertex_blend.id, "uLightsIndex" );
    _uniform_scene_vertex_blend_g_world_depth = glGetUniformLocation( _shader_scene_vertex_blend.id, "g_world_depth" );
 }
 #endif /* SHADER_scene_vertex_blend_H */