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