_pack_ = 1
_fields_ = [("type",c_uint32),
("colour",c_float*4),
- ("angle",c_float)]
+ ("angle",c_float),
+ ("range",c_float)]
def encode_obj(_, node, node_def):
#{
_.colour[1] = data.color[1]
_.colour[2] = data.color[2]
_.colour[3] = data.energy
+ _.range = data.cutoff_distance # this has to be manually set
+ # TODO: At some point, automate a min
+ # threshold value
if obj.data.type == 'POINT':
#{
elif obj.data.type == 'SPOT':
#{
_.type = 1
- _.angle = math.cos(data.spot_size*0.5)
+ _.angle = data.spot_size*0.5
#}
if data.cv_data.bp0:
type;
v4f colour; /* RGB, Energy */
- float angle;
+ float angle, range;
};
#pragma pack(pop)
flat in ivec4 light_indices;
uniform samplerBuffer uLightsArray;
+uniform usampler3D uLightsIndex;
#include "common_world.glsl"
#include "light_clearskies.glsl"
return mix( vfrag, colour, min( 1.0, dist ) );
}
+vec3 rand33(vec3 p3)
+{
+ p3 = fract(p3 * vec3(.1031, .1030, .0973));
+ p3 += dot(p3, p3.yxz+33.33);
+ return fract((p3.xxy + p3.yxx)*p3.zyx);
+}
+
+vec3 scene_calculate_light( int light_index,
+ vec3 halfview, vec3 co, vec3 normal )
+{
+ vec4 light_colour = texelFetch( uLightsArray, light_index+0 );
+ vec4 light_co = texelFetch( uLightsArray, light_index+1 );
+ vec4 light_dir = texelFetch( uLightsArray, light_index+2 );
+
+ vec3 light_delta = light_co.xyz-co;
+ float dist2 = dot(light_delta,light_delta);
+
+ light_delta = normalize( light_delta );
+
+ float quadratic = dist2*100.0;
+ float attenuation = 1.0f/( 1.0f + quadratic );
+ attenuation *= max( dot( light_delta, normal ), 0.0 );
+
+ float falloff = max( 0.0, 1.0-(dist2*light_co.w) );
+
+ if( light_dir.w < 0.999999 )
+ {
+ float spot_theta = max( 0.0, dot( light_delta, -light_dir.xyz ) );
+ falloff *= max( 0.0, (spot_theta - light_dir.w) / (1.0-light_dir.w) );
+ }
+
+ return light_colour.rgb * attenuation * falloff;
+}
+
+vec3 scene_calculate_packed_light_patch( uint packed_index,
+ vec3 halfview, vec3 co, vec3 normal )
+{
+ uint light_count = packed_index & 0x3u;
+
+ vec3 l = vec3(0.0);
+
+ if( light_count >= 1u )
+ {
+ int index_0 = int( ((packed_index >> 2u) & 0x3ffu) * 3u );
+ int index_1 = int( ((packed_index >> 12u) & 0x3ffu) * 3u );
+ int index_2 = int( ((packed_index >> 22u) & 0x3ffu) * 3u );
+
+ l += scene_calculate_light( index_0, halfview, co, normal );
+
+ if( light_count >= 2u )
+ {
+ l += scene_calculate_light( index_1, halfview, co, normal );
+
+ if( light_count >= 3u )
+ {
+ l += scene_calculate_light( index_2, halfview, co, normal );
+ }
+ }
+ }
+
+ return l;
+}
+
vec3 scene_do_lighting( vec3 diffuse, vec3 wnormal )
{
world_info world;
halfview /= fdist;
vec3 total_light = vec3(0.0);
-
-
float world_shadow = newlight_compute_sun_shadow( world.sun_dir
- * (1.0/(max(world.sun_dir.y,0.0)+0.2)) );
+ * (1.0/(max(world.sun_dir.y,0.0)+0.2)) );
float board_shadow = compute_board_shadow();
total_light += scene_lighting( wnormal, min( board_shadow, world_shadow ),
halfview, world );
- //total_light += scene_lighting_old( wnormal, world );
-
- // Compute the other lights that exist in the map, not effected by the sun
- // shadow
-
- // read lights
- vec4 light_colour_0 = texelFetch( uLightsArray, light_indices.x*3+0 );
- vec4 light_colour_1 = texelFetch( uLightsArray, light_indices.y*3+0 );
- vec4 light_colour_2 = texelFetch( uLightsArray, light_indices.z*3+0 );
- vec4 light_co_0 = texelFetch( uLightsArray, light_indices.x*3+1 );
- vec4 light_co_1 = texelFetch( uLightsArray, light_indices.y*3+1 );
- vec4 light_co_2 = texelFetch( uLightsArray, light_indices.z*3+1 );
- vec4 light_dir_0 = texelFetch( uLightsArray, light_indices.x*3+2 );
- vec4 light_dir_1 = texelFetch( uLightsArray, light_indices.y*3+2 );
- vec4 light_dir_2 = texelFetch( uLightsArray, light_indices.z*3+2 );
+ vec3 cube_coord = (aWorldCo - g_cube_min.xyz) * g_cube_inv_range.xyz;
+ cube_coord = floor( cube_coord );
if( g_debug_indices == 1 )
{
- float rings = min( fract(distance(light_co_0.xyz,aWorldCo)),
- min( fract(distance(light_co_1.xyz,aWorldCo)),
- fract(distance(light_co_2.xyz,aWorldCo)) )
- );
-
- return vec3(fract(light_indices.x * 0.125), fract(light_indices.y*0.125),
- fract(light_indices.z * 0.125 )) + (rings-0.5) * 0.25;
+ return rand33(cube_coord);
}
if( g_debug_complexity == 1 )
{
- return vec3(1.0,0.0,0.0) * ( light_indices.w/3.0 );
- }
+ ivec3 coord = ivec3( cube_coord );
+ uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 );
- if( light_indices.w >= 1 )
- {
- total_light += newlight_compute_spot
- (
- wnormal, halfview,
- light_colour_0.rgb,
- light_co_0.xyz,
- light_dir_0
- ) * board_shadow
- * step( world.day_phase, light_colour_0.w );
-
- if( light_indices.w >= 2 )
- {
- total_light += newlight_compute_spot
- (
- wnormal, halfview,
- light_colour_1.rgb,
- light_co_1.xyz,
- light_dir_1
- ) * board_shadow
- * step( world.day_phase, light_colour_1.w );
-
- if( light_indices.w >= 3 )
- {
- total_light += newlight_compute_spot
- (
- wnormal, halfview,
- light_colour_2.rgb,
- light_co_2.xyz,
- light_dir_2
- ) * board_shadow
- * step( world.day_phase, light_colour_2.w );
- }
- }
+ uint light_count = (index_sample.x & 0x3u) + (index_sample.y & 0x3u);
+ return vec3( float(light_count)*(1.0/6.0), 0.0, 0.5 );
}
- vec3 fog_colour = scene_sky( -halfview, world );
+ // FIXME: this should absolutely must be clamped!
+ ivec3 coord = ivec3( cube_coord );
+ uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 );
+
+ total_light +=
+ scene_calculate_packed_light_patch( index_sample.x,
+ halfview, aWorldCo, wnormal )
+ * board_shadow;
+ total_light +=
+ scene_calculate_packed_light_patch( index_sample.y,
+ halfview, aWorldCo, wnormal )
+ * board_shadow;
+
+ vec3 fog_colour = scene_sky( -halfview, world );
return scene_apply_fog( diffuse * total_light, fog_colour, fdist );
}
layout (std140) uniform ub_world_lighting
{
+ vec4 g_cube_min;
+ vec4 g_cube_inv_range;
+
vec4 g_light_colours[3];
vec4 g_light_directions[3];
vec4 g_ambient_colour;
return pow(max(dot( halfview, specdir ), 0.0), exponent);
}
-vec3 newlight_compute_quadratic( vec3 wnormal, vec3 halfview,
+vec3 newlight_compute_quadratic( vec3 wnormal, float max_dist,
vec3 light_colour, vec3 light_pos )
{
- vec3 light_delta = (light_pos-aWorldCo) * 10.0;
+ vec3 light_delta = light_pos-aWorldCo;
- float quadratic = dot(light_delta,light_delta);
- float attenuation = 1.0f/( 1.0f + quadratic );
- attenuation *= max( 0.0, dot( normalize(light_delta), wnormal ) );
+ float dist2 = dot(light_delta,light_delta);
+
+ float quadratic = dist2*100.0;
+ float attenuation = 1.0f/( 1.0f + quadratic );
+ attenuation *= max( dot( normalize(light_delta), wnormal ), 0.0 );
- return light_colour*attenuation;
+ float falloff = max( 0.0, 1.0-(dist2*max_dist) );
+ return light_colour * attenuation * falloff;
}
vec3 newlight_compute_spot( vec3 wnormal, vec3 halfview,
"\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"
" 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"
"\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"
" 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"
"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"
" 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"
" 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"
"}\n"
"\n"
"\n"
-"#line 13 0 \n"
+"#line 14 0 \n"
"\n"
"float sdLine( vec3 p, vec3 a, vec3 b )\n"
"{\n"
" 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"
" 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"
static GLuint _uniform_scene_depth_uBoard0;
static GLuint _uniform_scene_depth_uBoard1;
static GLuint _uniform_scene_depth_uLightsArray;
+static GLuint _uniform_scene_depth_uLightsIndex;
static GLuint _uniform_scene_depth_g_world_depth;
static void shader_scene_depth_uMdl(m4x3f m){
glUniformMatrix4x3fv(_uniform_scene_depth_uMdl,1,GL_FALSE,(float*)m);
_uniform_scene_depth_uBoard0 = glGetUniformLocation( _shader_scene_depth.id, "uBoard0" );
_uniform_scene_depth_uBoard1 = glGetUniformLocation( _shader_scene_depth.id, "uBoard1" );
_uniform_scene_depth_uLightsArray = glGetUniformLocation( _shader_scene_depth.id, "uLightsArray" );
+ _uniform_scene_depth_uLightsIndex = glGetUniformLocation( _shader_scene_depth.id, "uLightsIndex" );
_uniform_scene_depth_g_world_depth = glGetUniformLocation( _shader_scene_depth.id, "g_world_depth" );
}
#endif /* SHADER_scene_depth_H */
"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"
" 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"
" 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"
"}\n"
"\n"
"\n"
-"#line 13 0 \n"
+"#line 14 0 \n"
"\n"
"float sdLine( vec3 p, vec3 a, vec3 b )\n"
"{\n"
" 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"
" 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"
static GLuint _uniform_scene_position_uBoard0;
static GLuint _uniform_scene_position_uBoard1;
static GLuint _uniform_scene_position_uLightsArray;
+static GLuint _uniform_scene_position_uLightsIndex;
static GLuint _uniform_scene_position_g_world_depth;
static void shader_scene_position_uMdl(m4x3f m){
glUniformMatrix4x3fv(_uniform_scene_position_uMdl,1,GL_FALSE,(float*)m);
_uniform_scene_position_uBoard0 = glGetUniformLocation( _shader_scene_position.id, "uBoard0" );
_uniform_scene_position_uBoard1 = glGetUniformLocation( _shader_scene_position.id, "uBoard1" );
_uniform_scene_position_uLightsArray = glGetUniformLocation( _shader_scene_position.id, "uLightsArray" );
+ _uniform_scene_position_uLightsIndex = glGetUniformLocation( _shader_scene_position.id, "uLightsIndex" );
_uniform_scene_position_g_world_depth = glGetUniformLocation( _shader_scene_position.id, "g_world_depth" );
}
#endif /* SHADER_scene_position_H */
"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"
" 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"
" 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"
"}\n"
"\n"
"\n"
-"#line 13 0 \n"
+"#line 14 0 \n"
"\n"
"float sdLine( vec3 p, vec3 a, vec3 b )\n"
"{\n"
" 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"
" 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"
static GLuint _uniform_scene_route_uBoard0;
static GLuint _uniform_scene_route_uBoard1;
static GLuint _uniform_scene_route_uLightsArray;
+static GLuint _uniform_scene_route_uLightsIndex;
static GLuint _uniform_scene_route_g_world_depth;
static void shader_scene_route_uMdl(m4x3f m){
glUniformMatrix4x3fv(_uniform_scene_route_uMdl,1,GL_FALSE,(float*)m);
_uniform_scene_route_uBoard0 = glGetUniformLocation( _shader_scene_route.id, "uBoard0" );
_uniform_scene_route_uBoard1 = glGetUniformLocation( _shader_scene_route.id, "uBoard1" );
_uniform_scene_route_uLightsArray = glGetUniformLocation( _shader_scene_route.id, "uLightsArray" );
+ _uniform_scene_route_uLightsIndex = glGetUniformLocation( _shader_scene_route.id, "uLightsIndex" );
_uniform_scene_route_g_world_depth = glGetUniformLocation( _shader_scene_route.id, "g_world_depth" );
}
#endif /* SHADER_scene_route_H */
"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"
" 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"
" 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"
"}\n"
"\n"
"\n"
-"#line 13 0 \n"
+"#line 14 0 \n"
"\n"
"float sdLine( vec3 p, vec3 a, vec3 b )\n"
"{\n"
" 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"
" 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"
static GLuint _uniform_scene_standard_uBoard0;
static GLuint _uniform_scene_standard_uBoard1;
static GLuint _uniform_scene_standard_uLightsArray;
+static GLuint _uniform_scene_standard_uLightsIndex;
static GLuint _uniform_scene_standard_g_world_depth;
static void shader_scene_standard_uMdl(m4x3f m){
glUniformMatrix4x3fv(_uniform_scene_standard_uMdl,1,GL_FALSE,(float*)m);
_uniform_scene_standard_uBoard0 = glGetUniformLocation( _shader_scene_standard.id, "uBoard0" );
_uniform_scene_standard_uBoard1 = glGetUniformLocation( _shader_scene_standard.id, "uBoard1" );
_uniform_scene_standard_uLightsArray = glGetUniformLocation( _shader_scene_standard.id, "uLightsArray" );
+ _uniform_scene_standard_uLightsIndex = glGetUniformLocation( _shader_scene_standard.id, "uLightsIndex" );
_uniform_scene_standard_g_world_depth = glGetUniformLocation( _shader_scene_standard.id, "g_world_depth" );
}
#endif /* SHADER_scene_standard_H */
"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"
" 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"
" 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"
"}\n"
"\n"
"\n"
-"#line 13 0 \n"
+"#line 14 0 \n"
"\n"
"float sdLine( vec3 p, vec3 a, vec3 b )\n"
"{\n"
" 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"
" 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"
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);
_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 */
"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"
" 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"
" 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"
"}\n"
"\n"
"\n"
-"#line 13 0 \n"
+"#line 14 0 \n"
"\n"
"float sdLine( vec3 p, vec3 a, vec3 b )\n"
"{\n"
" 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"
" 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"
static GLuint _uniform_scene_terrain_uBoard0;
static GLuint _uniform_scene_terrain_uBoard1;
static GLuint _uniform_scene_terrain_uLightsArray;
+static GLuint _uniform_scene_terrain_uLightsIndex;
static GLuint _uniform_scene_terrain_g_world_depth;
static void shader_scene_terrain_uMdl(m4x3f m){
glUniformMatrix4x3fv(_uniform_scene_terrain_uMdl,1,GL_FALSE,(float*)m);
_uniform_scene_terrain_uBoard0 = glGetUniformLocation( _shader_scene_terrain.id, "uBoard0" );
_uniform_scene_terrain_uBoard1 = glGetUniformLocation( _shader_scene_terrain.id, "uBoard1" );
_uniform_scene_terrain_uLightsArray = glGetUniformLocation( _shader_scene_terrain.id, "uLightsArray" );
+ _uniform_scene_terrain_uLightsIndex = glGetUniformLocation( _shader_scene_terrain.id, "uLightsIndex" );
_uniform_scene_terrain_g_world_depth = glGetUniformLocation( _shader_scene_terrain.id, "g_world_depth" );
}
#endif /* SHADER_scene_terrain_H */
"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"
" 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"
" 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"
"}\n"
"\n"
"\n"
-"#line 13 0 \n"
+"#line 14 0 \n"
"\n"
"float sdLine( vec3 p, vec3 a, vec3 b )\n"
"{\n"
" 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"
" 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"
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);
_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 */
"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"
" 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"
" 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"
"}\n"
"\n"
"\n"
-"#line 13 0 \n"
+"#line 14 0 \n"
"\n"
"float sdLine( vec3 p, vec3 a, vec3 b )\n"
"{\n"
" 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"
" 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"
static GLuint _uniform_scene_water_uShoreColour;
static GLuint _uniform_scene_water_uOceanColour;
static GLuint _uniform_scene_water_uLightsArray;
+static GLuint _uniform_scene_water_uLightsIndex;
static GLuint _uniform_scene_water_g_world_depth;
static void shader_scene_water_uMdl(m4x3f m){
glUniformMatrix4x3fv(_uniform_scene_water_uMdl,1,GL_FALSE,(float*)m);
_uniform_scene_water_uShoreColour = glGetUniformLocation( _shader_scene_water.id, "uShoreColour" );
_uniform_scene_water_uOceanColour = glGetUniformLocation( _shader_scene_water.id, "uOceanColour" );
_uniform_scene_water_uLightsArray = glGetUniformLocation( _shader_scene_water.id, "uLightsArray" );
+ _uniform_scene_water_uLightsIndex = glGetUniformLocation( _shader_scene_water.id, "uLightsIndex" );
_uniform_scene_water_g_world_depth = glGetUniformLocation( _shader_scene_water.id, "g_world_depth" );
}
#endif /* SHADER_scene_water_H */
"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"
" 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"
" 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"
"}\n"
"\n"
"\n"
-"#line 13 0 \n"
+"#line 14 0 \n"
"\n"
"float sdLine( vec3 p, vec3 a, vec3 b )\n"
"{\n"
" 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"
" 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"
static GLuint _uniform_scene_water_fast_uShoreColour;
static GLuint _uniform_scene_water_fast_uOceanColour;
static GLuint _uniform_scene_water_fast_uLightsArray;
+static GLuint _uniform_scene_water_fast_uLightsIndex;
static GLuint _uniform_scene_water_fast_g_world_depth;
static void shader_scene_water_fast_uMdl(m4x3f m){
glUniformMatrix4x3fv(_uniform_scene_water_fast_uMdl,1,GL_FALSE,(float*)m);
_uniform_scene_water_fast_uShoreColour = glGetUniformLocation( _shader_scene_water_fast.id, "uShoreColour" );
_uniform_scene_water_fast_uOceanColour = glGetUniformLocation( _shader_scene_water_fast.id, "uOceanColour" );
_uniform_scene_water_fast_uLightsArray = glGetUniformLocation( _shader_scene_water_fast.id, "uLightsArray" );
+ _uniform_scene_water_fast_uLightsIndex = glGetUniformLocation( _shader_scene_water_fast.id, "uLightsIndex" );
_uniform_scene_water_fast_g_world_depth = glGetUniformLocation( _shader_scene_water_fast.id, "g_world_depth" );
}
#endif /* SHADER_scene_water_fast_H */
world_audio_init();
/* 'systems' are completely loaded now */
-#if 0
- strcpy( world.world_name, "maps/mp_mtzero.mdl" );
- strcpy( world.world_name, "maps/mp_gridmap.mdl" );
-#endif
/* load home world */
world_load( &world_global.worlds[0], "maps/mp_home.mdl" );
+
world_load( &world_global.worlds[1], "maps/mp_gridmap.mdl" );
world_load( &world_global.worlds[2], "maps/mp_mtzero.mdl" );
world_link_nonlocal_gates( 0, 1 );
k_geo_type_water = 2
};
+static const float k_light_cube_size = 8.0f;
+
struct world_instance
{
/* This is a small flag we use to changelevel.
/* STD140 */
struct ub_world_lighting
{
+ v4f g_cube_min,
+ g_cube_inv_range;
+
/* v3f (padded) */
v4f g_light_colours[3],
g_light_directions[3],
int ubo_bind_point;
GLuint tbo_light_entities,
- tex_light_entities;
+ tex_light_entities,
+ tex_light_cubes;
+
+ v3i light_cubes;
struct framebuffer heightmap;
{
mdl_node *node;
struct classtype_world_light *inf;
+ m4x3f inverse_world;
+ v2f angle_sin_cos;
/* enabled.. etc?
* TODO: we should order entities in the binary by their type */
struct world_light *light = &world->lights[ world->light_count ++ ];
light->node = pnode;
light->inf = mdl_get_entdata( world->meta, pnode );
+
+ q_m3x3( pnode->q, light->inverse_world );
+ v3_copy( pnode->co, light->inverse_world[3] );
+ m4x3_invert_affine( light->inverse_world, light->inverse_world );
+
+ light->angle_sin_cos[0] = sinf( light->inf->angle * 0.5f );
+ light->angle_sin_cos[1] = cosf( light->inf->angle * 0.5f );
}
VG_STATIC void world_pct_nonlocal_gate( world_instance *world, mdl_node *pnode )
world->scene_no_collide = NULL;
}
+float fsd_cone_infinite( v3f p, v2f c )
+{
+ v2f q = { v2_length( (v2f){ p[0], p[2] } ), -p[1] };
+ float s = vg_maxf( 0.0f, v2_dot( q, c ) );
+
+ v2f v0;
+ v2_muls( c, s, v0 );
+ v2_sub( q, v0, v0 );
+
+ float d = v2_length( v0 );
+ return d * ((q[0]*c[1]-q[1]*c[0]<0.0f)?-1.0f:1.0f);
+}
+
VG_STATIC int reset_player( int argc, char const *argv[] );
VG_STATIC void world_post_process( world_instance *world )
{
/* buffer layout
*
- * colour position direction (spots)
- * | . . . . | . . . . | . . . . |
- * | Re Ge Be Night | Xco Yco Zco | Dx Dy Dz Da |
+ * colour position direction (spots)
+ * | . . . . | . . . . | . . . . |
+ * | Re Ge Be Night | Xco Yco Zco Range | Dx Dy Dz Da |
*
*/
/* colour + night */
v3_muls( inf->colour, inf->colour[3] * 2.0f, light_dst[i*3+0] );
- light_dst[i*3+0][3] = 0.0f;
+ light_dst[i*3+0][3] = -1.0f;
if( (inf->type == k_light_type_spot_nighttime_only) ||
(inf->type == k_light_type_point_nighttime_only ) )
light_dst[i*3+0][3] = 0.44f + switch_on * 0.015f;
}
- /* position + nothing */
+ /* position + 1/range^2 */
v3_copy( light->node->co, light_dst[i*3+1] );
+ light_dst[i*3+1][3] = 1.0f/(inf->range*inf->range);
/* direction + angle */
q_mulv( light->node->q, (v3f){0.0f,-1.0f,0.0f}, light_dst[i*3+2]);
- light_dst[i*3+2][3] = inf->angle;
+ light_dst[i*3+2][3] = cosf( inf->angle );
}
glUnmapBuffer( GL_TEXTURE_BUFFER );
info_vec[3] = 1.0f/ (bounds[1][2]-bounds[0][2]);
v4_copy( info_vec, world->ub_lighting.g_depth_bounds );
- /* upload full buffer */
- glBindBuffer( GL_UNIFORM_BUFFER, world->ubo_lighting );
- glBufferSubData( GL_UNIFORM_BUFFER, 0,
- sizeof(struct ub_world_lighting), &world->ub_lighting );
-
/*
* Rendering the depth map
glEnable(GL_DEPTH_TEST);
glBindFramebuffer( GL_FRAMEBUFFER, 0 );
+
+
+
+ /* light cubes */
+
+ v3f cubes_min, cubes_max;
+ v3_muls( world->scene_geo->bbx[0], 1.0f/k_light_cube_size, cubes_min );
+ v3_muls( world->scene_geo->bbx[1], 1.0f/k_light_cube_size, cubes_max );
+
+ v3_sub( cubes_min, (v3f){ 0.5f, 0.5f, 0.5f }, cubes_min );
+ v3_add( cubes_max, (v3f){ 0.5f, 0.5f, 0.5f }, cubes_max );
+
+ v3_floor( cubes_min, cubes_min );
+ v3_floor( cubes_max, cubes_max );
+
+ v3i icubes_min, icubes_max;
+
+ for( int i=0; i<3; i++ )
+ {
+ icubes_min[i] = cubes_min[i];
+ icubes_max[i] = cubes_max[i];
+ }
+
+ v3i icubes_count;
+ v3i_sub( icubes_max, icubes_min, icubes_count );
+
+ for( int i=0; i<3; i++ )
+ {
+ icubes_count[i] = VG_MIN( 128, icubes_count[i]+1 );
+ cubes_max[i] = icubes_min[i] + icubes_count[i];
+ }
+
+ v3_muls( cubes_min, k_light_cube_size, cubes_min );
+ v3_muls( cubes_max, k_light_cube_size, cubes_max );
+
+ for( int i=0; i<3; i++ )
+ {
+ float range = cubes_max[i]-cubes_min[i];
+ world->ub_lighting.g_cube_inv_range[i] = 1.0f / range;
+ world->ub_lighting.g_cube_inv_range[i] *= (float)icubes_count[i];
+
+ vg_info( "cubes[%d]: %d\n", i, icubes_count[i] );
+ }
+
+ int total_cubes = icubes_count[0]*icubes_count[1]*icubes_count[2];
+
+ u32 *cubes_index = vg_linear_alloc( world_global.generic_heap,
+ total_cubes * sizeof(u32) * 2.0f );
+
+ vg_info( "Computing light cubes (%d) [%f %f %f] -> [%f %f %f]\n",
+ total_cubes, cubes_min[0], -cubes_min[2], cubes_min[1],
+ cubes_max[0], -cubes_max[2], cubes_max[1] );
+
+ v3_copy( cubes_min, world->ub_lighting.g_cube_min );
+
+ vg_info( "g_cube_min[%f %f %f]\n",
+ world->ub_lighting.g_cube_min[0],
+ world->ub_lighting.g_cube_min[1],
+ world->ub_lighting.g_cube_min[2] );
+ vg_info( "g_cube_inv_range[%f %f %f]\n",
+ world->ub_lighting.g_cube_inv_range[0],
+ world->ub_lighting.g_cube_inv_range[1],
+ world->ub_lighting.g_cube_inv_range[2] );
+
+ v3f cube_size;
+ v3_div( (v3f){1.0f,1.0f,1.0f}, world->ub_lighting.g_cube_inv_range,
+ cube_size );
+ float bound_radius = v3_length( cube_size );
+
+ for( int iz = 0; iz<icubes_count[2]; iz ++ )
+ {
+ for( int iy = 0; iy<icubes_count[1]; iy++ )
+ {
+ for( int ix = 0; ix<icubes_count[0]; ix++ )
+ {
+ boxf bbx;
+ v3_div( (v3f){ ix, iy, iz }, world->ub_lighting.g_cube_inv_range,
+ bbx[0] );
+ v3_div( (v3f){ ix+1, iy+1, iz+1 },
+ world->ub_lighting.g_cube_inv_range,
+ bbx[1] );
+
+ v3_add( bbx[0], world->ub_lighting.g_cube_min, bbx[0] );
+ v3_add( bbx[1], world->ub_lighting.g_cube_min, bbx[1] );
+
+ v3f center;
+ v3_add( bbx[0], bbx[1], center );
+ v3_muls( center, 0.5f, center );
+
+ u32 indices[6] = { 0, 0, 0, 0, 0, 0 };
+ u32 count = 0;
+
+ float influences[6] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
+ const int N = vg_list_size( influences );
+
+ for( int j=0; j<world->light_count; j ++ )
+ {
+ struct world_light *light = &world->lights[j];
+ v3f closest;
+ closest_point_aabb( light->node->co, bbx, closest );
+
+ float dist = v3_dist( closest, light->node->co ),
+ influence = 1.0f/(dist+1.0f);
+
+ if( dist > light->inf->range )
+ continue;
+
+ if( (light->inf->type == k_light_type_spot) ||
+ (light->inf->type == k_light_type_spot_nighttime_only) )
+ {
+ v3f local;
+ m4x3_mulv( light->inverse_world, center, local );
+
+ float r = fsd_cone_infinite( local, light->angle_sin_cos );
+
+ if( r > bound_radius )
+ continue;
+ }
+
+ int best_pos = N;
+ for( int k=best_pos-1; k>=0; k -- )
+ if( influence > influences[k] )
+ best_pos = k;
+
+ if( best_pos < N )
+ {
+ for( int k=N-1; k>best_pos; k -- )
+ {
+ influences[k] = influences[k-1];
+ indices[k] = indices[k-1];
+ }
+
+ influences[best_pos] = influence;
+ indices[best_pos] = j;
+ }
+ }
+
+ for( int j=0; j<N; j++ )
+ if( influences[j] > 0.0f )
+ count ++;
+
+ int base_index = iz * (icubes_count[0]*icubes_count[1]) +
+ iy * (icubes_count[0]) +
+ ix;
+
+ int lower_count = VG_MIN( 3, count );
+ u32 packed_index_lower = lower_count;
+ packed_index_lower |= indices[0]<<2;
+ packed_index_lower |= indices[1]<<12;
+ packed_index_lower |= indices[2]<<22;
+
+ int upper_count = VG_MAX( 0, count - lower_count );
+ u32 packed_index_upper = upper_count;
+ packed_index_upper |= indices[3]<<2;
+ packed_index_upper |= indices[4]<<12;
+ packed_index_upper |= indices[5]<<22;
+
+ cubes_index[ base_index * 2 + 0 ] = packed_index_lower;
+ cubes_index[ base_index * 2 + 1 ] = packed_index_upper;
+ }
+ }
+ }
+
+ glGenTextures( 1, &world->tex_light_cubes ); /* FIXME: glDeleteTextures */
+ glBindTexture( GL_TEXTURE_3D, world->tex_light_cubes );
+ glTexImage3D( GL_TEXTURE_3D, 0, GL_RG32UI,
+ icubes_count[0], icubes_count[1], icubes_count[2],
+ 0, GL_RG_INTEGER, GL_UNSIGNED_INT, cubes_index );
+ glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
+ glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
+
+ vg_linear_del( world_global.generic_heap, cubes_index );
+
+
+
+
+
+ /* upload full buffer */
+ glBindBuffer( GL_UNIFORM_BUFFER, world->ubo_lighting );
+ glBufferSubData( GL_UNIFORM_BUFFER, 0,
+ sizeof(struct ub_world_lighting), &world->ub_lighting );
}
vg_release_thread_sync();
glUniform1i( location, slot );
}
+VG_STATIC void world_bind_light_index( world_instance *world,
+ GLuint shader, GLuint location,
+ int slot )
+{
+ glActiveTexture( GL_TEXTURE0 + slot );
+ glBindTexture( GL_TEXTURE_3D, world->tex_light_cubes );
+ glUniform1i( location, slot );
+}
+
VG_STATIC void render_world_depth( world_instance *world, camera *cam );
/*
_uniform_scene_vertex_blend_g_world_depth, 2 );
world_bind_light_array( world, _shader_scene_vertex_blend.id,
_uniform_scene_vertex_blend_uLightsArray, 3 );
+ world_bind_light_index( world, _shader_scene_vertex_blend.id,
+ _uniform_scene_vertex_blend_uLightsIndex, 4 );
vg_tex2d_bind( &tex_terrain_noise, 0 );
_uniform_scene_standard_g_world_depth, 2 );
world_bind_light_array( world, _shader_scene_standard.id,
_uniform_scene_standard_uLightsArray, 3 );
+ world_bind_light_index( world, _shader_scene_standard.id,
+ _uniform_scene_standard_uLightsIndex, 4 );
bind_terrain_noise();
_uniform_scene_standard_alphatest_g_world_depth, 2 );
world_bind_light_array( world, _shader_scene_standard_alphatest.id,
_uniform_scene_standard_alphatest_uLightsArray, 3 );
+ world_bind_light_index( world, _shader_scene_standard_alphatest.id,
+ _uniform_scene_standard_alphatest_uLightsIndex, 4 );
bind_terrain_noise();
_uniform_scene_terrain_g_world_depth, 2 );
world_bind_light_array( world, _shader_scene_terrain.id,
_uniform_scene_terrain_uLightsArray, 3 );
+ world_bind_light_index( world, _shader_scene_terrain.id,
+ _uniform_scene_terrain_uLightsIndex, 4 );
vg_tex2d_bind( &tex_terrain_noise, 0 );
VG_STATIC void world_bind_light_array( world_instance *world,
GLuint shader, GLuint location,
int slot );
+VG_STATIC void world_bind_light_index( world_instance *world,
+ GLuint shader, GLuint location,
+ int slot );
VG_STATIC void render_world_routes( world_instance *world, camera *cam )
{
_uniform_scene_route_g_world_depth, 2 );
world_bind_light_array( world, _shader_scene_route.id,
_uniform_scene_route_uLightsArray, 3 );
+ world_bind_light_index( world, _shader_scene_route.id,
+ _uniform_scene_route_uLightsIndex, 4 );
bind_terrain_noise();
shader_scene_route_uPv( cam->mtx.pv );
VG_STATIC void world_bind_light_array( world_instance *world,
GLuint shader, GLuint location,
int slot );
+VG_STATIC void world_bind_light_index( world_instance *world,
+ GLuint shader, GLuint location,
+ int slot );
/*
* Does not write motion vectors
_uniform_scene_water_g_world_depth, 2 );
world_bind_light_array( world, _shader_scene_water.id,
_uniform_scene_water_uLightsArray, 4 );
+ world_bind_light_index( world, _shader_scene_water.id,
+ _uniform_scene_water_uLightsIndex, 5 );
render_fb_bind_texture( gpipeline.fb_water_beneath, 0, 3 );
shader_scene_water_uTexBack( 3 );