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_STATIC GLuint tex_water_surf
;
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_tex2d_load_qoi_async_file( "textures/water_surf.qoi",
23 VG_TEX2D_LINEAR
|VG_TEX2D_REPEAT
,
26 vg_success( "done\n" );
29 VG_STATIC
void water_set_surface( world_instance
*world
, float height
)
31 world
->water
.height
= height
;
32 v4_copy( (v4f
){ 0.0f
, 1.0f
, 0.0f
, height
}, world
->water
.plane
);
35 VG_STATIC
void world_link_lighting_ub( world_instance
*world
, GLuint shader
);
36 VG_STATIC
void world_bind_position_texture( world_instance
*world
,
37 GLuint shader
, GLuint location
,
39 VG_STATIC
void world_bind_light_array( world_instance
*world
,
40 GLuint shader
, GLuint location
,
42 VG_STATIC
void world_bind_light_index( world_instance
*world
,
43 GLuint shader
, GLuint location
,
47 * Does not write motion vectors
49 VG_STATIC
void render_water_texture( world_instance
*world
, camera
*cam
,
52 if( !world
->water
.enabled
|| (vg
.quality_profile
== k_quality_profile_low
) )
55 /* Draw reflection buffa */
56 render_fb_bind( gpipeline
.fb_water_reflection
, 1 );
57 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
60 * Create flipped view matrix. Don't care about motion vectors
62 float cam_height
= cam
->transform
[3][1] - world
->water
.height
;
65 water_cam
.farz
= cam
->farz
;
66 water_cam
.nearz
= cam
->nearz
;
67 v3_copy( cam
->transform
[3], water_cam
.transform
[3] );
68 water_cam
.transform
[3][1] -= 2.0f
* cam_height
;
71 m3x3_identity( flip
);
73 m3x3_mul( flip
, cam
->transform
, water_cam
.transform
);
75 camera_update_view( &water_cam
);
78 * Create clipped projection
80 v4f clippa
= { 0.0f
, 1.0f
, 0.0f
, world
->water
.height
-0.1f
};
81 m4x3_mulp( water_cam
.transform_inverse
, clippa
, clippa
);
84 m4x4_copy( cam
->mtx
.p
, water_cam
.mtx
.p
);
85 m4x4_clip_projection( water_cam
.mtx
.p
, clippa
);
87 camera_finalize( &water_cam
);
92 glEnable( GL_DEPTH_TEST
);
93 glDisable( GL_BLEND
);
94 glCullFace( GL_FRONT
);
95 render_world( world
, &water_cam
, layer_depth
);
96 glCullFace( GL_BACK
);
99 * Create beneath view matrix
102 render_fb_bind( gpipeline
.fb_water_beneath
, 1 );
103 glClearColor( 1.0f
, 0.0f
, 0.0f
, 0.0f
);
104 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
106 m4x3_copy( cam
->transform
, beneath_cam
.transform
);
107 camera_update_view( &beneath_cam
);
109 float bias
= -(cam
->transform
[3][1]-world
->water
.height
)*0.1f
;
111 v4f clippb
= { 0.0f
, -1.0f
, 0.0f
, -(world
->water
.height
) + bias
};
112 m4x3_mulp( beneath_cam
.transform_inverse
, clippb
, clippb
);
115 m4x4_copy( cam
->mtx
.p
, beneath_cam
.mtx
.p
);
116 m4x4_clip_projection( beneath_cam
.mtx
.p
, clippb
);
117 camera_finalize( &beneath_cam
);
119 glEnable( GL_DEPTH_TEST
);
120 glDisable( GL_BLEND
);
121 render_world_depth( world
, &beneath_cam
);
122 //glViewport( 0,0, g_render_x, g_render_y );
125 VG_STATIC
void render_water_surface( world_instance
*world
, camera
*cam
)
127 if( !world
->water
.enabled
)
130 if( vg
.quality_profile
== k_quality_profile_high
){
132 shader_scene_water_use();
134 render_fb_bind_texture( gpipeline
.fb_water_reflection
, 0, 0 );
135 shader_scene_water_uTexMain( 0 );
137 glActiveTexture( GL_TEXTURE1
);
138 glBindTexture( GL_TEXTURE_2D
, tex_water_surf
);
139 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 glActiveTexture( GL_TEXTURE1
);
189 glBindTexture( GL_TEXTURE_2D
, tex_water_surf
);
190 shader_scene_water_fast_uTexDudv( 1 );
192 shader_scene_water_fast_uTime( world_global
.time
);
193 shader_scene_water_fast_uCamera( cam
->transform
[3] );
194 shader_scene_water_fast_uSurfaceY( world
->water
.height
);
195 world_link_lighting_ub( world
, _shader_scene_water_fast
.id
);
196 world_bind_position_texture( world
, _shader_scene_water_fast
.id
,
197 _uniform_scene_water_fast_g_world_depth
, 2 );
198 world_bind_light_array( world
, _shader_scene_water_fast
.id
,
199 _uniform_scene_water_fast_uLightsArray
, 4 );
202 m4x3_identity( full
);
203 shader_scene_water_fast_uMdl( full
);
204 shader_scene_water_fast_uPv( cam
->mtx
.pv
);
205 shader_scene_water_fast_uPvmPrev( cam
->mtx_prev
.pv
);
208 glBlendFunc(GL_SRC_ALPHA
,GL_ONE_MINUS_SRC_ALPHA
);
209 glBlendEquation(GL_FUNC_ADD
);
211 mesh_bind( &world
->mesh_no_collide
);
213 for( int i
=0; i
<world
->surface_count
; i
++ ){
214 struct world_surface
*mat
= &world
->surfaces
[i
];
216 if( mat
->info
.shader
== k_shader_water
){
217 shader_scene_water_fast_uShoreColour( mat
->info
.colour
);
218 shader_scene_water_fast_uOceanColour( mat
->info
.colour1
);
220 mdl_draw_submesh( &mat
->sm_no_collide
);