frame rate independence
[carveJwlIkooP6JGAAIwe30JlM.git] / world_water.h
1 /*
2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #ifndef WATER_H
6 #define WATER_H
7
8 #include "world.h"
9 #include "render.h"
10 #include "shaders/water.h"
11 #include "scene.h"
12
13 vg_tex2d tex_water_surf = { .path = "textures/water_surf.qoi" };
14
15 static struct
16 {
17 struct framebuffer fbreflect, fbdepth;
18 glmesh mdl;
19
20 boxf depthbounds;
21 int depth_computed;
22
23 float height;
24 int enabled;
25 v4f plane;
26 }
27 wrender =
28 {
29 .fbreflect = { .format = GL_RGB, .div = 3 },
30 .fbdepth = { .format = GL_RGBA, .div = 4 }
31 };
32
33 static void world_water_init(void)
34 {
35 vg_info( "world_water_init\n" );
36 shader_water_register();
37
38 vg_acquire_thread_sync();
39 {
40 fb_init( &wrender.fbreflect );
41 fb_init( &wrender.fbdepth );
42
43 vg_tex2d_init( (vg_tex2d *[]){&tex_water_surf}, 1 );
44 vg_success( "done\n" );
45 }
46 vg_release_thread_sync();
47 }
48
49 static void world_water_free(void *_)
50 {
51 vg_tex2d_free( (vg_tex2d *[]){&tex_water_surf}, 1 );
52 fb_free( &wrender.fbreflect );
53 fb_free( &wrender.fbdepth );
54 }
55
56 static void water_fb_resize(void)
57 {
58 if( !wrender.enabled )
59 return;
60
61 fb_resize( &wrender.fbreflect );
62 fb_resize( &wrender.fbdepth );
63 }
64
65 static void water_set_surface( glmesh *surf, float height )
66 {
67 wrender.mdl = *surf;
68 wrender.height = height;
69 wrender.enabled = 1;
70
71 v4_copy( (v4f){ 0.0f, 1.0f, 0.0f, height }, wrender.plane );
72 }
73
74 static void render_water_texture( m4x3f camera )
75 {
76 if( !wrender.enabled )
77 return;
78
79 /* Draw reflection buffa */
80 fb_use( &wrender.fbreflect );
81 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
82
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);
86
87 m3x3f flip;
88 m3x3_identity( flip );
89 flip[1][1] = -1.0f;
90 m3x3_mul( flip, camera, new_cam );
91
92
93 v3f p0;
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 );
97
98 m4x4f view;
99 vg_line_pt3( new_cam[3], 0.3f, 0xff00ffff );
100
101 m4x3_invert_affine( new_cam, inverse );
102 m4x3_expand( inverse, view );
103
104 v4f clippa = { 0.0f, 1.0f, 0.0f, wrender.height-0.1f };
105 m4x3_mulp( inverse, clippa, clippa );
106 clippa[3] *= -1.0f;
107
108 m4x4f projection;
109 m4x4_projection( projection,
110 gpipeline.fov,
111 (float)vg.window_x / (float)vg.window_y,
112 0.1f, 900.0f );
113 plane_clip_projection( projection, clippa );
114 m4x4_mul( projection, view, projection );
115
116 glCullFace( GL_FRONT );
117 render_world( projection, new_cam );
118 glCullFace( GL_BACK );
119
120
121 /* Draw beneath texture */
122 fb_use( &wrender.fbdepth );
123 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
124
125 m4x3_invert_affine( camera, inverse );
126 m4x3_expand( inverse, view );
127
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 );
131 clippb[3] *= -1.0f;
132
133 m4x4_projection( projection,
134 gpipeline.fov,
135 (float)vg.window_x / (float)vg.window_y,
136 0.1f, 900.0f );
137
138 plane_clip_projection( projection, clippb );
139 m4x4_mul( projection, view, projection );
140 render_world_depth( projection, camera );
141
142 glViewport( 0, 0, vg.window_x, vg.window_y );
143 }
144
145 static void render_water_surface( m4x4f pv, m4x3f camera )
146 {
147 if( !wrender.enabled )
148 return;
149
150 /* Draw surface */
151 shader_water_use();
152
153 fb_bindtex( &wrender.fbreflect, 0 );
154 shader_water_uTexMain( 0 );
155
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 });
161
162 shader_link_standard_ub( _shader_water.id, 2 );
163
164 fb_bindtex( &wrender.fbdepth, 3 );
165 shader_water_uTexBack( 3 );
166 shader_water_uTime( vg.time );
167 shader_water_uCamera( camera[3] );
168 shader_water_uSurfaceY( wrender.height );
169
170 shader_water_uPv( pv );
171
172 m4x3f full;
173 m4x3_identity( full );
174 full[3][1] = wrender.height;
175
176 shader_water_uMdl( full );
177
178 glEnable(GL_BLEND);
179 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
180 glBlendEquation(GL_FUNC_ADD);
181
182 mesh_bind( &wrender.mdl );
183 mesh_draw( &wrender.mdl );
184
185 glDisable(GL_BLEND);
186 }
187
188 #endif /* WATER_H */