4 static void water_register(void);
5 static void water_init(void);
6 static void water_fb_resize(void);
7 static void water_compute_depth( boxf bounds
);
8 static void water_set_surface( glmesh
*surf
, float height
);
9 static float water_height(void);
16 #include "shaders/water.h"
19 vg_tex2d tex_water_surf
= { .path
= "textures/water_surf.qoi" };
23 struct framebuffer fbreflect
, fbdepth
;
35 .fbreflect
= { .format
= GL_RGB
, .div
= 2 },
36 .fbdepth
= { .format
= GL_RGBA
, .div
= 2 }
39 static float water_height(void)
41 return wrender
.height
;
44 static void water_register(void)
46 shader_water_register();
49 static void water_init(void)
51 /* TODO: probably dont do this every time */
54 fb_init( &wrender
.fbreflect
);
55 fb_init( &wrender
.fbdepth
);
58 static void water_fb_resize(void)
60 if( !wrender
.enabled
)
63 fb_resize( &wrender
.fbreflect
);
64 fb_resize( &wrender
.fbdepth
);
67 static void water_compute_depth( boxf bounds
)
69 if( !wrender
.enabled
)
78 vg_info( "Computing depth map\n" );
79 u8
*img
= malloc( kres
*kres
);
82 v3_sub( bounds
[1], bounds
[0], volume
);
83 box_copy( bounds
, wrender
.depthbounds
);
85 for( int y
=0; y
<kres
; y
++ )
87 for( int x
=0; x
<kres
; x
++ )
89 v3f pos
= { x
, 0, y
};
90 v3_divs( pos
, kres
, pos
);
91 v3_muladd( bounds
[0], pos
, volume
, pos
);
92 pos
[1] = wrender
.height
;
96 u8
*dst
= &img
[ y
*kres
+x
];
98 if( ray_world( pos
, (v3f
){0.0f
,-1.0f
,0.0f
}, &hit
))
100 float h
= wrender
.height
- hit
.pos
[1];
102 h
= vg_clampf( h
, 0.0f
, 1.0f
);
103 *dst
= (u8
)(h
*255.0f
);
110 if( wrender
.depth_computed
)
111 glDeleteTextures( 1, &wrender
.depthmap
);
113 glGenTextures( 1, &wrender
.depthmap
);
114 glBindTexture( GL_TEXTURE_2D
, wrender
.depthmap
);
115 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RED
, kres
, kres
, 0,
116 GL_RED
, GL_UNSIGNED_BYTE
, img
);
119 vg_tex2d_linear_mipmap();
122 wrender
.depth_computed
= 1;
124 vg_success( "Done.\n" );
127 static void water_set_surface( glmesh
*surf
, float height
)
130 wrender
.height
= height
;
133 static void render_water_texture( m4x3f camera
)
135 if( !wrender
.enabled
)
138 /* Draw reflection buffa */
139 fb_use( &wrender
.fbreflect
);
140 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
142 m4x3f new_cam
, inverse
;
143 v3_copy( camera
[3], new_cam
[3] );
144 new_cam
[3][1] -= 2.0f
* (camera
[3][1] - wrender
.height
);
147 m3x3_identity( flip
);
149 m3x3_mul( flip
, camera
, new_cam
);
153 m3x3_mulv( new_cam
, (v3f
){0.0f
,0.0f
,-1.0f
}, p0
);
154 v3_add( new_cam
[3], p0
, p0
);
155 vg_line( new_cam
[3], p0
, 0xffffffff );
158 vg_line_pt3( new_cam
[3], 0.3f
, 0xff00ffff );
160 m4x3_invert_affine( new_cam
, inverse
);
161 m4x3_expand( inverse
, view
);
163 v4f clippa
= { 0.0f
, 1.0f
, 0.0f
, wrender
.height
-0.1f
};
164 m4x3_mulp( inverse
, clippa
, clippa
);
168 m4x4_projection( projection
,
170 (float)vg_window_x
/ (float)vg_window_y
,
172 plane_clip_projection( projection
, clippa
);
173 m4x4_mul( projection
, view
, projection
);
175 glCullFace( GL_FRONT
);
176 render_world( projection
, new_cam
);
177 glCullFace( GL_BACK
);
180 /* Draw beneath texture */
181 fb_use( &wrender
.fbdepth
);
182 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
184 m4x3_invert_affine( camera
, inverse
);
185 m4x3_expand( inverse
, view
);
187 v4f clippb
= { 0.0f
, -1.0f
, 0.0f
, -(wrender
.height
) };
188 m4x3_mulp( inverse
, clippb
, clippb
);
191 float depth_loss
= camera
[3][1]-wrender
.height
;
193 m4x4_projection( projection
,
195 (float)vg_window_x
/ (float)vg_window_y
,
198 plane_clip_projection( projection
, clippb
);
199 m4x4_mul( projection
, view
, projection
);
200 render_world( projection
, camera
);
202 glViewport( 0, 0, vg_window_x
, vg_window_y
);
205 static void render_water_surface( m4x4f pv
, m4x3f camera
)
207 if( !wrender
.enabled
)
213 fb_bindtex( &wrender
.fbreflect
, 0 );
214 shader_water_uTexMain( 0 );
216 vg_tex2d_bind( &tex_water_surf
, 1 );
217 shader_water_uTexDudv( 1 );
218 shader_water_uInvRes( (v2f
){
219 1.0f
/ (float)vg_window_x
,
220 1.0f
/ (float)vg_window_y
});
222 glActiveTexture( GL_TEXTURE2
);
223 glBindTexture( GL_TEXTURE_2D
, wrender
.depthmap
);
224 shader_water_uTexDepth( 2 );
225 shader_water_uDepthBounds( (v4f
){
226 wrender
.depthbounds
[0][0],
227 wrender
.depthbounds
[0][2],
228 1.0f
/ (wrender
.depthbounds
[1][0]-wrender
.depthbounds
[0][0]),
229 1.0f
/ (wrender
.depthbounds
[1][2]-wrender
.depthbounds
[0][2])} );
231 fb_bindtex( &wrender
.fbdepth
, 3 );
232 shader_water_uTexBack( 3 );
234 shader_water_uTime( vg_time
);
235 shader_water_uCamera( camera
[3] );
236 shader_water_uSurfaceY( wrender
.height
);
238 shader_water_uPv( pv
);
241 m4x3_identity( full
);
242 full
[3][1] = wrender
.height
;
244 shader_water_uMdl( full
);
247 glBlendFunc(GL_SRC_ALPHA
,GL_ONE_MINUS_SRC_ALPHA
);
248 glBlendEquation(GL_FUNC_ADD
);
250 mesh_bind( &wrender
.mdl
);
251 mesh_draw( &wrender
.mdl
);