reduced shader bind code dupe & adjust skate uprighter strenght
[carveJwlIkooP6JGAAIwe30JlM.git] / world_water.c
1 /*
2 * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #ifndef WATER_C
6 #define WATER_C
7
8 #include "world_water.h"
9 #include "world_render.h"
10 #include "render.h"
11 #include "shaders/scene_water.h"
12 #include "shaders/scene_water_fast.h"
13 #include "scene.h"
14
15 static void world_water_init(void){
16 vg_info( "world_water_init\n" );
17 shader_scene_water_register();
18 shader_scene_water_fast_register();
19
20 vg_tex2d_load_qoi_async_file( "textures/water_surf.qoi",
21 VG_TEX2D_LINEAR|VG_TEX2D_REPEAT,
22 &world_water.tex_water_surf );
23
24 vg_success( "done\n" );
25 }
26
27 static void water_set_surface( world_instance *world, float height ){
28 world->water.height = height;
29 v4_copy( (v4f){ 0.0f, 1.0f, 0.0f, height }, world->water.plane );
30 }
31
32 static void world_link_lighting_ub( world_instance *world, GLuint shader );
33 static void world_bind_position_texture( world_instance *world,
34 GLuint shader, GLuint location,
35 int slot );
36 static void world_bind_light_array( world_instance *world,
37 GLuint shader, GLuint location,
38 int slot );
39 static void world_bind_light_index( world_instance *world,
40 GLuint shader, GLuint location,
41 int slot );
42
43 /*
44 * Does not write motion vectors
45 */
46 static void render_water_texture( world_instance *world, camera *cam,
47 int layer_depth ){
48 if( !world->water.enabled || (vg.quality_profile == k_quality_profile_low) )
49 return;
50
51 /* Draw reflection buffa */
52 render_fb_bind( gpipeline.fb_water_reflection, 1 );
53 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
54
55 /*
56 * Create flipped view matrix. Don't care about motion vectors
57 */
58 float cam_height = cam->transform[3][1] - world->water.height;
59
60 camera water_cam;
61 water_cam.farz = cam->farz;
62 water_cam.nearz = cam->nearz;
63 v3_copy( cam->transform[3], water_cam.transform[3] );
64 water_cam.transform[3][1] -= 2.0f * cam_height;
65
66 m3x3f flip;
67 m3x3_identity( flip );
68 flip[1][1] = -1.0f;
69 m3x3_mul( flip, cam->transform, water_cam.transform );
70
71 camera_update_view( &water_cam );
72
73 /*
74 * Create clipped projection
75 */
76 v4f clippa = { 0.0f, 1.0f, 0.0f, world->water.height-0.1f };
77 m4x3_mulp( water_cam.transform_inverse, clippa, clippa );
78 clippa[3] *= -1.0f;
79
80 m4x4_copy( cam->mtx.p, water_cam.mtx.p );
81 m4x4_clip_projection( water_cam.mtx.p, clippa );
82
83 camera_finalize( &water_cam );
84
85 /*
86 * Draw world
87 */
88 glEnable( GL_DEPTH_TEST );
89 glDisable( GL_BLEND );
90 glCullFace( GL_FRONT );
91 render_world( world, &water_cam, layer_depth );
92 glCullFace( GL_BACK );
93
94 /*
95 * Create beneath view matrix
96 */
97 camera beneath_cam;
98 render_fb_bind( gpipeline.fb_water_beneath, 1 );
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 glEnable( GL_DEPTH_TEST );
116 glDisable( GL_BLEND );
117 render_world_depth( world, &beneath_cam );
118 //glViewport( 0,0, g_render_x, g_render_y );
119 }
120
121 static void render_water_surface( world_instance *world, camera *cam ){
122 if( !world->water.enabled )
123 return;
124
125 if( vg.quality_profile == k_quality_profile_high ){
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 glActiveTexture( GL_TEXTURE1 );
133 glBindTexture( GL_TEXTURE_2D, world_water.tex_water_surf );
134 shader_scene_water_uTexDudv( 1 );
135
136 shader_scene_water_uInvRes( (v2f){
137 1.0f / (float)vg.window_x,
138 1.0f / (float)vg.window_y });
139
140 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, scene_water );
141
142 render_fb_bind_texture( gpipeline.fb_water_beneath, 0, 5 );
143 shader_scene_water_uTexBack( 5 );
144 shader_scene_water_uTime( world_static.time );
145 shader_scene_water_uCamera( cam->transform[3] );
146 shader_scene_water_uSurfaceY( world->water.height );
147
148 shader_scene_water_uPv( cam->mtx.pv );
149 shader_scene_water_uPvmPrev( cam->mtx_prev.pv );
150
151 m4x3f full;
152 m4x3_identity( full );
153 shader_scene_water_uMdl( full );
154
155 glEnable(GL_BLEND);
156 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
157 glBlendEquation(GL_FUNC_ADD);
158
159 mesh_bind( &world->mesh_no_collide );
160
161 for( int i=0; i<world->surface_count; i++ ){
162 struct world_surface *mat = &world->surfaces[i];
163
164 if( mat->info.shader == k_shader_water ){
165 shader_scene_water_uShoreColour( mat->info.colour );
166 shader_scene_water_uOceanColour( mat->info.colour1 );
167
168 mdl_draw_submesh( &mat->sm_no_collide );
169 }
170 }
171
172 glDisable(GL_BLEND);
173 }
174 else if( vg.quality_profile == k_quality_profile_low ){
175 shader_scene_water_fast_use();
176
177 glActiveTexture( GL_TEXTURE1 );
178 glBindTexture( GL_TEXTURE_2D, world_water.tex_water_surf );
179 shader_scene_water_fast_uTexDudv( 1 );
180
181 shader_scene_water_fast_uTime( world_static.time );
182 shader_scene_water_fast_uCamera( cam->transform[3] );
183 shader_scene_water_fast_uSurfaceY( world->water.height );
184
185 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, scene_water_fast );
186
187 m4x3f full;
188 m4x3_identity( full );
189 shader_scene_water_fast_uMdl( full );
190 shader_scene_water_fast_uPv( cam->mtx.pv );
191 shader_scene_water_fast_uPvmPrev( cam->mtx_prev.pv );
192
193 glEnable(GL_BLEND);
194 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
195 glBlendEquation(GL_FUNC_ADD);
196
197 mesh_bind( &world->mesh_no_collide );
198
199 for( int i=0; i<world->surface_count; i++ ){
200 struct world_surface *mat = &world->surfaces[i];
201
202 if( mat->info.shader == k_shader_water ){
203 shader_scene_water_fast_uShoreColour( mat->info.colour );
204 shader_scene_water_fast_uOceanColour( mat->info.colour1 );
205
206 mdl_draw_submesh( &mat->sm_no_collide );
207 }
208 }
209
210 glDisable(GL_BLEND);
211 }
212 }
213
214 #endif /* WATER_C */