2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
10 #include "shaders/water.h"
11 #include "shaders/water_fast.h"
14 vg_tex2d tex_water_surf
= { .path
= "textures/water_surf.qoi" };
16 VG_STATIC
void world_water_init(void)
18 vg_info( "world_water_init\n" );
19 shader_water_register();
20 shader_water_fast_register();
22 vg_acquire_thread_sync();
24 world
.water
.fbreflect
.format
= GL_RGB
;
25 world
.water
.fbreflect
.div
= 3;
26 world
.water
.fbdepth
.format
= GL_RGBA
;
27 world
.water
.fbdepth
.div
= 4;
29 fb_init( &world
.water
.fbreflect
);
30 fb_init( &world
.water
.fbdepth
);
32 vg_tex2d_init( (vg_tex2d
*[]){&tex_water_surf
}, 1 );
34 vg_release_thread_sync();
36 vg_success( "done\n" );
39 VG_STATIC
void water_fb_resize(void)
41 if( !world
.water
.enabled
)
44 fb_resize( &world
.water
.fbreflect
);
45 fb_resize( &world
.water
.fbdepth
);
48 VG_STATIC
void water_set_surface( float height
)
50 world
.water
.height
= height
;
51 v4_copy( (v4f
){ 0.0f
, 1.0f
, 0.0f
, height
}, world
.water
.plane
);
55 * Does not write motion vectors
57 VG_STATIC
void render_water_texture( camera
*cam
)
59 if( !world
.water
.enabled
|| (vg
.quality_profile
== k_quality_profile_low
) )
62 /* Draw reflection buffa */
63 fb_use( &world
.water
.fbreflect
);
64 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
67 * Create flipped view matrix. Don't care about motion vectors
69 float cam_height
= cam
->transform
[3][1] - world
.water
.height
;
72 v3_copy( cam
->transform
[3], water_cam
.transform
[3] );
73 water_cam
.transform
[3][1] -= 2.0f
* cam_height
;
76 m3x3_identity( flip
);
78 m3x3_mul( flip
, cam
->transform
, water_cam
.transform
);
80 camera_update_view( &water_cam
);
83 * Create clipped projection
85 v4f clippa
= { 0.0f
, 1.0f
, 0.0f
, world
.water
.height
-0.1f
};
86 m4x3_mulp( water_cam
.transform_inverse
, clippa
, clippa
);
89 m4x4_copy( cam
->mtx
.p
, water_cam
.mtx
.p
);
90 m4x4_clip_projection( water_cam
.mtx
.p
, clippa
);
92 camera_finalize( &water_cam
);
97 glCullFace( GL_FRONT
);
98 render_world( &water_cam
);
99 glCullFace( GL_BACK
);
102 * Create beneath view matrix
105 fb_use( &world
.water
.fbdepth
);
106 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
108 m4x3_copy( cam
->transform
, beneath_cam
.transform
);
109 camera_update_view( &beneath_cam
);
111 float bias
= -(cam
->transform
[3][1]-world
.water
.height
)*0.1f
;
113 v4f clippb
= { 0.0f
, -1.0f
, 0.0f
, -(world
.water
.height
) + bias
};
114 m4x3_mulp( beneath_cam
.transform_inverse
, clippb
, clippb
);
117 m4x4_copy( cam
->mtx
.p
, beneath_cam
.mtx
.p
);
118 m4x4_clip_projection( beneath_cam
.mtx
.p
, clippb
);
119 camera_finalize( &beneath_cam
);
121 render_world_depth( &beneath_cam
);
122 glViewport( 0, 0, vg
.window_x
, vg
.window_y
);
125 VG_STATIC
void render_water_surface( camera
*cam
)
127 if( !world
.water
.enabled
)
130 if( vg
.quality_profile
== k_quality_profile_high
)
135 fb_bindtex( &world
.water
.fbreflect
, 0 );
136 shader_water_uTexMain( 0 );
138 vg_tex2d_bind( &tex_water_surf
, 1 );
139 shader_water_uTexDudv( 1 );
140 shader_water_uInvRes( (v2f
){
141 1.0f
/ (float)vg
.window_x
,
142 1.0f
/ (float)vg
.window_y
});
144 shader_link_standard_ub( _shader_water
.id
, 2 );
146 fb_bindtex( &world
.water
.fbdepth
, 3 );
147 shader_water_uTexBack( 3 );
148 shader_water_uTime( world
.time
);
149 shader_water_uCamera( cam
->transform
[3] );
150 shader_water_uSurfaceY( world
.water
.height
);
152 shader_water_uPv( cam
->mtx
.pv
);
153 shader_water_uPvmPrev( cam
->mtx_prev
.pv
);
156 m4x3_identity( full
);
157 shader_water_uMdl( full
);
160 glBlendFunc(GL_SRC_ALPHA
,GL_ONE_MINUS_SRC_ALPHA
);
161 glBlendEquation(GL_FUNC_ADD
);
163 mesh_bind( &world
.mesh_no_collide
);
165 for( int i
=0; i
<world
.material_count
; i
++ )
167 struct world_material
*mat
= &world
.materials
[i
];
169 if( mat
->info
.shader
== k_shader_water
)
171 shader_water_uShoreColour( mat
->info
.colour
);
172 shader_water_uOceanColour( mat
->info
.colour1
);
174 mdl_draw_submesh( &mat
->sm_no_collide
);
180 else if( vg
.quality_profile
== k_quality_profile_low
)
182 shader_water_fast_use();
184 vg_tex2d_bind( &tex_water_surf
, 1 );
185 shader_water_fast_uTexDudv( 1 );
186 shader_water_fast_uTime( world
.time
);
187 shader_water_fast_uCamera( cam
->transform
[3] );
188 shader_water_fast_uSurfaceY( world
.water
.height
);
189 shader_link_standard_ub( _shader_water_fast
.id
, 2 );
192 m4x3_identity( full
);
193 shader_water_fast_uMdl( full
);
194 shader_water_fast_uPv( cam
->mtx
.pv
);
195 shader_water_fast_uPvmPrev( cam
->mtx_prev
.pv
);
198 glBlendFunc(GL_SRC_ALPHA
,GL_ONE_MINUS_SRC_ALPHA
);
199 glBlendEquation(GL_FUNC_ADD
);
201 mesh_bind( &world
.mesh_no_collide
);
203 for( int i
=0; i
<world
.material_count
; i
++ )
205 struct world_material
*mat
= &world
.materials
[i
];
207 if( mat
->info
.shader
== k_shader_water
)
209 shader_water_fast_uShoreColour( mat
->info
.colour
);
210 shader_water_fast_uOceanColour( mat
->info
.colour1
);
212 mdl_draw_submesh( &mat
->sm_no_collide
);