fix rendering issue in workshop board preview
[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 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, 1 );
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 glEnable( GL_DEPTH_TEST );
88 glDisable( GL_BLEND );
89 glCullFace( GL_FRONT );
90 render_world( world, &water_cam, 0, 1, 0, 1 );
91 glCullFace( GL_BACK );
92
93 /*
94 * Create beneath view matrix
95 */
96 camera beneath_cam;
97 render_fb_bind( gpipeline.fb_water_beneath, 1 );
98 glClearColor( 1.0f, 0.0f, 0.0f, 0.0f );
99 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
100
101 m4x3_copy( cam->transform, beneath_cam.transform );
102 camera_update_view( &beneath_cam );
103
104 float bias = -(cam->transform[3][1]-world->water.height)*0.1f;
105
106 v4f clippb = { 0.0f, -1.0f, 0.0f, -(world->water.height) + bias };
107 m4x3_mulp( beneath_cam.transform_inverse, clippb, clippb );
108 clippb[3] *= -1.0f;
109
110 m4x4_copy( cam->mtx.p, beneath_cam.mtx.p );
111 m4x4_clip_projection( beneath_cam.mtx.p, clippb );
112 camera_finalize( &beneath_cam );
113
114 glEnable( GL_DEPTH_TEST );
115 glDisable( GL_BLEND );
116 render_world_depth( world, &beneath_cam );
117 //glViewport( 0,0, g_render_x, g_render_y );
118 }
119
120 static void render_water_surface( world_instance *world, camera *cam ){
121 if( !world->water.enabled )
122 return;
123
124 if( vg.quality_profile == k_quality_profile_high ){
125 /* Draw surface */
126 shader_scene_water_use();
127
128 render_fb_bind_texture( gpipeline.fb_water_reflection, 0, 0 );
129 shader_scene_water_uTexMain( 0 );
130
131 glActiveTexture( GL_TEXTURE1 );
132 glBindTexture( GL_TEXTURE_2D, world_water.tex_water_surf );
133 shader_scene_water_uTexDudv( 1 );
134
135 shader_scene_water_uInvRes( (v2f){
136 1.0f / (float)vg.window_x,
137 1.0f / (float)vg.window_y });
138
139 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, scene_water );
140
141 render_fb_bind_texture( gpipeline.fb_water_beneath, 0, 5 );
142 shader_scene_water_uTexBack( 5 );
143 shader_scene_water_uTime( world_static.time );
144 shader_scene_water_uCamera( cam->transform[3] );
145 shader_scene_water_uSurfaceY( world->water.height );
146
147 shader_scene_water_uPv( cam->mtx.pv );
148 shader_scene_water_uPvmPrev( cam->mtx_prev.pv );
149
150 m4x3f full;
151 m4x3_identity( full );
152 shader_scene_water_uMdl( full );
153
154 glEnable(GL_BLEND);
155 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
156 glBlendEquation(GL_FUNC_ADD);
157
158 mesh_bind( &world->mesh_no_collide );
159
160 for( int i=0; i<world->surface_count; i++ ){
161 struct world_surface *mat = &world->surfaces[i];
162
163 if( mat->info.shader == k_shader_water ){
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 shader_scene_water_fast_use();
175
176 glActiveTexture( GL_TEXTURE1 );
177 glBindTexture( GL_TEXTURE_2D, world_water.tex_water_surf );
178 shader_scene_water_fast_uTexDudv( 1 );
179
180 shader_scene_water_fast_uTime( world_static.time );
181 shader_scene_water_fast_uCamera( cam->transform[3] );
182 shader_scene_water_fast_uSurfaceY( world->water.height );
183
184 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, scene_water_fast );
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->surface_count; i++ ){
199 struct world_surface *mat = &world->surfaces[i];
200
201 if( mat->info.shader == k_shader_water ){
202 shader_scene_water_fast_uShoreColour( mat->info.colour );
203 shader_scene_water_fast_uOceanColour( mat->info.colour1 );
204
205 mdl_draw_submesh( &mat->sm_no_collide );
206 }
207 }
208
209 glDisable(GL_BLEND);
210 }
211 }
212
213 #endif /* WATER_C */