add motion vectors to all shaders
[carveJwlIkooP6JGAAIwe30JlM.git] / world_water.h
1 /*
2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #ifndef WATER_H
6 #define WATER_H
7
8 #include "world.h"
9 #include "render.h"
10 #include "shaders/water.h"
11 #include "shaders/water_fast.h"
12 #include "scene.h"
13
14 vg_tex2d tex_water_surf = { .path = "textures/water_surf.qoi" };
15
16 VG_STATIC void world_water_init(void)
17 {
18 vg_info( "world_water_init\n" );
19 shader_water_register();
20 shader_water_fast_register();
21
22 vg_acquire_thread_sync();
23 {
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;
28
29 fb_init( &world.water.fbreflect );
30 fb_init( &world.water.fbdepth );
31
32 vg_tex2d_init( (vg_tex2d *[]){&tex_water_surf}, 1 );
33 }
34 vg_release_thread_sync();
35
36 vg_success( "done\n" );
37 }
38
39 VG_STATIC void water_fb_resize(void)
40 {
41 if( !world.water.enabled )
42 return;
43
44 fb_resize( &world.water.fbreflect );
45 fb_resize( &world.water.fbdepth );
46 }
47
48 VG_STATIC void water_set_surface( float height )
49 {
50 world.water.height = height;
51 v4_copy( (v4f){ 0.0f, 1.0f, 0.0f, height }, world.water.plane );
52 }
53
54 /*
55 * Does not write motion vectors
56 */
57 VG_STATIC void render_water_texture( camera *cam )
58 {
59 if( !world.water.enabled || (vg.quality_profile == k_quality_profile_low) )
60 return;
61
62 /* Draw reflection buffa */
63 fb_use( &world.water.fbreflect );
64 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
65
66 /*
67 * Create flipped view matrix. Don't care about motion vectors
68 */
69 float cam_height = cam->transform[3][1] - world.water.height;
70
71 camera water_cam;
72 v3_copy( cam->transform[3], water_cam.transform[3] );
73 water_cam.transform[3][1] -= 2.0f * cam_height;
74
75 m3x3f flip;
76 m3x3_identity( flip );
77 flip[1][1] = -1.0f;
78 m3x3_mul( flip, cam->transform, water_cam.transform );
79
80 camera_update_view( &water_cam );
81
82 /*
83 * Create clipped projection
84 */
85 v4f clippa = { 0.0f, 1.0f, 0.0f, world.water.height-0.1f };
86 m4x3_mulp( water_cam.transform_inverse, clippa, clippa );
87 clippa[3] *= -1.0f;
88
89 m4x4_copy( cam->mtx.p, water_cam.mtx.p );
90 m4x4_clip_projection( water_cam.mtx.p, clippa );
91
92 camera_finalize( &water_cam );
93
94 /*
95 * Draw world
96 */
97 glCullFace( GL_FRONT );
98 render_world( &water_cam );
99 glCullFace( GL_BACK );
100
101 /*
102 * Create beneath view matrix
103 */
104 camera beneath_cam;
105 fb_use( &world.water.fbdepth );
106 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
107
108 m4x3_copy( cam->transform, beneath_cam.transform );
109 camera_update_view( &beneath_cam );
110
111 float bias = -(cam->transform[3][1]-world.water.height)*0.1f;
112
113 v4f clippb = { 0.0f, -1.0f, 0.0f, -(world.water.height) + bias };
114 m4x3_mulp( beneath_cam.transform_inverse, clippb, clippb );
115 clippb[3] *= -1.0f;
116
117 m4x4_copy( cam->mtx.p, beneath_cam.mtx.p );
118 m4x4_clip_projection( beneath_cam.mtx.p, clippb );
119 camera_finalize( &beneath_cam );
120
121 render_world_depth( &beneath_cam );
122 glViewport( 0, 0, vg.window_x, vg.window_y );
123 }
124
125 VG_STATIC void render_water_surface( camera *cam )
126 {
127 if( !world.water.enabled )
128 return;
129
130 if( vg.quality_profile == k_quality_profile_high )
131 {
132 /* Draw surface */
133 shader_water_use();
134
135 fb_bindtex( &world.water.fbreflect, 0 );
136 shader_water_uTexMain( 0 );
137
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 });
143
144 shader_link_standard_ub( _shader_water.id, 2 );
145
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 );
151
152 shader_water_uPv( cam->mtx.pv );
153 shader_water_uPvmPrev( cam->mtx_prev.pv );
154
155 m4x3f full;
156 m4x3_identity( full );
157 shader_water_uMdl( full );
158
159 glEnable(GL_BLEND);
160 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
161 glBlendEquation(GL_FUNC_ADD);
162
163 mesh_bind( &world.mesh_no_collide );
164
165 for( int i=0; i<world.material_count; i++ )
166 {
167 struct world_material *mat = &world.materials[i];
168
169 if( mat->info.shader == k_shader_water )
170 {
171 shader_water_uShoreColour( mat->info.colour );
172 shader_water_uOceanColour( mat->info.colour1 );
173
174 mdl_draw_submesh( &mat->sm_no_collide );
175 }
176 }
177
178 glDisable(GL_BLEND);
179 }
180 else if( vg.quality_profile == k_quality_profile_low )
181 {
182 shader_water_fast_use();
183
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 );
190
191 m4x3f full;
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 );
196
197 glEnable(GL_BLEND);
198 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
199 glBlendEquation(GL_FUNC_ADD);
200
201 mesh_bind( &world.mesh_no_collide );
202
203 for( int i=0; i<world.material_count; i++ )
204 {
205 struct world_material *mat = &world.materials[i];
206
207 if( mat->info.shader == k_shader_water )
208 {
209 shader_water_fast_uShoreColour( mat->info.colour );
210 shader_water_fast_uOceanColour( mat->info.colour1 );
211
212 mdl_draw_submesh( &mat->sm_no_collide );
213 }
214 }
215
216 glDisable(GL_BLEND);
217 }
218 }
219
220 #endif /* WATER_H */