better water
authorhgn <hgodden00@gmail.com>
Mon, 4 Jul 2022 23:37:19 +0000 (00:37 +0100)
committerhgn <hgodden00@gmail.com>
Mon, 4 Jul 2022 23:37:19 +0000 (00:37 +0100)
15 files changed:
main.c
shaders/planeinf.fs [new file with mode: 0644]
shaders/planeinf.h [new file with mode: 0644]
shaders/sky.fs
shaders/sky.h
shaders/terrain.fs
shaders/terrain.h
shaders/water.fs
shaders/water.h
shaders/water.vs
shaders/water_old.fs [new file with mode: 0644]
terrain.h
textures/water_surf.png
vg.conf
water.h

diff --git a/main.c b/main.c
index 4462c0ff8ea14eba22ece198f4495bd008713310..b80a1a20527f7c2bfd99f5390d93a30ed12122b3 100644 (file)
--- a/main.c
+++ b/main.c
@@ -181,7 +181,7 @@ void vg_render(void)
    gpipeline.fov = freecam? 60.0f: 135.0f; /* 120 */
    m4x4_projection( vg_pv, gpipeline.fov, 
          (float)vg_window_x / (float)vg_window_y, 
-         0.1f, 1500.0f );
+         0.1f, 2100.0f );
 
    m4x4_mul( vg_pv, world_4x4, vg_pv );
 
diff --git a/shaders/planeinf.fs b/shaders/planeinf.fs
new file mode 100644 (file)
index 0000000..926c79d
--- /dev/null
@@ -0,0 +1,26 @@
+#include "water_ref.glsl"
+
+out vec4 FragColor;
+
+uniform vec3 uCamera;
+uniform vec4 uPlane;
+
+in vec4 aColour;
+in vec2 aUv;
+in vec3 aNorm;
+in vec3 aCo;
+
+float ray_plane( vec3 pos, vec3 dir, vec4 plane )
+{
+   float d = dot( plane.xyz, dir );
+   float t = dot((plane.xyz*plane.w - pos),plane.xyz) / d;
+   return t;
+}
+
+void main()
+{
+   float fdist = ray_plane( uCamera, -aNorm, uPlane );
+   vec3 world_pos = uCamera - aNorm*fdist;
+
+   FragColor = vec4(fract(world_pos*0.1),1.0);
+}
diff --git a/shaders/planeinf.h b/shaders/planeinf.h
new file mode 100644 (file)
index 0000000..0cc17cd
--- /dev/null
@@ -0,0 +1,118 @@
+#ifndef SHADER_planeinf_H
+#define SHADER_planeinf_H
+static void shader_planeinf_link(void);
+static void shader_planeinf_register(void);
+static struct vg_shader _shader_planeinf = {
+   .name = "planeinf",
+   .link = shader_planeinf_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 vec4 a_colour;\n"
+"layout (location=3) in vec2 a_uv;\n"
+"\n"
+"#line      2        0 \n"
+"\n"
+"uniform mat4 uPv;\n"
+"uniform mat4x3 uMdl;\n"
+"\n"
+"out vec4 aColour;\n"
+"out vec2 aUv;\n"
+"out vec3 aNorm;\n"
+"out vec3 aCo;\n"
+"\n"
+"void main()\n"
+"{\n"
+"   gl_Position = uPv * vec4( uMdl * vec4(a_co,1.0), 1.0 );\n"
+"   aColour = a_colour;\n"
+"   aUv = a_uv;\n"
+"   aNorm = mat3(uMdl) * a_norm;\n"
+"   aCo = a_co;\n"
+"}\n"
+""},
+   .fs = 
+{
+.orig_file = "../shaders/planeinf.fs",
+.static_src = 
+"vec4 water_surf( vec3 halfview, vec3 vnorm, float depthvalue, \n"
+"      vec4 beneath, vec4 above )\n"
+"{\n"
+"   vec3 colour_shore = vec3( 0.21, 0.6, 0.8 );\n"
+"   vec3 colour_ocean = vec3( 0.01, 0.1, 0.2 );\n"
+"   vec3 surface_tint = mix(colour_shore, colour_ocean, 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"
+"   // Depth \n"
+"   float depthblend = pow( beneath.a,0.8 );\n"
+"\n"
+"   // Composite\n"
+"   vec3 vsurface = mix(surface_tint, above.rgb, ffresnel );\n"
+"   //vsurface += spec;\n"
+"\n"
+"   return vec4( vsurface,depthblend );\n"
+"}\n"
+"\n"
+"#line      2        0 \n"
+"\n"
+"out vec4 FragColor;\n"
+"\n"
+"uniform vec3 uCamera;\n"
+"uniform vec4 uPlane;\n"
+"\n"
+"in vec4 aColour;\n"
+"in vec2 aUv;\n"
+"in vec3 aNorm;\n"
+"in vec3 aCo;\n"
+"\n"
+"float ray_plane( vec3 pos, vec3 dir, vec4 plane )\n"
+"{\n"
+"   float d = dot( plane.xyz, dir );\n"
+"   float t = dot((plane.xyz*plane.w - pos),plane.xyz) / d;\n"
+"   return t;\n"
+"}\n"
+"\n"
+"void main()\n"
+"{\n"
+"   float fdist = ray_plane( uCamera, -aNorm, uPlane );\n"
+"   vec3 world_pos = uCamera - aNorm*fdist;\n"
+"\n"
+"   FragColor = vec4(fract(world_pos*0.1),1.0);\n"
+"}\n"
+""},
+};
+
+static GLuint _uniform_planeinf_uPv;
+static GLuint _uniform_planeinf_uMdl;
+static GLuint _uniform_planeinf_uCamera;
+static GLuint _uniform_planeinf_uPlane;
+static void shader_planeinf_uPv(m4x4f m){
+   glUniformMatrix4fv( _uniform_planeinf_uPv, 1, GL_FALSE, (float *)m );
+}
+static void shader_planeinf_uMdl(m4x3f m){
+   glUniformMatrix4x3fv( _uniform_planeinf_uMdl, 1, GL_FALSE, (float *)m );
+}
+static void shader_planeinf_uCamera(v3f v){
+   glUniform3fv( _uniform_planeinf_uCamera, 1, v );
+}
+static void shader_planeinf_uPlane(v4f v){
+   glUniform4fv( _uniform_planeinf_uPlane, 1, v );
+}
+static void shader_planeinf_register(void){
+   vg_shader_register( &_shader_planeinf );
+}
+static void shader_planeinf_use(void){ glUseProgram(_shader_planeinf.id); }
+static void shader_planeinf_link(void){
+   _uniform_planeinf_uPv = glGetUniformLocation( _shader_planeinf.id, "uPv" );
+   _uniform_planeinf_uMdl = glGetUniformLocation( _shader_planeinf.id, "uMdl" );
+   _uniform_planeinf_uCamera = glGetUniformLocation( _shader_planeinf.id, "uCamera" );
+   _uniform_planeinf_uPlane = glGetUniformLocation( _shader_planeinf.id, "uPlane" );
+}
+#endif /* SHADER_planeinf_H */
index 653f4b9c41546eb1c28c4415f1a98888e4438397..435b2703cea5347d459cfb952fd6025e7889b0e8 100644 (file)
@@ -12,13 +12,13 @@ in vec3 aCo;
 void main()
 {
    float fintensity = 1.0-(abs(aNorm.y)*0.7);
-   float fblend = pow(fintensity,6.0);
-   vec3 horizon = vec3( 0.61, 0.71, 0.86 )*1.5;
-   vec3 skycolour = vec3( 0.31, 0.56, 0.91 );
+   float fblend = pow(fintensity,8.0);
+   vec3 horizon = vec3( 0.8, 0.9, 0.9 );
+   vec3 skycolour = vec3( 0.5, 0.6, 0.9 );
    vec3 diffuse = mix( skycolour, horizon, fblend );
 
    float fmove = uTime * 0.004;
-   vec2 cloudplane = (aCo.xz / (aCo.y*sign(aNorm.y))) * 0.03;
+   vec2 cloudplane = (aNorm.xz / (aNorm.y*sign(aNorm.y))) * 0.05;
    vec4 clouds1 = texture( uTexGarbage, cloudplane + vec2(0.1,0.4)*fmove*2.0 );
    vec4 clouds2 = texture( uTexGarbage, cloudplane + vec2(0.3,0.1)*fmove );
 
@@ -29,7 +29,5 @@ void main()
    float fhorizon = step( aNorm.y * 0.5 + 0.5, 0.5 );
 
    vec3 skycomp = mix(diffuse, vec3(1.0,1.0,1.0), cloud_e);
-   skycomp = mix(mix(pow(colour_ocean,vec3(6.0))*0.6,skycomp, 0.7),skycomp,fhorizon);
-
-   FragColor = vec4(skycomp, 0.0);
+   FragColor = vec4(skycomp,1.0);
 }
index 653173ec8d4d617d59b549527daa8a78ea64c9c3..01bc5c2fbce9d26ae0d5ae0a5dfd490c0f5fa106 100644 (file)
@@ -51,13 +51,13 @@ static struct vg_shader _shader_sky = {
 "void main()\n"
 "{\n"
 "   float fintensity = 1.0-(abs(aNorm.y)*0.7);\n"
-"   float fblend = pow(fintensity,6.0);\n"
-"   vec3 horizon = vec3( 0.61, 0.71, 0.86 )*1.5;\n"
-"   vec3 skycolour = vec3( 0.31, 0.56, 0.91 );\n"
+"   float fblend = pow(fintensity,8.0);\n"
+"   vec3 horizon = vec3( 0.8, 0.9, 0.9 );\n"
+"   vec3 skycolour = vec3( 0.5, 0.6, 0.9 );\n"
 "   vec3 diffuse = mix( skycolour, horizon, fblend );\n"
 "\n"
 "   float fmove = uTime * 0.004;\n"
-"   vec2 cloudplane = (aCo.xz / (aCo.y*sign(aNorm.y))) * 0.03;\n"
+"   vec2 cloudplane = (aNorm.xz / (aNorm.y*sign(aNorm.y))) * 0.05;\n"
 "   vec4 clouds1 = texture( uTexGarbage, cloudplane + vec2(0.1,0.4)*fmove*2.0 );\n"
 "   vec4 clouds2 = texture( uTexGarbage, cloudplane + vec2(0.3,0.1)*fmove );\n"
 "\n"
@@ -67,10 +67,8 @@ static struct vg_shader _shader_sky = {
 "   vec3 colour_ocean = vec3( 0.61, 0.84, 0.9 );\n"
 "   float fhorizon = step( aNorm.y * 0.5 + 0.5, 0.5 );\n"
 "\n"
-"   vec3 skycomp = mix(diffuse, vec3(1.0,1.0,1.0), cloud_e);\n"
-"   skycomp = mix(mix(pow(colour_ocean,vec3(6.0))*0.6,skycomp, 0.7),skycomp,fhorizon);\n"
-"\n"
-"   FragColor = vec4(skycomp, 0.0);\n"
+"   vec3 skycomp = mix(diffuse, vec3(1.0,1.0,1.0), cloud_e*(1.0-fblend*3.0));\n"
+"   FragColor = vec4(skycomp,1.0);\n"
 "}\n"
 ""},
 };
index 7cf4dde38e87ce628229e6cf43d65431ddec4e95..a6d24006fbfdf9a224a5fff0f6c4891e2d6a7ea7 100644 (file)
@@ -14,7 +14,7 @@ float water_depth( vec3 pos, vec3 dir, vec4 plane )
 {
    float d = dot( plane.xyz, dir );
    float t = dot((plane.xyz*plane.w - pos),plane.xyz) / d;
-   return t*0.05;
+   return t*0.04;
 }
 
 void main()
@@ -40,7 +40,7 @@ void main()
 
    // Lighting
    vec3 lightdir = vec3(0.95,0.0,-0.3);
-   vec3 shadow = pow(vec3(0.014,0.034,0.084),vec3(1.0/3.2));
+   vec3 shadow = vec3(0.27,0.25,0.34);
    float light1 = dot( lightdir, mix(qnorm,aNorm,amtsand) )*0.5+0.5;
    diffuse = diffuse * (light1*vec3(1.0,0.96,0.9)*1.2 + shadow*(1.0-light1));
    
index 1880395cc91a5932cd76ab070b4aa88689b2b13c..9bbb939439e330b0e1e1b9b1d9447deff45bfd09 100644 (file)
@@ -53,7 +53,7 @@ static struct vg_shader _shader_terrain = {
 "{\n"
 "   float d = dot( plane.xyz, dir );\n"
 "   float t = dot((plane.xyz*plane.w - pos),plane.xyz) / d;\n"
-"   return t*0.05;\n"
+"   return t*0.04;\n"
 "}\n"
 "\n"
 "void main()\n"
@@ -79,7 +79,7 @@ static struct vg_shader _shader_terrain = {
 "\n"
 "   // Lighting\n"
 "   vec3 lightdir = vec3(0.95,0.0,-0.3);\n"
-"   vec3 shadow = pow(vec3(0.014,0.034,0.084),vec3(1.0/3.2));\n"
+"   vec3 shadow = vec3(0.3,0.25,0.34);\n"
 "   float light1 = dot( lightdir, mix(qnorm,aNorm,amtsand) )*0.5+0.5;\n"
 "   diffuse = diffuse * (light1*vec3(1.0,0.96,0.9)*1.2 + shadow*(1.0-light1));\n"
 "   \n"
index 077a8e68848e0916fa31ca7a8dcb26dba2dfcba6..1de00ecac59ec0c9345e040331f2afc68a734673 100644 (file)
@@ -1,3 +1,5 @@
+#include "water_ref.glsl"
+
 out vec4 FragColor;
 
 uniform sampler2D uTexMain;
@@ -17,43 +19,36 @@ in float aDepth;
 
 void main()
 {
-   // Reflected and warped texture
+   // Create texture coords
    vec2 ssuv = gl_FragCoord.xy*uInvRes;
-
-   vec4 dudva = texture( uTexDudv, aUv.xy - vec2(uTime*0.004f,uTime*0.003f) );
-   vec4 dudvb = texture( uTexDudv, aUv.xy*0.7 - vec2(uTime*0.01,uTime*0.0054) );
-   vec2 distortamt = (dudva.rg-0.5) * (dudvb.ba-0.5) * 0.6;
-
-   vec4 reflected = texture( uTexMain, ssuv+distortamt );
    
    // Surface colour composite
    float depthvalue = texture( uTexDepth, aUv.zw ).r;
 
-   vec3 colour_shore = vec3( 0.21, 0.6, 0.8 );
-   vec3 colour_ocean = vec3( 0.01, 0.1, 0.2 );
-   vec3 surface_tint = mix(colour_shore, colour_ocean, pow(depthvalue,1.8))*1.5;
+   vec4 dudva = texture(uTexDudv, aUv.xy + vec2(uTime*0.008,uTime*0.006))-0.5;
+   vec4 dudvb = texture(uTexDudv, aUv.xy*7.0-vec2(uTime*0.003,uTime*0.03))-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.1+uTime*0.1-depthvalue*10.0 );
-   fband = step( fband+dudvb.g*0.8, 0.5 ) * max((1.0-depthvalue*4.0),0.0);
+   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 surfnorm = vec3(distortamt.x,1.0,distortamt.y);
-   
    vec3 halfview = -normalize( aCo-uCamera );
-   float ffresnel = pow(1.0-dot( surfnorm, halfview ),5.0);
 
-   vec3 lightdir = vec3(0.95,0.0,-0.3);
-   vec3 specdir = reflect( -lightdir, surfnorm );
-   float spec = pow(max(dot(halfview,specdir),0.0),20.0)*0.3;
-   
-   // Depth 
-   vec4 backsample = texture( uTexBack, ssuv+distortamt*0.1 );
-   float depthblend = pow(backsample.a,0.8);
+   // Sample textures
+   vec4 above = texture( uTexMain, ssuv+ surfnorm.xz*0.2 );
+   vec4 beneath = texture( uTexBack, ssuv );
+
+   // Fog
+   //vec4 horizon = vec4( 0.5, 0.6, 0.9, 1.0 );
+   vec4 horizon = vec4( 0.7,0.8,0.88, 1.0 );
+   float fdist = pow(length( aCo.xz-uCamera.xz ) * 0.00047, 2.6);
 
    // Composite
-   vec3 vsurface = mix(surface_tint*backsample.rgb, reflected.rgb, ffresnel );
-   vsurface += spec;
-   
-   FragColor = mix( vec4(vsurface,depthblend), vec4(1.0,1.0,1.0,0.8), fband );
+   vec4 vsurface = water_surf( halfview, surfnorm, depthvalue, beneath, above );
+   vsurface.a -= fdist;
+   FragColor = mix( vsurface, vec4(1.0,1.0,1.0,0.5), fband );
 }
index 67b07454cbb3eb7a22117bc0d51a49feba8c56de..995cdb8012adf7b69c119c254c098afa02f04c74 100644 (file)
@@ -30,7 +30,7 @@ static struct vg_shader _shader_water = {
 "   gl_Position = uPv * vec4(world_pos,1.0);\n"
 "\n"
 "   vec2 depth_coords = (world_pos.xz-uDepthBounds.xy)*uDepthBounds.zw;\n"
-"   aUv = vec4(world_pos.xz*0.01,depth_coords);\n"
+"   aUv = vec4(world_pos.xz*0.005,depth_coords);\n"
 "   aCo = world_pos;\n"
 "\n"
 "   aDepth = gl_Position.z;\n"
@@ -40,6 +40,31 @@ static struct vg_shader _shader_water = {
 {
 .orig_file = "../shaders/water.fs",
 .static_src = 
+"vec4 water_surf( vec3 halfview, vec3 vnorm, float depthvalue, \n"
+"      vec4 beneath, vec4 above )\n"
+"{\n"
+"   vec3 colour_shore = vec3( 0.21, 0.6, 0.8 );\n"
+"   vec3 colour_ocean = vec3( 0.01, 0.1, 0.2 );\n"
+"   vec3 surface_tint = mix(colour_shore, colour_ocean, 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"
+"   // Depth \n"
+"   float depthblend = pow( beneath.a,0.8 );\n"
+"\n"
+"   // Composite\n"
+"   vec3 vsurface = mix(surface_tint, above.rgb, ffresnel );\n"
+"   //vsurface += spec;\n"
+"\n"
+"   return vec4( vsurface,depthblend );\n"
+"}\n"
+"\n"
+"#line      2        0 \n"
+"\n"
 "out vec4 FragColor;\n"
 "\n"
 "uniform sampler2D uTexMain;\n"
@@ -59,45 +84,38 @@ static struct vg_shader _shader_water = {
 "\n"
 "void main()\n"
 "{\n"
-"   // Reflected and warped texture\n"
+"   // Create texture coords\n"
 "   vec2 ssuv = gl_FragCoord.xy*uInvRes;\n"
-"\n"
-"   vec4 dudva = texture( uTexDudv, aUv.xy - vec2(uTime*0.004f,uTime*0.003f) );\n"
-"   vec4 dudvb = texture( uTexDudv, aUv.xy*0.7 - vec2(uTime*0.01,uTime*0.0054) );\n"
-"   vec2 distortamt = (dudva.rg-0.5) * (dudvb.ba-0.5) * 0.6;\n"
-"\n"
-"   vec4 reflected = texture( uTexMain, ssuv+distortamt );\n"
 "   \n"
 "   // Surface colour composite\n"
 "   float depthvalue = texture( uTexDepth, aUv.zw ).r;\n"
 "\n"
-"   vec3 colour_shore = vec3( 0.21, 0.6, 0.8 );\n"
-"   vec3 colour_ocean = vec3( 0.01, 0.1, 0.2 );\n"
-"   vec3 surface_tint = mix(colour_shore, colour_ocean, pow(depthvalue,1.8))*1.5;\n"
+"   vec4 dudva = texture(uTexDudv, aUv.xy + vec2(uTime*0.008,uTime*0.006))-0.5;\n"
+"   vec4 dudvb = texture(uTexDudv, aUv.xy*7.0-vec2(uTime*0.003,uTime*0.03))-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.1+uTime*0.1-depthvalue*10.0 );\n"
-"   fband = step( fband+dudvb.g*0.8, 0.5 ) * max((1.0-depthvalue*4.0),0.0);\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 surfnorm = vec3(distortamt.x,1.0,distortamt.y);\n"
-"   \n"
 "   vec3 halfview = -normalize( aCo-uCamera );\n"
-"   float ffresnel = pow(1.0-dot( surfnorm, halfview ),5.0);\n"
 "\n"
-"   vec3 lightdir = vec3(0.95,0.0,-0.3);\n"
-"   vec3 specdir = reflect( -lightdir, surfnorm );\n"
-"   float spec = pow(max(dot(halfview,specdir),0.0),20.0)*0.3;\n"
-"   \n"
-"   // Depth \n"
-"   vec4 backsample = texture( uTexBack, ssuv+distortamt*0.1 );\n"
-"   float depthblend = pow(backsample.a,0.8);\n"
+"   // Sample textures\n"
+"   vec4 above = texture( uTexMain, ssuv+ surfnorm.xz*0.2 );\n"
+"   vec4 beneath = texture( uTexBack, ssuv );\n"
+"\n"
+"   // Fog\n"
+"   //vec4 horizon = vec4( 0.5, 0.6, 0.9, 1.0 );\n"
+"   vec4 horizon = vec4( 0.7,0.8,0.88, 1.0 );\n"
+"   float fdist = pow(length( aCo.xz-uCamera.xz ) * 0.00047, 2.6);\n"
 "\n"
 "   // Composite\n"
-"   vec3 vsurface = mix(surface_tint*backsample.rgb, reflected.rgb, ffresnel );\n"
-"   vsurface += spec;\n"
-"   \n"
-"   FragColor = mix( vec4(vsurface,depthblend), vec4(1.0,1.0,1.0,0.8), fband );\n"
+"   vec4 vsurface = water_surf( halfview, surfnorm, depthvalue, beneath, above );\n"
+"   vsurface.a -= fdist;\n"
+"   FragColor = mix( vsurface, vec4(1.0,1.0,1.0,0.5), fband );\n"
 "}\n"
 ""},
 };
index c8ac51bcefa266db568a8c12b23ad7895c2bdf1a..a78bf308c9656c2974f72f9f936d1667f3b4752b 100644 (file)
@@ -14,7 +14,7 @@ void main()
    gl_Position = uPv * vec4(world_pos,1.0);
 
    vec2 depth_coords = (world_pos.xz-uDepthBounds.xy)*uDepthBounds.zw;
-   aUv = vec4(world_pos.xz*0.01,depth_coords);
+   aUv = vec4(world_pos.xz*0.005,depth_coords);
    aCo = world_pos;
 
    aDepth = gl_Position.z;
diff --git a/shaders/water_old.fs b/shaders/water_old.fs
new file mode 100644 (file)
index 0000000..7ffee31
--- /dev/null
@@ -0,0 +1,61 @@
+#include "water_ref.glsl"
+
+out vec4 FragColor;
+
+uniform sampler2D uTexMain;
+uniform sampler2D uTexDudv;
+uniform sampler2D uTexDepth;
+uniform sampler2D uTexBack;
+
+uniform vec2 uInvRes;
+uniform float uTime;
+
+uniform vec3 uCamera;
+uniform float uSurfaceY;
+
+in vec4 aUv;
+in vec3 aCo;
+in float aDepth;
+
+void main()
+{
+   // Reflected and warped texture
+   vec2 ssuv = gl_FragCoord.xy*uInvRes;
+
+   vec4 dudva = texture( uTexDudv, aUv.xy - vec2(uTime*0.004f,uTime*0.003f) );
+   vec4 dudvb = texture( uTexDudv, aUv.xy*0.7 - vec2(uTime*0.01,uTime*0.0054) );
+   vec2 distortamt = (dudva.rg-0.5) * (dudvb.ba-0.5) * 0.6;
+
+   vec4 reflected = texture( uTexMain, ssuv+distortamt );
+   
+   // Surface colour composite
+   float depthvalue = texture( uTexDepth, aUv.zw ).r;
+
+   vec3 colour_shore = vec3( 0.21, 0.6, 0.8 );
+   vec3 colour_ocean = vec3( 0.01, 0.1, 0.2 );
+   vec3 surface_tint = mix(colour_shore, colour_ocean, pow(depthvalue,1.8))*1.5;
+   
+   // Foam
+   float fband = fract( aCo.z*0.1+uTime*0.1-depthvalue*10.0 );
+   fband = step( fband+dudvb.g*0.8, 0.5 ) * max((1.0-depthvalue*4.0),0.0);
+
+   // Lighting
+   vec3 surfnorm = vec3(distortamt.x,1.0,distortamt.y);
+   
+   vec3 halfview = -normalize( aCo-uCamera );
+   float ffresnel = pow(1.0-dot( surfnorm, halfview ),5.0);
+
+   vec3 lightdir = vec3(0.95,0.0,-0.3);
+   vec3 specdir = reflect( -lightdir, surfnorm );
+   float spec = pow(max(dot(halfview,specdir),0.0),20.0)*0.3;
+   
+   // Depth 
+   vec4 backsample = texture( uTexBack, ssuv+distortamt*0.1 );
+   float depthblend = pow(backsample.a,0.8);
+
+   // Composite
+   vec3 vsurface = mix(surface_tint*backsample.rgb, reflected.rgb, ffresnel );
+   vsurface += spec;
+   
+   FragColor = mix( vec4(vsurface,depthblend), vec4(1.0,1.0,1.0,0.8), fband );
+}
index d35dc0ab0f39ffdaa3c57b53bb891d86b48ee124..81b311ac737d64c861d67a67fc6b841e3d8c0bc7 100644 (file)
--- a/terrain.h
+++ b/terrain.h
@@ -12,6 +12,7 @@ static void render_sky(m4x3f camera);
 
 #include "shaders/terrain.h"
 #include "shaders/sky.h"
+#include "shaders/planeinf.h"
 
 vg_tex2d tex_terrain_colours = { .path = "textures/gradients.qoi",
                                  .flags = VG_TEXTURE_CLAMP | VG_TEXTURE_NEAREST
@@ -22,6 +23,8 @@ vg_tex2d tex_terrain_noise = { .path = "textures/garbage.qoi",
 static struct
 {
    glmesh skydome;
+   submodel dome_upper,
+            dome_lower;
 }
 trender;
 
@@ -29,6 +32,7 @@ static void terrain_register(void)
 {
    shader_terrain_register();
    shader_sky_register();
+   shader_planeinf_register();
 }
 
 static void terrain_init(void)
@@ -39,6 +43,10 @@ static void terrain_init(void)
 
    model *msky = vg_asset_read("models/rs_skydome.mdl");
    model_unpack( msky, &trender.skydome );
+   
+   trender.dome_lower = *submodel_get( msky, "dome_lower" );
+   trender.dome_upper = *submodel_get( msky, "dome_upper" );
+
    free(msky);
 }
 
@@ -59,6 +67,29 @@ static void render_terrain(m4x4f projection, v3f camera)
    shader_terrain_uPlane( (v4f){ 0.0f,1.0f,0.0f, wrender.height } );
 }
 
+static void render_lowerdome( m4x3f camera )
+{
+   m4x4f projection, full;
+   pipeline_projection( projection, 0.4f, 1000.0f );
+
+   m4x3f inverse;
+   m3x3_transpose( camera, inverse );
+   v3_copy((v3f){0.0f,0.0f,0.0f}, inverse[3]);
+   m4x3_expand( inverse, full );
+   m4x4_mul( projection, full, full );
+
+   m4x3f identity_matrix;
+   m4x3_identity( identity_matrix );
+   
+   shader_planeinf_use();
+   shader_planeinf_uMdl(identity_matrix);
+   shader_planeinf_uPv(full);
+   shader_planeinf_uCamera(camera[3]);
+   shader_planeinf_uPlane( (v4f){0.0f,1.0f,0.0f, water_height()} );
+
+   submodel_draw( &trender.dome_lower );
+}
+
 static void render_sky(m4x3f camera)
 {
    m4x4f projection, full;
@@ -78,14 +109,16 @@ static void render_sky(m4x3f camera)
    shader_sky_uPv(full);
    shader_sky_uTexGarbage(0);
    shader_sky_uTime( vg_time );
+
+
    vg_tex2d_bind( &tex_terrain_noise, 0 );
 
    glDepthMask(GL_FALSE);
    glDisable(GL_DEPTH_TEST);
 
    mesh_bind( &trender.skydome );
-   mesh_draw( &trender.skydome );
-
+   submodel_draw( &trender.dome_upper );
+   
    glEnable(GL_DEPTH_TEST);
    glDepthMask(GL_TRUE);
 }
index 493f823606544abe08d78a4183c49ee7596f335a..3c1c6f5cf0d7b61ca17b52efd245f129f50abbb7 100644 (file)
Binary files a/textures/water_surf.png and b/textures/water_surf.png differ
diff --git a/vg.conf b/vg.conf
index 99fc92eda4c04ea11ad775254be68a94231381bb..634e451e305ab26ddd8724d5cf7010ec507e8a47 100644 (file)
--- a/vg.conf
+++ b/vg.conf
@@ -10,3 +10,4 @@ shader gate gate.vs gate.fs
 shader gatelq gate.vs gate_lq.fs
 shader water water.vs water.fs
 shader sky standard.vs sky.fs
+shader planeinf standard.vs planeinf.fs
diff --git a/water.h b/water.h
index c2a7122c2244c1774e9f49f428803ad6afb85ec1..ac1db00dbafaf53001728bc6253f70b855a95a49 100644 (file)
--- a/water.h
+++ b/water.h
@@ -32,7 +32,7 @@ static struct
 }
 wrender =
 {
-   .fbreflect = { .format = GL_RGB,  .div = 4 },
+   .fbreflect = { .format = GL_RGB,  .div = 3 },
    .fbdepth   = { .format = GL_RGBA, .div = 4 }
 };
 
@@ -78,18 +78,24 @@ static void water_compute_depth( boxf bounds )
    vg_info( "Computing depth map\n" );
    u8 *img = malloc( kres*kres );
 
+   boxf interior;
+   v3_add(bounds[0],(v3f){1.0f,1.0f,1.0f},interior[0]);
+   v3_sub(bounds[1],(v3f){1.0f,1.0f,1.0f},interior[1]);
+
    v3f volume;
-   v3_sub( bounds[1], bounds[0], volume );
-   box_copy( bounds, wrender.depthbounds );
+   v3_sub( interior[1], interior[0], volume );
+   box_copy( interior, wrender.depthbounds );
    
    for( int y=0; y<kres; y++ )
    {
       for( int x=0; x<kres; x++ )
       {
          v3f pos = { x, 0.0f, y };
-         v3_divs( pos, kres, pos );
-         v3_muladd( bounds[0], pos, volume, pos );
-         pos[1] = wrender.height;
+         pos[0] += 0.5f;
+         pos[1] += 0.5f;
+         v3_divs( pos, kres+1, pos );
+         v3_muladd( interior[0], pos, volume, pos );
+         pos[1] = 2000.0f;
          
          ray_hit hit;
          hit.dist = INFINITY;
@@ -103,7 +109,9 @@ static void water_compute_depth( boxf bounds )
             *dst = (u8)(h*255.0f);
          }
          else
+         {
             *dst = 0;
+         }
       }
    }
 
@@ -184,7 +192,8 @@ static void render_water_texture( m4x3f camera )
    m4x3_invert_affine( camera, inverse );
    m4x3_expand( inverse, view );
 
-   v4f clippb = { 0.0f, -1.0f, 0.0f, -(wrender.height) };
+   float bias = -(camera[3][1]-wrender.height)*0.1f;
+   v4f clippb = { 0.0f, -1.0f, 0.0f, -(wrender.height) + bias };
    m4x3_mulp( inverse, clippb, clippb );
    clippb[3] *= -1.0f;