marker improvement
[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 VG_STATIC void world_bind_light_index( world_instance *world,
45 GLuint shader, GLuint location,
46 int slot );
47
48 /*
49 * Does not write motion vectors
50 */
51 VG_STATIC void render_water_texture( world_instance *world, camera *cam,
52 int layer_depth )
53 {
54 if( !world->water.enabled || (vg.quality_profile == k_quality_profile_low) )
55 return;
56
57 /* Draw reflection buffa */
58 render_fb_bind( gpipeline.fb_water_reflection );
59 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
60
61 /*
62 * Create flipped view matrix. Don't care about motion vectors
63 */
64 float cam_height = cam->transform[3][1] - world->water.height;
65
66 camera water_cam;
67 water_cam.farz = cam->farz;
68 water_cam.nearz = cam->nearz;
69 v3_copy( cam->transform[3], water_cam.transform[3] );
70 water_cam.transform[3][1] -= 2.0f * cam_height;
71
72 m3x3f flip;
73 m3x3_identity( flip );
74 flip[1][1] = -1.0f;
75 m3x3_mul( flip, cam->transform, water_cam.transform );
76
77 camera_update_view( &water_cam );
78
79 /*
80 * Create clipped projection
81 */
82 v4f clippa = { 0.0f, 1.0f, 0.0f, world->water.height-0.1f };
83 m4x3_mulp( water_cam.transform_inverse, clippa, clippa );
84 clippa[3] *= -1.0f;
85
86 m4x4_copy( cam->mtx.p, water_cam.mtx.p );
87 m4x4_clip_projection( water_cam.mtx.p, clippa );
88
89 camera_finalize( &water_cam );
90
91 /*
92 * Draw world
93 */
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 );
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 render_world_depth( world, &beneath_cam );
120 glViewport( 0, 0, vg.window_x, vg.window_y );
121 }
122
123 VG_STATIC void render_water_surface( world_instance *world, camera *cam )
124 {
125 if( !world->water.enabled )
126 return;
127
128 if( vg.quality_profile == k_quality_profile_high ){
129 /* Draw surface */
130 shader_scene_water_use();
131
132 render_fb_bind_texture( gpipeline.fb_water_reflection, 0, 0 );
133 shader_scene_water_uTexMain( 0 );
134
135 vg_tex2d_bind( &tex_water_surf, 1 );
136 shader_scene_water_uTexDudv( 1 );
137 shader_scene_water_uInvRes( (v2f){
138 1.0f / (float)vg.window_x,
139 1.0f / (float)vg.window_y });
140
141 world_link_lighting_ub( world, _shader_scene_water.id );
142 world_bind_position_texture( world, _shader_scene_water.id,
143 _uniform_scene_water_g_world_depth, 2 );
144 world_bind_light_array( world, _shader_scene_water.id,
145 _uniform_scene_water_uLightsArray, 4 );
146 world_bind_light_index( world, _shader_scene_water.id,
147 _uniform_scene_water_uLightsIndex, 5 );
148
149 render_fb_bind_texture( gpipeline.fb_water_beneath, 0, 3 );
150 shader_scene_water_uTexBack( 3 );
151 shader_scene_water_uTime( world_global.time );
152 shader_scene_water_uCamera( cam->transform[3] );
153 shader_scene_water_uSurfaceY( world->water.height );
154
155 shader_scene_water_uPv( cam->mtx.pv );
156 shader_scene_water_uPvmPrev( cam->mtx_prev.pv );
157
158 m4x3f full;
159 m4x3_identity( full );
160 shader_scene_water_uMdl( full );
161
162 glEnable(GL_BLEND);
163 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
164 glBlendEquation(GL_FUNC_ADD);
165
166 mesh_bind( &world->mesh_no_collide );
167
168 for( int i=0; i<world->surface_count; i++ ){
169 struct world_surface *mat = &world->surfaces[i];
170
171 if( mat->info.shader == k_shader_water ){
172 shader_scene_water_uShoreColour( mat->info.colour );
173 shader_scene_water_uOceanColour( mat->info.colour1 );
174
175 mdl_draw_submesh( &mat->sm_no_collide );
176 }
177 }
178
179 glDisable(GL_BLEND);
180 }
181 else if( vg.quality_profile == k_quality_profile_low ){
182 shader_scene_water_fast_use();
183
184 vg_tex2d_bind( &tex_water_surf, 1 );
185 shader_scene_water_fast_uTexDudv( 1 );
186 shader_scene_water_fast_uTime( world_global.time );
187 shader_scene_water_fast_uCamera( cam->transform[3] );
188 shader_scene_water_fast_uSurfaceY( world->water.height );
189 world_link_lighting_ub( world, _shader_scene_water_fast.id );
190 world_bind_position_texture( world, _shader_scene_water_fast.id,
191 _uniform_scene_water_fast_g_world_depth, 2 );
192 world_bind_light_array( world, _shader_scene_water_fast.id,
193 _uniform_scene_water_fast_uLightsArray, 4 );
194
195 m4x3f full;
196 m4x3_identity( full );
197 shader_scene_water_fast_uMdl( full );
198 shader_scene_water_fast_uPv( cam->mtx.pv );
199 shader_scene_water_fast_uPvmPrev( cam->mtx_prev.pv );
200
201 glEnable(GL_BLEND);
202 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
203 glBlendEquation(GL_FUNC_ADD);
204
205 mesh_bind( &world->mesh_no_collide );
206
207 for( int i=0; i<world->surface_count; i++ ){
208 struct world_surface *mat = &world->surfaces[i];
209
210 if( mat->info.shader == k_shader_water ){
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 */