chaos caused by async
[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/scene_water.h"
11 #include "shaders/scene_water_fast.h"
12 #include "scene.h"
13
14 VG_STATIC GLuint tex_water_surf;
15
16 VG_STATIC void world_water_init(void)
17 {
18 vg_info( "world_water_init\n" );
19 shader_scene_water_register();
20 shader_scene_water_fast_register();
21
22 vg_tex2d_load_qoi_async_file( "textures/water_surf.qoi",
23 VG_TEX2D_LINEAR|VG_TEX2D_REPEAT,
24 &tex_water_surf );
25
26 vg_success( "done\n" );
27 }
28
29 VG_STATIC void water_set_surface( world_instance *world, float height )
30 {
31 world->water.height = height;
32 v4_copy( (v4f){ 0.0f, 1.0f, 0.0f, height }, world->water.plane );
33 }
34
35 VG_STATIC void world_link_lighting_ub( world_instance *world, GLuint shader );
36 VG_STATIC void world_bind_position_texture( world_instance *world,
37 GLuint shader, GLuint location,
38 int slot );
39 VG_STATIC void world_bind_light_array( world_instance *world,
40 GLuint shader, GLuint location,
41 int slot );
42 VG_STATIC void world_bind_light_index( world_instance *world,
43 GLuint shader, GLuint location,
44 int slot );
45
46 /*
47 * Does not write motion vectors
48 */
49 VG_STATIC void render_water_texture( world_instance *world, camera *cam,
50 int layer_depth )
51 {
52 if( !world->water.enabled || (vg.quality_profile == k_quality_profile_low) )
53 return;
54
55 /* Draw reflection buffa */
56 render_fb_bind( gpipeline.fb_water_reflection, 1 );
57 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
58
59 /*
60 * Create flipped view matrix. Don't care about motion vectors
61 */
62 float cam_height = cam->transform[3][1] - world->water.height;
63
64 camera water_cam;
65 water_cam.farz = cam->farz;
66 water_cam.nearz = cam->nearz;
67 v3_copy( cam->transform[3], water_cam.transform[3] );
68 water_cam.transform[3][1] -= 2.0f * cam_height;
69
70 m3x3f flip;
71 m3x3_identity( flip );
72 flip[1][1] = -1.0f;
73 m3x3_mul( flip, cam->transform, water_cam.transform );
74
75 camera_update_view( &water_cam );
76
77 /*
78 * Create clipped projection
79 */
80 v4f clippa = { 0.0f, 1.0f, 0.0f, world->water.height-0.1f };
81 m4x3_mulp( water_cam.transform_inverse, clippa, clippa );
82 clippa[3] *= -1.0f;
83
84 m4x4_copy( cam->mtx.p, water_cam.mtx.p );
85 m4x4_clip_projection( water_cam.mtx.p, clippa );
86
87 camera_finalize( &water_cam );
88
89 /*
90 * Draw world
91 */
92 glEnable( GL_DEPTH_TEST );
93 glDisable( GL_BLEND );
94 glCullFace( GL_FRONT );
95 render_world( world, &water_cam, layer_depth );
96 glCullFace( GL_BACK );
97
98 /*
99 * Create beneath view matrix
100 */
101 camera beneath_cam;
102 render_fb_bind( gpipeline.fb_water_beneath, 1 );
103 glClearColor( 1.0f, 0.0f, 0.0f, 0.0f );
104 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
105
106 m4x3_copy( cam->transform, beneath_cam.transform );
107 camera_update_view( &beneath_cam );
108
109 float bias = -(cam->transform[3][1]-world->water.height)*0.1f;
110
111 v4f clippb = { 0.0f, -1.0f, 0.0f, -(world->water.height) + bias };
112 m4x3_mulp( beneath_cam.transform_inverse, clippb, clippb );
113 clippb[3] *= -1.0f;
114
115 m4x4_copy( cam->mtx.p, beneath_cam.mtx.p );
116 m4x4_clip_projection( beneath_cam.mtx.p, clippb );
117 camera_finalize( &beneath_cam );
118
119 glEnable( GL_DEPTH_TEST );
120 glDisable( GL_BLEND );
121 render_world_depth( world, &beneath_cam );
122 //glViewport( 0,0, g_render_x, g_render_y );
123 }
124
125 VG_STATIC void render_water_surface( world_instance *world, camera *cam )
126 {
127 if( !world->water.enabled )
128 return;
129
130 if( vg.quality_profile == k_quality_profile_high ){
131 /* Draw surface */
132 shader_scene_water_use();
133
134 render_fb_bind_texture( gpipeline.fb_water_reflection, 0, 0 );
135 shader_scene_water_uTexMain( 0 );
136
137 glActiveTexture( GL_TEXTURE1 );
138 glBindTexture( GL_TEXTURE_2D, tex_water_surf );
139 shader_scene_water_uTexDudv( 1 );
140
141 shader_scene_water_uInvRes( (v2f){
142 1.0f / (float)vg.window_x,
143 1.0f / (float)vg.window_y });
144
145 world_link_lighting_ub( world, _shader_scene_water.id );
146 world_bind_position_texture( world, _shader_scene_water.id,
147 _uniform_scene_water_g_world_depth, 2 );
148 world_bind_light_array( world, _shader_scene_water.id,
149 _uniform_scene_water_uLightsArray, 4 );
150 world_bind_light_index( world, _shader_scene_water.id,
151 _uniform_scene_water_uLightsIndex, 5 );
152
153 render_fb_bind_texture( gpipeline.fb_water_beneath, 0, 3 );
154 shader_scene_water_uTexBack( 3 );
155 shader_scene_water_uTime( world_global.time );
156 shader_scene_water_uCamera( cam->transform[3] );
157 shader_scene_water_uSurfaceY( world->water.height );
158
159 shader_scene_water_uPv( cam->mtx.pv );
160 shader_scene_water_uPvmPrev( cam->mtx_prev.pv );
161
162 m4x3f full;
163 m4x3_identity( full );
164 shader_scene_water_uMdl( full );
165
166 glEnable(GL_BLEND);
167 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
168 glBlendEquation(GL_FUNC_ADD);
169
170 mesh_bind( &world->mesh_no_collide );
171
172 for( int i=0; i<world->surface_count; i++ ){
173 struct world_surface *mat = &world->surfaces[i];
174
175 if( mat->info.shader == k_shader_water ){
176 shader_scene_water_uShoreColour( mat->info.colour );
177 shader_scene_water_uOceanColour( mat->info.colour1 );
178
179 mdl_draw_submesh( &mat->sm_no_collide );
180 }
181 }
182
183 glDisable(GL_BLEND);
184 }
185 else if( vg.quality_profile == k_quality_profile_low ){
186 shader_scene_water_fast_use();
187
188 glActiveTexture( GL_TEXTURE1 );
189 glBindTexture( GL_TEXTURE_2D, tex_water_surf );
190 shader_scene_water_fast_uTexDudv( 1 );
191
192 shader_scene_water_fast_uTime( world_global.time );
193 shader_scene_water_fast_uCamera( cam->transform[3] );
194 shader_scene_water_fast_uSurfaceY( world->water.height );
195 world_link_lighting_ub( world, _shader_scene_water_fast.id );
196 world_bind_position_texture( world, _shader_scene_water_fast.id,
197 _uniform_scene_water_fast_g_world_depth, 2 );
198 world_bind_light_array( world, _shader_scene_water_fast.id,
199 _uniform_scene_water_fast_uLightsArray, 4 );
200
201 m4x3f full;
202 m4x3_identity( full );
203 shader_scene_water_fast_uMdl( full );
204 shader_scene_water_fast_uPv( cam->mtx.pv );
205 shader_scene_water_fast_uPvmPrev( cam->mtx_prev.pv );
206
207 glEnable(GL_BLEND);
208 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
209 glBlendEquation(GL_FUNC_ADD);
210
211 mesh_bind( &world->mesh_no_collide );
212
213 for( int i=0; i<world->surface_count; i++ ){
214 struct world_surface *mat = &world->surfaces[i];
215
216 if( mat->info.shader == k_shader_water ){
217 shader_scene_water_fast_uShoreColour( mat->info.colour );
218 shader_scene_water_fast_uOceanColour( mat->info.colour1 );
219
220 mdl_draw_submesh( &mat->sm_no_collide );
221 }
222 }
223
224 glDisable(GL_BLEND);
225 }
226 }
227
228 #endif /* WATER_H */