change port
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / model_character_view.h
index d70953dd4696232f5a68f47dfe66298afc436784..060e5b2419c3fd715fcf9c1c5e7447ba12749c20 100644 (file)
@@ -65,7 +65,7 @@ static struct vg_shader _shader_model_character_view = {
 "   aCo = a_co;\n"
 "   aWorldCo = world_pos;\n"
 "\n"
-"   // TODO:\n"
+"   // TODO: motion vectors\n"
 "   aMotionVec0 = vec3(1.0);\n"
 "   aMotionVec1 = vec3(1.0);\n"
 "}\n"
@@ -102,9 +102,12 @@ static struct vg_shader _shader_model_character_view = {
 "   vec4 g_sunset_ambient;\n"
 "   vec4 g_sun_colour;\n"
 "   vec4 g_sun_dir;\n"
+"   vec4 g_board_0;\n"
+"   vec4 g_board_1;\n"
 "\n"
 "   float g_water_fog;\n"
 "   float g_time;\n"
+"   float g_realtime;\n"
 "   float g_shadow_length;\n"
 "   float g_shadow_spread;\n"
 "\n"
@@ -201,7 +204,7 @@ static struct vg_shader _shader_model_character_view = {
 "   return ambient + (light_sun + sky_reflection) * shadow;\n"
 "}\n"
 "\n"
-"#line     41        0 \n"
+"#line     44        0 \n"
 "\n"
 "float world_depth_sample( vec3 pos )\n"
 "{\n"
@@ -211,9 +214,8 @@ static struct vg_shader _shader_model_character_view = {
 "\n"
 "float world_water_depth( vec3 pos )\n"
 "{\n"
-"   vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
 "   float ref_depth = g_water_plane.y*g_water_plane.w;\n"
-"   return texture( g_world_depth, depth_coord ).g - ref_depth;\n"
+"   return world_depth_sample( pos ) - ref_depth;\n"
 "}\n"
 "\n"
 "float shadow_sample( vec3 vdir )\n"
@@ -281,18 +283,18 @@ static struct vg_shader _shader_model_character_view = {
 "   light_delta = normalize( light_delta );\n"
 "\n"
 "   float quadratic = dist2*100.0;\n"
-"   float attenuation  = 1.0f/( 1.0f + quadratic );\n"
+"   float attenuation  = 1.0/( 1.0 + 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"
+"   if( light_dir.w < 0.999999 ){\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"
+"   return light_colour.rgb * attenuation * falloff \n"
+"            * step( g_day_phase, light_colour.w );\n"
 "}\n"
 "\n"
 "vec3 scene_calculate_packed_light_patch( uint packed_index, \n"
@@ -302,20 +304,17 @@ static struct vg_shader _shader_model_character_view = {
 "\n"
 "   vec3 l = vec3(0.0);\n"
 "\n"
-"   if( light_count >= 1u )\n"
-"   {\n"
+"   if( light_count >= 1u ){\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"
+"      if( light_count >= 2u ){\n"
 "         l += scene_calculate_light( index_1, halfview, co, normal );\n"
 "\n"
-"         if( light_count >= 3u )\n"
-"         {\n"
+"         if( light_count >= 3u ){\n"
 "            l += scene_calculate_light( index_2, halfview, co, normal );\n"
 "         }\n"
 "      }\n"
@@ -407,12 +406,43 @@ static struct vg_shader _shader_model_character_view = {
 "}\n"
 "\n"
 "#line     12        0 \n"
+"#line       1        3 \n"
+"uniform sampler2D uTexSceneDepth;\n"
+"uniform vec3 uInverseRatioDepth;\n"
+"uniform vec3 uInverseRatioMain;\n"
+"uniform bool uDepthCompare;\n"
+"\n"
+"float linear_depth( float depth, float near, float far ) {\n"
+"   float z = depth * 2.0 - 1.0;\n"
+"   return (2.0 * near * far) / (far + near - z * (far - near));       \n"
+"}\n"
 "\n"
-"void main()\n"
-"{\n"
+"void depth_compare_dither(){\n"
+"   if( uDepthCompare ){\n"
+"      vec2 back_coord = gl_FragCoord.xy * uInverseRatioMain.xy \n"
+"                                        * uInverseRatioDepth.xy;\n"
+"      float back_depth = texture( uTexSceneDepth, back_coord ).r;\n"
+"      float front_depth = gl_FragCoord.z/gl_FragCoord.w;\n"
+"\n"
+"      back_depth = linear_depth( back_depth, 0.1, 2100.0 );\n"
+"      float diff = back_depth - front_depth;\n"
+"\n"
+"      vec2 ssuv = gl_FragCoord.xy;\n"
+"      vec3 vDither = vec3( dot( vec2( 171.0, 231.0 ), ssuv) );\n"
+"      float dither = fract( vDither.g / 71.0 ) - 0.5;\n"
+"\n"
+"      if( step(0.0,diff)+dither<0.3 )\n"
+"         discard;\n"
+"   }\n"
+"}\n"
+"\n"
+"#line     13        0 \n"
+"\n"
+"void main(){\n"
+"   depth_compare_dither();\n"
 "   compute_motion_vectors();\n"
 "\n"
-"   vec3 qnorm     = normalize(floor(aNorm*2.0)*0.5) + vec3(0.001,0.0,0.0);\n"
+"   vec3 qnorm     = aNorm;\n"
 "   vec3 diffuse   = texture( uTexMain, aUv ).rgb;\n"
 "   vec3 composite = world_compute_lighting( diffuse, qnorm, aWorldCo, 1.0 );\n"
 "\n"
@@ -431,6 +461,10 @@ static GLuint _uniform_model_character_view_uCamera;
 static GLuint _uniform_model_character_view_g_world_depth;
 static GLuint _uniform_model_character_view_uLightsArray;
 static GLuint _uniform_model_character_view_uLightsIndex;
+static GLuint _uniform_model_character_view_uTexSceneDepth;
+static GLuint _uniform_model_character_view_uInverseRatioDepth;
+static GLuint _uniform_model_character_view_uInverseRatioMain;
+static GLuint _uniform_model_character_view_uDepthCompare;
 static void shader_model_character_view_uPv(m4x4f m){
    glUniformMatrix4fv(_uniform_model_character_view_uPv,1,GL_FALSE,(float*)m);
 }
@@ -443,6 +477,18 @@ static void shader_model_character_view_uCamera(v3f v){
 static void shader_model_character_view_g_world_depth(int i){
    glUniform1i(_uniform_model_character_view_g_world_depth,i);
 }
+static void shader_model_character_view_uTexSceneDepth(int i){
+   glUniform1i(_uniform_model_character_view_uTexSceneDepth,i);
+}
+static void shader_model_character_view_uInverseRatioDepth(v3f v){
+   glUniform3fv(_uniform_model_character_view_uInverseRatioDepth,1,v);
+}
+static void shader_model_character_view_uInverseRatioMain(v3f v){
+   glUniform3fv(_uniform_model_character_view_uInverseRatioMain,1,v);
+}
+static void shader_model_character_view_uDepthCompare(int b){
+   glUniform1i(_uniform_model_character_view_uDepthCompare,b);
+}
 static void shader_model_character_view_register(void){
    vg_shader_register( &_shader_model_character_view );
 }
@@ -455,5 +501,9 @@ static void shader_model_character_view_link(void){
    _uniform_model_character_view_g_world_depth = glGetUniformLocation( _shader_model_character_view.id, "g_world_depth" );
    _uniform_model_character_view_uLightsArray = glGetUniformLocation( _shader_model_character_view.id, "uLightsArray" );
    _uniform_model_character_view_uLightsIndex = glGetUniformLocation( _shader_model_character_view.id, "uLightsIndex" );
+   _uniform_model_character_view_uTexSceneDepth = glGetUniformLocation( _shader_model_character_view.id, "uTexSceneDepth" );
+   _uniform_model_character_view_uInverseRatioDepth = glGetUniformLocation( _shader_model_character_view.id, "uInverseRatioDepth" );
+   _uniform_model_character_view_uInverseRatioMain = glGetUniformLocation( _shader_model_character_view.id, "uInverseRatioMain" );
+   _uniform_model_character_view_uDepthCompare = glGetUniformLocation( _shader_model_character_view.id, "uDepthCompare" );
 }
 #endif /* SHADER_model_character_view_H */