cleanup+walgrid init
[carveJwlIkooP6JGAAIwe30JlM.git] / water.h
1 #ifndef WATER_H
2 #define WATER_H
3
4 #define VG_3D
5 #include "vg/vg.h"
6
7 #include "model.h"
8 #include "render.h"
9 #include "shaders/water.h"
10 #include "scene.h"
11
12 vg_tex2d tex_water_surf = { .path = "textures/water_surf.qoi" };
13
14 static struct
15 {
16 GLuint fb, rgb, rb;
17 glmesh mdl;
18
19 GLuint depthmap;
20 boxf depthbounds;
21 int depth_computed;
22
23 float height;
24 }
25 wrender;
26
27 static void water_register(void)
28 {
29 shader_water_register();
30 }
31
32 static void water_init(void)
33 {
34 create_renderbuffer_std( &wrender.fb, &wrender.rgb, &wrender.rb );
35 }
36
37 static int ray_world( v3f pos, v3f dir, ray_hit *hit );
38
39 #ifndef VG_RELEASE
40 __attribute__((minsize))
41 #endif
42 static void water_compute_depth( boxf bounds )
43 {
44 #ifdef VG_RELEASE
45 int const kres = 512;
46 #else
47 int const kres = 64;
48 #endif
49
50 vg_info( "Computing depth map\n" );
51 u8 *img = malloc( kres*kres );
52
53 v3f volume;
54 v3_sub( bounds[1], bounds[0], volume );
55 box_copy( bounds, wrender.depthbounds );
56
57 for( int y=0; y<kres; y++ )
58 {
59 for( int x=0; x<kres; x++ )
60 {
61 v3f pos = { x, 0, y };
62 v3_divs( pos, kres, pos );
63 v3_muladd( bounds[0], pos, volume, pos );
64 pos[1] = wrender.height;
65
66 ray_hit hit;
67 hit.dist = INFINITY;
68 u8 *dst = &img[ y*kres+x ];
69
70 if( ray_world( pos, (v3f){0.0f,-1.0f,0.0f}, &hit ))
71 {
72 float h = wrender.height - hit.pos[1];
73 h *= 1.0f/15.0f;
74 h = vg_clampf( h, 0.0f, 1.0f );
75 *dst = (u8)(h*255.0f);
76 }
77 else
78 *dst = 0;
79 }
80 }
81
82 if( wrender.depth_computed )
83 glDeleteTextures( 1, &wrender.depthmap );
84
85 glGenTextures( 1, &wrender.depthmap );
86 glBindTexture( GL_TEXTURE_2D, wrender.depthmap );
87 glTexImage2D( GL_TEXTURE_2D, 0, GL_RED, kres, kres, 0,
88 GL_RED, GL_UNSIGNED_BYTE, img );
89
90 vg_tex2d_mipmap();
91 vg_tex2d_linear_mipmap();
92 vg_tex2d_clamp();
93
94 wrender.depth_computed = 1;
95 free( img );
96 vg_success( "Done.\n" );
97 }
98
99 static void water_set_surface( glmesh *surf, float height )
100 {
101 wrender.mdl = *surf;
102 wrender.height = height;
103 }
104
105 static void water_fb_resize(void)
106 {
107 resize_renderbuffer_std( &wrender.fb, &wrender.rgb, &wrender.rb );
108 }
109
110 static void render_water_texture( m4x3f camera )
111 {
112 /* Draw reflection buffa */
113 glBindFramebuffer( GL_FRAMEBUFFER, wrender.fb );
114 glClearColor( 0.11f, 0.35f, 0.37f, 1.0f );
115 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
116
117 m4x3f new_cam, inverse;
118 v3_copy( camera[3], new_cam[3] );
119 new_cam[3][1] -= 2.0f * (camera[3][1] - wrender.height);
120
121 m3x3f flip;
122 m3x3_identity( flip );
123 flip[1][1] = -1.0f;
124 m3x3_mul( flip, camera, new_cam );
125
126
127 v3f p0;
128 m3x3_mulv( new_cam, (v3f){0.0f,0.0f,-1.0f}, p0 );
129 v3_add( new_cam[3], p0, p0 );
130 vg_line( new_cam[3], p0, 0xffffffff );
131
132 m4x4f view;
133 vg_line_pt3( new_cam[3], 0.3f, 0xff00ffff );
134
135 m4x3_invert_affine( new_cam, inverse );
136 m4x3_expand( inverse, view );
137
138 v4f clippa = { 0.0f, 1.0f, 0.0f, wrender.height-0.1f };
139 m4x3_mulp( inverse, clippa, clippa );
140 clippa[3] *= -1.0f;
141
142 m4x4f projection;
143 m4x4_projection( projection,
144 gpipeline.fov,
145 (float)vg_window_x / (float)vg_window_y,
146 0.1f, 900.0f );
147 plane_clip_projection( projection, clippa );
148 m4x4_mul( projection, view, projection );
149
150 glCullFace( GL_FRONT );
151 render_world( projection, new_cam );
152 glCullFace( GL_BACK );
153 }
154
155 static void render_water_surface( m4x4f pv )
156 {
157 /* Draw surface */
158 shader_water_use();
159
160 glActiveTexture( GL_TEXTURE0 );
161 glBindTexture( GL_TEXTURE_2D, wrender.rgb );
162 shader_water_uTexMain( 0 );
163
164 vg_tex2d_bind( &tex_water_surf, 1 );
165 shader_water_uTexDudv( 1 );
166 shader_water_uInvRes( (v2f){
167 1.0f / (float)vg_window_x,
168 1.0f / (float)vg_window_y });
169
170 glActiveTexture( GL_TEXTURE2 );
171 glBindTexture( GL_TEXTURE_2D, wrender.depthmap );
172 shader_water_uTexDepth( 2 );
173 shader_water_uDepthBounds( (v4f){
174 wrender.depthbounds[0][0],
175 wrender.depthbounds[0][2],
176 1.0f/ (wrender.depthbounds[1][0]-wrender.depthbounds[0][0]),
177 1.0f/ (wrender.depthbounds[1][2]-wrender.depthbounds[0][2])} );
178
179 shader_water_uTime( vg_time );
180 shader_water_uPv( pv );
181
182 m4x3f full;
183 m4x3_identity( full );
184 full[3][1] = wrender.height;
185
186 shader_water_uMdl( full );
187
188 glEnable(GL_BLEND);
189 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
190 glBlendEquation(GL_FUNC_ADD);
191
192 mesh_bind( &wrender.mdl );
193 mesh_draw( &wrender.mdl );
194
195 glDisable(GL_BLEND);
196 }
197
198 #endif /* WATER_H */