928d36c95175c6d73cbad7bfee03f0b61a67fd93
2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
10 #include "shaders/water.h"
13 vg_tex2d tex_water_surf
= { .path
= "textures/water_surf.qoi" };
15 VG_STATIC
void world_water_init(void)
17 vg_info( "world_water_init\n" );
18 shader_water_register();
20 vg_acquire_thread_sync();
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;
27 fb_init( &world
.water
.fbreflect
);
28 fb_init( &world
.water
.fbdepth
);
30 vg_tex2d_init( (vg_tex2d
*[]){&tex_water_surf
}, 1 );
32 vg_release_thread_sync();
34 vg_success( "done\n" );
37 VG_STATIC
void water_fb_resize(void)
39 if( !world
.water
.enabled
)
42 fb_resize( &world
.water
.fbreflect
);
43 fb_resize( &world
.water
.fbdepth
);
46 VG_STATIC
void water_set_surface( float height
)
48 world
.water
.height
= height
;
49 v4_copy( (v4f
){ 0.0f
, 1.0f
, 0.0f
, height
}, world
.water
.plane
);
52 VG_STATIC
void render_water_texture( m4x3f camera
)
54 if( !world
.water
.enabled
)
57 /* Draw reflection buffa */
58 fb_use( &world
.water
.fbreflect
);
59 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
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
);
66 m3x3_identity( flip
);
68 m3x3_mul( flip
, camera
, new_cam
);
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 );
77 vg_line_pt3( new_cam
[3], 0.3f
, 0xff00ffff );
79 m4x3_invert_affine( new_cam
, inverse
);
80 m4x3_expand( inverse
, view
);
82 v4f clippa
= { 0.0f
, 1.0f
, 0.0f
, world
.water
.height
-0.1f
};
83 m4x3_mulp( inverse
, clippa
, clippa
);
87 m4x4_projection( projection
,
89 (float)vg
.window_x
/ (float)vg
.window_y
,
91 plane_clip_projection( projection
, clippa
);
92 m4x4_mul( projection
, view
, projection
);
94 glCullFace( GL_FRONT
);
95 render_world( projection
, new_cam
);
96 glCullFace( GL_BACK
);
99 /* Draw beneath texture */
100 fb_use( &world
.water
.fbdepth
);
101 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
103 m4x3_invert_affine( camera
, inverse
);
104 m4x3_expand( inverse
, view
);
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
);
111 m4x4_projection( projection
,
113 (float)vg
.window_x
/ (float)vg
.window_y
,
116 plane_clip_projection( projection
, clippb
);
117 m4x4_mul( projection
, view
, projection
);
118 render_world_depth( projection
, camera
);
120 glViewport( 0, 0, vg
.window_x
, vg
.window_y
);
123 VG_STATIC
void render_water_surface( m4x4f pv
, m4x3f camera
)
125 if( !world
.water
.enabled
)
131 fb_bindtex( &world
.water
.fbreflect
, 0 );
132 shader_water_uTexMain( 0 );
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
});
140 shader_link_standard_ub( _shader_water
.id
, 2 );
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
);
148 shader_water_uPv( pv
);
151 m4x3_identity( full
);
152 full
[3][1] = world
.water
.height
;
154 shader_water_uMdl( full
);
157 glBlendFunc(GL_SRC_ALPHA
,GL_ONE_MINUS_SRC_ALPHA
);
158 glBlendEquation(GL_FUNC_ADD
);
160 mesh_bind( &world
.mesh_water
);
162 for( int i
=0; i
<world
.material_count
; i
++ )
164 struct world_material
*mat
= &world
.materials
[i
];
166 if( mat
->info
.shader
== k_shader_water
)
168 shader_water_uShoreColour( mat
->info
.colour
);
169 shader_water_uOceanColour( mat
->info
.colour1
);
171 mdl_draw_submesh( &mat
->sm_no_collide
);