2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
10 #include "shaders/water.h"
11 #include "shaders/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_water_register();
20 shader_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( float height
)
33 world
.water
.height
= height
;
34 v4_copy( (v4f
){ 0.0f
, 1.0f
, 0.0f
, height
}, world
.water
.plane
);
38 * Does not write motion vectors
40 VG_STATIC
void render_water_texture( camera
*cam
)
42 if( !world
.water
.enabled
|| (vg
.quality_profile
== k_quality_profile_low
) )
45 /* Draw reflection buffa */
46 render_fb_bind( gpipeline
.fb_water_reflection
);
47 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
50 * Create flipped view matrix. Don't care about motion vectors
52 float cam_height
= cam
->transform
[3][1] - world
.water
.height
;
55 v3_copy( cam
->transform
[3], water_cam
.transform
[3] );
56 water_cam
.transform
[3][1] -= 2.0f
* cam_height
;
59 m3x3_identity( flip
);
61 m3x3_mul( flip
, cam
->transform
, water_cam
.transform
);
63 camera_update_view( &water_cam
);
66 * Create clipped projection
68 v4f clippa
= { 0.0f
, 1.0f
, 0.0f
, world
.water
.height
-0.1f
};
69 m4x3_mulp( water_cam
.transform_inverse
, clippa
, clippa
);
72 m4x4_copy( cam
->mtx
.p
, water_cam
.mtx
.p
);
73 m4x4_clip_projection( water_cam
.mtx
.p
, clippa
);
75 camera_finalize( &water_cam
);
80 glCullFace( GL_FRONT
);
81 render_world( &water_cam
);
82 glCullFace( GL_BACK
);
85 * Create beneath view matrix
88 render_fb_bind( gpipeline
.fb_water_beneath
);
89 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
91 m4x3_copy( cam
->transform
, beneath_cam
.transform
);
92 camera_update_view( &beneath_cam
);
94 float bias
= -(cam
->transform
[3][1]-world
.water
.height
)*0.1f
;
96 v4f clippb
= { 0.0f
, -1.0f
, 0.0f
, -(world
.water
.height
) + bias
};
97 m4x3_mulp( beneath_cam
.transform_inverse
, clippb
, clippb
);
100 m4x4_copy( cam
->mtx
.p
, beneath_cam
.mtx
.p
);
101 m4x4_clip_projection( beneath_cam
.mtx
.p
, clippb
);
102 camera_finalize( &beneath_cam
);
104 render_world_depth( &beneath_cam
);
105 glViewport( 0, 0, vg
.window_x
, vg
.window_y
);
108 VG_STATIC
void render_water_surface( camera
*cam
)
110 if( !world
.water
.enabled
)
113 if( vg
.quality_profile
== k_quality_profile_high
)
118 render_fb_bind_texture( gpipeline
.fb_water_reflection
, 0, 0 );
119 shader_water_uTexMain( 0 );
121 vg_tex2d_bind( &tex_water_surf
, 1 );
122 shader_water_uTexDudv( 1 );
123 shader_water_uInvRes( (v2f
){
124 1.0f
/ (float)vg
.window_x
,
125 1.0f
/ (float)vg
.window_y
});
127 shader_link_standard_ub( _shader_water
.id
, 2 );
129 render_fb_bind_texture( gpipeline
.fb_water_beneath
, 0, 3 );
130 shader_water_uTexBack( 3 );
131 shader_water_uTime( world
.time
);
132 shader_water_uCamera( cam
->transform
[3] );
133 shader_water_uSurfaceY( world
.water
.height
);
135 shader_water_uPv( cam
->mtx
.pv
);
136 shader_water_uPvmPrev( cam
->mtx_prev
.pv
);
139 m4x3_identity( full
);
140 shader_water_uMdl( full
);
143 glBlendFunc(GL_SRC_ALPHA
,GL_ONE_MINUS_SRC_ALPHA
);
144 glBlendEquation(GL_FUNC_ADD
);
146 mesh_bind( &world
.mesh_no_collide
);
148 for( int i
=0; i
<world
.material_count
; i
++ )
150 struct world_material
*mat
= &world
.materials
[i
];
152 if( mat
->info
.shader
== k_shader_water
)
154 shader_water_uShoreColour( mat
->info
.colour
);
155 shader_water_uOceanColour( mat
->info
.colour1
);
157 mdl_draw_submesh( &mat
->sm_no_collide
);
163 else if( vg
.quality_profile
== k_quality_profile_low
)
165 shader_water_fast_use();
167 vg_tex2d_bind( &tex_water_surf
, 1 );
168 shader_water_fast_uTexDudv( 1 );
169 shader_water_fast_uTime( world
.time
);
170 shader_water_fast_uCamera( cam
->transform
[3] );
171 shader_water_fast_uSurfaceY( world
.water
.height
);
172 shader_link_standard_ub( _shader_water_fast
.id
, 2 );
175 m4x3_identity( full
);
176 shader_water_fast_uMdl( full
);
177 shader_water_fast_uPv( cam
->mtx
.pv
);
178 shader_water_fast_uPvmPrev( cam
->mtx_prev
.pv
);
181 glBlendFunc(GL_SRC_ALPHA
,GL_ONE_MINUS_SRC_ALPHA
);
182 glBlendEquation(GL_FUNC_ADD
);
184 mesh_bind( &world
.mesh_no_collide
);
186 for( int i
=0; i
<world
.material_count
; i
++ )
188 struct world_material
*mat
= &world
.materials
[i
];
190 if( mat
->info
.shader
== k_shader_water
)
192 shader_water_fast_uShoreColour( mat
->info
.colour
);
193 shader_water_fast_uOceanColour( mat
->info
.colour1
);
195 mdl_draw_submesh( &mat
->sm_no_collide
);