grid based
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / scene_standard_alphatest.h
index 46c1976a7933631ca933439b49578fc4ee4e78cf..5cab9a80321f73403d266eddffd0b14d397a18a1 100644 (file)
@@ -84,12 +84,16 @@ static struct vg_shader _shader_scene_standard_alphatest = {
 "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"
@@ -205,16 +209,19 @@ static struct vg_shader _shader_scene_standard_alphatest = {
 "   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"
-"   return light_colour*attenuation;\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"
+"   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"
@@ -235,7 +242,7 @@ static struct vg_shader _shader_scene_standard_alphatest = {
 "   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"
@@ -382,7 +389,7 @@ static struct vg_shader _shader_scene_standard_alphatest = {
 "}\n"
 "\n"
 "\n"
-"#line     13        0 \n"
+"#line     14        0 \n"
 "\n"
 "float sdLine( vec3 p, vec3 a, vec3 b )\n"
 "{\n"
@@ -409,6 +416,69 @@ static struct vg_shader _shader_scene_standard_alphatest = {
 "   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"
@@ -420,85 +490,45 @@ static struct vg_shader _shader_scene_standard_alphatest = {
 "   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"
+"   vec3 cube_coord = (aWorldCo - g_cube_min.xyz) * g_cube_inv_range.xyz;\n"
+"        cube_coord = floor( cube_coord );\n"
 "\n"
 "   if( g_debug_indices == 1 )\n"
 "   {\n"
-"      float rings = min( fract(distance(light_co_0.xyz,aWorldCo)),\n"
-"                      min( fract(distance(light_co_1.xyz,aWorldCo)),\n"
-"                           fract(distance(light_co_2.xyz,aWorldCo)) ) \n"
-"                        );\n"
-"               \n"
-"      return vec3(fract(light_indices.x * 0.125), fract(light_indices.y*0.125),\n"
-"                  fract(light_indices.z * 0.125 )) + (rings-0.5) * 0.25;\n"
+"      return rand33(cube_coord);\n"
 "   }\n"
 "\n"
 "   if( g_debug_complexity == 1 )\n"
 "   {\n"
-"      return vec3(1.0,0.0,0.0) * ( light_indices.w/3.0 );\n"
-"   }\n"
+"      ivec3 coord = ivec3( cube_coord );\n"
+"      uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 );\n"
 "\n"
-"   if( light_indices.w >= 1 )\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"
-"                       * step( world.day_phase, light_colour_0.w );\n"
-"\n"
-"      if( light_indices.w >= 2 )\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"
-"                          * step( world.day_phase, light_colour_1.w );\n"
-"\n"
-"         if( light_indices.w >= 3 )\n"
-"         {\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"
-"                             * step( world.day_phase, light_colour_2.w );\n"
-"         }\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"
-"   vec3 fog_colour = scene_sky( -halfview, world );\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"
@@ -558,6 +588,7 @@ static GLuint _uniform_scene_standard_alphatest_uBoard1;
 static GLuint _uniform_scene_standard_alphatest_uCamera;
 static GLuint _uniform_scene_standard_alphatest_uPlane;
 static GLuint _uniform_scene_standard_alphatest_uLightsArray;
+static GLuint _uniform_scene_standard_alphatest_uLightsIndex;
 static GLuint _uniform_scene_standard_alphatest_g_world_depth;
 static void shader_scene_standard_alphatest_uMdl(m4x3f m){
    glUniformMatrix4x3fv(_uniform_scene_standard_alphatest_uMdl,1,GL_FALSE,(float*)m);
@@ -604,6 +635,7 @@ static void shader_scene_standard_alphatest_link(void){
    _uniform_scene_standard_alphatest_uCamera = glGetUniformLocation( _shader_scene_standard_alphatest.id, "uCamera" );
    _uniform_scene_standard_alphatest_uPlane = glGetUniformLocation( _shader_scene_standard_alphatest.id, "uPlane" );
    _uniform_scene_standard_alphatest_uLightsArray = glGetUniformLocation( _shader_scene_standard_alphatest.id, "uLightsArray" );
+   _uniform_scene_standard_alphatest_uLightsIndex = glGetUniformLocation( _shader_scene_standard_alphatest.id, "uLightsIndex" );
    _uniform_scene_standard_alphatest_g_world_depth = glGetUniformLocation( _shader_scene_standard_alphatest.id, "g_world_depth" );
 }
 #endif /* SHADER_scene_standard_alphatest_H */