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
,
38 GLuint shader
, int texture_id
);
41 * Does not write motion vectors
43 VG_STATIC
void render_water_texture( world_instance
*world
, camera
*cam
)
45 if( !world
->water
.enabled
|| (vg
.quality_profile
== k_quality_profile_low
) )
48 /* Draw reflection buffa */
49 render_fb_bind( gpipeline
.fb_water_reflection
);
50 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
53 * Create flipped view matrix. Don't care about motion vectors
55 float cam_height
= cam
->transform
[3][1] - world
->water
.height
;
58 water_cam
.farz
= cam
->farz
;
59 water_cam
.nearz
= cam
->nearz
;
60 v3_copy( cam
->transform
[3], water_cam
.transform
[3] );
61 water_cam
.transform
[3][1] -= 2.0f
* cam_height
;
64 m3x3_identity( flip
);
66 m3x3_mul( flip
, cam
->transform
, water_cam
.transform
);
68 camera_update_view( &water_cam
);
71 * Create clipped projection
73 v4f clippa
= { 0.0f
, 1.0f
, 0.0f
, world
->water
.height
-0.1f
};
74 m4x3_mulp( water_cam
.transform_inverse
, clippa
, clippa
);
77 m4x4_copy( cam
->mtx
.p
, water_cam
.mtx
.p
);
78 m4x4_clip_projection( water_cam
.mtx
.p
, clippa
);
80 camera_finalize( &water_cam
);
85 glCullFace( GL_FRONT
);
86 render_world( world
, &water_cam
);
87 glCullFace( GL_BACK
);
90 * Create beneath view matrix
93 render_fb_bind( gpipeline
.fb_water_beneath
);
94 glClearColor( 1.0f
, 0.0f
, 0.0f
, 0.0f
);
95 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
97 m4x3_copy( cam
->transform
, beneath_cam
.transform
);
98 camera_update_view( &beneath_cam
);
100 float bias
= -(cam
->transform
[3][1]-world
->water
.height
)*0.1f
;
102 v4f clippb
= { 0.0f
, -1.0f
, 0.0f
, -(world
->water
.height
) + bias
};
103 m4x3_mulp( beneath_cam
.transform_inverse
, clippb
, clippb
);
106 m4x4_copy( cam
->mtx
.p
, beneath_cam
.mtx
.p
);
107 m4x4_clip_projection( beneath_cam
.mtx
.p
, clippb
);
108 camera_finalize( &beneath_cam
);
110 render_world_depth( world
, &beneath_cam
);
111 glViewport( 0, 0, vg
.window_x
, vg
.window_y
);
114 VG_STATIC
void render_water_surface( world_instance
*world
, camera
*cam
)
116 if( !world
->water
.enabled
)
119 if( vg
.quality_profile
== k_quality_profile_high
)
122 shader_scene_water_use();
124 render_fb_bind_texture( gpipeline
.fb_water_reflection
, 0, 0 );
125 shader_scene_water_uTexMain( 0 );
127 vg_tex2d_bind( &tex_water_surf
, 1 );
128 shader_scene_water_uTexDudv( 1 );
129 shader_scene_water_uInvRes( (v2f
){
130 1.0f
/ (float)vg
.window_x
,
131 1.0f
/ (float)vg
.window_y
});
133 world_link_lighting_ub( world
, _shader_scene_water
.id
, 2 );
135 render_fb_bind_texture( gpipeline
.fb_water_beneath
, 0, 3 );
136 shader_scene_water_uTexBack( 3 );
137 shader_scene_water_uTime( world_global
.time
);
138 shader_scene_water_uCamera( cam
->transform
[3] );
139 shader_scene_water_uSurfaceY( world
->water
.height
);
141 shader_scene_water_uPv( cam
->mtx
.pv
);
142 shader_scene_water_uPvmPrev( cam
->mtx_prev
.pv
);
145 m4x3_identity( full
);
146 shader_scene_water_uMdl( full
);
149 glBlendFunc(GL_SRC_ALPHA
,GL_ONE_MINUS_SRC_ALPHA
);
150 glBlendEquation(GL_FUNC_ADD
);
152 mesh_bind( &world
->mesh_no_collide
);
154 for( int i
=0; i
<world
->material_count
; i
++ )
156 struct world_material
*mat
= &world
->materials
[i
];
158 if( mat
->info
.shader
== k_shader_water
)
160 shader_scene_water_uShoreColour( mat
->info
.colour
);
161 shader_scene_water_uOceanColour( mat
->info
.colour1
);
163 mdl_draw_submesh( &mat
->sm_no_collide
);
169 else if( vg
.quality_profile
== k_quality_profile_low
)
171 shader_scene_water_fast_use();
173 vg_tex2d_bind( &tex_water_surf
, 1 );
174 shader_scene_water_fast_uTexDudv( 1 );
175 shader_scene_water_fast_uTime( world_global
.time
);
176 shader_scene_water_fast_uCamera( cam
->transform
[3] );
177 shader_scene_water_fast_uSurfaceY( world
->water
.height
);
178 world_link_lighting_ub( world
, _shader_scene_water_fast
.id
, 2 );
181 m4x3_identity( full
);
182 shader_scene_water_fast_uMdl( full
);
183 shader_scene_water_fast_uPv( cam
->mtx
.pv
);
184 shader_scene_water_fast_uPvmPrev( cam
->mtx_prev
.pv
);
187 glBlendFunc(GL_SRC_ALPHA
,GL_ONE_MINUS_SRC_ALPHA
);
188 glBlendEquation(GL_FUNC_ADD
);
190 mesh_bind( &world
->mesh_no_collide
);
192 for( int i
=0; i
<world
->material_count
; i
++ )
194 struct world_material
*mat
= &world
->materials
[i
];
196 if( mat
->info
.shader
== k_shader_water
)
198 shader_scene_water_fast_uShoreColour( mat
->info
.colour
);
199 shader_scene_water_fast_uOceanColour( mat
->info
.colour1
);
201 mdl_draw_submesh( &mat
->sm_no_collide
);