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