better water
[carveJwlIkooP6JGAAIwe30JlM.git] / water.h
diff --git a/water.h b/water.h
index c2a7122c2244c1774e9f49f428803ad6afb85ec1..ac1db00dbafaf53001728bc6253f70b855a95a49 100644 (file)
--- a/water.h
+++ b/water.h
@@ -32,7 +32,7 @@ static struct
 }
 wrender =
 {
-   .fbreflect = { .format = GL_RGB,  .div = 4 },
+   .fbreflect = { .format = GL_RGB,  .div = 3 },
    .fbdepth   = { .format = GL_RGBA, .div = 4 }
 };
 
@@ -78,18 +78,24 @@ static void water_compute_depth( boxf bounds )
    vg_info( "Computing depth map\n" );
    u8 *img = malloc( kres*kres );
 
+   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[1] += 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;
@@ -103,7 +109,9 @@ static void water_compute_depth( boxf bounds )
             *dst = (u8)(h*255.0f);
          }
          else
+         {
             *dst = 0;
+         }
       }
    }
 
@@ -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;