the better walkgrid
[carveJwlIkooP6JGAAIwe30JlM.git] / gate.h
diff --git a/gate.h b/gate.h
index c0bdbaab9ce8b0b8f1705582a43f6bcb25e079f2..6a6a0a9f91c59e37f66d38db2535850f04d88c42 100644 (file)
--- a/gate.h
+++ b/gate.h
@@ -1,40 +1,18 @@
 #ifndef GATE_H
 #define GATE_H
 
+#define VG_3D
 #include "vg/vg.h"
-
-static const float k_gatesize = 4.0f;
-
-SHADER_DEFINE( shader_gate,
-   "layout (location=0) in vec3 a_co;"
-   "uniform mat4 uPv;"
-   "uniform mat4x3 uMdl;"
-   ""
-       "void main()"
-       "{"
-               "gl_Position = uPv * vec4(uMdl * vec4( a_co, 1.0 ),1.0);"
-       "}",
-
-   /* Fragment */
-       "out vec4 FragColor;"
-       ""
-   "uniform sampler2D uTexMain;"
-   "uniform vec2 uInvRes;"
-   ""
-       "void main()"
-       "{"
-      "vec2 uv = gl_FragCoord.xy*uInvRes;"
-      "FragColor = texture( uTexMain, uv );"
-       "}"
-       ,
-       UNIFORMS({ "uPv", "uMdl", "uTexMain", "uInvRes" })
-)
+#include "model.h"
+#include "render.h"
+#include "shaders/gate.h"
 
 typedef struct teleport_gate teleport_gate;
 
 static struct
 {
-   GLuint fb, rgb, rb, vao, vbo;
+   GLuint fb, rgb, rb;
+   glmesh mdl;
 }
 grender;
 
@@ -42,6 +20,7 @@ struct teleport_gate
 { 
    v3f co;
    v4f q;
+   v2f dims;
 
    m4x3f to_world, to_local;
    teleport_gate *other;
@@ -57,90 +36,49 @@ static void gate_transform_update( teleport_gate *gate )
 
 static void gate_register(void)
 {
-   SHADER_INIT( shader_gate );
+   shader_gate_register();
 }
 
 static void gate_init(void)
 {
-   glGenFramebuffers( 1, &grender.fb );
-   glBindFramebuffer( GL_FRAMEBUFFER, grender.fb );
-
-   glGenTextures( 1, &grender.rgb );
-   glBindTexture( GL_TEXTURE_2D, grender.rgb );
-   glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, vg_window_x, vg_window_y, 
-         0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
-
-   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
-   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
-   glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, 
-         GL_TEXTURE_2D, grender.rgb, 0);
-
-   /* TODO: Check for DEPTH32f availiblity and use if possible */
+   create_renderbuffer_std( &grender.fb, &grender.rgb, &grender.rb );
 
-   glGenRenderbuffers( 1, &grender.rb );
-   glBindRenderbuffer( GL_RENDERBUFFER, grender.rb );
-   glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 
-         vg_window_x, vg_window_y );
-
-   glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
-         GL_RENDERBUFFER, grender.rb );
-
-   {
-      float ksz = k_gatesize;
-      float quad[] = { -ksz,-ksz,0.0f,  ksz, ksz,0.0f, -ksz, ksz,0.0f,
-                       -ksz,-ksz,0.0f,  ksz,-ksz,0.0f,  ksz, ksz,0.0f,
-                       -ksz,-ksz,0.0f, -ksz, ksz,0.0f,  ksz, ksz,0.0f, 
-                       -ksz,-ksz,0.0f,  ksz, ksz,0.0f,  ksz,-ksz,0.0f };
-
-      glGenVertexArrays( 1, &grender.vao );
-      glGenBuffers( 1, &grender.vbo );
-      glBindVertexArray( grender.vao );
-      glBindBuffer( GL_ARRAY_BUFFER, grender.vbo );
-      glBufferData( GL_ARRAY_BUFFER, sizeof(quad), quad, GL_STATIC_DRAW );
-      glBindVertexArray( grender.vao );
-      glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 
-            sizeof(float)*3, (void*)0 );
-      glEnableVertexAttribArray( 0 );
-      VG_CHECK_GL();
-   }
+   model *mgate = vg_asset_read( "models/rs_gate.mdl" );
+   model_unpack( mgate, &grender.mdl );
+   free( mgate );
 }
 
-static void render_world(m4x4f pv);
-
-/* 
- * http://www.terathon.com/lengyel/Lengyel-Oblique.pdf 
- */
-static void plane_clip_projection( m4x4f mat, v4f plane )
+static void gate_fb_resize(void)
 {
-   v4f c = 
-   {
-      (vg_signf(plane[0]) + mat[2][0]) / mat[0][0],
-      (vg_signf(plane[1]) + mat[2][1]) / mat[1][1],
-      -1.0f,
-      (1.0f + mat[2][2]) / mat[3][2]
-   };
-
-   v4_muls( plane, 2.0f / v4_dot(plane,c), c );
-
-   mat[0][2] = c[0];
-   mat[1][2] = c[1];
-   mat[2][2] = c[2] + 1.0f;
-   mat[3][2] = c[3];
+   resize_renderbuffer_std( &grender.fb, &grender.rgb, &grender.rb );
 }
 
-static void render_gate( teleport_gate *gate, m4x3f camera, float fov )
+static void render_gate( teleport_gate *gate, m4x3f camera )
 {
-   m4x3f transport;
+   v3f viewpos, viewdir, gatedir;
+   v3_copy( camera[3], viewpos );
+   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 );
+   if( v3_dot(v0, gatedir) >= 0.0f )
+      return;
 
+   m4x3f transport;
    m4x3_mul( gate->other->to_world, gate->to_local, transport );
    
    v3f a,b,c,d;
-   
-   float ksz = k_gatesize;
-   m4x3_mulv( gate->to_world, (v3f){-ksz,-ksz,0.0f}, a );
-   m4x3_mulv( gate->to_world, (v3f){ ksz,-ksz,0.0f}, b );
-   m4x3_mulv( gate->to_world, (v3f){ ksz, ksz,0.0f}, c );
-   m4x3_mulv( gate->to_world, (v3f){-ksz, ksz,0.0f}, d );
+
+   float sx = gate->dims[0],
+         sy = gate->dims[1];
+   m4x3_mulv( gate->to_world, (v3f){-sx,-sy,0.0f}, a );
+   m4x3_mulv( gate->to_world, (v3f){ sx,-sy,0.0f}, b );
+   m4x3_mulv( gate->to_world, (v3f){ sx, sy,0.0f}, c );
+   m4x3_mulv( gate->to_world, (v3f){-sx, sy,0.0f}, d );
 
    vg_line( a,b, 0xffffa000 );
    vg_line( b,c, 0xffffa000 );
@@ -155,10 +93,9 @@ static void render_gate( teleport_gate *gate, m4x3f camera, float fov )
    vg_line_pt3( cam_new[3], 0.3f, 0xff00ff00 );
 
    glBindFramebuffer( GL_FRAMEBUFFER, grender.fb );
-   glClearColor( 0.0f, 0.0f, 1.0f, 1.0f );
+   glClearColor( 0.11f, 0.35f, 0.37f, 1.0f );
    glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
 
-
    m4x3f inverse;
    m4x3_invert_affine( cam_new, inverse );
 
@@ -170,10 +107,7 @@ static void render_gate( teleport_gate *gate, m4x3f camera, float fov )
    surface[3] = v3_dot( surface, gate->other->co );
 
    m4x4f projection;
-   m4x4_projection( projection,
-         fov,
-         (float)vg_window_x / (float)vg_window_y, 
-         0.01f, 900.0f );
+   pipeline_projection( projection, 0.1f, 900.0f );
 
 #if 0 /* For debugging frustum */
    {
@@ -243,25 +177,39 @@ static void render_gate( teleport_gate *gate, m4x3f camera, float fov )
 
    m4x4_mul( projection, view, projection );
    
-   render_world( 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_USE( shader_gate );
+   shader_gate_use();
+
+   m4x3f full;
+   m4x3_copy( gate->to_world, full );
+   m4x3_scalev( full, (v3f){ gate->dims[0], gate->dims[1], 1.0f } );
 
-       glUniformMatrix4fv( SHADER_UNIFORM( shader_gate, "uPv" ), 
-         1, GL_FALSE, (float *)vg_pv );
-       glUniformMatrix4x3fv( SHADER_UNIFORM( shader_gate, "uMdl" ), 
-         1, GL_FALSE, (float *)gate->to_world );
+   shader_gate_uPv( vg_pv );
+   shader_gate_uMdl( full );
 
    glActiveTexture( GL_TEXTURE0 );
    glBindTexture( GL_TEXTURE_2D, grender.rgb );
-   glUniform1i( SHADER_UNIFORM( shader_gate, "uTexMain"), 0 );
-   glUniform2f( SHADER_UNIFORM( shader_gate, "uInvRes"),
+   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 );
+         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 );
 
-   glBindVertexArray( grender.vao );
-   glDrawArrays( GL_TRIANGLES, 0, 12 );
+   glDisable(GL_BLEND);
 }
 
 static int gate_intersect( teleport_gate *gate, v3f pos, v3f last )
@@ -295,7 +243,7 @@ static int gate_intersect( teleport_gate *gate, v3f pos, v3f last )
 
          v2f xy = { v3_dot( rel, vside ), v3_dot( rel, vup ) };
 
-         if( fabsf(xy[0]) <= k_gatesize && fabsf(xy[1]) <= k_gatesize )
+         if( fabsf(xy[0]) <= gate->dims[0] && fabsf(xy[1]) <= gate->dims[1] )
          {
             return 1;
          }