well yeah i guess
[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 VG_STATIC void world_water_init(void)
16 {
17 vg_info( "world_water_init\n" );
18 shader_water_register();
19
20 vg_acquire_thread_sync();
21 {
22 world.water.fbreflect.format = GL_RGB;
23 world.water.fbreflect.div = 3;
24 world.water.fbdepth.format = GL_RGBA;
25 world.water.fbdepth.div = 4;
26
27 fb_init( &world.water.fbreflect );
28 fb_init( &world.water.fbdepth );
29
30 vg_tex2d_init( (vg_tex2d *[]){&tex_water_surf}, 1 );
31 }
32 vg_release_thread_sync();
33
34 vg_success( "done\n" );
35 }
36
37 VG_STATIC void water_fb_resize(void)
38 {
39 if( !world.water.enabled )
40 return;
41
42 fb_resize( &world.water.fbreflect );
43 fb_resize( &world.water.fbdepth );
44 }
45
46 VG_STATIC void water_set_surface( float height )
47 {
48 world.water.height = height;
49 v4_copy( (v4f){ 0.0f, 1.0f, 0.0f, height }, world.water.plane );
50 }
51
52 VG_STATIC void render_water_texture( m4x3f camera )
53 {
54 if( !world.water.enabled )
55 return;
56
57 /* Draw reflection buffa */
58 fb_use( &world.water.fbreflect );
59 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
60
61 m4x3f new_cam, inverse;
62 v3_copy( camera[3], new_cam[3] );
63 new_cam[3][1] -= 2.0f * (camera[3][1] - world.water.height);
64
65 m3x3f flip;
66 m3x3_identity( flip );
67 flip[1][1] = -1.0f;
68 m3x3_mul( flip, camera, new_cam );
69
70
71 v3f p0;
72 m3x3_mulv( new_cam, (v3f){0.0f,0.0f,-1.0f}, p0 );
73 v3_add( new_cam[3], p0, p0 );
74 vg_line( new_cam[3], p0, 0xffffffff );
75
76 m4x4f view;
77 vg_line_pt3( new_cam[3], 0.3f, 0xff00ffff );
78
79 m4x3_invert_affine( new_cam, inverse );
80 m4x3_expand( inverse, view );
81
82 v4f clippa = { 0.0f, 1.0f, 0.0f, world.water.height-0.1f };
83 m4x3_mulp( inverse, clippa, clippa );
84 clippa[3] *= -1.0f;
85
86 m4x4f projection;
87 m4x4_projection( projection,
88 gpipeline.fov,
89 (float)vg.window_x / (float)vg.window_y,
90 0.1f, 900.0f );
91 plane_clip_projection( projection, clippa );
92 m4x4_mul( projection, view, projection );
93
94 glCullFace( GL_FRONT );
95 render_world( projection, new_cam );
96 glCullFace( GL_BACK );
97
98
99 /* Draw beneath texture */
100 fb_use( &world.water.fbdepth );
101 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
102
103 m4x3_invert_affine( camera, inverse );
104 m4x3_expand( inverse, view );
105
106 float bias = -(camera[3][1]-world.water.height)*0.1f;
107 v4f clippb = { 0.0f, -1.0f, 0.0f, -(world.water.height) + bias };
108 m4x3_mulp( inverse, clippb, clippb );
109 clippb[3] *= -1.0f;
110
111 m4x4_projection( projection,
112 gpipeline.fov,
113 (float)vg.window_x / (float)vg.window_y,
114 0.1f, 900.0f );
115
116 plane_clip_projection( projection, clippb );
117 m4x4_mul( projection, view, projection );
118 render_world_depth( projection, camera );
119
120 glViewport( 0, 0, vg.window_x, vg.window_y );
121 }
122
123 VG_STATIC void render_water_surface( m4x4f pv, m4x3f camera )
124 {
125 if( !world.water.enabled )
126 return;
127
128 /* Draw surface */
129 shader_water_use();
130
131 fb_bindtex( &world.water.fbreflect, 0 );
132 shader_water_uTexMain( 0 );
133
134 vg_tex2d_bind( &tex_water_surf, 1 );
135 shader_water_uTexDudv( 1 );
136 shader_water_uInvRes( (v2f){
137 1.0f / (float)vg.window_x,
138 1.0f / (float)vg.window_y });
139
140 shader_link_standard_ub( _shader_water.id, 2 );
141
142 fb_bindtex( &world.water.fbdepth, 3 );
143 shader_water_uTexBack( 3 );
144 shader_water_uTime( world.time );
145 shader_water_uCamera( camera[3] );
146 shader_water_uSurfaceY( world.water.height );
147
148 shader_water_uPv( pv );
149
150 m4x3f full;
151 m4x3_identity( full );
152 full[3][1] = world.water.height;
153
154 shader_water_uMdl( full );
155
156 glEnable(GL_BLEND);
157 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
158 glBlendEquation(GL_FUNC_ADD);
159
160 mesh_bind( &world.mesh_water );
161 mesh_draw( &world.mesh_water );
162
163 glDisable(GL_BLEND);
164 }
165
166 #endif /* WATER_H */