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