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
){
47 if( !world
->water
.enabled
|| (vg
.quality_profile
== k_quality_profile_low
) )
50 /* Draw reflection buffa */
51 render_fb_bind( gpipeline
.fb_water_reflection
, 1 );
52 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
55 * Create flipped view matrix. Don't care about motion vectors
57 float cam_height
= cam
->transform
[3][1] - world
->water
.height
;
60 water_cam
.farz
= cam
->farz
;
61 water_cam
.nearz
= cam
->nearz
;
62 v3_copy( cam
->transform
[3], water_cam
.transform
[3] );
63 water_cam
.transform
[3][1] -= 2.0f
* cam_height
;
66 m3x3_identity( flip
);
68 m3x3_mul( flip
, cam
->transform
, water_cam
.transform
);
70 camera_update_view( &water_cam
);
73 * Create clipped projection
75 v4f clippa
= { 0.0f
, 1.0f
, 0.0f
, world
->water
.height
-0.1f
};
76 m4x3_mulp( water_cam
.transform_inverse
, clippa
, clippa
);
79 m4x4_copy( cam
->mtx
.p
, water_cam
.mtx
.p
);
80 m4x4_clip_projection( water_cam
.mtx
.p
, clippa
);
82 camera_finalize( &water_cam
);
87 glEnable( GL_DEPTH_TEST
);
88 glDisable( GL_BLEND
);
89 glCullFace( GL_FRONT
);
90 render_world( world
, &water_cam
, 0, 1, 0, 1 );
91 glCullFace( GL_BACK
);
94 * Create beneath view matrix
97 render_fb_bind( gpipeline
.fb_water_beneath
, 1 );
98 glClearColor( 1.0f
, 0.0f
, 0.0f
, 0.0f
);
99 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
101 m4x3_copy( cam
->transform
, beneath_cam
.transform
);
102 camera_update_view( &beneath_cam
);
104 float bias
= -(cam
->transform
[3][1]-world
->water
.height
)*0.1f
;
106 v4f clippb
= { 0.0f
, -1.0f
, 0.0f
, -(world
->water
.height
) + bias
};
107 m4x3_mulp( beneath_cam
.transform_inverse
, clippb
, clippb
);
110 m4x4_copy( cam
->mtx
.p
, beneath_cam
.mtx
.p
);
111 m4x4_clip_projection( beneath_cam
.mtx
.p
, clippb
);
112 camera_finalize( &beneath_cam
);
114 glEnable( GL_DEPTH_TEST
);
115 glDisable( GL_BLEND
);
116 render_world_depth( world
, &beneath_cam
);
117 //glViewport( 0,0, g_render_x, g_render_y );
120 static void render_water_surface( world_instance
*world
, camera
*cam
){
121 if( !world
->water
.enabled
)
124 if( vg
.quality_profile
== k_quality_profile_high
){
126 shader_scene_water_use();
128 render_fb_bind_texture( gpipeline
.fb_water_reflection
, 0, 0 );
129 shader_scene_water_uTexMain( 0 );
131 glActiveTexture( GL_TEXTURE1
);
132 glBindTexture( GL_TEXTURE_2D
, world_water
.tex_water_surf
);
133 shader_scene_water_uTexDudv( 1 );
135 shader_scene_water_uInvRes( (v2f
){
136 1.0f
/ (float)vg
.window_x
,
137 1.0f
/ (float)vg
.window_y
});
139 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, scene_water
);
141 render_fb_bind_texture( gpipeline
.fb_water_beneath
, 0, 5 );
142 shader_scene_water_uTexBack( 5 );
143 shader_scene_water_uTime( world_static
.time
);
144 shader_scene_water_uCamera( cam
->transform
[3] );
145 shader_scene_water_uSurfaceY( world
->water
.height
);
147 shader_scene_water_uPv( cam
->mtx
.pv
);
148 shader_scene_water_uPvmPrev( cam
->mtx_prev
.pv
);
151 m4x3_identity( full
);
152 shader_scene_water_uMdl( full
);
155 glBlendFunc(GL_SRC_ALPHA
,GL_ONE_MINUS_SRC_ALPHA
);
156 glBlendEquation(GL_FUNC_ADD
);
158 mesh_bind( &world
->mesh_no_collide
);
160 for( int i
=0; i
<world
->surface_count
; i
++ ){
161 struct world_surface
*mat
= &world
->surfaces
[i
];
163 if( mat
->info
.shader
== k_shader_water
){
164 shader_scene_water_uShoreColour( mat
->info
.colour
);
165 shader_scene_water_uOceanColour( mat
->info
.colour1
);
167 mdl_draw_submesh( &mat
->sm_no_collide
);
173 else if( vg
.quality_profile
== k_quality_profile_low
){
174 shader_scene_water_fast_use();
176 glActiveTexture( GL_TEXTURE1
);
177 glBindTexture( GL_TEXTURE_2D
, world_water
.tex_water_surf
);
178 shader_scene_water_fast_uTexDudv( 1 );
180 shader_scene_water_fast_uTime( world_static
.time
);
181 shader_scene_water_fast_uCamera( cam
->transform
[3] );
182 shader_scene_water_fast_uSurfaceY( world
->water
.height
);
184 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, scene_water_fast
);
187 m4x3_identity( full
);
188 shader_scene_water_fast_uMdl( full
);
189 shader_scene_water_fast_uPv( cam
->mtx
.pv
);
190 shader_scene_water_fast_uPvmPrev( cam
->mtx_prev
.pv
);
193 glBlendFunc(GL_SRC_ALPHA
,GL_ONE_MINUS_SRC_ALPHA
);
194 glBlendEquation(GL_FUNC_ADD
);
196 mesh_bind( &world
->mesh_no_collide
);
198 for( int i
=0; i
<world
->surface_count
; i
++ ){
199 struct world_surface
*mat
= &world
->surfaces
[i
];
201 if( mat
->info
.shader
== k_shader_water
){
202 shader_scene_water_fast_uShoreColour( mat
->info
.colour
);
203 shader_scene_water_fast_uOceanColour( mat
->info
.colour1
);
205 mdl_draw_submesh( &mat
->sm_no_collide
);