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