From: hgn Date: Mon, 4 Jul 2022 23:37:19 +0000 (+0100) Subject: better water X-Git-Url: https://harrygodden.com/git/?a=commitdiff_plain;h=3bb0287d544a4cb75de9afe2927ac8e946f3a18e;hp=cb16ccb05a796178c879ea8d5091663d215a5217;p=carveJwlIkooP6JGAAIwe30JlM.git better water --- diff --git a/main.c b/main.c index 4462c0f..b80a1a2 100644 --- 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 index 0000000..926c79d --- /dev/null +++ b/shaders/planeinf.fs @@ -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 index 0000000..0cc17cd --- /dev/null +++ b/shaders/planeinf.h @@ -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 */ diff --git a/shaders/sky.fs b/shaders/sky.fs index 653f4b9..435b270 100644 --- a/shaders/sky.fs +++ b/shaders/sky.fs @@ -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); } diff --git a/shaders/sky.h b/shaders/sky.h index 653173e..01bc5c2 100644 --- a/shaders/sky.h +++ b/shaders/sky.h @@ -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" ""}, }; diff --git a/shaders/terrain.fs b/shaders/terrain.fs index 7cf4dde..a6d2400 100644 --- a/shaders/terrain.fs +++ b/shaders/terrain.fs @@ -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)); diff --git a/shaders/terrain.h b/shaders/terrain.h index 1880395..9bbb939 100644 --- a/shaders/terrain.h +++ b/shaders/terrain.h @@ -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" diff --git a/shaders/water.fs b/shaders/water.fs index 077a8e6..1de00ec 100644 --- a/shaders/water.fs +++ b/shaders/water.fs @@ -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 ); } diff --git a/shaders/water.h b/shaders/water.h index 67b0745..995cdb8 100644 --- a/shaders/water.h +++ b/shaders/water.h @@ -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" ""}, }; diff --git a/shaders/water.vs b/shaders/water.vs index c8ac51b..a78bf30 100644 --- a/shaders/water.vs +++ b/shaders/water.vs @@ -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 index 0000000..7ffee31 --- /dev/null +++ b/shaders/water_old.fs @@ -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 ); +} diff --git a/terrain.h b/terrain.h index d35dc0a..81b311a 100644 --- 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); } diff --git a/textures/water_surf.png b/textures/water_surf.png index 493f823..3c1c6f5 100644 Binary files a/textures/water_surf.png and b/textures/water_surf.png differ diff --git a/vg.conf b/vg.conf index 99fc92e..634e451 100644 --- 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 c2a7122..ac1db00 100644 --- 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