framebuffer viewer
[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 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( 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 /*
38 * Does not write motion vectors
39 */
40 VG_STATIC void render_water_texture( camera *cam )
41 {
42 if( !world.water.enabled || (vg.quality_profile == k_quality_profile_low) )
43 return;
44
45 /* Draw reflection buffa */
46 render_fb_bind( gpipeline.fb_water_reflection );
47 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
48
49 /*
50 * Create flipped view matrix. Don't care about motion vectors
51 */
52 float cam_height = cam->transform[3][1] - world.water.height;
53
54 camera water_cam;
55 water_cam.farz = cam->farz;
56 water_cam.nearz = cam->nearz;
57 v3_copy( cam->transform[3], water_cam.transform[3] );
58 water_cam.transform[3][1] -= 2.0f * cam_height;
59
60 m3x3f flip;
61 m3x3_identity( flip );
62 flip[1][1] = -1.0f;
63 m3x3_mul( flip, cam->transform, water_cam.transform );
64
65 camera_update_view( &water_cam );
66
67 /*
68 * Create clipped projection
69 */
70 v4f clippa = { 0.0f, 1.0f, 0.0f, world.water.height-0.1f };
71 m4x3_mulp( water_cam.transform_inverse, clippa, clippa );
72 clippa[3] *= -1.0f;
73
74 m4x4_copy( cam->mtx.p, water_cam.mtx.p );
75 m4x4_clip_projection( water_cam.mtx.p, clippa );
76
77 camera_finalize( &water_cam );
78
79 /*
80 * Draw world
81 */
82 glCullFace( GL_FRONT );
83 render_world( &water_cam );
84 glCullFace( GL_BACK );
85
86 /*
87 * Create beneath view matrix
88 */
89 camera beneath_cam;
90 render_fb_bind( gpipeline.fb_water_beneath );
91 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
92
93 m4x3_copy( cam->transform, beneath_cam.transform );
94 camera_update_view( &beneath_cam );
95
96 float bias = -(cam->transform[3][1]-world.water.height)*0.1f;
97
98 v4f clippb = { 0.0f, -1.0f, 0.0f, -(world.water.height) + bias };
99 m4x3_mulp( beneath_cam.transform_inverse, clippb, clippb );
100 clippb[3] *= -1.0f;
101
102 m4x4_copy( cam->mtx.p, beneath_cam.mtx.p );
103 m4x4_clip_projection( beneath_cam.mtx.p, clippb );
104 camera_finalize( &beneath_cam );
105
106 render_world_depth( &beneath_cam );
107 glViewport( 0, 0, vg.window_x, vg.window_y );
108 }
109
110 VG_STATIC void render_water_surface( camera *cam )
111 {
112 if( !world.water.enabled )
113 return;
114
115 if( vg.quality_profile == k_quality_profile_high )
116 {
117 /* Draw surface */
118 shader_water_use();
119
120 render_fb_bind_texture( gpipeline.fb_water_reflection, 0, 0 );
121 shader_water_uTexMain( 0 );
122
123 vg_tex2d_bind( &tex_water_surf, 1 );
124 shader_water_uTexDudv( 1 );
125 shader_water_uInvRes( (v2f){
126 1.0f / (float)vg.window_x,
127 1.0f / (float)vg.window_y });
128
129 shader_link_standard_ub( _shader_water.id, 2 );
130
131 render_fb_bind_texture( gpipeline.fb_water_beneath, 0, 3 );
132 shader_water_uTexBack( 3 );
133 shader_water_uTime( world.time );
134 shader_water_uCamera( cam->transform[3] );
135 shader_water_uSurfaceY( world.water.height );
136
137 shader_water_uPv( cam->mtx.pv );
138 shader_water_uPvmPrev( cam->mtx_prev.pv );
139
140 m4x3f full;
141 m4x3_identity( full );
142 shader_water_uMdl( full );
143
144 glEnable(GL_BLEND);
145 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
146 glBlendEquation(GL_FUNC_ADD);
147
148 mesh_bind( &world.mesh_no_collide );
149
150 for( int i=0; i<world.material_count; i++ )
151 {
152 struct world_material *mat = &world.materials[i];
153
154 if( mat->info.shader == k_shader_water )
155 {
156 shader_water_uShoreColour( mat->info.colour );
157 shader_water_uOceanColour( mat->info.colour1 );
158
159 mdl_draw_submesh( &mat->sm_no_collide );
160 }
161 }
162
163 glDisable(GL_BLEND);
164 }
165 else if( vg.quality_profile == k_quality_profile_low )
166 {
167 shader_water_fast_use();
168
169 vg_tex2d_bind( &tex_water_surf, 1 );
170 shader_water_fast_uTexDudv( 1 );
171 shader_water_fast_uTime( world.time );
172 shader_water_fast_uCamera( cam->transform[3] );
173 shader_water_fast_uSurfaceY( world.water.height );
174 shader_link_standard_ub( _shader_water_fast.id, 2 );
175
176 m4x3f full;
177 m4x3_identity( full );
178 shader_water_fast_uMdl( full );
179 shader_water_fast_uPv( cam->mtx.pv );
180 shader_water_fast_uPvmPrev( cam->mtx_prev.pv );
181
182 glEnable(GL_BLEND);
183 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
184 glBlendEquation(GL_FUNC_ADD);
185
186 mesh_bind( &world.mesh_no_collide );
187
188 for( int i=0; i<world.material_count; i++ )
189 {
190 struct world_material *mat = &world.materials[i];
191
192 if( mat->info.shader == k_shader_water )
193 {
194 shader_water_fast_uShoreColour( mat->info.colour );
195 shader_water_fast_uOceanColour( mat->info.colour1 );
196
197 mdl_draw_submesh( &mat->sm_no_collide );
198 }
199 }
200
201 glDisable(GL_BLEND);
202 }
203 }
204
205 #endif /* WATER_H */