2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
10 #include "shaders/scene_water.h"
11 #include "shaders/scene_water_fast.h"
14 vg_tex2d tex_water_surf
= { .path
= "textures/water_surf.qoi" };
16 VG_STATIC
void world_water_init(void)
18 vg_info( "world_water_init\n" );
19 shader_scene_water_register();
20 shader_scene_water_fast_register();
22 vg_acquire_thread_sync();
24 vg_tex2d_init( (vg_tex2d
*[]){&tex_water_surf
}, 1 );
26 vg_release_thread_sync();
28 vg_success( "done\n" );
31 VG_STATIC
void water_set_surface( world_instance
*world
, float height
)
33 world
->water
.height
= height
;
34 v4_copy( (v4f
){ 0.0f
, 1.0f
, 0.0f
, height
}, world
->water
.plane
);
37 VG_STATIC
void world_link_lighting_ub( world_instance
*world
, GLuint shader
);
38 VG_STATIC
void world_bind_position_texture( world_instance
*world
,
39 GLuint shader
, GLuint location
,
41 VG_STATIC
void world_bind_light_array( world_instance
*world
,
42 GLuint shader
, GLuint location
,
46 * Does not write motion vectors
48 VG_STATIC
void render_water_texture( world_instance
*world
, camera
*cam
)
50 if( !world
->water
.enabled
|| (vg
.quality_profile
== k_quality_profile_low
) )
53 /* Draw reflection buffa */
54 render_fb_bind( gpipeline
.fb_water_reflection
);
55 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
58 * Create flipped view matrix. Don't care about motion vectors
60 float cam_height
= cam
->transform
[3][1] - world
->water
.height
;
63 water_cam
.farz
= cam
->farz
;
64 water_cam
.nearz
= cam
->nearz
;
65 v3_copy( cam
->transform
[3], water_cam
.transform
[3] );
66 water_cam
.transform
[3][1] -= 2.0f
* cam_height
;
69 m3x3_identity( flip
);
71 m3x3_mul( flip
, cam
->transform
, water_cam
.transform
);
73 camera_update_view( &water_cam
);
76 * Create clipped projection
78 v4f clippa
= { 0.0f
, 1.0f
, 0.0f
, world
->water
.height
-0.1f
};
79 m4x3_mulp( water_cam
.transform_inverse
, clippa
, clippa
);
82 m4x4_copy( cam
->mtx
.p
, water_cam
.mtx
.p
);
83 m4x4_clip_projection( water_cam
.mtx
.p
, clippa
);
85 camera_finalize( &water_cam
);
90 glCullFace( GL_FRONT
);
91 render_world( world
, &water_cam
);
92 glCullFace( GL_BACK
);
95 * Create beneath view matrix
98 render_fb_bind( gpipeline
.fb_water_beneath
);
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 render_world_depth( world
, &beneath_cam
);
116 glViewport( 0, 0, vg
.window_x
, vg
.window_y
);
119 VG_STATIC
void render_water_surface( world_instance
*world
, camera
*cam
)
121 if( !world
->water
.enabled
)
124 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 vg_tex2d_bind( &tex_water_surf
, 1 );
133 shader_scene_water_uTexDudv( 1 );
134 shader_scene_water_uInvRes( (v2f
){
135 1.0f
/ (float)vg
.window_x
,
136 1.0f
/ (float)vg
.window_y
});
138 world_link_lighting_ub( world
, _shader_scene_water
.id
);
139 world_bind_position_texture( world
, _shader_scene_water
.id
,
140 _uniform_scene_water_g_world_depth
, 2 );
141 world_bind_light_array( world
, _shader_scene_water
.id
,
142 _uniform_scene_water_uLightsArray
, 4 );
144 render_fb_bind_texture( gpipeline
.fb_water_beneath
, 0, 3 );
145 shader_scene_water_uTexBack( 3 );
146 shader_scene_water_uTime( world_global
.time
);
147 shader_scene_water_uCamera( cam
->transform
[3] );
148 shader_scene_water_uSurfaceY( world
->water
.height
);
150 shader_scene_water_uPv( cam
->mtx
.pv
);
151 shader_scene_water_uPvmPrev( cam
->mtx_prev
.pv
);
154 m4x3_identity( full
);
155 shader_scene_water_uMdl( full
);
158 glBlendFunc(GL_SRC_ALPHA
,GL_ONE_MINUS_SRC_ALPHA
);
159 glBlendEquation(GL_FUNC_ADD
);
161 mesh_bind( &world
->mesh_no_collide
);
163 for( int i
=0; i
<world
->material_count
; i
++ )
165 struct world_material
*mat
= &world
->materials
[i
];
167 if( mat
->info
.shader
== k_shader_water
)
169 shader_scene_water_uShoreColour( mat
->info
.colour
);
170 shader_scene_water_uOceanColour( mat
->info
.colour1
);
172 mdl_draw_submesh( &mat
->sm_no_collide
);
178 else if( vg
.quality_profile
== k_quality_profile_low
)
180 shader_scene_water_fast_use();
182 vg_tex2d_bind( &tex_water_surf
, 1 );
183 shader_scene_water_fast_uTexDudv( 1 );
184 shader_scene_water_fast_uTime( world_global
.time
);
185 shader_scene_water_fast_uCamera( cam
->transform
[3] );
186 shader_scene_water_fast_uSurfaceY( world
->water
.height
);
187 world_link_lighting_ub( world
, _shader_scene_water_fast
.id
);
188 world_bind_position_texture( world
, _shader_scene_water_fast
.id
,
189 _uniform_scene_water_fast_g_world_depth
, 2 );
190 world_bind_light_array( world
, _shader_scene_water_fast
.id
,
191 _uniform_scene_water_fast_uLightsArray
, 4 );
194 m4x3_identity( full
);
195 shader_scene_water_fast_uMdl( full
);
196 shader_scene_water_fast_uPv( cam
->mtx
.pv
);
197 shader_scene_water_fast_uPvmPrev( cam
->mtx_prev
.pv
);
200 glBlendFunc(GL_SRC_ALPHA
,GL_ONE_MINUS_SRC_ALPHA
);
201 glBlendEquation(GL_FUNC_ADD
);
203 mesh_bind( &world
->mesh_no_collide
);
205 for( int i
=0; i
<world
->material_count
; i
++ )
207 struct world_material
*mat
= &world
->materials
[i
];
209 if( mat
->info
.shader
== k_shader_water
)
211 shader_scene_water_fast_uShoreColour( mat
->info
.colour
);
212 shader_scene_water_fast_uOceanColour( mat
->info
.colour1
);
214 mdl_draw_submesh( &mat
->sm_no_collide
);