2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
10 #include "shaders/water.h"
13 vg_tex2d tex_water_surf
= { .path
= "textures/water_surf.qoi" };
17 struct framebuffer fbreflect
, fbdepth
;
29 .fbreflect
= { .format
= GL_RGB
, .div
= 3 },
30 .fbdepth
= { .format
= GL_RGBA
, .div
= 4 }
33 static void world_water_init(void)
35 vg_info( "world_water_init\n" );
36 shader_water_register();
38 vg_acquire_thread_sync();
40 fb_init( &wrender
.fbreflect
);
41 fb_init( &wrender
.fbdepth
);
43 vg_tex2d_init( (vg_tex2d
*[]){&tex_water_surf
}, 1 );
44 vg_success( "done\n" );
46 vg_release_thread_sync();
49 static void world_water_free(void *_
)
51 vg_tex2d_free( (vg_tex2d
*[]){&tex_water_surf
}, 1 );
52 fb_free( &wrender
.fbreflect
);
53 fb_free( &wrender
.fbdepth
);
56 static void water_fb_resize(void)
58 if( !wrender
.enabled
)
61 fb_resize( &wrender
.fbreflect
);
62 fb_resize( &wrender
.fbdepth
);
65 static void water_set_surface( glmesh
*surf
, float height
)
68 wrender
.height
= height
;
71 v4_copy( (v4f
){ 0.0f
, 1.0f
, 0.0f
, height
}, wrender
.plane
);
74 static void render_water_texture( m4x3f camera
)
76 if( !wrender
.enabled
)
79 /* Draw reflection buffa */
80 fb_use( &wrender
.fbreflect
);
81 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
83 m4x3f new_cam
, inverse
;
84 v3_copy( camera
[3], new_cam
[3] );
85 new_cam
[3][1] -= 2.0f
* (camera
[3][1] - wrender
.height
);
88 m3x3_identity( flip
);
90 m3x3_mul( flip
, camera
, new_cam
);
94 m3x3_mulv( new_cam
, (v3f
){0.0f
,0.0f
,-1.0f
}, p0
);
95 v3_add( new_cam
[3], p0
, p0
);
96 vg_line( new_cam
[3], p0
, 0xffffffff );
99 vg_line_pt3( new_cam
[3], 0.3f
, 0xff00ffff );
101 m4x3_invert_affine( new_cam
, inverse
);
102 m4x3_expand( inverse
, view
);
104 v4f clippa
= { 0.0f
, 1.0f
, 0.0f
, wrender
.height
-0.1f
};
105 m4x3_mulp( inverse
, clippa
, clippa
);
109 m4x4_projection( projection
,
111 (float)vg
.window_x
/ (float)vg
.window_y
,
113 plane_clip_projection( projection
, clippa
);
114 m4x4_mul( projection
, view
, projection
);
116 glCullFace( GL_FRONT
);
117 render_world( projection
, new_cam
);
118 glCullFace( GL_BACK
);
121 /* Draw beneath texture */
122 fb_use( &wrender
.fbdepth
);
123 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
125 m4x3_invert_affine( camera
, inverse
);
126 m4x3_expand( inverse
, view
);
128 float bias
= -(camera
[3][1]-wrender
.height
)*0.1f
;
129 v4f clippb
= { 0.0f
, -1.0f
, 0.0f
, -(wrender
.height
) + bias
};
130 m4x3_mulp( inverse
, clippb
, clippb
);
133 m4x4_projection( projection
,
135 (float)vg
.window_x
/ (float)vg
.window_y
,
138 plane_clip_projection( projection
, clippb
);
139 m4x4_mul( projection
, view
, projection
);
140 render_world_depth( projection
, camera
);
142 glViewport( 0, 0, vg
.window_x
, vg
.window_y
);
145 static void render_water_surface( m4x4f pv
, m4x3f camera
)
147 if( !wrender
.enabled
)
153 fb_bindtex( &wrender
.fbreflect
, 0 );
154 shader_water_uTexMain( 0 );
156 vg_tex2d_bind( &tex_water_surf
, 1 );
157 shader_water_uTexDudv( 1 );
158 shader_water_uInvRes( (v2f
){
159 1.0f
/ (float)vg
.window_x
,
160 1.0f
/ (float)vg
.window_y
});
162 shader_link_standard_ub( _shader_water
.id
, 2 );
164 fb_bindtex( &wrender
.fbdepth
, 3 );
165 shader_water_uTexBack( 3 );
166 shader_water_uTime( world_routes
.time
);
167 shader_water_uCamera( camera
[3] );
168 shader_water_uSurfaceY( wrender
.height
);
170 shader_water_uPv( pv
);
173 m4x3_identity( full
);
174 full
[3][1] = wrender
.height
;
176 shader_water_uMdl( full
);
179 glBlendFunc(GL_SRC_ALPHA
,GL_ONE_MINUS_SRC_ALPHA
);
180 glBlendEquation(GL_FUNC_ADD
);
182 mesh_bind( &wrender
.mdl
);
183 mesh_draw( &wrender
.mdl
);