stuff
[carveJwlIkooP6JGAAIwe30JlM.git] / gate.h
diff --git a/gate.h b/gate.h
index 6a6a0a9f91c59e37f66d38db2535850f04d88c42..7088c7dc5c236dc03223e5b3948904a6011278d0 100644 (file)
--- a/gate.h
+++ b/gate.h
@@ -6,42 +6,61 @@
 #include "model.h"
 #include "render.h"
 #include "shaders/gate.h"
+#include "shaders/gatelq.h"
+#include "water.h"
 
 typedef struct teleport_gate teleport_gate;
 
 static struct
 {
-   GLuint fb, rgb, rb;
+   struct framebuffer fb;
    glmesh mdl;
+
+   int high_qual; /* If in high performance mode, we don't use RT's, and
+                      instead use stencil buffers.
+                      There is therefore no heat warp effect. */
 }
-grender;
+grender =
+{
+   .high_qual = 0,
+   .fb = {
+      .format = GL_RGB,
+      .div = 1
+   }
+};
 
 struct teleport_gate
 { 
-   v3f co;
-   v4f q;
+   v3f co[2];
+   v4f q[2];
    v2f dims;
 
-   m4x3f to_world, to_local;
-   teleport_gate *other;
+   m4x3f to_world, recv_to_world, transport;
 };
 
 static void gate_transform_update( teleport_gate *gate )
 {
-   q_m3x3( gate->q, gate->to_world );
-   v3_copy( gate->co, gate->to_world[3] );
+   m4x3f to_local;
+
+   q_m3x3( gate->q[0], gate->to_world );
+   v3_copy( gate->co[0], gate->to_world[3] );
    
-   m4x3_invert_affine( gate->to_world, gate->to_local );
+   m4x3_invert_affine( gate->to_world, to_local );
+
+   q_m3x3( gate->q[1], gate->recv_to_world );
+   v3_copy( gate->co[1], gate->recv_to_world[3] );
+   m4x3_mul( gate->recv_to_world, to_local, gate->transport );
 }
 
 static void gate_register(void)
 {
    shader_gate_register();
+   shader_gatelq_register();
 }
 
 static void gate_init(void)
 {
-   create_renderbuffer_std( &grender.fb, &grender.rgb, &grender.rb );
+   fb_init( &grender.fb );
 
    model *mgate = vg_asset_read( "models/rs_gate.mdl" );
    model_unpack( mgate, &grender.mdl );
@@ -50,7 +69,7 @@ static void gate_init(void)
 
 static void gate_fb_resize(void)
 {
-   resize_renderbuffer_std( &grender.fb, &grender.rgb, &grender.rb );
+   fb_resize( &grender.fb );
 }
 
 static void render_gate( teleport_gate *gate, m4x3f camera )
@@ -60,17 +79,14 @@ static void render_gate( teleport_gate *gate, m4x3f camera )
    m3x3_mulv( camera, (v3f){0.0f,0.0f,-1.0f}, viewdir );
    m3x3_mulv( gate->to_world, (v3f){0.0f,0.0f,-1.0f}, gatedir );
 
-   if( v3_dot(viewdir, gatedir) <= 0.0f )
-      return;
-
    v3f v0;
-   v3_sub( viewpos, gate->co, v0 );
+   v3_sub( viewpos, gate->co[0], v0 );
    if( v3_dot(v0, gatedir) >= 0.0f )
       return;
 
-   m4x3f transport;
-   m4x3_mul( gate->other->to_world, gate->to_local, transport );
-   
+   if( v3_dist( viewpos, gate->co[0] ) > 100.0f )
+      return;
+
    v3f a,b,c,d;
 
    float sx = gate->dims[0],
@@ -85,17 +101,17 @@ static void render_gate( teleport_gate *gate, m4x3f camera )
    vg_line( c,d, 0xffffa000 );
    vg_line( d,a, 0xffffa000 );
 
-   vg_line( gate->co, gate->other->co, 0xffffffff );
+   vg_line2( gate->co[0], gate->co[1], 0xff0000ff, 0x00000000 );
 
    m4x3f cam_new;
-   m4x3_mul( transport, camera, cam_new );
+   m4x3_mul( gate->transport, camera, cam_new );
    
    vg_line_pt3( cam_new[3], 0.3f, 0xff00ff00 );
 
-   glBindFramebuffer( GL_FRAMEBUFFER, grender.fb );
-   glClearColor( 0.11f, 0.35f, 0.37f, 1.0f );
-   glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
-
+   m4x3f gate_xform;
+   m4x3_copy( gate->to_world, gate_xform );
+   m4x3_scalev( gate_xform, (v3f){ gate->dims[0], gate->dims[1], 1.0f } );
+   
    m4x3f inverse;
    m4x3_invert_affine( cam_new, inverse );
 
@@ -103,120 +119,100 @@ static void render_gate( teleport_gate *gate, m4x3f camera )
    m4x3_expand( inverse, view );
 
    v4f surface;
-   m3x3_mulv( gate->other->to_world, (v3f){0.0f,0.0f,-1.0f}, surface );
-   surface[3] = v3_dot( surface, gate->other->co );
+   m3x3_mulv( gate->recv_to_world, (v3f){0.0f,0.0f,-1.0f}, surface );
+   surface[3] = v3_dot( surface, gate->co[1] );
 
    m4x4f projection;
    pipeline_projection( projection, 0.1f, 900.0f );
+   
+   m4x3_mulp( inverse, surface, surface );
+   surface[3] = -fabsf(surface[3]);
+   plane_clip_projection( projection, surface );
 
-#if 0 /* For debugging frustum */
-   {
-      m4x4f devm;
-      m4x4_mul( projection, view, devm );
-      m4x4_inv( devm, devm );
-      
-      v4f corners[] =
-      {
-         {-1,-1,-1, 1}, { 1,-1,-1, 1}, { 1, 1,-1, 1}, {-1, 1,-1, 1},
-         {-1,-1, 1, 1}, { 1,-1, 1, 1}, { 1, 1, 1, 1}, {-1, 1, 1, 1}
-      };
+   m4x4_mul( projection, view, projection );
 
-      for( int i=0; i<vg_list_size(corners); i++ )
-      {
-         m4x4_mulv( devm, corners[i], corners[i] );
-         v3_muls( corners[i], 1.0f/corners[i][3], corners[i] );
-      }
+   if( grender.high_qual )
+   {
+      fb_use( &grender.fb );
+      glClearColor( 0.11f, 0.35f, 0.37f, 1.0f );
+      glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
+   }
+   else
+   {
+      shader_gatelq_use();
+      shader_gatelq_uPv( vg_pv );
+      shader_gatelq_uMdl( gate_xform );
+      shader_gatelq_uCam( viewpos );
+      shader_gatelq_uTime( vg_time*0.25f );
+      shader_gatelq_uInvRes( (v2f){
+            1.0f / (float)vg_window_x,
+            1.0f / (float)vg_window_y });
+
+      glEnable( GL_STENCIL_TEST );
+      glStencilOp( GL_KEEP, GL_KEEP, GL_REPLACE );  
+      glStencilFunc( GL_ALWAYS, 1, 0xFF ); 
+      glStencilMask( 0xFF );
+
+      mesh_bind( &grender.mdl );
+      mesh_draw( &grender.mdl );
+
+      glClear( GL_DEPTH_BUFFER_BIT );
+      glStencilFunc( GL_EQUAL, 1, 0xFF );
+      glStencilMask( 0x00 ); 
+   }
 
-      vg_line( corners[0], corners[1], 0xffffffff );
-      vg_line( corners[1], corners[2], 0xffffffff );
-      vg_line( corners[2], corners[3], 0xffffffff );
-      vg_line( corners[3], corners[0], 0xffffffff );
-      vg_line( corners[4], corners[5], 0xffffffff );
-      vg_line( corners[5], corners[6], 0xffffffff );
-      vg_line( corners[6], corners[7], 0xffffffff );
-      vg_line( corners[7], corners[4], 0xffffffff );
-      vg_line( corners[0], corners[4], 0xffffffff );
-      vg_line( corners[1], corners[5], 0xffffffff );
-      vg_line( corners[2], corners[6], 0xffffffff );
-      vg_line( corners[3], corners[7], 0xffffffff );
-      
-      v3f clipped[4];
-      for( int i=0; i<4; i++ )
-      {
-         v3f v0, c, delta, p0;
-         v3_sub( corners[4+i],corners[0+i], v0 );
-         v3_normalize(v0);
+   render_world( projection, cam_new );
+   
+   if( grender.high_qual )
+   {
+      /*
+       * TODO: Need to find a way to draw a stencil buffer into the water 
+       *       rendering 
+       */
 
-         v3_muls( surface, surface[3], c );
-         v3_sub( c, corners[0+i], delta );
+      render_water_texture( cam_new );
+      fb_use( &grender.fb );
 
-         float t = v3_dot(delta, surface) / v3_dot(surface, v0);
-         v3_muladds( corners[0+i], v0, t, clipped[i] );
-      }
+      render_water_surface( projection, cam_new );
+      fb_use( NULL );
 
-      vg_line( clipped[0], clipped[1], 0xff0000ff );
-      vg_line( clipped[1], clipped[2], 0xff0000ff );
-      vg_line( clipped[2], clipped[3], 0xff0000ff );
-      vg_line( clipped[3], clipped[0], 0xff0000ff );
+      shader_gate_use();
 
-      m4x3_mulv( gate->other->to_world, (v3f){-2.0f,-2.0f,0.0f}, a );
-      m4x3_mulv( gate->other->to_world, (v3f){ 2.0f,-2.0f,0.0f}, b );
-      m4x3_mulv( gate->other->to_world, (v3f){ 2.0f, 2.0f,0.0f}, c );
-      m4x3_mulv( gate->other->to_world, (v3f){-2.0f, 2.0f,0.0f}, d );
+      shader_gate_uPv( vg_pv );
+      shader_gate_uMdl( gate_xform );
 
-      vg_line( clipped[0], a, 0xff0000ff );
-      vg_line( clipped[1], b, 0xff0000ff );
-      vg_line( clipped[2], c, 0xff0000ff );
-      vg_line( clipped[3], d, 0xff0000ff );
-   }
-#endif
-   
-   m4x3_mulp( inverse, surface, surface );
-   surface[3] = -fabsf(surface[3]);
-   plane_clip_projection( projection, surface );
+      fb_bindtex( &grender.fb, 0 );
 
-   m4x4_mul( projection, view, projection );
-   
-   render_world( projection, cam_new );
-   render_water_texture( cam_new );
-   glBindFramebuffer( GL_FRAMEBUFFER, grender.fb );
-   render_water_surface( projection );
-   glBindFramebuffer( GL_FRAMEBUFFER, 0 );
-
-   shader_gate_use();
-
-   m4x3f full;
-   m4x3_copy( gate->to_world, full );
-   m4x3_scalev( full, (v3f){ gate->dims[0], gate->dims[1], 1.0f } );
-
-   shader_gate_uPv( vg_pv );
-   shader_gate_uMdl( full );
-
-   glActiveTexture( GL_TEXTURE0 );
-   glBindTexture( GL_TEXTURE_2D, grender.rgb );
-   shader_gate_uCam( viewpos );
-   shader_gate_uTexMain( 0 );
-   shader_gate_uTexWater( 1 );
-   shader_gate_uTime( vg_time*0.25f );
-   shader_gate_uInvRes( (v2f){
-         1.0f / (float)vg_window_x,
-         1.0f / (float)vg_window_y });
-   
-   glEnable(GL_BLEND);
-   glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
-   glBlendEquation(GL_FUNC_ADD);
+      shader_gate_uCam( viewpos );
+      shader_gate_uTexMain( 0 );
+      shader_gate_uTexWater( 1 );
+      shader_gate_uTime( vg_time*0.25f );
+      shader_gate_uInvRes( (v2f){
+            1.0f / (float)vg_window_x,
+            1.0f / (float)vg_window_y });
+      
+      glEnable(GL_BLEND);
+      glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
+      glBlendEquation(GL_FUNC_ADD);
 
-   mesh_bind( &grender.mdl );
-   mesh_draw( &grender.mdl );
+      mesh_bind( &grender.mdl );
+      mesh_draw( &grender.mdl );
 
-   glDisable(GL_BLEND);
+      glDisable(GL_BLEND);
+   }
+   else
+   {
+      glStencilMask( 0xFF );
+      glStencilFunc( GL_ALWAYS, 1, 0xFF );
+      glDisable( GL_STENCIL_TEST );
+   }
 }
 
 static int gate_intersect( teleport_gate *gate, v3f pos, v3f last )
 {
    v4f surface;
    m3x3_mulv( gate->to_world, (v3f){0.0f,0.0f,-1.0f}, surface );
-   surface[3] = v3_dot( surface, gate->co );
+   surface[3] = v3_dot( surface, gate->co[0] );
 
    v3f v0, c, delta, p0;
    v3_sub( pos, last, v0 );
@@ -235,7 +231,7 @@ static int gate_intersect( teleport_gate *gate, v3f pos, v3f last )
       {
          v3f local, rel;
          v3_muladds( last, v0, t, local );
-         v3_sub( gate->co, local, rel );
+         v3_sub( gate->co[0], local, rel );
 
          v3f vup, vside;
          m3x3_mulv( gate->to_world, (v3f){0.0f,1.0f,0.0f}, vup );