2 * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
8 #include "world_water.h"
9 #include "world_render.h"
11 #include "shaders/scene_water.h"
12 #include "shaders/scene_water_fast.h"
15 static void world_water_init(void)
17 vg_info( "world_water_init\n" );
18 shader_scene_water_register();
19 shader_scene_water_fast_register();
21 vg_tex2d_load_qoi_async_file( "textures/water_surf.qoi",
22 VG_TEX2D_LINEAR
|VG_TEX2D_REPEAT
,
23 &world_water
.tex_water_surf
);
25 vg_success( "done\n" );
28 static void water_set_surface( world_instance
*world
, float height
)
30 world
->water
.height
= height
;
31 v4_copy( (v4f
){ 0.0f
, 1.0f
, 0.0f
, height
}, world
->water
.plane
);
34 static void world_link_lighting_ub( world_instance
*world
, GLuint shader
);
35 static void world_bind_position_texture( world_instance
*world
,
36 GLuint shader
, GLuint location
,
38 static void world_bind_light_array( world_instance
*world
,
39 GLuint shader
, GLuint location
,
41 static void world_bind_light_index( world_instance
*world
,
42 GLuint shader
, GLuint location
,
46 * Does not write motion vectors
48 static void render_water_texture( world_instance
*world
, camera
*cam
,
51 if( !world
->water
.enabled
|| (vg
.quality_profile
== k_quality_profile_low
) )
54 /* Draw reflection buffa */
55 render_fb_bind( gpipeline
.fb_water_reflection
, 1 );
56 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
59 * Create flipped view matrix. Don't care about motion vectors
61 float cam_height
= cam
->transform
[3][1] - world
->water
.height
;
64 water_cam
.farz
= cam
->farz
;
65 water_cam
.nearz
= cam
->nearz
;
66 v3_copy( cam
->transform
[3], water_cam
.transform
[3] );
67 water_cam
.transform
[3][1] -= 2.0f
* cam_height
;
70 m3x3_identity( flip
);
72 m3x3_mul( flip
, cam
->transform
, water_cam
.transform
);
74 camera_update_view( &water_cam
);
77 * Create clipped projection
79 v4f clippa
= { 0.0f
, 1.0f
, 0.0f
, world
->water
.height
-0.1f
};
80 m4x3_mulp( water_cam
.transform_inverse
, clippa
, clippa
);
83 m4x4_copy( cam
->mtx
.p
, water_cam
.mtx
.p
);
84 m4x4_clip_projection( water_cam
.mtx
.p
, clippa
);
86 camera_finalize( &water_cam
);
91 glEnable( GL_DEPTH_TEST
);
92 glDisable( GL_BLEND
);
93 glCullFace( GL_FRONT
);
94 render_world( world
, &water_cam
, layer_depth
);
95 glCullFace( GL_BACK
);
98 * Create beneath view matrix
101 render_fb_bind( gpipeline
.fb_water_beneath
, 1 );
102 glClearColor( 1.0f
, 0.0f
, 0.0f
, 0.0f
);
103 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
105 m4x3_copy( cam
->transform
, beneath_cam
.transform
);
106 camera_update_view( &beneath_cam
);
108 float bias
= -(cam
->transform
[3][1]-world
->water
.height
)*0.1f
;
110 v4f clippb
= { 0.0f
, -1.0f
, 0.0f
, -(world
->water
.height
) + bias
};
111 m4x3_mulp( beneath_cam
.transform_inverse
, clippb
, clippb
);
114 m4x4_copy( cam
->mtx
.p
, beneath_cam
.mtx
.p
);
115 m4x4_clip_projection( beneath_cam
.mtx
.p
, clippb
);
116 camera_finalize( &beneath_cam
);
118 glEnable( GL_DEPTH_TEST
);
119 glDisable( GL_BLEND
);
120 render_world_depth( world
, &beneath_cam
);
121 //glViewport( 0,0, g_render_x, g_render_y );
124 static void render_water_surface( world_instance
*world
, camera
*cam
)
126 if( !world
->water
.enabled
)
129 if( vg
.quality_profile
== k_quality_profile_high
){
131 shader_scene_water_use();
133 render_fb_bind_texture( gpipeline
.fb_water_reflection
, 0, 0 );
134 shader_scene_water_uTexMain( 0 );
136 glActiveTexture( GL_TEXTURE1
);
137 glBindTexture( GL_TEXTURE_2D
, world_water
.tex_water_surf
);
138 shader_scene_water_uTexDudv( 1 );
140 shader_scene_water_uInvRes( (v2f
){
141 1.0f
/ (float)vg
.window_x
,
142 1.0f
/ (float)vg
.window_y
});
144 world_link_lighting_ub( world
, _shader_scene_water
.id
);
145 world_bind_position_texture( world
, _shader_scene_water
.id
,
146 _uniform_scene_water_g_world_depth
, 2 );
147 world_bind_light_array( world
, _shader_scene_water
.id
,
148 _uniform_scene_water_uLightsArray
, 4 );
149 world_bind_light_index( world
, _shader_scene_water
.id
,
150 _uniform_scene_water_uLightsIndex
, 5 );
152 render_fb_bind_texture( gpipeline
.fb_water_beneath
, 0, 3 );
153 shader_scene_water_uTexBack( 3 );
154 shader_scene_water_uTime( world_static
.time
);
155 shader_scene_water_uCamera( cam
->transform
[3] );
156 shader_scene_water_uSurfaceY( world
->water
.height
);
158 shader_scene_water_uPv( cam
->mtx
.pv
);
159 shader_scene_water_uPvmPrev( cam
->mtx_prev
.pv
);
162 m4x3_identity( full
);
163 shader_scene_water_uMdl( full
);
166 glBlendFunc(GL_SRC_ALPHA
,GL_ONE_MINUS_SRC_ALPHA
);
167 glBlendEquation(GL_FUNC_ADD
);
169 mesh_bind( &world
->mesh_no_collide
);
171 for( int i
=0; i
<world
->surface_count
; i
++ ){
172 struct world_surface
*mat
= &world
->surfaces
[i
];
174 if( mat
->info
.shader
== k_shader_water
){
175 shader_scene_water_uShoreColour( mat
->info
.colour
);
176 shader_scene_water_uOceanColour( mat
->info
.colour1
);
178 mdl_draw_submesh( &mat
->sm_no_collide
);
184 else if( vg
.quality_profile
== k_quality_profile_low
){
185 shader_scene_water_fast_use();
187 glActiveTexture( GL_TEXTURE1
);
188 glBindTexture( GL_TEXTURE_2D
, world_water
.tex_water_surf
);
189 shader_scene_water_fast_uTexDudv( 1 );
191 shader_scene_water_fast_uTime( world_static
.time
);
192 shader_scene_water_fast_uCamera( cam
->transform
[3] );
193 shader_scene_water_fast_uSurfaceY( world
->water
.height
);
194 world_link_lighting_ub( world
, _shader_scene_water_fast
.id
);
195 world_bind_position_texture( world
, _shader_scene_water_fast
.id
,
196 _uniform_scene_water_fast_g_world_depth
, 2 );
197 world_bind_light_array( world
, _shader_scene_water_fast
.id
,
198 _uniform_scene_water_fast_uLightsArray
, 4 );
201 m4x3_identity( full
);
202 shader_scene_water_fast_uMdl( full
);
203 shader_scene_water_fast_uPv( cam
->mtx
.pv
);
204 shader_scene_water_fast_uPvmPrev( cam
->mtx_prev
.pv
);
207 glBlendFunc(GL_SRC_ALPHA
,GL_ONE_MINUS_SRC_ALPHA
);
208 glBlendEquation(GL_FUNC_ADD
);
210 mesh_bind( &world
->mesh_no_collide
);
212 for( int i
=0; i
<world
->surface_count
; i
++ ){
213 struct world_surface
*mat
= &world
->surfaces
[i
];
215 if( mat
->info
.shader
== k_shader_water
){
216 shader_scene_water_fast_uShoreColour( mat
->info
.colour
);
217 shader_scene_water_fast_uOceanColour( mat
->info
.colour1
);
219 mdl_draw_submesh( &mat
->sm_no_collide
);