2 * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
8 #include "world_water.h"
9 #include "world_render.h"
11 #include "shaders/scene_water.h"
12 #include "shaders/scene_water_fast.h"
15 static void world_water_init(void){
16 vg_info( "world_water_init\n" );
17 shader_scene_water_register();
18 shader_scene_water_fast_register();
20 vg_tex2d_load_qoi_async_file( "textures/water_surf.qoi",
21 VG_TEX2D_LINEAR
|VG_TEX2D_REPEAT
,
22 &world_water
.tex_water_surf
);
24 vg_success( "done\n" );
27 static void water_set_surface( world_instance
*world
, float height
){
28 world
->water
.height
= height
;
29 v4_copy( (v4f
){ 0.0f
, 1.0f
, 0.0f
, height
}, world
->water
.plane
);
32 static void world_link_lighting_ub( world_instance
*world
, GLuint shader
);
33 static void world_bind_position_texture( world_instance
*world
,
34 GLuint shader
, GLuint location
,
36 static void world_bind_light_array( world_instance
*world
,
37 GLuint shader
, GLuint location
,
39 static void world_bind_light_index( world_instance
*world
,
40 GLuint shader
, GLuint location
,
44 * Does not write motion vectors
46 static void render_water_texture( world_instance
*world
, camera
*cam
,
48 if( !world
->water
.enabled
|| (vg
.quality_profile
== k_quality_profile_low
) )
51 /* Draw reflection buffa */
52 render_fb_bind( gpipeline
.fb_water_reflection
, 1 );
53 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
56 * Create flipped view matrix. Don't care about motion vectors
58 float cam_height
= cam
->transform
[3][1] - world
->water
.height
;
61 water_cam
.farz
= cam
->farz
;
62 water_cam
.nearz
= cam
->nearz
;
63 v3_copy( cam
->transform
[3], water_cam
.transform
[3] );
64 water_cam
.transform
[3][1] -= 2.0f
* cam_height
;
67 m3x3_identity( flip
);
69 m3x3_mul( flip
, cam
->transform
, water_cam
.transform
);
71 camera_update_view( &water_cam
);
74 * Create clipped projection
76 v4f clippa
= { 0.0f
, 1.0f
, 0.0f
, world
->water
.height
-0.1f
};
77 m4x3_mulp( water_cam
.transform_inverse
, clippa
, clippa
);
80 m4x4_copy( cam
->mtx
.p
, water_cam
.mtx
.p
);
81 m4x4_clip_projection( water_cam
.mtx
.p
, clippa
);
83 camera_finalize( &water_cam
);
88 glEnable( GL_DEPTH_TEST
);
89 glDisable( GL_BLEND
);
90 glCullFace( GL_FRONT
);
91 render_world( world
, &water_cam
, layer_depth
);
92 glCullFace( GL_BACK
);
95 * Create beneath view matrix
98 render_fb_bind( gpipeline
.fb_water_beneath
, 1 );
99 glClearColor( 1.0f
, 0.0f
, 0.0f
, 0.0f
);
100 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
102 m4x3_copy( cam
->transform
, beneath_cam
.transform
);
103 camera_update_view( &beneath_cam
);
105 float bias
= -(cam
->transform
[3][1]-world
->water
.height
)*0.1f
;
107 v4f clippb
= { 0.0f
, -1.0f
, 0.0f
, -(world
->water
.height
) + bias
};
108 m4x3_mulp( beneath_cam
.transform_inverse
, clippb
, clippb
);
111 m4x4_copy( cam
->mtx
.p
, beneath_cam
.mtx
.p
);
112 m4x4_clip_projection( beneath_cam
.mtx
.p
, clippb
);
113 camera_finalize( &beneath_cam
);
115 glEnable( GL_DEPTH_TEST
);
116 glDisable( GL_BLEND
);
117 render_world_depth( world
, &beneath_cam
);
118 //glViewport( 0,0, g_render_x, g_render_y );
121 static void render_water_surface( world_instance
*world
, camera
*cam
){
122 if( !world
->water
.enabled
)
125 if( vg
.quality_profile
== k_quality_profile_high
){
127 shader_scene_water_use();
129 render_fb_bind_texture( gpipeline
.fb_water_reflection
, 0, 0 );
130 shader_scene_water_uTexMain( 0 );
132 glActiveTexture( GL_TEXTURE1
);
133 glBindTexture( GL_TEXTURE_2D
, world_water
.tex_water_surf
);
134 shader_scene_water_uTexDudv( 1 );
136 shader_scene_water_uInvRes( (v2f
){
137 1.0f
/ (float)vg
.window_x
,
138 1.0f
/ (float)vg
.window_y
});
140 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, scene_water
);
142 render_fb_bind_texture( gpipeline
.fb_water_beneath
, 0, 5 );
143 shader_scene_water_uTexBack( 5 );
144 shader_scene_water_uTime( world_static
.time
);
145 shader_scene_water_uCamera( cam
->transform
[3] );
146 shader_scene_water_uSurfaceY( world
->water
.height
);
148 shader_scene_water_uPv( cam
->mtx
.pv
);
149 shader_scene_water_uPvmPrev( cam
->mtx_prev
.pv
);
152 m4x3_identity( full
);
153 shader_scene_water_uMdl( full
);
156 glBlendFunc(GL_SRC_ALPHA
,GL_ONE_MINUS_SRC_ALPHA
);
157 glBlendEquation(GL_FUNC_ADD
);
159 mesh_bind( &world
->mesh_no_collide
);
161 for( int i
=0; i
<world
->surface_count
; i
++ ){
162 struct world_surface
*mat
= &world
->surfaces
[i
];
164 if( mat
->info
.shader
== k_shader_water
){
165 shader_scene_water_uShoreColour( mat
->info
.colour
);
166 shader_scene_water_uOceanColour( mat
->info
.colour1
);
168 mdl_draw_submesh( &mat
->sm_no_collide
);
174 else if( vg
.quality_profile
== k_quality_profile_low
){
175 shader_scene_water_fast_use();
177 glActiveTexture( GL_TEXTURE1
);
178 glBindTexture( GL_TEXTURE_2D
, world_water
.tex_water_surf
);
179 shader_scene_water_fast_uTexDudv( 1 );
181 shader_scene_water_fast_uTime( world_static
.time
);
182 shader_scene_water_fast_uCamera( cam
->transform
[3] );
183 shader_scene_water_fast_uSurfaceY( world
->water
.height
);
185 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, scene_water_fast
);
188 m4x3_identity( full
);
189 shader_scene_water_fast_uMdl( full
);
190 shader_scene_water_fast_uPv( cam
->mtx
.pv
);
191 shader_scene_water_fast_uPvmPrev( cam
->mtx_prev
.pv
);
194 glBlendFunc(GL_SRC_ALPHA
,GL_ONE_MINUS_SRC_ALPHA
);
195 glBlendEquation(GL_FUNC_ADD
);
197 mesh_bind( &world
->mesh_no_collide
);
199 for( int i
=0; i
<world
->surface_count
; i
++ ){
200 struct world_surface
*mat
= &world
->surfaces
[i
];
202 if( mat
->info
.shader
== k_shader_water
){
203 shader_scene_water_fast_uShoreColour( mat
->info
.colour
);
204 shader_scene_water_fast_uOceanColour( mat
->info
.colour1
);
206 mdl_draw_submesh( &mat
->sm_no_collide
);