9 #include "shaders/water.h"
12 vg_tex2d tex_water_surf
= { .path
= "textures/water_surf.qoi" };
27 static void water_register(void)
29 shader_water_register();
32 static void water_init(void)
34 create_renderbuffer_std( &wrender
.fb
, &wrender
.rgb
, &wrender
.rb
);
37 static int ray_world( v3f pos
, v3f dir
, ray_hit
*hit
);
38 static void water_compute_depth( boxf bounds
)
46 vg_info( "Computing depth map\n" );
47 u8
*img
= malloc( kres
*kres
);
50 v3_sub( bounds
[1], bounds
[0], volume
);
51 box_copy( bounds
, wrender
.depthbounds
);
53 for( int y
=0; y
<kres
; y
++ )
55 for( int x
=0; x
<kres
; x
++ )
57 v3f pos
= { x
, 0, y
};
58 v3_divs( pos
, kres
, pos
);
59 v3_muladd( bounds
[0], pos
, volume
, pos
);
60 pos
[1] = wrender
.height
;
64 u8
*dst
= &img
[ y
*kres
+x
];
66 if( ray_world( pos
, (v3f
){0.0f
,-1.0f
,0.0f
}, &hit
))
68 float h
= wrender
.height
- hit
.pos
[1];
70 h
= vg_clampf( h
, 0.0f
, 1.0f
);
71 *dst
= (u8
)(h
*255.0f
);
78 if( wrender
.depth_computed
)
79 glDeleteTextures( 1, &wrender
.depthmap
);
81 glGenTextures( 1, &wrender
.depthmap
);
82 glBindTexture( GL_TEXTURE_2D
, wrender
.depthmap
);
83 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RED
, kres
, kres
, 0,
84 GL_RED
, GL_UNSIGNED_BYTE
, img
);
87 vg_tex2d_linear_mipmap();
90 wrender
.depth_computed
= 1;
92 vg_success( "Done.\n" );
95 static void water_set_surface( glmesh
*surf
, float height
)
98 wrender
.height
= height
;
101 static void water_fb_resize(void)
103 resize_renderbuffer_std( &wrender
.fb
, &wrender
.rgb
, &wrender
.rb
);
106 static void render_water_texture( m4x3f camera
)
108 /* Draw reflection buffa */
109 glBindFramebuffer( GL_FRAMEBUFFER
, wrender
.fb
);
110 glClearColor( 0.11f
, 0.35f
, 0.37f
, 1.0f
);
111 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
113 m4x3f new_cam
, inverse
;
114 v3_copy( camera
[3], new_cam
[3] );
115 new_cam
[3][1] -= 2.0f
* (camera
[3][1] - wrender
.height
);
118 m3x3_identity( flip
);
120 m3x3_mul( flip
, camera
, new_cam
);
124 m3x3_mulv( new_cam
, (v3f
){0.0f
,0.0f
,-1.0f
}, p0
);
125 v3_add( new_cam
[3], p0
, p0
);
126 vg_line( new_cam
[3], p0
, 0xffffffff );
129 vg_line_pt3( new_cam
[3], 0.3f
, 0xff00ffff );
131 m4x3_invert_affine( new_cam
, inverse
);
132 m4x3_expand( inverse
, view
);
134 v4f clippa
= { 0.0f
, 1.0f
, 0.0f
, wrender
.height
-0.1f
};
135 m4x3_mulp( inverse
, clippa
, clippa
);
139 m4x4_projection( projection
,
141 (float)vg_window_x
/ (float)vg_window_y
,
143 plane_clip_projection( projection
, clippa
);
144 m4x4_mul( projection
, view
, projection
);
146 glCullFace( GL_FRONT
);
147 render_world( projection
, new_cam
);
148 glCullFace( GL_BACK
);
151 static void render_water_surface( m4x4f pv
)
156 glActiveTexture( GL_TEXTURE0
);
157 glBindTexture( GL_TEXTURE_2D
, wrender
.rgb
);
158 shader_water_uTexMain( 0 );
160 vg_tex2d_bind( &tex_water_surf
, 1 );
161 shader_water_uTexDudv( 1 );
162 shader_water_uInvRes( (v2f
){
163 1.0f
/ (float)vg_window_x
,
164 1.0f
/ (float)vg_window_y
});
166 glActiveTexture( GL_TEXTURE2
);
167 glBindTexture( GL_TEXTURE_2D
, wrender
.depthmap
);
168 shader_water_uTexDepth( 2 );
169 shader_water_uDepthBounds( (v4f
){
170 wrender
.depthbounds
[0][0],
171 wrender
.depthbounds
[0][2],
172 1.0f
/ (wrender
.depthbounds
[1][0]-wrender
.depthbounds
[0][0]),
173 1.0f
/ (wrender
.depthbounds
[1][2]-wrender
.depthbounds
[0][2])} );
175 shader_water_uTime( vg_time
);
176 shader_water_uPv( pv
);
179 m4x3_identity( full
);
180 full
[3][1] = wrender
.height
;
182 shader_water_uMdl( full
);
185 glBlendFunc(GL_SRC_ALPHA
,GL_ONE_MINUS_SRC_ALPHA
);
186 glBlendEquation(GL_FUNC_ADD
);
188 mesh_bind( &wrender
.mdl
);
189 mesh_draw( &wrender
.mdl
);