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 world
.water
.fbreflect
.format
= GL_RGB
;
25 world
.water
.fbreflect
.div
= 3;
26 world
.water
.fbdepth
.format
= GL_RGBA
;
27 world
.water
.fbdepth
.div
= 4;
29 fb_init( &world
.water
.fbreflect
);
30 fb_init( &world
.water
.fbdepth
);
32 vg_tex2d_init( (vg_tex2d
*[]){&tex_water_surf
}, 1 );
34 vg_release_thread_sync();
36 vg_success( "done\n" );
39 VG_STATIC
void water_fb_resize(void)
41 if( !world
.water
.enabled
)
44 fb_resize( &world
.water
.fbreflect
);
45 fb_resize( &world
.water
.fbdepth
);
48 VG_STATIC
void water_set_surface( float height
)
50 world
.water
.height
= height
;
51 v4_copy( (v4f
){ 0.0f
, 1.0f
, 0.0f
, height
}, world
.water
.plane
);
54 VG_STATIC
void render_water_texture( m4x3f camera
)
56 if( !world
.water
.enabled
|| (vg
.quality_profile
== k_quality_profile_low
) )
59 /* Draw reflection buffa */
60 fb_use( &world
.water
.fbreflect
);
61 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
63 m4x3f new_cam
, inverse
;
64 v3_copy( camera
[3], new_cam
[3] );
65 new_cam
[3][1] -= 2.0f
* (camera
[3][1] - world
.water
.height
);
68 m3x3_identity( flip
);
70 m3x3_mul( flip
, camera
, new_cam
);
74 m3x3_mulv( new_cam
, (v3f
){0.0f
,0.0f
,-1.0f
}, p0
);
75 v3_add( new_cam
[3], p0
, p0
);
76 vg_line( new_cam
[3], p0
, 0xffffffff );
79 vg_line_pt3( new_cam
[3], 0.3f
, 0xff00ffff );
81 m4x3_invert_affine( new_cam
, inverse
);
82 m4x3_expand( inverse
, view
);
84 v4f clippa
= { 0.0f
, 1.0f
, 0.0f
, world
.water
.height
-0.1f
};
85 m4x3_mulp( inverse
, clippa
, clippa
);
89 m4x4_projection( projection
,
91 (float)vg
.window_x
/ (float)vg
.window_y
,
93 plane_clip_projection( projection
, clippa
);
94 m4x4_mul( projection
, view
, projection
);
96 glCullFace( GL_FRONT
);
97 render_world( projection
, new_cam
);
98 glCullFace( GL_BACK
);
101 /* Draw beneath texture */
102 fb_use( &world
.water
.fbdepth
);
103 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
105 m4x3_invert_affine( camera
, inverse
);
106 m4x3_expand( inverse
, view
);
108 float bias
= -(camera
[3][1]-world
.water
.height
)*0.1f
;
109 v4f clippb
= { 0.0f
, -1.0f
, 0.0f
, -(world
.water
.height
) + bias
};
110 m4x3_mulp( inverse
, clippb
, clippb
);
113 m4x4_projection( projection
,
115 (float)vg
.window_x
/ (float)vg
.window_y
,
118 plane_clip_projection( projection
, clippb
);
119 m4x4_mul( projection
, view
, projection
);
120 render_world_depth( projection
, camera
);
122 glViewport( 0, 0, vg
.window_x
, vg
.window_y
);
125 VG_STATIC
void render_water_surface( m4x4f pv
, m4x3f camera
)
127 if( !world
.water
.enabled
)
130 if( vg
.quality_profile
== k_quality_profile_high
)
135 fb_bindtex( &world
.water
.fbreflect
, 0 );
136 shader_water_uTexMain( 0 );
138 vg_tex2d_bind( &tex_water_surf
, 1 );
139 shader_water_uTexDudv( 1 );
140 shader_water_uInvRes( (v2f
){
141 1.0f
/ (float)vg
.window_x
,
142 1.0f
/ (float)vg
.window_y
});
144 shader_link_standard_ub( _shader_water
.id
, 2 );
146 fb_bindtex( &world
.water
.fbdepth
, 3 );
147 shader_water_uTexBack( 3 );
148 shader_water_uTime( world
.time
);
149 shader_water_uCamera( camera
[3] );
150 shader_water_uSurfaceY( world
.water
.height
);
152 shader_water_uPv( pv
);
155 m4x3_identity( full
);
156 shader_water_uMdl( full
);
159 glBlendFunc(GL_SRC_ALPHA
,GL_ONE_MINUS_SRC_ALPHA
);
160 glBlendEquation(GL_FUNC_ADD
);
162 mesh_bind( &world
.mesh_no_collide
);
164 for( int i
=0; i
<world
.material_count
; i
++ )
166 struct world_material
*mat
= &world
.materials
[i
];
168 if( mat
->info
.shader
== k_shader_water
)
170 shader_water_uShoreColour( mat
->info
.colour
);
171 shader_water_uOceanColour( mat
->info
.colour1
);
173 mdl_draw_submesh( &mat
->sm_no_collide
);
179 else if( vg
.quality_profile
== k_quality_profile_low
)
181 shader_water_fast_use();
183 vg_tex2d_bind( &tex_water_surf
, 1 );
184 shader_water_fast_uTexDudv( 1 );
185 shader_water_fast_uTime( world
.time
);
186 shader_water_fast_uCamera( camera
[3] );
187 shader_water_fast_uSurfaceY( world
.water
.height
);
188 shader_link_standard_ub( _shader_water_fast
.id
, 2 );
191 m4x3_identity( full
);
192 shader_water_fast_uMdl( full
);
193 shader_water_fast_uPv( pv
);
196 glBlendFunc(GL_SRC_ALPHA
,GL_ONE_MINUS_SRC_ALPHA
);
197 glBlendEquation(GL_FUNC_ADD
);
199 mesh_bind( &world
.mesh_no_collide
);
201 for( int i
=0; i
<world
.material_count
; i
++ )
203 struct world_material
*mat
= &world
.materials
[i
];
205 if( mat
->info
.shader
== k_shader_water
)
207 shader_water_fast_uShoreColour( mat
->info
.colour
);
208 shader_water_fast_uOceanColour( mat
->info
.colour1
);
210 mdl_draw_submesh( &mat
->sm_no_collide
);