routes
authorhgn <hgodden00@gmail.com>
Mon, 1 Aug 2022 00:29:25 +0000 (01:29 +0100)
committerhgn <hgodden00@gmail.com>
Mon, 1 Aug 2022 00:29:25 +0000 (01:29 +0100)
14 files changed:
blender_export.py
common.h
gate.h
main.c
model.h
models/mp_dev.mdl
player.h
scene.h
shaders/route.fs [new file with mode: 0644]
shaders/route.h [new file with mode: 0644]
traffic.h
vg.conf
world.h
world_routes.h

index e52757b16d6edbbb1b12e3ca3456376c59ae3463..6fadae28decb6428fee5dc197795724c75884b17 100644 (file)
@@ -113,7 +113,8 @@ class classtype_route_node(Structure):
 class classtype_route(Structure):
    _pack_ = 1
    _fields_ = [("pstr_name",c_uint32),
-               ("id_start",c_uint32)]
+               ("id_start",c_uint32),
+               ("colour",c_float*3)]
 
 # Exporter
 # ==============================================================================
@@ -311,8 +312,12 @@ def write_model(name):
          entdata_length += sizeof( classtype_route )
          r = classtype_route()
          r.pstr_name = emplace_string("not-implemented")
+         r.colour[0] = obj.cv_data.colour[0]
+         r.colour[1] = obj.cv_data.colour[1]
+         r.colour[2] = obj.cv_data.colour[2]
+
          if obj.cv_data.target != None: 
-            r.target = obj.cv_data.target.cv_data.uid
+            r.id_start = obj.cv_data.target.cv_data.uid
 
          entdata_buffer += [r]
 
@@ -574,29 +579,14 @@ def cv_draw():
    def drawbpath(o0,o1,c0,c1):
       drawsbpath(o0,o1,c0,c1,1.0,1.0)
 
-   def drawbline(o0,o1,c0,c1):
+   def drawbline(p0,p1,c0,c1):
       nonlocal verts, colours
-      verts += [o0.location]
-      verts += [o1.location]
+      verts += [p0,p1]
       colours += [c0,c1]
-   
-   for obj in bpy.context.collection.objects:
-      if obj.cv_data.classtype == 'k_classtype_gate' and False:
-         if obj.cv_data.target != None:
-            p0 = obj.location
-            p1 = obj.cv_data.target.location
 
-            for i in range(20):
-               t = i/20.0
-               t1 = (i+0.5)/20.0
-               
-               pa = p0*t+p1*(1.0-t)
-               pb = p0*t1+p1*(1.0-t1)
-
-               verts += [(pa[0],pa[1],pa[2])]
-               verts += [(pb[0],pb[1],pb[2])]
-               colours += [(0,1,0,1.0),(1,0,0,1.0)]
+   for obj in bpy.context.collection.objects:
 
+      if obj.cv_data.classtype == 'k_classtype_gate':
          if obj.type == 'MESH':
             dims = obj.data.cv_data.v0
          else:
@@ -624,6 +614,26 @@ def cv_draw():
             verts += [(v1[0],v1[1],v1[2])]
             colours += [(1,1,0,1),(1,1,0,1)]
 
+         sw = (0.4,0.4,0.4,0.2)
+         if obj.cv_data.target != None:
+            drawbline( obj.location, obj.cv_data.target.location, sw,sw )
+
+      elif obj.cv_data.classtype == 'k_classtype_route_node':
+         sw = Vector((0.4,0.4,0.4,0.2))
+         sw2 = Vector((1.5,0.2,0.2,0.0))
+         if obj.cv_data.target != None:
+            drawbpath( obj, obj.cv_data.target, sw, sw )
+         if obj.cv_data.target1 != None:
+            drawbpath( obj, obj.cv_data.target1, sw, sw )
+
+         drawbhandle( obj,  1.0, (0.8,0.8,0.8,1.0) )
+         drawbhandle( obj, -1.0, (0.4,0.4,0.4,1.0) )
+
+         p1 = obj.location+ \
+               obj.matrix_world.to_quaternion() @ Vector((0,0,-6+1.5))
+         drawbline( obj.location, p1, sw,sw2 )
+
+
       elif obj.cv_data.classtype == 'k_classtype_block':
          a = obj.data.cv_data.v0
          b = obj.data.cv_data.v1
@@ -686,7 +696,7 @@ def cv_draw():
             verts += [(v0[0],v0[1],v0[2])]
             verts += [(v1[0],v1[1],v1[2])]
             colours += [(0,1,1,1),(0,1,1,1)]
-
+      
       elif obj.cv_data.classtype == 'k_classtype_route':
          vs = [None]*2
          vs[0] = obj.location
@@ -706,6 +716,13 @@ def cv_draw():
          loop_complete = False
 
          while si > 0:
+            if stack_i[si-1] == 2:
+               si -= 1
+               continue
+
+               if si == 0: # Loop failed to complete
+                  break
+
             node = stack[si-1]
 
             targets = [None,None]
@@ -713,7 +730,7 @@ def cv_draw():
 
             if node.cv_data.classtype == 'k_classtype_route_node':
                targets[1] = node.cv_data.target1
-
+            
             nextnode = targets[stack_i[si-1]]
             stack_i[si-1] += 1
 
@@ -734,12 +751,6 @@ def cv_draw():
                   si += 1
                   continue
 
-            if stack_i[si-1] == 2:
-               si -= 1
-
-               if si == 0: # Loop failed to complete
-                  break
-
          if loop_complete:
             course_colours = [Vector((0,0.8,0.2,1.0)),  \
                               Vector((0,0.3,0.9,1.0)),  \
@@ -809,6 +820,9 @@ class CV_OBJ_SETTINGS(bpy.types.PropertyGroup):
    target1: bpy.props.PointerProperty( type=bpy.types.Object, name="target1", \
          poll=cv_poll_target )
 
+   colour: bpy.props.FloatVectorProperty(name="colour",subtype='COLOR',\
+                                         min=0.0,max=1.0)
+
    classtype: bpy.props.EnumProperty(
       name="Format", 
       items = [
@@ -849,6 +863,7 @@ class CV_OBJ_PANEL(bpy.types.Panel):
 
       elif active_object.cv_data.classtype == 'k_classtype_route':
          _.layout.prop( active_object.cv_data, "target" )
+         _.layout.prop( active_object.cv_data, "colour" )
 
       elif active_object.cv_data.classtype == 'k_classtype_block':
          mesh = active_object.data
index 952b51d8b6b6053db9580acbb1d02740dc04e2ff..5e2f48298dbdafcb43c784d532b87b799f3c794e 100644 (file)
--- a/common.h
+++ b/common.h
@@ -32,4 +32,16 @@ struct ray_hit
    v3f pos, normal;
 };
 
+/* TODO: he needs a home somewhere */
+static void eval_bezier_time( v3f p0, v3f p1, v3f h0, v3f h1, float t, v3f p )
+{
+   float tt = t*t,
+         ttt = tt*t;
+
+   v3_muls( p1, ttt, p );
+   v3_muladds( p, h1, 3.0f*tt  -3.0f*ttt, p );
+   v3_muladds( p, h0, 3.0f*ttt -6.0f*tt  +3.0f*t, p );
+   v3_muladds( p, p0, 3.0f*tt  -ttt -3.0f*t +1.0f, p );
+}
+
 #endif /* COMMON_H */
diff --git a/gate.h b/gate.h
index a370d81dde091a61c7c0f34235a158af48df33ef..eb0f335782b0ff367c2d1685548a4263e24cf494 100644 (file)
--- a/gate.h
+++ b/gate.h
@@ -72,7 +72,7 @@ static void gate_fb_resize(void)
    fb_resize( &grender.fb );
 }
 
-static void render_gate( teleport_gate *gate, m4x3f camera )
+static int render_gate( teleport_gate *gate, m4x3f camera )
 {
    v3f viewpos, viewdir, gatedir;
    v3_copy( camera[3], viewpos );
@@ -82,10 +82,10 @@ static void render_gate( teleport_gate *gate, m4x3f camera )
    v3f v0;
    v3_sub( viewpos, gate->co[0], v0 );
    if( v3_dot(v0, gatedir) >= 0.0f )
-      return;
+      return 0;
 
    if( v3_dist( viewpos, gate->co[0] ) > 100.0f )
-      return;
+      return 0;
 
    v3f a,b,c,d;
 
@@ -214,6 +214,8 @@ static void render_gate( teleport_gate *gate, m4x3f camera )
       glStencilFunc( GL_ALWAYS, 1, 0xFF );
       glDisable( GL_STENCIL_TEST );
    }
+
+   return 1;
 }
 
 static int gate_intersect( teleport_gate *gate, v3f pos, v3f last )
diff --git a/main.c b/main.c
index e053c42177fb515e180930775d1c2486e251b948..a2a1f3a8a1151ec18293cbdbac7fb6e1e1dc28b9 100644 (file)
--- a/main.c
+++ b/main.c
@@ -240,20 +240,15 @@ static void render_main_game(void)
    /* 
     * Draw world
     */
-
+   
    render_world( vg_pv, player.camera );
    render_water_texture( player.camera );
 
    glBindFramebuffer( GL_FRAMEBUFFER, 0 );
    render_water_surface( vg_pv, player.camera );
 
-   vg_tex2d_bind( &tex_water, 1 );
-
-   for( int i=0; i<world.gate_count; i++ )
-   {
-      render_gate( &world.gates[i], player.camera );
-   }
-
+   vg_tex2d_bind( &tex_water, 1 ); /*TODO: ?*/
+   render_world_gates( vg_pv, player.camera );
    
    /* Copy the RGB of what we have into the background buffer */
    glBindFramebuffer( GL_READ_FRAMEBUFFER, 0 );
diff --git a/model.h b/model.h
index f8b09b85fcbdb2af8f8d5ae6f0753b6114bdc0dc..ad9dfeb53ce6e782125243df58b27a6f7ba49619 100644 (file)
--- a/model.h
+++ b/model.h
@@ -120,6 +120,7 @@ struct classtype_route
 {
    u32 pstr_name;
    u32 id_start;
+   v3f colour;
 };
 
 #pragma pack(pop)
index 6e1b7045adee921c38924bd203dc9c78762f0946..1768051983ff41beacb94cdc6a575e31f5dc9a62 100644 (file)
Binary files a/models/mp_dev.mdl and b/models/mp_dev.mdl differ
index b70717eb5076b0fbc8aa3cae33f07f47fe5f29c3..b05714e4e893209fe2198628700419d72c600c50 100644 (file)
--- a/player.h
+++ b/player.h
@@ -33,7 +33,7 @@ static int walk_grid_iterations = 1;
 static struct gplayer
 {
    /* Physics */
-   rigidbody rb, collide_front, collide_back;
+   rigidbody rb, collide_front, collide_back, rb_gate_frame;
 
    v3f a, v_last, m, bob, vl;
 
@@ -463,12 +463,14 @@ static void player_physics(void)
       {
          v3_add( manifold[i].n, surface_avg, surface_avg );
 
+#if 0
          if( manifold[i].element_id <= world.sm_geo_std_oob.vertex_count )
          {
             player.is_dead = 1;
             character_ragdoll_copypose( &player.mdl, player.rb.v );
             return;
          }
+#endif
       }
 
       v3_normalize( surface_avg );
@@ -600,9 +602,10 @@ static void player_do_motion(void)
    /* 
     * Gate intersection, by tracing a line over the gate planes 
     */
-   for( int i=0; i<world.gate_count; i++ )
+   for( int i=0; i<world.routes.gate_count; i++ )
    {
-      teleport_gate *gate = &world.gates[i];
+      struct route_gate *rg = &world.routes.gates[i];
+      teleport_gate *gate = &rg->gate;
 
       if( gate_intersect( gate, player.rb.co, prevco ) )
       {
@@ -616,7 +619,9 @@ static void player_do_motion(void)
          v4f transport_rotation;
          m3x3_q( gate->transport, transport_rotation );
          q_mul( transport_rotation, player.rb.q, player.rb.q );
-
+         
+         world_routes_activate_gate( i );
+         player.rb_gate_frame = player.rb;
          break;
       }
    }
@@ -1980,6 +1985,8 @@ static int reset_player( int argc, char const *argv[] )
    
    rb_update_transform( &player.rb );
    m3x3_mulv( player.rb.to_world, (v3f){ 0.0f, 0.0f, -1.2f }, player.rb.v );
+
+   player.rb_gate_frame = player.rb;
    return 1;
 }
 
@@ -1990,7 +1997,15 @@ static void player_update(void)
             player.land_target_colours[i], 0.25f);
 
    if( vg_get_axis("grabl")>0.0f)
-      reset_player(0,NULL);
+   {
+      player.rb = player.rb_gate_frame;
+      player.is_dead = 0;
+      player.in_air = 1;
+      m3x3_identity( player.vr );
+
+      player.mdl.shoes[0] = 1;
+      player.mdl.shoes[1] = 1;
+   }
 
    if( vg_get_button_down( "switchmode" ) )
    {
diff --git a/scene.h b/scene.h
index aaecfcce803f3fe04b6af604b83600bb002ac071..b4deaa95bfc96c6ab56095dfe0e2bd1e1d6edb90 100644 (file)
--- a/scene.h
+++ b/scene.h
@@ -106,7 +106,6 @@ static void *buffer_fix( void *buffer, u32 count, u32 *cap, size_t emsize )
 /* 
  * Append a model into the scene with a given transform
  */
-
 static void scene_add_submesh( scene *pscene, mdl_header *mdl, 
       mdl_submesh *sm, m4x3f transform )
 {
@@ -152,6 +151,30 @@ static void scene_add_submesh( scene *pscene, mdl_header *mdl,
    pscene->indice_count += sm->indice_count;
 }
 
+/*
+ * One by one adders for simplified access (mostly procedural stuff)
+ */
+static void scene_push_tri( scene *pscene, u32 tri[3] )
+{
+   pscene->indices = buffer_reserve( pscene->indices, pscene->indice_count,
+         &pscene->indice_cap, 3, sizeof(u32) );
+
+   u32 *dst = &pscene->indices[pscene->indice_count];
+   dst[0] = tri[0];
+   dst[1] = tri[1];
+   dst[2] = tri[2];
+
+   pscene->indice_count += 3;
+}
+
+static void scene_push_vert( scene *pscene, mdl_vert *v )
+{
+   pscene->verts = buffer_reserve( pscene->verts, pscene->vertex_count, 
+         &pscene->vertex_cap, 1, sizeof(mdl_vert) );
+
+   pscene->verts[pscene->vertex_count ++] = *v;
+}
+
 static void scene_copy_slice( scene *pscene, mdl_submesh *sm )
 {
    sm->indice_start = pscene->submesh.indice_start;
@@ -194,11 +217,15 @@ static void scene_draw( scene *pscene )
    mesh_drawn( 0, pscene->indice_count );
 }
 
-static void scene_free( scene *pscene )
+static void scene_free_offline_buffers( scene *pscene )
 {
    free( pscene->verts );
    free( pscene->indices );
+}
 
+static void scene_free( scene *pscene )
+{
+   scene_free_offline_buffers( pscene );
    /* TODO: bvh */
 }
 
diff --git a/shaders/route.fs b/shaders/route.fs
new file mode 100644 (file)
index 0000000..dfaa7f9
--- /dev/null
@@ -0,0 +1,56 @@
+out vec4 FragColor;
+
+uniform sampler2D uTexGarbage;
+uniform sampler2D uTexGradients;
+uniform vec3 uCamera;
+uniform vec4 uColour;
+
+in vec4 aColour;
+in vec2 aUv;
+in vec3 aNorm;
+in vec3 aCo;
+in vec3 aWorldCo;
+
+#include "common_world.glsl"
+
+void main()
+{
+   vec3 vfrag = vec3(0.5,0.5,0.5);
+
+   // ws modulation
+   vec4 wgarbage = texture( uTexGarbage, aCo.xz * 0.015 );
+
+   // Creating normal patches
+   vec3 modnorm = (wgarbage.rgb-0.4) * 1.4;
+   vec3 qnorm = normalize(floor(aNorm*4.0+modnorm)*0.25) + vec3(0.001,0.0,0.0);
+
+   vec3 tangent0 = normalize(cross(qnorm,vec3(0.0,1.0,0.0)));
+   vec3 tangent1 = cross(qnorm,tangent0);
+   vec2 uvdiffuse = vec2( dot(tangent0,aCo), dot(tangent1,aCo) ) * 0.035;
+   
+   // Patch local noise
+   vec4 rgarbage = texture( uTexGarbage, uvdiffuse );
+
+   vfrag = pow(uColour.rgb,vec3(1.0/2.2));
+   vfrag -= rgarbage.a*0.1;
+
+   if( wgarbage.g < 0.3 )
+      discard;
+
+   if( g_light_preview == 1 )
+   {
+      vfrag = vec3(0.5);
+   }
+
+   // Lighting
+   vec3 halfview = uCamera - aWorldCo;
+   float fdist = length( halfview );
+   halfview /= fdist;
+
+   vfrag = do_light_diffuse( vfrag, qnorm );
+   vfrag = do_light_spec( vfrag, qnorm, halfview, 0.1 );
+   vfrag = do_light_shadowing( vfrag );
+   vfrag = apply_fog( vfrag, fdist );
+
+   FragColor = vec4(vfrag, 1.0 );
+}
diff --git a/shaders/route.h b/shaders/route.h
new file mode 100644 (file)
index 0000000..650f8a9
--- /dev/null
@@ -0,0 +1,241 @@
+#ifndef SHADER_route_H
+#define SHADER_route_H
+static void shader_route_link(void);
+static void shader_route_register(void);
+static struct vg_shader _shader_route = {
+   .name = "route",
+   .link = shader_route_link,
+   .vs = 
+{
+.orig_file = "../shaders/standard.vs",
+.static_src = 
+"layout (location=0) in vec3 a_co;\n"
+"layout (location=1) in vec3 a_norm;\n"
+"layout (location=2) in vec4 a_colour;\n"
+"layout (location=3) in vec2 a_uv;\n"
+"\n"
+"#line      2        0 \n"
+"\n"
+"uniform mat4 uPv;\n"
+"uniform mat4x3 uMdl;\n"
+"\n"
+"out vec4 aColour;\n"
+"out vec2 aUv;\n"
+"out vec3 aNorm;\n"
+"out vec3 aCo;\n"
+"out vec3 aWorldCo;\n"
+"\n"
+"void main()\n"
+"{\n"
+"   vec3 world_pos = uMdl * vec4(a_co,1.0);\n"
+"   gl_Position = uPv * vec4( world_pos, 1.0 );\n"
+"   aColour = a_colour;\n"
+"   aUv = a_uv;\n"
+"   aNorm = mat3(uMdl) * a_norm;\n"
+"   aCo = a_co;\n"
+"   aWorldCo = world_pos;\n"
+"}\n"
+""},
+   .fs = 
+{
+.orig_file = "../shaders/route.fs",
+.static_src = 
+"out vec4 FragColor;\n"
+"\n"
+"uniform sampler2D uTexGarbage;\n"
+"uniform sampler2D uTexGradients;\n"
+"uniform vec3 uCamera;\n"
+"uniform vec4 uColour;\n"
+"\n"
+"in vec4 aColour;\n"
+"in vec2 aUv;\n"
+"in vec3 aNorm;\n"
+"in vec3 aCo;\n"
+"in vec3 aWorldCo;\n"
+"\n"
+"#line       1        1 \n"
+"layout (std140) uniform ub_world_lighting\n"
+"{\n"
+"   vec4 g_light_colours[3];\n"
+"   vec4 g_light_directions[3];\n"
+"   vec4 g_ambient_colour;\n"
+"\n"
+"   vec4 g_water_plane;\n"
+"   vec4 g_depth_bounds;\n"
+"   float g_water_fog;\n"
+"   int g_light_count;\n"
+"   int g_light_preview;\n"
+"};\n"
+"\n"
+"uniform sampler2D g_world_depth;\n"
+"\n"
+"// Standard diffuse + spec models\n"
+"// ==============================\n"
+"\n"
+"vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )\n"
+"{\n"
+"   vec3 vtotal = g_ambient_colour.rgb;\n"
+"\n"
+"   for( int i=0; i<g_light_count; i++ )\n"
+"   {\n"
+"      vec3 vcolour = g_light_colours[i].rgb;\n"
+"      vec3 vdir = g_light_directions[i].xyz;\n"
+"\n"
+"      float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
+"      vtotal += vcolour*flight;\n"
+"   }\n"
+"\n"
+"   return vfrag * vtotal;\n"
+"}\n"
+"\n"
+"vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )\n"
+"{\n"
+"   vec3 vcolour = g_light_colours[0].rgb;\n"
+"   vec3 vdir = g_light_directions[0].xyz;\n"
+"\n"
+"   vec3 specdir = reflect( -vdir, wnormal );\n"
+"   float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
+"   return vfrag + vcolour*spec*fintensity;\n"
+"}\n"
+"\n"
+"float world_depth_sample( vec3 pos )\n"
+"{\n"
+"   vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
+"   return texture( g_world_depth, depth_coord ).r;\n"
+"}\n"
+"\n"
+"float shadow_sample( vec3 vdir )\n"
+"{\n"
+"   vec3 sample_pos = aWorldCo + vdir;\n"
+"   float height_sample = world_depth_sample( sample_pos );\n"
+"\n"
+"   float fdelta = height_sample - sample_pos.y;\n"
+"   return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
+"}\n"
+"\n"
+"vec3 do_light_shadowing_old( vec3 vfrag )\n"
+"{\n"
+"   float faccum = 0.0;\n"
+"   faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));\n"
+"   faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));\n"
+"   faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));\n"
+"   faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));\n"
+"   faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);\n"
+"   faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);\n"
+"   faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);\n"
+"   faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);\n"
+"   return mix( vfrag, g_ambient_colour.rgb, faccum );\n"
+"}\n"
+"\n"
+"vec3 do_light_shadowing( vec3 vfrag )\n"
+"{\n"
+"   float fspread = g_light_colours[0].w;\n"
+"   vec3  vdir = g_light_directions[0].xyz;\n"
+"   float flength = g_light_directions[0].w;\n"
+"\n"
+"   float famt = 0.0;\n"
+"   famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
+"   famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
+"   famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
+"   famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
+"   famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
+"   famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
+"   famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
+"   famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
+"   return mix( vfrag, g_ambient_colour.rgb, famt );\n"
+"}\n"
+"\n"
+"vec3 apply_fog( vec3 vfrag, float fdist )\n"
+"{\n"
+"   float dist = pow(fdist*0.0008,1.2);\n"
+"   return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
+"}\n"
+"\n"
+"#line     15        0 \n"
+"\n"
+"void main()\n"
+"{\n"
+"   vec3 vfrag = vec3(0.5,0.5,0.5);\n"
+"\n"
+"   // ws modulation\n"
+"   vec4 wgarbage = texture( uTexGarbage, aCo.xz * 0.015 );\n"
+"\n"
+"   // Creating normal patches\n"
+"   vec3 modnorm = (wgarbage.rgb-0.4) * 1.4;\n"
+"   vec3 qnorm = normalize(floor(aNorm*4.0+modnorm)*0.25) + vec3(0.001,0.0,0.0);\n"
+"\n"
+"   vec3 tangent0 = normalize(cross(qnorm,vec3(0.0,1.0,0.0)));\n"
+"   vec3 tangent1 = cross(qnorm,tangent0);\n"
+"   vec2 uvdiffuse = vec2( dot(tangent0,aCo), dot(tangent1,aCo) ) * 0.035;\n"
+"   \n"
+"   // Patch local noise\n"
+"   vec4 rgarbage = texture( uTexGarbage, uvdiffuse );\n"
+"\n"
+"   vfrag = pow(uColour.rgb,vec3(1.0/2.2));\n"
+"   vfrag -= rgarbage.a*0.1;\n"
+"\n"
+"   if( wgarbage.g < 0.3 )\n"
+"      discard;\n"
+"\n"
+"   if( g_light_preview == 1 )\n"
+"   {\n"
+"      vfrag = vec3(0.5);\n"
+"   }\n"
+"\n"
+"   // Lighting\n"
+"   vec3 halfview = uCamera - aWorldCo;\n"
+"   float fdist = length( halfview );\n"
+"   halfview /= fdist;\n"
+"\n"
+"   vfrag = do_light_diffuse( vfrag, qnorm );\n"
+"   vfrag = do_light_spec( vfrag, qnorm, halfview, 0.1 );\n"
+"   vfrag = do_light_shadowing( vfrag );\n"
+"   vfrag = apply_fog( vfrag, fdist );\n"
+"\n"
+"   FragColor = vec4(vfrag, 1.0 );\n"
+"}\n"
+""},
+};
+
+static GLuint _uniform_route_uPv;
+static GLuint _uniform_route_uMdl;
+static GLuint _uniform_route_uTexGarbage;
+static GLuint _uniform_route_uTexGradients;
+static GLuint _uniform_route_uCamera;
+static GLuint _uniform_route_uColour;
+static GLuint _uniform_route_g_world_depth;
+static void shader_route_uPv(m4x4f m){
+   glUniformMatrix4fv( _uniform_route_uPv, 1, GL_FALSE, (float *)m );
+}
+static void shader_route_uMdl(m4x3f m){
+   glUniformMatrix4x3fv( _uniform_route_uMdl, 1, GL_FALSE, (float *)m );
+}
+static void shader_route_uTexGarbage(int i){
+   glUniform1i( _uniform_route_uTexGarbage, i );
+}
+static void shader_route_uTexGradients(int i){
+   glUniform1i( _uniform_route_uTexGradients, i );
+}
+static void shader_route_uCamera(v3f v){
+   glUniform3fv( _uniform_route_uCamera, 1, v );
+}
+static void shader_route_uColour(v4f v){
+   glUniform4fv( _uniform_route_uColour, 1, v );
+}
+static void shader_route_g_world_depth(int i){
+   glUniform1i( _uniform_route_g_world_depth, i );
+}
+static void shader_route_register(void){
+   vg_shader_register( &_shader_route );
+}
+static void shader_route_use(void){ glUseProgram(_shader_route.id); }
+static void shader_route_link(void){
+   _uniform_route_uPv = glGetUniformLocation( _shader_route.id, "uPv" );
+   _uniform_route_uMdl = glGetUniformLocation( _shader_route.id, "uMdl" );
+   _uniform_route_uTexGarbage = glGetUniformLocation( _shader_route.id, "uTexGarbage" );
+   _uniform_route_uTexGradients = glGetUniformLocation( _shader_route.id, "uTexGradients" );
+   _uniform_route_uCamera = glGetUniformLocation( _shader_route.id, "uCamera" );
+   _uniform_route_uColour = glGetUniformLocation( _shader_route.id, "uColour" );
+   _uniform_route_g_world_depth = glGetUniformLocation( _shader_route.id, "g_world_depth" );
+}
+#endif /* SHADER_route_H */
index 8850306b450d9aa534c3e1c024c058343190c3b3..98b8c7b57aa0de3df514ac7d13aceecb3076c2b1 100644 (file)
--- a/traffic.h
+++ b/traffic.h
@@ -28,17 +28,6 @@ struct traffic_driver
    float t, speed;
 };
 
-static void eval_bezier_time( v3f p0, v3f p1, v3f h0, v3f h1, float t, v3f p )
-{
-   float tt = t*t,
-         ttt = tt*t;
-
-   v3_muls( p1, ttt, p );
-   v3_muladds( p, h1, 3.0f*tt  -3.0f*ttt, p );
-   v3_muladds( p, h0, 3.0f*ttt -6.0f*tt  +3.0f*t, p );
-   v3_muladds( p, p0, 3.0f*tt  -ttt -3.0f*t +1.0f, p );
-}
-
 static float eval_bezier_length( v3f p0, v3f p1, v3f h0, v3f h1, int res )
 {
    float length = 0.0f, m = 1.0f/(float)res;
diff --git a/vg.conf b/vg.conf
index 42c7f0d456e57008c973ca698770c50976f30916..ec19f6aaaf6c1529c40616e72517dcfa2fa814b4 100644 (file)
--- a/vg.conf
+++ b/vg.conf
@@ -15,3 +15,4 @@ shader water standard.vs water.fs
 shader sky standard.vs sky.fs
 shader planeinf standard.vs planeinf.fs
 shader gpos standard.vs gpos.fs
+shader route standard.vs route.fs
diff --git a/world.h b/world.h
index 45003cccbe9d87afa6b4fed9c0912bfec860d454..f015a63e0ae43c93a9b4cd15b762bc133d3a7b18 100644 (file)
--- a/world.h
+++ b/world.h
@@ -46,9 +46,6 @@ static struct gworld
    struct subworld_physics system_physics;
    */
 
-   teleport_gate gates[64];
-   u32 gate_count;
-
    /* Paths */
    traffic_node traffic[128];
    u32 traffic_count;
@@ -123,6 +120,8 @@ static void world_register(void)
    shader_gpos_register();
    shader_fscolour_register();
    shader_alphatest_register();
+
+   world_routes_register();
 }
 
 static void world_free(void)
@@ -222,7 +221,6 @@ static void world_load(void)
    mdl_header *mworld = mdl_load( "models/mp_dev.mdl" );
 
    world.spawn_count = 0;
-   world.gate_count = 0;
    world.traffic_count = 0;
    world.instance_cache = NULL;
    
@@ -235,27 +233,6 @@ static void world_load(void)
       
       if( pnode->classtype == k_classtype_none )
       {}
-#if 0
-      else if( pnode->classtype == k_classtype_gate )
-      {
-         struct classtype_gate *entgate = mdl_get_entdata( mworld, pnode );
-
-         if( entgate->target )
-         {
-            mdl_node *pother = mdl_node_from_id( mworld, entgate->target );
-            
-            teleport_gate *gate = &world.gates[ world.gate_count ++ ];
-            
-            v3_copy( pnode->co,  gate->co[0] );
-            v3_copy( pother->co, gate->co[1] );
-            v4_copy( pnode->q,   gate->q[0] );
-            v4_copy( pother->q,  gate->q[1] );
-            v2_copy( pnode->s,   gate->dims );
-
-            gate_transform_update( gate );
-         }
-      }
-#endif
       else if( pnode->classtype == k_classtype_spawn )
       {
          struct respawn_point *rp = &world.spawns[ world.spawn_count ++ ];
@@ -416,7 +393,7 @@ static void world_load(void)
    scene_copy_slice( &world.foliage, &world.sm_foliage_alphatest );
 
    scene_upload( &world.foliage );
-   world_routes_init( mworld );
+   world_routes_loadfrom( mworld );
 
    for( int i=0; i<world.instance_cache_count; i++ )
       free( world.instance_cache[i].mdl );
@@ -688,9 +665,23 @@ static void render_sky(m4x3f camera)
    glDepthMask( GL_TRUE );
 }
 
+static void render_world_gates( m4x4f projection, m4x3f camera )
+{
+   int count = 0;
+   for( int i=0; i<world.routes.gate_count; i++ )
+   {
+      struct route_gate *rg = &world.routes.gates[i];
+      count += render_gate( &rg->gate, camera );
+
+      if( count == 2 )
+         return;
+   }
+}
+
 static void render_world( m4x4f projection, m4x3f camera )
 {
    render_sky( camera );
+   render_world_routes( projection, camera[3] );
    render_world_vb( projection, camera[3] );
    render_world_alphatest( projection, camera[3] );
    render_terrain( projection, camera[3] );
index 429c0fb1cdad6805cb519c4a9b362d9d6715d1d6..82d5da7925b0ee67749bb35f91ba77ec1e658fe0 100644 (file)
@@ -5,14 +5,18 @@
 #include "model.h"
 #include "gate.h"
 
+#include "shaders/vblend.h"
+#include "shaders/route.h"
+
 struct subworld_routes
 {
    struct route_node
    {
-      v3f co, right, h;
+      v3f co, right, up, h;
       u32 next[2];
 
-      u32 is_gate, gate_id;
+      u32 is_gate, gate_id, current_refs, ref_count;
+      u32 route_ids[4];    /* Gates can be linked into up to four routes */
    }
    *nodes;
 
@@ -25,6 +29,10 @@ struct subworld_routes
       v4f colour;
 
       u32 start;
+      mdl_submesh sm;
+
+      int active;
+      float factive;
    }
    *routes;
 
@@ -35,7 +43,6 @@ struct subworld_routes
    {
       teleport_gate gate;
       
-      u32 route_ids[4];    /* Gates can be linked into up to four routes */
       u32 route_count,
           node_id;
    }
@@ -43,6 +50,10 @@ struct subworld_routes
 
    u32 gate_count,
        gate_cap;
+
+   u32 active_gate;
+
+   scene scene_lines;
 };
 
 static struct subworld_routes *subworld_routes(void);
@@ -50,7 +61,115 @@ static struct subworld_routes *subworld_routes(void);
 /* 
  * TODO list:
  *      when a gate is passed through it needs to trigger into an active state
- */
+ */      
+
+static void debug_sbpath( struct route_node *rna, struct route_node *rnb,
+                          u32 colour, float xoffset )
+{
+   v3f p0, h0, p1, h1, l, p;
+   
+   v3_copy( rna->co, p0 );
+   v3_muladds( rna->co, rna->h,  1.0f, h0 );
+   v3_copy( rnb->co, p1 );
+   v3_muladds( rnb->co, rnb->h, -1.0f, h1 );
+
+   v3_muladds( p0, rna->right, xoffset, p0 );
+   v3_muladds( h0, rna->right, xoffset, h0 );
+   v3_muladds( p1, rnb->right, xoffset, p1 );
+   v3_muladds( h1, rnb->right, xoffset, h1 );
+
+   v3_copy( p0, l );
+
+   for( int i=0; i<5; i++ )
+   {
+      float t = (float)(i+1)/5.0f;
+      eval_bezier_time( p0, p1, h0, h1, t, p );
+      vg_line( p, l, colour );
+      v3_copy( p, l );
+   }
+}
+
+static void world_routes_activate_gate( u32 id )
+{
+   struct subworld_routes *r = subworld_routes();
+   struct route_gate *ig = &r->gates[id];
+   struct route_node *pnode = &r->nodes[ig->node_id],
+                     *pdest = &r->nodes[pnode->next[0]];
+   
+   r->active_gate = id;
+
+   for( int i=0; i<r->route_count; i++ )
+   {
+      struct route *route = &r->routes[i];
+
+      route->active = 0;
+      for( int j=0; j<pdest->ref_count; j++ )
+      {
+         if( pdest->route_ids[j] == i )
+         {
+            route->active = 1;
+            break;
+         }
+      }
+   }
+}
+
+static u32 world_routes_get_path( struct route *route, u32 stack[64] )
+{
+   struct subworld_routes *r = subworld_routes();
+   u32 stack_i[64];
+
+   stack[0] = route->start;
+   stack_i[0] = 0;
+
+   u32 si = 1;
+   int loop_complete = 0;
+
+   while( si )
+   {
+      if( stack_i[si-1] == 2 )
+      {
+         si --;
+         continue;
+      }
+
+      struct route_node *rn = &r->nodes[stack[si-1]];
+      u32 nextid = rn->next[stack_i[si-1]];
+      stack_i[si-1] ++;
+
+      if( nextid != 0xffffffff )
+      {
+         if( nextid == stack[0] )
+         {
+            loop_complete = 1;
+            break;
+         }
+
+         int valid = 1;
+         for( int sj=0; sj<si; sj++ )
+         {
+            if( stack[sj] == nextid )
+            {
+               valid = 0;
+               break;
+            }
+         }
+
+         if( valid )
+         {
+            stack_i[si] = 0;
+            stack[si] = nextid;
+            si ++;
+            continue;
+         }
+      }
+   }
+
+   if( loop_complete )
+      return si;
+
+   return 0;
+}
 
 static void world_routes_debug(void)
 {
@@ -61,6 +180,41 @@ static void world_routes_debug(void)
       struct route_node *rn = &r->nodes[i];
       vg_line_pt3( rn->co, 1.0f, rn->is_gate? 0xffffff00: 0xff00b2ff );
    }
+
+   for( int i=0; i<r->route_count; i++ )
+   {
+      struct route *route = &r->routes[i];
+
+      u32 stack[64];
+      u32 si = world_routes_get_path( route, stack );
+
+      u32 colours[] = { 0xfff58142, 0xff42cbf5, 0xff42f56c, 0xfff542b3,
+                        0xff5442f5 };
+
+      u32 cc = colours[i%vg_list_size(colours)];
+
+      for( int sj=0; sj<si; sj++ )
+      {
+         int sk = (sj+1)%si;
+         debug_sbpath( &r->nodes[stack[sj]], &r->nodes[stack[sk]], cc,
+               (float)i );
+      }
+   }
+
+   for( int i=0; i<r->node_count; i++ )
+   {
+      struct route_node *ri = &r->nodes[i],
+                        *rj = NULL;
+      
+      for( int j=0; j<2; j++ )
+      {
+         if( ri->next[j] != 0xffffffff )
+         {
+            rj = &r->nodes[ri->next[j]];
+            vg_line( ri->co, rj->co, 0x20ffffff );
+         }
+      }
+   }
 }
 
 static void world_routes_free(void)
@@ -76,9 +230,180 @@ static void world_id_fixup( u32 *uid, mdl_header *mdl )
 {
    if( *uid )
       *uid = mdl_node_from_id( mdl, *uid )->sub_uid;
+   else
+      *uid = 0xffffffff;
 }
 
-static void world_routes_init( mdl_header *mdl )
+static void world_routes_gen_meshes(void)
+{
+   struct subworld_routes *r = subworld_routes();
+   scene_init( &r->scene_lines );
+
+   for( int i=0; i<r->route_count; i++ )
+   {
+      struct route *route = &r->routes[i];
+
+      u32 stack[64];
+      u32 si = world_routes_get_path( route, stack );
+
+      u32 last_valid = 0;
+
+      for( int sj=0; sj<si; sj++ )
+      {
+         int sk=(sj+1)%si;
+
+         struct route_node *rnj = &r->nodes[ stack[sj] ],
+                           *rnk = &r->nodes[ stack[sk] ],
+                           *rnl;
+         
+         if( rnj->is_gate && rnk->is_gate )
+         {
+            last_valid = 0;
+            continue;
+         }
+
+         float base_x0 = (float)rnj->ref_count*-0.5f + (float)rnj->current_refs,
+               base_x1 = (float)rnk->ref_count*-0.5f + (float)rnk->current_refs;
+
+         if( rnk->is_gate )
+         {
+            rnl = &r->nodes[ rnk->next[0] ];
+            base_x1 = (float)rnl->ref_count*-0.5f + (float)rnl->current_refs;
+         }
+
+         if( sk == 0 )
+         {
+            base_x1 -= 1.0f;
+         }
+
+         v3f p0, h0, p1, h1, p, pd;
+         
+         v3_copy( rnj->co, p0 );
+         v3_muladds( rnj->co, rnj->h,  1.0f, h0 );
+         v3_copy( rnk->co, p1 );
+         v3_muladds( rnk->co, rnk->h, -1.0f, h1 );
+
+         float t=0.0f;
+         int it = 0;
+
+         for( int it=0; it<256; it ++ )
+         {
+            float const k_sample_dist = 0.02f;
+            eval_bezier_time( p0,p1,h0,h1, t,p );
+            eval_bezier_time( p0,p1,h0,h1, t+k_sample_dist,pd );
+
+            float mod = k_sample_dist / v3_dist( p, pd );
+
+            v3f v0,up, right;
+            v3_muls( rnj->up, 1.0f-t, up );
+            v3_muladds( up, rnk->up, t, up );
+
+            v3_sub( pd,p,v0 );
+            v3_cross( up, v0, right );
+            v3_normalize( right );
+
+            float cur_x = (1.0f-t)*base_x0 + t*base_x1;
+            
+            v3f sc, sa, sb, down;
+            v3_muladds( p, right, cur_x, sc );
+            v3_muladds( sc, up, 1.5f, sc );
+            v3_muladds( sc, right,  0.45f, sa );
+            v3_muladds( sc, right, -0.45f, sb );
+            v3_muls( up, -1.0f, down );
+            
+            ray_hit ha, hb;
+            ha.dist = 8.0f;
+            hb.dist = 8.0f;
+            if(ray_world( sa, down, &ha ) && 
+               ray_world( sb, down, &hb ))
+            {
+               mdl_vert va, vb;
+               
+               v3_muladds( ha.pos, up, 0.06f, va.co );
+               v3_muladds( hb.pos, up, 0.06f, vb.co );
+               v3_copy( up, va.norm );
+               v3_copy( up, vb.norm );
+               v3_zero( va.colour );
+               v3_zero( vb.colour );
+               v2_zero( va.uv );
+               v2_zero( vb.uv );
+
+               scene_push_vert( &r->scene_lines, &va );
+               scene_push_vert( &r->scene_lines, &vb );
+
+               if( last_valid )
+               {
+                  /* Connect them with triangles */
+                  scene_push_tri( &r->scene_lines, (u32[3]){ 
+                        last_valid+0-2, last_valid+1-2, last_valid+2-2} );
+                  scene_push_tri( &r->scene_lines, (u32[3]){ 
+                        last_valid+1-2, last_valid+3-2, last_valid+2-2} );
+               }
+               
+               last_valid = r->scene_lines.vertex_count;
+            }
+            else
+               last_valid = 0;
+
+            t += 1.0f*mod;
+
+            if( t >= 1.0f )
+            {
+               /* TODO special case for end of loop, need to add triangles
+                *      between first and last rungs */
+               break;
+            }
+         }
+
+         rnj->current_refs ++;
+      }
+
+      scene_copy_slice( &r->scene_lines, &route->sm );
+   }
+
+   scene_upload( &r->scene_lines );
+   scene_free_offline_buffers( &r->scene_lines );
+}
+
+static void bind_terrain_textures(void);
+static void render_world_routes( m4x4f projection, v3f camera )
+{
+   struct subworld_routes *r = subworld_routes();
+
+   m4x3f identity_matrix;
+   m4x3_identity( identity_matrix );
+
+   shader_route_use();
+   shader_route_uTexGarbage(0);
+   shader_link_standard_ub( _shader_route.id, 2 );
+   bind_terrain_textures();
+
+   shader_route_uPv( projection );
+   shader_route_uMdl( identity_matrix );
+   shader_route_uCamera( camera );
+
+   scene_bind( &r->scene_lines );
+
+   for( int i=0; i<r->route_count; i++ )
+   {
+      struct route *route = &r->routes[i];
+      route->factive = vg_lerpf( route->factive, route->active, 0.01f );
+
+      v4f colour;
+      v3_lerp( (v3f){0.7f,0.7f,0.7f}, route->colour, route->factive, colour );
+      colour[3] = 1.0f;
+
+      shader_route_uColour( colour );
+      mdl_draw_submesh( &route->sm );
+   }
+}
+
+static void world_routes_register(void)
+{
+   shader_route_register();
+}
+
+static void world_routes_loadfrom( mdl_header *mdl )
 {
    struct subworld_routes *r = subworld_routes();
    r->nodes = NULL;
@@ -109,8 +434,12 @@ static void world_routes_init( mdl_header *mdl )
 
          v3_copy( transform[0], rn->right );
          v3_normalize( rn->right );
-         v3_copy( transform[2], rn->h );
+         v3_copy( transform[1], rn->up );
+         v3_normalize( rn->up );
+         v3_muls( transform[2], -1.0f, rn->h );
          v3_copy( transform[3], rn->co );
+         rn->ref_count = 0;
+         rn->current_refs = 0;
 
          if( pnode->classtype == k_classtype_gate )
          {
@@ -126,12 +455,27 @@ static void world_routes_init( mdl_header *mdl )
             rn->gate_id = r->gate_count;
             rn->is_gate = 1;
 
-            struct route_gate *rg = &r->gates[r->gate_count];
-            rg->node_id = r->node_count;
-            
             /* TODO */
-
-            r->gate_count ++;
+            if( inf->target )
+            {
+               mdl_node *pother = mdl_node_from_id( mdl, inf->target );
+               
+               if( pother->classtype == k_classtype_gate )
+               {
+                  struct route_gate *rg = &r->gates[r->gate_count];
+                  rg->node_id = r->node_count;
+
+                  v3_copy( pnode->co,  rg->gate.co[0] );
+                  v3_copy( pother->co, rg->gate.co[1] );
+                  v4_copy( pnode->q,   rg->gate.q[0] );
+                  v4_copy( pother->q,  rg->gate.q[1] );
+                  v2_copy( inf->dims,  rg->gate.dims );
+
+                  gate_transform_update( &rg->gate );
+
+                  r->gate_count ++;
+               }
+            }
          }
          else
          {
@@ -151,9 +495,13 @@ static void world_routes_init( mdl_header *mdl )
 
          struct route *route = &r->routes[r->route_count];
 
-         v4_zero( route->colour );
+         v3_copy( inf->colour, route->colour );
+         route->colour[3] = 1.0f;
+
          route->name = NULL;
          route->start = inf->id_start;
+         route->active = 0;
+         route->factive = 0.0f;
 
          r->route_count ++;
       }
@@ -168,22 +516,35 @@ static void world_routes_init( mdl_header *mdl )
       
       for( int j=0; j<2; j++ )
          world_id_fixup( &rn->next[j], mdl );
-
-      if( rn->is_gate )
-         world_id_fixup( &rn->gate_id, mdl );
    }
 
-   for( int i=0; i<r->gate_count; i++ )
+   for( int i=0; i<r->route_count; i++ )
    {
-      struct route_gate *rg = &r->gates[i];
-      world_id_fixup( &rg->node_id, mdl );
+      struct route *route = &r->routes[i];
+      world_id_fixup( &route->start, mdl );
    }
 
+   /*
+    * Gather references
+    */
    for( int i=0; i<r->route_count; i++ )
    {
       struct route *route = &r->routes[i];
-      world_id_fixup( &route->start, mdl );
+
+      u32 stack[64];
+      u32 si = world_routes_get_path( route, stack );
+
+      for( int sj=0; sj<si; sj++ )
+      {
+         struct route_node *rn = &r->nodes[ stack[sj] ];
+         rn->route_ids[ rn->ref_count ++ ] = i;
+
+         if( rn->ref_count > 4 )
+            vg_warn( "Too many references on route node %i\n", i );
+      }
    }
+
+   world_routes_gen_meshes();
 }
 
 #endif /* ROUTES_H */