longjump gates
[carveJwlIkooP6JGAAIwe30JlM.git] / world_gate.h
index 9d1287711782d7570fb0c26505647beb2b9c5978..b6942519c4fe93059a9f881284dcd51db9a799d3 100644 (file)
 #include "render.h"
 #include "camera.h"
 
-#include "shaders/gatelq.h"
+#include "shaders/model_gate.h"
 #include "world_water.h"
 
-
 VG_STATIC void gate_transform_update( teleport_gate *gate )
 {
    m4x3f to_local;
@@ -32,19 +31,20 @@ VG_STATIC void world_gates_init(void)
 {
    vg_info( "world_gates_init\n" );
 
-   shader_gatelq_register();
+   shader_model_gate_register();
 
    vg_linear_clear( vg_mem.scratch );
    mdl_context *mgate = mdl_load_full( vg_mem.scratch, "models/rs_gate.mdl" );
 
    vg_acquire_thread_sync();
    {
-      mdl_unpack_glmesh( mgate, &world.mesh_gate_surface );
+      mdl_unpack_glmesh( mgate, &world_global.mesh_gate_surface );
    }
    vg_release_thread_sync();
 }
 
-VG_STATIC int render_gate( teleport_gate *gate, camera *cam )
+VG_STATIC int render_gate( world_instance *world_inside,
+                           teleport_gate *gate, camera *cam )
 {
    v3f viewdir, gatedir;
    m3x3_mulv( cam->transform, (v3f){0.0f,0.0f,-1.0f}, viewdir );
@@ -81,41 +81,40 @@ VG_STATIC int render_gate( teleport_gate *gate, camera *cam )
    }
 
    /* update gate camera */
-   static camera gate_view;
-   gate_view.fov = cam->fov;
-   gate_view.nearz = 0.1f;
-   gate_view.farz  = 2000.0f;
+   gate_camera.fov = cam->fov;
+   gate_camera.nearz = 0.1f;
+   gate_camera.farz  = 2000.0f;
 
-   m4x3_mul( gate->transport, cam->transform, gate_view.transform );
-   camera_update_view( &gate_view );
-   camera_update_projection( &gate_view );
+   m4x3_mul( gate->transport, cam->transform, gate_camera.transform );
+   camera_update_view( &gate_camera );
+   camera_update_projection( &gate_camera );
 
    /* Add special clipping plane to projection */
    v4f surface;
    m3x3_mulv( gate->recv_to_world, (v3f){0.0f,0.0f,-1.0f}, surface );
    surface[3] = v3_dot( surface, gate->co[1] );
    
-   m4x3_mulp( gate_view.transform_inverse, surface, surface );
+   m4x3_mulp( gate_camera.transform_inverse, surface, surface );
    surface[3] = -fabsf(surface[3]);
 
    if( dist < -0.5f )
-      m4x4_clip_projection( gate_view.mtx.p, surface );
+      m4x4_clip_projection( gate_camera.mtx.p, surface );
 
    /* Ready to draw with new camrea */
-   camera_finalize( &gate_view );
+   camera_finalize( &gate_camera );
 
-   vg_line_pt3( gate_view.transform[3], 0.3f, 0xff00ff00 );
+   vg_line_pt3( gate_camera.transform[3], 0.3f, 0xff00ff00 );
    {
       m4x3f gate_xform;
       m4x3_copy( gate->to_world, gate_xform );
       m4x3_scalev( gate_xform, (v3f){ gate->dims[0], gate->dims[1], 1.0f } );
    
-      shader_gatelq_use();
-      shader_gatelq_uPv( cam->mtx.pv );
-      shader_gatelq_uMdl( gate_xform );
-      shader_gatelq_uCam( cam->pos );
-      shader_gatelq_uTime( vg.time*0.25f );
-      shader_gatelq_uInvRes( (v2f){
+      shader_model_gate_use();
+      shader_model_gate_uPv( cam->mtx.pv );
+      shader_model_gate_uMdl( gate_xform );
+      shader_model_gate_uCam( cam->pos );
+      shader_model_gate_uTime( vg.time*0.25f );
+      shader_model_gate_uInvRes( (v2f){
             1.0f / (float)vg.window_x,
             1.0f / (float)vg.window_y });
 
@@ -124,25 +123,25 @@ VG_STATIC int render_gate( teleport_gate *gate, camera *cam )
       glStencilFunc( GL_ALWAYS, 1, 0xFF ); 
       glStencilMask( 0xFF );
 
-      mesh_bind( &world.mesh_gate_surface );
-      mesh_draw( &world.mesh_gate_surface );
+      mesh_bind( &world_global.mesh_gate_surface );
+      mesh_draw( &world_global.mesh_gate_surface );
 
       glClear( GL_DEPTH_BUFFER_BIT );
       glStencilFunc( GL_EQUAL, 1, 0xFF );
       glStencilMask( 0x00 ); 
    }
 
-   render_world( &gate_view );
+   render_world( world_inside, &gate_camera );
 
    {
       glDisable( GL_STENCIL_TEST );
 
-      render_water_texture( &gate_view );
+      render_water_texture( world_inside, &gate_camera );
       render_fb_bind( gpipeline.fb_main );
 
       glEnable( GL_STENCIL_TEST );
 
-      render_water_surface( &gate_view );
+      render_water_surface( world_inside, &gate_camera );
 
       glStencilMask( 0xFF );
       glStencilFunc( GL_ALWAYS, 1, 0xFF );
@@ -152,7 +151,8 @@ VG_STATIC int render_gate( teleport_gate *gate, camera *cam )
    return 1;
 }
 
-VG_STATIC int gate_intersect( teleport_gate *gate, v3f pos, v3f last )
+VG_STATIC int gate_intersect_plane( teleport_gate *gate, v3f pos, v3f last,
+                                    v2f where )
 {
    v4f surface;
    m3x3_mulv( gate->to_world, (v3f){0.0f,0.0f,-1.0f}, surface );
@@ -185,16 +185,73 @@ VG_STATIC int gate_intersect( teleport_gate *gate, v3f pos, v3f last )
          m3x3_mulv( gate->to_world, (v3f){0.0f,1.0f,0.0f}, vup );
          m3x3_mulv( gate->to_world, (v3f){1.0f,0.0f,0.0f}, vside );
 
-         v2f xy = { v3_dot( rel, vside ), v3_dot( rel, vup ) };
+         where[0] = v3_dot( rel, vside );
+         where[1] = v3_dot( rel, vup );
+
+         return 1;
+      }
+   }
+
+   return 0;
+}
+
+VG_STATIC int gate_intersect( teleport_gate *gate, v3f pos, v3f last )
+{
+   v2f xy;
 
-         if( fabsf(xy[0]) <= gate->dims[0] && fabsf(xy[1]) <= gate->dims[1] )
-         {
-            return 1;
-         }
+   if( gate_intersect_plane( gate, pos, last, xy ) )
+   {
+      if( fabsf(xy[0]) <= gate->dims[0] && fabsf(xy[1]) <= gate->dims[1] )
+      {
+         return 1;
       }
    }
 
    return 0;
 }
 
+struct gate_hit
+{
+   struct nonlocal_gate *nonlocal;
+   struct route_gate    *route;
+   teleport_gate *gate;
+};
+
+/* 
+ * Intersect all gates in the world
+ */
+VG_STATIC int world_intersect_gates( world_instance *world,
+                                     v3f pos, v3f last, struct gate_hit *hit )
+{
+   for( int i=0; i<world->gate_count; i++ )
+   {
+      struct route_gate *rg = &world->gates[i];
+      teleport_gate *gate = &rg->gate;
+
+      if( gate_intersect( gate, pos, last ) )
+      {
+         hit->gate = gate;
+         hit->nonlocal = NULL;
+         hit->route = rg;
+         return 1;
+      }
+   }
+
+   for( int i=0; i<world->nonlocalgate_count; i++ )
+   {
+      struct nonlocal_gate *nlg = &world->nonlocal_gates[i];
+      teleport_gate *gate = &nlg->gate;
+
+      if( gate_intersect( gate, pos, last ) )
+      {
+         hit->gate = gate;
+         hit->nonlocal = nlg;
+         hit->route = NULL;
+         return 1;
+     }
+   }
+
+   return 0;
+}
+
 #endif /* WORLD_GATE_H */