1.0
[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 VG_STATIC void render_water_texture( m4x3f camera )
55 {
56 if( !world.water.enabled || (vg.quality_profile == k_quality_profile_low) )
57 return;
58
59 /* Draw reflection buffa */
60 fb_use( &world.water.fbreflect );
61 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
62
63 m4x3f new_cam, inverse;
64 v3_copy( camera[3], new_cam[3] );
65 new_cam[3][1] -= 2.0f * (camera[3][1] - world.water.height);
66
67 m3x3f flip;
68 m3x3_identity( flip );
69 flip[1][1] = -1.0f;
70 m3x3_mul( flip, camera, new_cam );
71
72
73 v3f p0;
74 m3x3_mulv( new_cam, (v3f){0.0f,0.0f,-1.0f}, p0 );
75 v3_add( new_cam[3], p0, p0 );
76 vg_line( new_cam[3], p0, 0xffffffff );
77
78 m4x4f view;
79 vg_line_pt3( new_cam[3], 0.3f, 0xff00ffff );
80
81 m4x3_invert_affine( new_cam, inverse );
82 m4x3_expand( inverse, view );
83
84 v4f clippa = { 0.0f, 1.0f, 0.0f, world.water.height-0.1f };
85 m4x3_mulp( inverse, clippa, clippa );
86 clippa[3] *= -1.0f;
87
88 m4x4f projection;
89 m4x4_projection( projection,
90 gpipeline.fov,
91 (float)vg.window_x / (float)vg.window_y,
92 0.1f, 900.0f );
93 plane_clip_projection( projection, clippa );
94 m4x4_mul( projection, view, projection );
95
96 glCullFace( GL_FRONT );
97 render_world( projection, new_cam );
98 glCullFace( GL_BACK );
99
100
101 /* Draw beneath texture */
102 fb_use( &world.water.fbdepth );
103 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
104
105 m4x3_invert_affine( camera, inverse );
106 m4x3_expand( inverse, view );
107
108 float bias = -(camera[3][1]-world.water.height)*0.1f;
109 v4f clippb = { 0.0f, -1.0f, 0.0f, -(world.water.height) + bias };
110 m4x3_mulp( inverse, clippb, clippb );
111 clippb[3] *= -1.0f;
112
113 m4x4_projection( projection,
114 gpipeline.fov,
115 (float)vg.window_x / (float)vg.window_y,
116 0.1f, 900.0f );
117
118 plane_clip_projection( projection, clippb );
119 m4x4_mul( projection, view, projection );
120 render_world_depth( projection, camera );
121
122 glViewport( 0, 0, vg.window_x, vg.window_y );
123 }
124
125 VG_STATIC void render_water_surface( m4x4f pv, m4x3f camera )
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( camera[3] );
150 shader_water_uSurfaceY( world.water.height );
151
152 shader_water_uPv( pv );
153
154 m4x3f full;
155 m4x3_identity( full );
156 shader_water_uMdl( full );
157
158 glEnable(GL_BLEND);
159 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
160 glBlendEquation(GL_FUNC_ADD);
161
162 mesh_bind( &world.mesh_no_collide );
163
164 for( int i=0; i<world.material_count; i++ )
165 {
166 struct world_material *mat = &world.materials[i];
167
168 if( mat->info.shader == k_shader_water )
169 {
170 shader_water_uShoreColour( mat->info.colour );
171 shader_water_uOceanColour( mat->info.colour1 );
172
173 mdl_draw_submesh( &mat->sm_no_collide );
174 }
175 }
176
177 glDisable(GL_BLEND);
178 }
179 else if( vg.quality_profile == k_quality_profile_low )
180 {
181 shader_water_fast_use();
182
183 vg_tex2d_bind( &tex_water_surf, 1 );
184 shader_water_fast_uTexDudv( 1 );
185 shader_water_fast_uTime( world.time );
186 shader_water_fast_uCamera( camera[3] );
187 shader_water_fast_uSurfaceY( world.water.height );
188 shader_link_standard_ub( _shader_water_fast.id, 2 );
189
190 m4x3f full;
191 m4x3_identity( full );
192 shader_water_fast_uMdl( full );
193 shader_water_fast_uPv( pv );
194
195 glEnable(GL_BLEND);
196 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
197 glBlendEquation(GL_FUNC_ADD);
198
199 mesh_bind( &world.mesh_no_collide );
200
201 for( int i=0; i<world.material_count; i++ )
202 {
203 struct world_material *mat = &world.materials[i];
204
205 if( mat->info.shader == k_shader_water )
206 {
207 shader_water_fast_uShoreColour( mat->info.colour );
208 shader_water_fast_uOceanColour( mat->info.colour1 );
209
210 mdl_draw_submesh( &mat->sm_no_collide );
211 }
212 }
213
214 glDisable(GL_BLEND);
215 }
216 }
217
218 #endif /* WATER_H */