1.0
authorhgn <hgodden00@gmail.com>
Mon, 31 Oct 2022 19:04:17 +0000 (19:04 +0000)
committerhgn <hgodden00@gmail.com>
Mon, 31 Oct 2022 19:04:17 +0000 (19:04 +0000)
main.c
models_src/ch_outlaw.mdl
shaders.sh
shaders/water_fast.fs [new file with mode: 0644]
shaders/water_fast.h [new file with mode: 0644]
world_water.h

diff --git a/main.c b/main.c
index 22052fa40b58a2c3def75e8ccd7c683ceeb8fb39..e1056218848286f9cec80ad27cad9dc3b6f02de7 100644 (file)
--- a/main.c
+++ b/main.c
@@ -19,8 +19,8 @@
  */
 
 #define VG_3D
-//#define VG_STATIC static
-#define VG_STATIC 
+#define VG_STATIC static
+//#define VG_STATIC 
 
 //#define VG_MINIMAL_TEST
 #ifndef VG_MINIMAL_TEST
index 6ff28b6a3ce579422419db1b76e9dabe7ea8de84..f3d78588041729450696b67dc85d709e414b5ffe 100644 (file)
Binary files a/models_src/ch_outlaw.mdl and b/models_src/ch_outlaw.mdl differ
index f2ca0d41cb36dddb7887d7fe650188f7a443457c..79604a6d66a7a8e6ad80c79ad100b98c4cd2a8e1 100755 (executable)
@@ -21,6 +21,7 @@ shader fscolour blit.vs colour.fs
 shader alphatest standard.vs std_alphatest.fs
 shader scoretext scoretext.vs vblend.fs
 shader water standard.vs water.fs
+shader water_fast standard.vs water_fast.fs
 shader gate gate.vs gate.fs
 shader gatelq gate.vs gate_lq.fs
 shader route standard.vs route.fs
diff --git a/shaders/water_fast.fs b/shaders/water_fast.fs
new file mode 100644 (file)
index 0000000..2919e09
--- /dev/null
@@ -0,0 +1,60 @@
+out vec4 FragColor;
+
+uniform sampler2D uTexDudv;
+
+uniform float uTime;
+uniform vec3 uCamera;
+uniform float uSurfaceY;
+
+uniform vec3 uShoreColour;
+uniform vec3 uOceanColour;
+
+in vec4 aColour;
+in vec2 aUv;
+in vec3 aNorm;
+in vec3 aCo;
+in vec3 aWorldCo;
+
+#include "common_world.glsl"
+
+vec4 water_surf( vec3 halfview, vec3 vnorm, float depthvalue )
+{
+   vec3 surface_tint = mix(uShoreColour, uOceanColour, depthvalue);
+
+   float ffresnel = pow(1.0-dot( vnorm, halfview ),5.0);
+
+   vec3 lightdir = vec3(0.95,0.0,-0.3);
+   vec3 specdir = reflect( -lightdir, vnorm );
+   float spec = pow(max(dot(halfview,specdir),0.0),20.0)*0.3;
+   
+   return vec4( surface_tint + spec, max(min(depthvalue*4.0, 1.0),0.0) );
+}
+
+void main()
+{
+   // Surface colour composite
+   float depthvalue = clamp( -world_depth_sample( aCo )*(1.0/25.0), 0.0, 1.0 );
+
+   vec2 world_coord = aCo.xz * 0.008;
+   vec4 time_offsets = vec4( uTime ) * vec4( 0.008, 0.006, 0.003, 0.03 );
+   vec4 dudva = texture( uTexDudv, world_coord + time_offsets.xy )-0.5;
+   vec4 dudvb = texture( uTexDudv, world_coord *7.0 - time_offsets.zw )-0.5;
+
+   vec3 surfnorm = dudva.rgb + dudvb.rgb;
+   surfnorm = normalize(vec3(0.0,1.0,0.0) + dudva.xyz*0.4 + dudvb.xyz*0.1);
+   
+   // Foam
+   float fband = fract( aCo.z*0.02+uTime*0.1+depthvalue*10.0 );
+   fband = step( fband+dudva.a*0.8, 0.3 ) * max((1.0-depthvalue*4.0),0.0);
+
+   // Lighting
+   vec3 halfview = -normalize( aCo-uCamera );
+
+   // Fog
+   float fdist = pow(length( aCo.xz-uCamera.xz ) * 0.00047, 2.6);
+
+   // Composite
+   vec4 vsurface = water_surf( halfview, surfnorm, depthvalue );
+   vsurface.a -= fdist;
+   FragColor = mix( vsurface, vec4(1.0,1.0,1.0,0.5), fband );
+}
diff --git a/shaders/water_fast.h b/shaders/water_fast.h
new file mode 100644 (file)
index 0000000..7b92ea8
--- /dev/null
@@ -0,0 +1,257 @@
+#ifndef SHADER_water_fast_H
+#define SHADER_water_fast_H
+static void shader_water_fast_link(void);
+static void shader_water_fast_register(void);
+static struct vg_shader _shader_water_fast = {
+   .name = "water_fast",
+   .link = shader_water_fast_link,
+   .vs = 
+{
+.orig_file = "../../shaders/standard.vs",
+.static_src = 
+"layout (location=0) in vec3 a_co;\n"
+"layout (location=1) in vec3 a_norm;\n"
+"layout (location=2) in vec2 a_uv;\n"
+"layout (location=3) in vec4 a_colour;\n"
+"layout (location=4) in vec4 a_weights;\n"
+"layout (location=5) in ivec4 a_groups;\n"
+"\n"
+"#line      2        0 \n"
+"\n"
+"uniform mat4x3 uMdl;\n"
+"uniform mat4 uPv;\n"
+"\n"
+"out vec4 aColour;\n"
+"out vec2 aUv;\n"
+"out vec3 aNorm;\n"
+"out vec3 aCo;\n"
+"out vec3 aWorldCo;\n"
+"\n"
+"void main()\n"
+"{\n"
+"   vec3 world_pos = uMdl * vec4(a_co,1.0);\n"
+"   gl_Position = uPv * vec4( world_pos, 1.0 );\n"
+"   aColour = a_colour;\n"
+"   aUv = a_uv;\n"
+"   aNorm = mat3(uMdl) * a_norm;\n"
+"   aCo = a_co;\n"
+"   aWorldCo = world_pos;\n"
+"}\n"
+""},
+   .fs = 
+{
+.orig_file = "../../shaders/water_fast.fs",
+.static_src = 
+"out vec4 FragColor;\n"
+"\n"
+"uniform sampler2D uTexDudv;\n"
+"\n"
+"uniform float uTime;\n"
+"uniform vec3 uCamera;\n"
+"uniform float uSurfaceY;\n"
+"\n"
+"uniform vec3 uShoreColour;\n"
+"uniform vec3 uOceanColour;\n"
+"\n"
+"in vec4 aColour;\n"
+"in vec2 aUv;\n"
+"in vec3 aNorm;\n"
+"in vec3 aCo;\n"
+"in vec3 aWorldCo;\n"
+"\n"
+"#line       1        1 \n"
+"layout (std140) uniform ub_world_lighting\n"
+"{\n"
+"   vec4 g_light_colours[3];\n"
+"   vec4 g_light_directions[3];\n"
+"   vec4 g_ambient_colour;\n"
+"\n"
+"   vec4 g_water_plane;\n"
+"   vec4 g_depth_bounds;\n"
+"   float g_water_fog;\n"
+"   int g_light_count;\n"
+"   int g_light_preview;\n"
+"};\n"
+"\n"
+"uniform sampler2D g_world_depth;\n"
+"\n"
+"// Standard diffuse + spec models\n"
+"// ==============================\n"
+"\n"
+"vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )\n"
+"{\n"
+"   vec3 vtotal = g_ambient_colour.rgb;\n"
+"\n"
+"   for( int i=0; i<g_light_count; i++ )\n"
+"   {\n"
+"      vec3 vcolour = g_light_colours[i].rgb;\n"
+"      vec3 vdir = g_light_directions[i].xyz;\n"
+"\n"
+"      float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
+"      vtotal += vcolour*flight;\n"
+"   }\n"
+"\n"
+"   return vfrag * vtotal;\n"
+"}\n"
+"\n"
+"vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )\n"
+"{\n"
+"   vec3 vcolour = g_light_colours[0].rgb;\n"
+"   vec3 vdir = g_light_directions[0].xyz;\n"
+"\n"
+"   vec3 specdir = reflect( -vdir, wnormal );\n"
+"   float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
+"   return vfrag + vcolour*spec*fintensity;\n"
+"}\n"
+"\n"
+"float world_depth_sample( vec3 pos )\n"
+"{\n"
+"   vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
+"   return texture( g_world_depth, depth_coord ).r;\n"
+"}\n"
+"\n"
+"float shadow_sample( vec3 vdir )\n"
+"{\n"
+"   vec3 sample_pos = aWorldCo + vdir;\n"
+"   float height_sample = world_depth_sample( sample_pos );\n"
+"\n"
+"   float fdelta = height_sample - sample_pos.y;\n"
+"   return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
+"}\n"
+"\n"
+"vec3 do_light_shadowing_old( vec3 vfrag )\n"
+"{\n"
+"   float faccum = 0.0;\n"
+"   faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));\n"
+"   faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));\n"
+"   faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));\n"
+"   faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));\n"
+"   faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);\n"
+"   faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);\n"
+"   faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);\n"
+"   faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);\n"
+"   return mix( vfrag, g_ambient_colour.rgb, faccum );\n"
+"}\n"
+"\n"
+"vec3 do_light_shadowing( vec3 vfrag )\n"
+"{\n"
+"   float fspread = g_light_colours[0].w;\n"
+"   vec3  vdir = g_light_directions[0].xyz;\n"
+"   float flength = g_light_directions[0].w;\n"
+"\n"
+"   float famt = 0.0;\n"
+"   famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
+"   famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
+"   famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
+"   famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
+"   famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
+"   famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
+"   famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
+"   famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
+"   return mix( vfrag, g_ambient_colour.rgb, famt );\n"
+"}\n"
+"\n"
+"vec3 apply_fog( vec3 vfrag, float fdist )\n"
+"{\n"
+"   float dist = pow(fdist*0.0008,1.2);\n"
+"   return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
+"}\n"
+"\n"
+"#line     19        0 \n"
+"\n"
+"vec4 water_surf( vec3 halfview, vec3 vnorm, float depthvalue )\n"
+"{\n"
+"   vec3 surface_tint = mix(uShoreColour, uOceanColour, depthvalue);\n"
+"\n"
+"   float ffresnel = pow(1.0-dot( vnorm, halfview ),5.0);\n"
+"\n"
+"   vec3 lightdir = vec3(0.95,0.0,-0.3);\n"
+"   vec3 specdir = reflect( -lightdir, vnorm );\n"
+"   float spec = pow(max(dot(halfview,specdir),0.0),20.0)*0.3;\n"
+"   \n"
+"   return vec4( surface_tint + spec, max(min(depthvalue*4.0, 1.0),0.0) );\n"
+"}\n"
+"\n"
+"void main()\n"
+"{\n"
+"   // Surface colour composite\n"
+"   float depthvalue = clamp( -world_depth_sample( aCo )*(1.0/25.0), 0.0, 1.0 );\n"
+"\n"
+"   vec2 world_coord = aCo.xz * 0.008;\n"
+"   vec4 time_offsets = vec4( uTime ) * vec4( 0.008, 0.006, 0.003, 0.03 );\n"
+"   vec4 dudva = texture( uTexDudv, world_coord + time_offsets.xy )-0.5;\n"
+"   vec4 dudvb = texture( uTexDudv, world_coord *7.0 - time_offsets.zw )-0.5;\n"
+"\n"
+"   vec3 surfnorm = dudva.rgb + dudvb.rgb;\n"
+"   surfnorm = normalize(vec3(0.0,1.0,0.0) + dudva.xyz*0.4 + dudvb.xyz*0.1);\n"
+"   \n"
+"   // Foam\n"
+"   float fband = fract( aCo.z*0.02+uTime*0.1+depthvalue*10.0 );\n"
+"   fband = step( fband+dudva.a*0.8, 0.3 ) * max((1.0-depthvalue*4.0),0.0);\n"
+"\n"
+"   // Lighting\n"
+"   vec3 halfview = -normalize( aCo-uCamera );\n"
+"\n"
+"   // Fog\n"
+"   float fdist = pow(length( aCo.xz-uCamera.xz ) * 0.00047, 2.6);\n"
+"\n"
+"   // Composite\n"
+"   vec4 vsurface = water_surf( halfview, surfnorm, depthvalue );\n"
+"   vsurface.a -= fdist;\n"
+"   FragColor = mix( vsurface, vec4(1.0,1.0,1.0,0.5), fband );\n"
+"}\n"
+""},
+};
+
+static GLuint _uniform_water_fast_uMdl;
+static GLuint _uniform_water_fast_uPv;
+static GLuint _uniform_water_fast_uTexDudv;
+static GLuint _uniform_water_fast_uTime;
+static GLuint _uniform_water_fast_uCamera;
+static GLuint _uniform_water_fast_uSurfaceY;
+static GLuint _uniform_water_fast_uShoreColour;
+static GLuint _uniform_water_fast_uOceanColour;
+static GLuint _uniform_water_fast_g_world_depth;
+static void shader_water_fast_uMdl(m4x3f m){
+   glUniformMatrix4x3fv( _uniform_water_fast_uMdl, 1, GL_FALSE, (float *)m );
+}
+static void shader_water_fast_uPv(m4x4f m){
+   glUniformMatrix4fv( _uniform_water_fast_uPv, 1, GL_FALSE, (float *)m );
+}
+static void shader_water_fast_uTexDudv(int i){
+   glUniform1i( _uniform_water_fast_uTexDudv, i );
+}
+static void shader_water_fast_uTime(float f){
+   glUniform1f( _uniform_water_fast_uTime, f );
+}
+static void shader_water_fast_uCamera(v3f v){
+   glUniform3fv( _uniform_water_fast_uCamera, 1, v );
+}
+static void shader_water_fast_uSurfaceY(float f){
+   glUniform1f( _uniform_water_fast_uSurfaceY, f );
+}
+static void shader_water_fast_uShoreColour(v3f v){
+   glUniform3fv( _uniform_water_fast_uShoreColour, 1, v );
+}
+static void shader_water_fast_uOceanColour(v3f v){
+   glUniform3fv( _uniform_water_fast_uOceanColour, 1, v );
+}
+static void shader_water_fast_g_world_depth(int i){
+   glUniform1i( _uniform_water_fast_g_world_depth, i );
+}
+static void shader_water_fast_register(void){
+   vg_shader_register( &_shader_water_fast );
+}
+static void shader_water_fast_use(void){ glUseProgram(_shader_water_fast.id); }
+static void shader_water_fast_link(void){
+   _uniform_water_fast_uMdl = glGetUniformLocation( _shader_water_fast.id, "uMdl" );
+   _uniform_water_fast_uPv = glGetUniformLocation( _shader_water_fast.id, "uPv" );
+   _uniform_water_fast_uTexDudv = glGetUniformLocation( _shader_water_fast.id, "uTexDudv" );
+   _uniform_water_fast_uTime = glGetUniformLocation( _shader_water_fast.id, "uTime" );
+   _uniform_water_fast_uCamera = glGetUniformLocation( _shader_water_fast.id, "uCamera" );
+   _uniform_water_fast_uSurfaceY = glGetUniformLocation( _shader_water_fast.id, "uSurfaceY" );
+   _uniform_water_fast_uShoreColour = glGetUniformLocation( _shader_water_fast.id, "uShoreColour" );
+   _uniform_water_fast_uOceanColour = glGetUniformLocation( _shader_water_fast.id, "uOceanColour" );
+   _uniform_water_fast_g_world_depth = glGetUniformLocation( _shader_water_fast.id, "g_world_depth" );
+}
+#endif /* SHADER_water_fast_H */
index e31994ab57a111ac30f221957eb865c6fb8b050b..4281981c5549376742e930e0883eb2ee22b5b240 100644 (file)
@@ -8,6 +8,7 @@
 #include "world.h"
 #include "render.h"
 #include "shaders/water.h"
+#include "shaders/water_fast.h"
 #include "scene.h"
 
 vg_tex2d tex_water_surf = { .path = "textures/water_surf.qoi" };
@@ -16,6 +17,7 @@ VG_STATIC void world_water_init(void)
 {
    vg_info( "world_water_init\n" );
    shader_water_register();
+   shader_water_fast_register();
 
    vg_acquire_thread_sync();
    {
@@ -51,7 +53,7 @@ VG_STATIC void water_set_surface( float height )
 
 VG_STATIC void render_water_texture( m4x3f camera )
 {
-   if( !world.water.enabled )
+   if( !world.water.enabled || (vg.quality_profile == k_quality_profile_low) )
       return;
 
    /* Draw reflection buffa */
@@ -125,54 +127,92 @@ VG_STATIC void render_water_surface( m4x4f pv, m4x3f camera )
    if( !world.water.enabled )
       return;
 
-   /* Draw surface */
-   shader_water_use();
-   
-   fb_bindtex( &world.water.fbreflect, 0 );
-   shader_water_uTexMain( 0 );
+   if( vg.quality_profile == k_quality_profile_high )
+   {
+      /* Draw surface */
+      shader_water_use();
+      
+      fb_bindtex( &world.water.fbreflect, 0 );
+      shader_water_uTexMain( 0 );
+
+      vg_tex2d_bind( &tex_water_surf, 1 );
+      shader_water_uTexDudv( 1 );
+      shader_water_uInvRes( (v2f){
+            1.0f / (float)vg.window_x,
+            1.0f / (float)vg.window_y });
 
-   vg_tex2d_bind( &tex_water_surf, 1 );
-   shader_water_uTexDudv( 1 );
-   shader_water_uInvRes( (v2f){
-         1.0f / (float)vg.window_x,
-         1.0f / (float)vg.window_y });
+      shader_link_standard_ub( _shader_water.id, 2 );
 
-   shader_link_standard_ub( _shader_water.id, 2 );
+      fb_bindtex( &world.water.fbdepth, 3 );
+      shader_water_uTexBack( 3 );
+      shader_water_uTime( world.time );
+      shader_water_uCamera( camera[3] );
+      shader_water_uSurfaceY( world.water.height );
 
-   fb_bindtex( &world.water.fbdepth, 3 );
-   shader_water_uTexBack( 3 );
-   shader_water_uTime( world.time );
-   shader_water_uCamera( camera[3] );
-   shader_water_uSurfaceY( world.water.height );
+      shader_water_uPv( pv );
 
-   shader_water_uPv( pv );
+      m4x3f full;
+      m4x3_identity( full );
+      shader_water_uMdl( full );
 
-   m4x3f full;
-   m4x3_identity( full );
-   full[3][1] = world.water.height;
+      glEnable(GL_BLEND);
+      glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
+      glBlendEquation(GL_FUNC_ADD);
 
-   shader_water_uMdl( full );
+      mesh_bind( &world.mesh_no_collide );
 
-   glEnable(GL_BLEND);
-   glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
-   glBlendEquation(GL_FUNC_ADD);
+      for( int i=0; i<world.material_count; i++ )
+      {
+         struct world_material *mat = &world.materials[i];
+
+         if( mat->info.shader == k_shader_water )
+         {
+            shader_water_uShoreColour( mat->info.colour );
+            shader_water_uOceanColour( mat->info.colour1 );
 
-   mesh_bind( &world.mesh_no_collide );
+            mdl_draw_submesh( &mat->sm_no_collide );
+         }
+      }
 
-   for( int i=0; i<world.material_count; i++ )
+      glDisable(GL_BLEND);
+   }
+   else if( vg.quality_profile == k_quality_profile_low )
    {
-      struct world_material *mat = &world.materials[i];
+      shader_water_fast_use();
 
-      if( mat->info.shader == k_shader_water )
+      vg_tex2d_bind( &tex_water_surf, 1 );
+      shader_water_fast_uTexDudv( 1 );
+      shader_water_fast_uTime( world.time );
+      shader_water_fast_uCamera( camera[3] );
+      shader_water_fast_uSurfaceY( world.water.height );
+      shader_link_standard_ub( _shader_water_fast.id, 2 );
+
+      m4x3f full;
+      m4x3_identity( full );
+      shader_water_fast_uMdl( full );
+      shader_water_fast_uPv( pv );
+
+      glEnable(GL_BLEND);
+      glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
+      glBlendEquation(GL_FUNC_ADD);
+
+      mesh_bind( &world.mesh_no_collide );
+
+      for( int i=0; i<world.material_count; i++ )
       {
-         shader_water_uShoreColour( mat->info.colour );
-         shader_water_uOceanColour( mat->info.colour1 );
+         struct world_material *mat = &world.materials[i];
 
-         mdl_draw_submesh( &mat->sm_no_collide );
+         if( mat->info.shader == k_shader_water )
+         {
+            shader_water_fast_uShoreColour( mat->info.colour );
+            shader_water_fast_uOceanColour( mat->info.colour1 );
+
+            mdl_draw_submesh( &mat->sm_no_collide );
+         }
       }
-   }
 
-   glDisable(GL_BLEND);
+      glDisable(GL_BLEND);
+   }
 }
 
 #endif /* WATER_H */