whole
[carveJwlIkooP6JGAAIwe30JlM.git] / water.h
diff --git a/water.h b/water.h
index c2a7122c2244c1774e9f49f428803ad6afb85ec1..1de2554abf8e459175a30438cb6146e0f24b09a3 100644 (file)
--- a/water.h
+++ b/water.h
@@ -4,7 +4,6 @@
 static void water_register(void);
 static void water_init(void);
 static void water_fb_resize(void);
-static void water_compute_depth( boxf bounds );
 static void water_set_surface( glmesh *surf, float height );
 static float water_height(void);
 
@@ -23,16 +22,16 @@ static struct
    struct framebuffer fbreflect, fbdepth;
    glmesh mdl;
 
-   GLuint depthmap;
    boxf depthbounds;
    int depth_computed;
 
    float height;
    int enabled;
+   v4f plane;
 }
 wrender =
 {
-   .fbreflect = { .format = GL_RGB,  .div = 4 },
+   .fbreflect = { .format = GL_RGB,  .div = 3 },
    .fbdepth   = { .format = GL_RGBA, .div = 4 }
 };
 
@@ -64,6 +63,7 @@ static void water_fb_resize(void)
    fb_resize( &wrender.fbdepth );
 }
 
+#if 0
 static void water_compute_depth( boxf bounds )
 {
    if( !wrender.enabled )
@@ -72,38 +72,43 @@ static void water_compute_depth( boxf bounds )
 #ifdef VG_RELEASE
    int const kres = 512;
 #else
-   int const kres = 64;
+   int const kres = 1024;
 #endif
 
    vg_info( "Computing depth map\n" );
-   u8 *img = malloc( kres*kres );
+   float *img = malloc( kres*kres*sizeof(float) );
+
+   boxf interior;
+   v3_add(bounds[0],(v3f){1.0f,1.0f,1.0f},interior[0]);
+   v3_sub(bounds[1],(v3f){1.0f,1.0f,1.0f},interior[1]);
 
    v3f volume;
-   v3_sub( bounds[1], bounds[0], volume );
-   box_copy( bounds, wrender.depthbounds );
+   v3_sub( interior[1], interior[0], volume );
+   box_copy( interior, wrender.depthbounds );
    
    for( int y=0; y<kres; y++ )
    {
       for( int x=0; x<kres; x++ )
       {
          v3f pos = { x, 0.0f, y };
-         v3_divs( pos, kres, pos );
-         v3_muladd( bounds[0], pos, volume, pos );
-         pos[1] = wrender.height;
+         pos[0] += 0.5f;
+         pos[2] += 0.5f;
+         v3_divs( pos, kres+1, pos );
+         v3_muladd( interior[0], pos, volume, pos );
+         pos[1] = 2000.0f;
          
          ray_hit hit;
          hit.dist = INFINITY;
-         u8 *dst = &img[ y*kres+x ];
+         float *dst = &img[ y*kres+x ];
 
          if( ray_world( pos, (v3f){0.0f,-1.0f,0.0f}, &hit ))
          {
-            float h = wrender.height - hit.pos[1];
-            h *= 1.0f/25.0f;
-            h = vg_clampf( h, 0.0f, 1.0f );
-            *dst = (u8)(h*255.0f);
+            *dst = hit.pos[1];
          }
          else
-            *dst = 0;
+         {
+            *dst = 0.0f;
+         }
       }
    }
 
@@ -112,8 +117,8 @@ static void water_compute_depth( boxf bounds )
 
    glGenTextures( 1, &wrender.depthmap );
    glBindTexture( GL_TEXTURE_2D, wrender.depthmap );
-   glTexImage2D( GL_TEXTURE_2D, 0, GL_RED, kres, kres, 0, 
-         GL_RED, GL_UNSIGNED_BYTE, img );
+   glTexImage2D( GL_TEXTURE_2D, 0, GL_R32F, kres, kres, 0, 
+         GL_RED, GL_FLOAT, img );
 
    vg_tex2d_mipmap();
    vg_tex2d_linear_mipmap();
@@ -123,11 +128,14 @@ static void water_compute_depth( boxf bounds )
    free( img );
    vg_success( "Done.\n" );
 }
+#endif
 
 static void water_set_surface( glmesh *surf, float height )
 {
    wrender.mdl = *surf;
    wrender.height = height;
+
+   v4_copy( (v4f){ 0.0f, 1.0f, 0.0f, height }, wrender.plane );
 }
 
 static void render_water_texture( m4x3f camera )
@@ -184,7 +192,8 @@ static void render_water_texture( m4x3f camera )
    m4x3_invert_affine( camera, inverse );
    m4x3_expand( inverse, view );
 
-   v4f clippb = { 0.0f, -1.0f, 0.0f, -(wrender.height) };
+   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 );
    clippb[3] *= -1.0f;
 
@@ -195,7 +204,7 @@ static void render_water_texture( m4x3f camera )
 
    plane_clip_projection( projection, clippb );
    m4x4_mul( projection, view, projection );
-   render_world( projection, camera );
+   render_world_depth( projection, camera );
    
    glViewport( 0, 0, vg_window_x, vg_window_y );
 }
@@ -217,18 +226,10 @@ static void render_water_surface( m4x4f pv, m4x3f camera )
          1.0f / (float)vg_window_x,
          1.0f / (float)vg_window_y });
 
-   glActiveTexture( GL_TEXTURE2 );
-   glBindTexture( GL_TEXTURE_2D, wrender.depthmap );
-   shader_water_uTexDepth( 2 );
-   shader_water_uDepthBounds( (v4f){ 
-         wrender.depthbounds[0][0],
-         wrender.depthbounds[0][2],
-         1.0f/ (wrender.depthbounds[1][0]-wrender.depthbounds[0][0]),
-         1.0f/ (wrender.depthbounds[1][2]-wrender.depthbounds[0][2])} );
+   shader_link_standard_ub( _shader_water.id, 2 );
 
    fb_bindtex( &wrender.fbdepth, 3 );
    shader_water_uTexBack( 3 );
-
    shader_water_uTime( vg_time );
    shader_water_uCamera( camera[3] );
    shader_water_uSurfaceY( wrender.height );