now we're doing a bunch of them
[carveJwlIkooP6JGAAIwe30JlM.git] / world_water.h
index 5b82b12987aa41bc33067b1d5d91660a73e3ab7d..c16e2c556f60caa9864826f46cc6396778998b7c 100644 (file)
 
 #include "world.h"
 #include "render.h"
-#include "shaders/water.h"
+#include "shaders/scene_water.h"
+#include "shaders/scene_water_fast.h"
 #include "scene.h"
 
 vg_tex2d tex_water_surf = { .path = "textures/water_surf.qoi" };
 
-static struct
-{
-   struct framebuffer fbreflect, fbdepth;
-   glmesh mdl;
-
-   boxf depthbounds;
-   int depth_computed;
-
-   float height;
-   int enabled;
-   v4f plane;
-}
-wrender =
-{
-   .fbreflect = { .format = GL_RGB,  .div = 3 },
-   .fbdepth   = { .format = GL_RGBA, .div = 4 }
-};
-
-static void world_water_init(void)
+VG_STATIC void world_water_init(void)
 {
    vg_info( "world_water_init\n" );
-   shader_water_register();
-   
+   shader_scene_water_register();
+   shader_scene_water_fast_register();
+
    vg_acquire_thread_sync();
    {
-      fb_init( &wrender.fbreflect );
-      fb_init( &wrender.fbdepth );
-
       vg_tex2d_init( (vg_tex2d *[]){&tex_water_surf}, 1 );
-      vg_success( "done\n" );
    }
    vg_release_thread_sync();
-}
 
-static void world_water_free(void *_)
-{
-   vg_tex2d_free( (vg_tex2d *[]){&tex_water_surf}, 1 );
-   fb_free( &wrender.fbreflect );
-   fb_free( &wrender.fbdepth );
+   vg_success( "done\n" );
 }
 
-static void water_fb_resize(void)
+VG_STATIC void water_set_surface( world_instance *world, float height )
 {
-   if( !wrender.enabled )
-      return;
-   
-   fb_resize( &wrender.fbreflect );
-   fb_resize( &wrender.fbdepth );
+   world->water.height = height;
+   v4_copy( (v4f){ 0.0f, 1.0f, 0.0f, height }, world->water.plane );
 }
 
-static void water_set_surface( glmesh *surf, float height )
-{
-   wrender.mdl = *surf;
-   wrender.height = height;
-   wrender.enabled = 1;
-
-   v4_copy( (v4f){ 0.0f, 1.0f, 0.0f, height }, wrender.plane );
-}
+VG_STATIC void world_link_lighting_ub( world_instance *world, GLuint shader );
+VG_STATIC void world_bind_position_texture( world_instance *world, 
+                                            GLuint shader, GLuint location,
+                                            int slot );
 
-static void render_water_texture( m4x3f camera )
+/*
+ * Does not write motion vectors
+ */
+VG_STATIC void render_water_texture( world_instance *world, camera *cam )
 {
-   if( !wrender.enabled )
+   if( !world->water.enabled || (vg.quality_profile == k_quality_profile_low) )
       return;
 
    /* Draw reflection buffa */
-   fb_use( &wrender.fbreflect );
+   render_fb_bind( gpipeline.fb_water_reflection );
    glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
 
-   m4x3f new_cam, inverse;
-   v3_copy( camera[3], new_cam[3] );
-   new_cam[3][1] -= 2.0f * (camera[3][1] - wrender.height);
+   /* 
+    * Create flipped view matrix. Don't care about motion vectors
+    */
+   float cam_height = cam->transform[3][1] - world->water.height;
+
+   camera water_cam;
+   water_cam.farz = cam->farz;
+   water_cam.nearz = cam->nearz;
+   v3_copy( cam->transform[3], water_cam.transform[3] );
+   water_cam.transform[3][1] -= 2.0f * cam_height;
 
    m3x3f flip;
    m3x3_identity( flip );
    flip[1][1] = -1.0f;
-   m3x3_mul( flip, camera, new_cam );
-
-
-   v3f p0;
-   m3x3_mulv( new_cam, (v3f){0.0f,0.0f,-1.0f}, p0 );
-   v3_add( new_cam[3], p0, p0 );
-   vg_line( new_cam[3], p0, 0xffffffff );
+   m3x3_mul( flip, cam->transform, water_cam.transform );
 
-   m4x4f view;
-   vg_line_pt3( new_cam[3], 0.3f, 0xff00ffff );
+   camera_update_view( &water_cam );
 
-   m4x3_invert_affine( new_cam, inverse );
-   m4x3_expand( inverse, view );
-
-   v4f clippa = { 0.0f, 1.0f, 0.0f, wrender.height-0.1f };
-   m4x3_mulp( inverse, clippa, clippa );
+   /* 
+    * Create clipped projection 
+    */
+   v4f clippa = { 0.0f, 1.0f, 0.0f, world->water.height-0.1f };
+   m4x3_mulp( water_cam.transform_inverse, clippa, clippa );
    clippa[3] *= -1.0f;
 
-   m4x4f projection;
-   m4x4_projection( projection,
-         gpipeline.fov,
-         (float)vg.window_x / (float)vg.window_y, 
-         0.1f, 900.0f );
-   plane_clip_projection( projection, clippa );
-   m4x4_mul( projection, view, projection );
+   m4x4_copy( cam->mtx.p, water_cam.mtx.p );
+   m4x4_clip_projection( water_cam.mtx.p, clippa );
+
+   camera_finalize( &water_cam );
 
+   /*
+    * Draw world
+    */
    glCullFace( GL_FRONT );
-   render_world( projection, new_cam );
+   render_world( world, &water_cam );
    glCullFace( GL_BACK );
-
-
-   /* Draw beneath texture */
-   fb_use( &wrender.fbdepth );
+   
+   /*
+    * Create beneath view matrix
+    */
+   camera beneath_cam;
+   render_fb_bind( gpipeline.fb_water_beneath );
+   glClearColor( 1.0f, 0.0f, 0.0f, 0.0f );
    glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
 
-   m4x3_invert_affine( camera, inverse );
-   m4x3_expand( inverse, view );
+   m4x3_copy( cam->transform, beneath_cam.transform );
+   camera_update_view( &beneath_cam );
+
+   float bias = -(cam->transform[3][1]-world->water.height)*0.1f;
 
-   float bias = -(camera[3][1]-wrender.height)*0.1f;
-   v4f clippb = { 0.0f, -1.0f, 0.0f, -(wrender.height) + bias };
-   m4x3_mulp( inverse, clippb, clippb );
+   v4f clippb = { 0.0f, -1.0f, 0.0f, -(world->water.height) + bias };
+   m4x3_mulp( beneath_cam.transform_inverse, clippb, clippb );
    clippb[3] *= -1.0f;
 
-   m4x4_projection( projection,
-         gpipeline.fov,
-         (float)vg.window_x / (float)vg.window_y, 
-         0.1f, 900.0f );
+   m4x4_copy( cam->mtx.p, beneath_cam.mtx.p );
+   m4x4_clip_projection( beneath_cam.mtx.p, clippb );
+   camera_finalize( &beneath_cam );
 
-   plane_clip_projection( projection, clippb );
-   m4x4_mul( projection, view, projection );
-   render_world_depth( projection, camera );
-   
+   render_world_depth( world, &beneath_cam );
    glViewport( 0, 0, vg.window_x, vg.window_y );
 }
 
-static void render_water_surface( m4x4f pv, m4x3f camera )
+VG_STATIC void render_water_surface( world_instance *world, camera *cam )
 {
-   if( !wrender.enabled )
+   if( !world->water.enabled )
       return;
 
-   /* Draw surface */
-   shader_water_use();
-   
-   fb_bindtex( &wrender.fbreflect, 0 );
-   shader_water_uTexMain( 0 );
-
-   vg_tex2d_bind( &tex_water_surf, 1 );
-   shader_water_uTexDudv( 1 );
-   shader_water_uInvRes( (v2f){
-         1.0f / (float)vg.window_x,
-         1.0f / (float)vg.window_y });
-
-   shader_link_standard_ub( _shader_water.id, 2 );
-
-   fb_bindtex( &wrender.fbdepth, 3 );
-   shader_water_uTexBack( 3 );
-   shader_water_uTime( world.routes.time );
-   shader_water_uCamera( camera[3] );
-   shader_water_uSurfaceY( wrender.height );
-
-   shader_water_uPv( pv );
-
-   m4x3f full;
-   m4x3_identity( full );
-   full[3][1] = wrender.height;
-
-   shader_water_uMdl( full );
-
-   glEnable(GL_BLEND);
-   glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
-   glBlendEquation(GL_FUNC_ADD);
-
-   mesh_bind( &wrender.mdl );
-   mesh_draw( &wrender.mdl );
-
-   glDisable(GL_BLEND);
+   if( vg.quality_profile == k_quality_profile_high )
+   {
+      /* Draw surface */
+      shader_scene_water_use();
+      
+      render_fb_bind_texture( gpipeline.fb_water_reflection, 0, 0 );
+      shader_scene_water_uTexMain( 0 );
+
+      vg_tex2d_bind( &tex_water_surf, 1 );
+      shader_scene_water_uTexDudv( 1 );
+      shader_scene_water_uInvRes( (v2f){
+            1.0f / (float)vg.window_x,
+            1.0f / (float)vg.window_y });
+
+      world_link_lighting_ub( world, _shader_scene_water.id );
+      world_bind_position_texture( world, _shader_scene_water.id, 
+                                    _uniform_scene_water_g_world_depth, 2 );
+
+      render_fb_bind_texture( gpipeline.fb_water_beneath, 0, 3 );
+      shader_scene_water_uTexBack( 3 );
+      shader_scene_water_uTime( world_global.time );
+      shader_scene_water_uCamera( cam->transform[3] );
+      shader_scene_water_uSurfaceY( world->water.height );
+
+      shader_scene_water_uPv( cam->mtx.pv );
+      shader_scene_water_uPvmPrev( cam->mtx_prev.pv );
+
+      m4x3f full;
+      m4x3_identity( full );
+      shader_scene_water_uMdl( full );
+
+      glEnable(GL_BLEND);
+      glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
+      glBlendEquation(GL_FUNC_ADD);
+
+      mesh_bind( &world->mesh_no_collide );
+
+      for( int i=0; i<world->material_count; i++ )
+      {
+         struct world_material *mat = &world->materials[i];
+
+         if( mat->info.shader == k_shader_water )
+         {
+            shader_scene_water_uShoreColour( mat->info.colour );
+            shader_scene_water_uOceanColour( mat->info.colour1 );
+
+            mdl_draw_submesh( &mat->sm_no_collide );
+         }
+      }
+
+      glDisable(GL_BLEND);
+   }
+   else if( vg.quality_profile == k_quality_profile_low )
+   {
+      shader_scene_water_fast_use();
+
+      vg_tex2d_bind( &tex_water_surf, 1 );
+      shader_scene_water_fast_uTexDudv( 1 );
+      shader_scene_water_fast_uTime( world_global.time );
+      shader_scene_water_fast_uCamera( cam->transform[3] );
+      shader_scene_water_fast_uSurfaceY( world->water.height );
+      world_link_lighting_ub( world, _shader_scene_water_fast.id );
+      world_bind_position_texture( world, _shader_scene_water_fast.id,
+                                   _uniform_scene_water_fast_g_world_depth, 2 );
+
+      m4x3f full;
+      m4x3_identity( full );
+      shader_scene_water_fast_uMdl( full );
+      shader_scene_water_fast_uPv( cam->mtx.pv );
+      shader_scene_water_fast_uPvmPrev( cam->mtx_prev.pv );
+
+      glEnable(GL_BLEND);
+      glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
+      glBlendEquation(GL_FUNC_ADD);
+
+      mesh_bind( &world->mesh_no_collide );
+
+      for( int i=0; i<world->material_count; i++ )
+      {
+         struct world_material *mat = &world->materials[i];
+
+         if( mat->info.shader == k_shader_water )
+         {
+            shader_scene_water_fast_uShoreColour( mat->info.colour );
+            shader_scene_water_fast_uOceanColour( mat->info.colour1 );
+
+            mdl_draw_submesh( &mat->sm_no_collide );
+         }
+      }
+
+      glDisable(GL_BLEND);
+   }
 }
 
 #endif /* WATER_H */