2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
10 #include "shaders/scene_water.h"
11 #include "shaders/scene_water_fast.h"
14 vg_tex2d tex_water_surf
= { .path
= "textures/water_surf.qoi" };
16 VG_STATIC
void world_water_init(void)
18 vg_info( "world_water_init\n" );
19 shader_scene_water_register();
20 shader_scene_water_fast_register();
22 vg_acquire_thread_sync();
24 vg_tex2d_init( (vg_tex2d
*[]){&tex_water_surf
}, 1 );
26 vg_release_thread_sync();
28 vg_success( "done\n" );
31 VG_STATIC
void water_set_surface( world_instance
*world
, float height
)
33 world
->water
.height
= height
;
34 v4_copy( (v4f
){ 0.0f
, 1.0f
, 0.0f
, height
}, world
->water
.plane
);
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
,
41 VG_STATIC
void world_bind_light_array( world_instance
*world
,
42 GLuint shader
, GLuint location
,
44 VG_STATIC
void world_bind_light_index( world_instance
*world
,
45 GLuint shader
, GLuint location
,
49 * Does not write motion vectors
51 VG_STATIC
void render_water_texture( world_instance
*world
, camera
*cam
,
54 if( !world
->water
.enabled
|| (vg
.quality_profile
== k_quality_profile_low
) )
57 /* Draw reflection buffa */
58 render_fb_bind( gpipeline
.fb_water_reflection
, 1 );
59 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
62 * Create flipped view matrix. Don't care about motion vectors
64 float cam_height
= cam
->transform
[3][1] - world
->water
.height
;
67 water_cam
.farz
= cam
->farz
;
68 water_cam
.nearz
= cam
->nearz
;
69 v3_copy( cam
->transform
[3], water_cam
.transform
[3] );
70 water_cam
.transform
[3][1] -= 2.0f
* cam_height
;
73 m3x3_identity( flip
);
75 m3x3_mul( flip
, cam
->transform
, water_cam
.transform
);
77 camera_update_view( &water_cam
);
80 * Create clipped projection
82 v4f clippa
= { 0.0f
, 1.0f
, 0.0f
, world
->water
.height
-0.1f
};
83 m4x3_mulp( water_cam
.transform_inverse
, clippa
, clippa
);
86 m4x4_copy( cam
->mtx
.p
, water_cam
.mtx
.p
);
87 m4x4_clip_projection( water_cam
.mtx
.p
, clippa
);
89 camera_finalize( &water_cam
);
94 glEnable( GL_DEPTH_TEST
);
95 glDisable( GL_BLEND
);
96 glCullFace( GL_FRONT
);
97 render_world( world
, &water_cam
, layer_depth
);
98 glCullFace( GL_BACK
);
101 * Create beneath view matrix
104 render_fb_bind( gpipeline
.fb_water_beneath
, 1 );
105 glClearColor( 1.0f
, 0.0f
, 0.0f
, 0.0f
);
106 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
108 m4x3_copy( cam
->transform
, beneath_cam
.transform
);
109 camera_update_view( &beneath_cam
);
111 float bias
= -(cam
->transform
[3][1]-world
->water
.height
)*0.1f
;
113 v4f clippb
= { 0.0f
, -1.0f
, 0.0f
, -(world
->water
.height
) + bias
};
114 m4x3_mulp( beneath_cam
.transform_inverse
, clippb
, clippb
);
117 m4x4_copy( cam
->mtx
.p
, beneath_cam
.mtx
.p
);
118 m4x4_clip_projection( beneath_cam
.mtx
.p
, clippb
);
119 camera_finalize( &beneath_cam
);
121 glEnable( GL_DEPTH_TEST
);
122 glDisable( GL_BLEND
);
123 render_world_depth( world
, &beneath_cam
);
124 //glViewport( 0,0, g_render_x, g_render_y );
127 VG_STATIC
void render_water_surface( world_instance
*world
, camera
*cam
)
129 if( !world
->water
.enabled
)
132 if( vg
.quality_profile
== k_quality_profile_high
){
134 shader_scene_water_use();
136 render_fb_bind_texture( gpipeline
.fb_water_reflection
, 0, 0 );
137 shader_scene_water_uTexMain( 0 );
139 vg_tex2d_bind( &tex_water_surf
, 1 );
140 shader_scene_water_uTexDudv( 1 );
141 shader_scene_water_uInvRes( (v2f
){
142 1.0f
/ (float)vg
.window_x
,
143 1.0f
/ (float)vg
.window_y
});
145 world_link_lighting_ub( world
, _shader_scene_water
.id
);
146 world_bind_position_texture( world
, _shader_scene_water
.id
,
147 _uniform_scene_water_g_world_depth
, 2 );
148 world_bind_light_array( world
, _shader_scene_water
.id
,
149 _uniform_scene_water_uLightsArray
, 4 );
150 world_bind_light_index( world
, _shader_scene_water
.id
,
151 _uniform_scene_water_uLightsIndex
, 5 );
153 render_fb_bind_texture( gpipeline
.fb_water_beneath
, 0, 3 );
154 shader_scene_water_uTexBack( 3 );
155 shader_scene_water_uTime( world_global
.time
);
156 shader_scene_water_uCamera( cam
->transform
[3] );
157 shader_scene_water_uSurfaceY( world
->water
.height
);
159 shader_scene_water_uPv( cam
->mtx
.pv
);
160 shader_scene_water_uPvmPrev( cam
->mtx_prev
.pv
);
163 m4x3_identity( full
);
164 shader_scene_water_uMdl( full
);
167 glBlendFunc(GL_SRC_ALPHA
,GL_ONE_MINUS_SRC_ALPHA
);
168 glBlendEquation(GL_FUNC_ADD
);
170 mesh_bind( &world
->mesh_no_collide
);
172 for( int i
=0; i
<world
->surface_count
; i
++ ){
173 struct world_surface
*mat
= &world
->surfaces
[i
];
175 if( mat
->info
.shader
== k_shader_water
){
176 shader_scene_water_uShoreColour( mat
->info
.colour
);
177 shader_scene_water_uOceanColour( mat
->info
.colour1
);
179 mdl_draw_submesh( &mat
->sm_no_collide
);
185 else if( vg
.quality_profile
== k_quality_profile_low
){
186 shader_scene_water_fast_use();
188 vg_tex2d_bind( &tex_water_surf
, 1 );
189 shader_scene_water_fast_uTexDudv( 1 );
190 shader_scene_water_fast_uTime( world_global
.time
);
191 shader_scene_water_fast_uCamera( cam
->transform
[3] );
192 shader_scene_water_fast_uSurfaceY( world
->water
.height
);
193 world_link_lighting_ub( world
, _shader_scene_water_fast
.id
);
194 world_bind_position_texture( world
, _shader_scene_water_fast
.id
,
195 _uniform_scene_water_fast_g_world_depth
, 2 );
196 world_bind_light_array( world
, _shader_scene_water_fast
.id
,
197 _uniform_scene_water_fast_uLightsArray
, 4 );
200 m4x3_identity( full
);
201 shader_scene_water_fast_uMdl( full
);
202 shader_scene_water_fast_uPv( cam
->mtx
.pv
);
203 shader_scene_water_fast_uPvmPrev( cam
->mtx_prev
.pv
);
206 glBlendFunc(GL_SRC_ALPHA
,GL_ONE_MINUS_SRC_ALPHA
);
207 glBlendEquation(GL_FUNC_ADD
);
209 mesh_bind( &world
->mesh_no_collide
);
211 for( int i
=0; i
<world
->surface_count
; i
++ ){
212 struct world_surface
*mat
= &world
->surfaces
[i
];
214 if( mat
->info
.shader
== k_shader_water
){
215 shader_scene_water_fast_uShoreColour( mat
->info
.colour
);
216 shader_scene_water_fast_uOceanColour( mat
->info
.colour1
);
218 mdl_draw_submesh( &mat
->sm_no_collide
);