i hope your hapy
[carveJwlIkooP6JGAAIwe30JlM.git] / world_gate.h
index bd4510b8aad89dd301235cad21b176e95a916d6c..f2a383c1f2856aea16a060f686d9e2f638bb52fa 100644 (file)
@@ -7,13 +7,14 @@
 
 #include "common.h"
 #include "model.h"
+#include "entity.h"
 #include "render.h"
 #include "camera.h"
 
 #include "shaders/model_gate.h"
 #include "world_water.h"
 
-VG_STATIC void gate_transform_update( teleport_gate *gate )
+VG_STATIC void gate_transform_update( ent_gate *gate )
 {
    m4x3f to_local, recv_to_world;
 
@@ -26,7 +27,8 @@ VG_STATIC void gate_transform_update( teleport_gate *gate )
    v3_copy( gate->co[1], recv_to_world[3] );
    m4x3_mul( recv_to_world, to_local, gate->transport );
 
-   m4x3_scalev( gate->to_world, (v3f){ gate->dims[0], gate->dims[1], 1.0f } );
+   m4x3_scalev( gate->to_world, (v3f){ gate->dimensions[0], 
+                                       gate->dimensions[1], 1.0f } );
 }
 
 VG_STATIC void world_gates_init(void)
@@ -36,17 +38,36 @@ VG_STATIC void world_gates_init(void)
    shader_model_gate_register();
 
    vg_linear_clear( vg_mem.scratch );
-   mdl_context *mgate = mdl_load_full( vg_mem.scratch, "models/rs_gate.mdl" );
+
+   mdl_context mgate;
+   mdl_open( &mgate, "models/rs_gate.mdl", vg_mem.scratch );
+   mdl_load_metadata_block( &mgate, vg_mem.scratch );
+   mdl_load_mesh_block( &mgate, vg_mem.scratch );
+
+   mdl_mesh *surface = mdl_find_mesh( &mgate, "rs_gate" );
+   mdl_submesh *sm = mdl_arritm(&mgate.submeshs,surface->submesh_start);
+   world_global.sm_gate_surface = *sm;
+
+   const char *names[] = { "rs_gate_marker", "rs_gate_marker.001", 
+                           "rs_gate_marker.002", "rs_gate_marker.003" };
+
+   for( int i=0; i<4; i++ ){
+      mdl_mesh *marker = mdl_find_mesh( &mgate, names[i] );
+      sm = mdl_arritm( &mgate.submeshs, marker->submesh_start );
+      world_global.sm_gate_marker[i] = *sm;
+   }
+
+   mdl_close( &mgate );
 
    vg_acquire_thread_sync();
    {
-      mdl_unpack_glmesh( mgate, &world_global.mesh_gate_surface );
+      mdl_unpack_glmesh( &mgate, &world_global.mesh_gate );
    }
    vg_release_thread_sync();
 }
 
 VG_STATIC int render_gate( world_instance *world_inside,
-                           teleport_gate *gate, camera *cam )
+                           ent_gate *gate, camera *cam )
 {
    v3f viewdir, gatedir;
    m3x3_mulv( cam->transform, (v3f){0.0f,0.0f,-1.0f}, viewdir );
@@ -109,6 +130,9 @@ VG_STATIC int render_gate( world_instance *world_inside,
       shader_model_gate_uPv( cam->mtx.pv );
       shader_model_gate_uMdl( gate->to_world );
       shader_model_gate_uCam( cam->pos );
+
+      /* TODO(ART IMPROVEMENT): animate alpha of this? */
+      shader_model_gate_uColour( (v4f){0.0f,1.0f,0.0f,0.0f} );
       shader_model_gate_uTime( vg.time*0.25f );
       shader_model_gate_uInvRes( (v2f){
             1.0f / (float)vg.window_x,
@@ -119,8 +143,8 @@ VG_STATIC int render_gate( world_instance *world_inside,
       glStencilFunc( GL_ALWAYS, 1, 0xFF ); 
       glStencilMask( 0xFF );
 
-      mesh_bind( &world_global.mesh_gate_surface );
-      mesh_draw( &world_global.mesh_gate_surface );
+      mesh_bind( &world_global.mesh_gate );
+      mdl_draw_submesh( &world_global.sm_gate_surface );
 
       glClear( GL_DEPTH_BUFFER_BIT );
       glStencilFunc( GL_EQUAL, 1, 0xFF );
@@ -147,8 +171,8 @@ VG_STATIC int render_gate( world_instance *world_inside,
    return 1;
 }
 
-VG_STATIC int gate_intersect_plane( teleport_gate *gate, v3f pos, v3f last,
-                                    v2f where )
+VG_STATIC int gate_intersect_plane( ent_gate *gate, 
+                                    v3f pos, v3f last, v2f where )
 {
    v4f surface;
    q_mulv( gate->q[0], (v3f){0.0f,0.0f,-1.0f}, surface );
@@ -168,11 +192,9 @@ VG_STATIC int gate_intersect_plane( teleport_gate *gate, v3f pos, v3f last,
 
    float d = v3_dot( surface, v0 );
 
-   if( d > 0.00001f )
-   {
+   if( d > 0.00001f ){
       float t = v3_dot(delta, surface) / d;
-      if( t >= 0.0f && t <= l )
-      {
+      if( t >= 0.0f && t <= l ){
          v3f local, rel;
          v3_muladds( last, v0, t, local );
          v3_sub( gate->co[0], local, rel );
@@ -190,14 +212,12 @@ VG_STATIC int gate_intersect_plane( teleport_gate *gate, v3f pos, v3f last,
    return 0;
 }
 
-VG_STATIC int gate_intersect( teleport_gate *gate, v3f pos, v3f last )
+VG_STATIC int gate_intersect( ent_gate *gate, v3f pos, v3f last )
 {
    v2f xy;
 
-   if( gate_intersect_plane( gate, pos, last, xy ) )
-   {
-      if( fabsf(xy[0]) <= 1.0f && fabsf(xy[1]) <= 1.0f )
-      {
+   if( gate_intersect_plane( gate, pos, last, xy ) ){
+      if( fabsf(xy[0]) <= 1.0f && fabsf(xy[1]) <= 1.0f ){
          return 1;
       }
    }
@@ -205,48 +225,23 @@ VG_STATIC int gate_intersect( teleport_gate *gate, v3f pos, v3f last )
    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 )
+VG_STATIC ent_gate *world_intersect_gates( world_instance *world,
+                                           v3f pos, v3f last )
 {
-   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( u32 i=0; i<mdl_arrcount(&world->ent_gate); i++ ){
+      ent_gate *gate = mdl_arritm( &world->ent_gate, i );
+      if( gate->type == k_gate_type_unlinked )
+         continue;
 
-   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;
-     }
+      if( gate_intersect( gate, pos, last ) ){
+         return gate;
+      }
    }
 
-   return 0;
+   return NULL;
 }
 
 #endif /* WORLD_GATE_H */