whole
[carveJwlIkooP6JGAAIwe30JlM.git] / main.c
diff --git a/main.c b/main.c
index e34ca5fff6b823dd7ec08f2cf7b3b81ccf066e7c..b46f3771b5a565abba28504e852f745fe97f5bfc 100644 (file)
--- a/main.c
+++ b/main.c
-#define VG_3D
-#include "vg/vg.h"
+/*
+ * Copyright (C) Mount0 Software, Harry Godden - All Rights Reserved
+ */
+
+#include "common.h"
 
 /* Resources */
 vg_tex2d tex_norwey = { .path = "textures/norway_foliage.qoi" };
 vg_tex2d tex_grid = { .path = "textures/grid.qoi" };
-vg_tex2d tex_road = { .path = "textures/road.qoi" };
+vg_tex2d tex_sky = { .path = "textures/sky.qoi" };
 vg_tex2d tex_gradients = { .path = "textures/gradients.qoi",
                            .flags = VG_TEXTURE_CLAMP };
-vg_tex2d *texture_list[] =
-{
-   &tex_norwey,
-   &tex_gradients,
-   &tex_grid,
-   &tex_road
-};
+vg_tex2d tex_cement = { .path = "textures/cement512.qoi" };
+vg_tex2d tex_water = { .path = "textures/water.qoi" };
 
 /* Convars */
-static int freecam = 0;
 static int debugview = 0;
-static int debugsdf = 0;
-static int debugroad = 0;
+static int sv_debugcam = 0;
+static int lightedit = 0;
+static int sv_scene = 0;
+static int cl_ui = 1;
 
 /* Components */
-#include "road.h"
-#include "scene.h"
+//#define SR_NETWORKED
 
-int main( int argc, char *argv[] )
-{ 
-   vg_init( argc, argv, "Voyager Game Engine" ); 
-}
+/* uncomment this to run the game without any graphics being drawn */
+//#define SR_NETWORK_TEST
 
-m4x3f world_matrix;
+#include "steam.h"
+#include "network.h"
 
-#if 0
-v3f player.co;
-v3f player.view;     /* Relative to pos */
-v3f player.v = { 0.0f, 0.0f, -0.2f };
-float player_yaw;
-v2f player.look_dir;
-v3f player.a;
-#endif
+#include "model.h"
+//#include "road.h"
+#include "scene.h"
+//#include "ik.h"
+#include "audio.h"
+//#include "terrain.h"
+//#include "character.h"
+#include "ragdoll.h"
+#include "rigidbody.h"
+#include "render.h"
+#include "world.h"
+#include "player.h"
+
+#include "shaders/blit.h"
+#include "shaders/standard.h"
+#include "shaders/unlit.h"
+
+#include "physics_test.h"
+#include "anim_test.h"
+
+#include "gate.h"
+#include "water.h"
 
-static struct gplayer
+void vg_register(void)
 {
-   v3f co, v, a;
-   v4f rot;
-
-   v3f view;
-   v2f look_dir;  /* TEMP */
-
-   m4x3f to_world, to_local;
+   shader_blit_register();
+   shader_standard_register();
+   shader_vblend_register();
+   shader_unlit_register();
+
+   world_register();
+   character_register();
+   water_register();
+   gate_register();
 }
-player;
 
-road_patch road_main;
-scene test_scene;
-scene world_scene;
-scene player_scene;
-u32 world_terrain_count,
-    world_road_count;
-
-static void player_transform_update(void)
+static void init_other(void)
 {
-   q_m3x3( player.rot, player.to_world );
-   v3_copy( player.co, player.to_world[3] );
-
-   v4f inv;
-   q_inv( player.rot, inv );
-   q_m3x3( inv, player.to_local );
-   v3_negate( player.co, player.to_local[3] );
+   player_init();
+   render_init();
+   gate_init();
+   world_init();
+   character_init();
+   audio_init();
 }
 
-static int reset_player( int argc, char const *argv[] )
+vg_tex2d *texture_list[] =
 {
-   v3_zero( player.co );
-   v3_copy( (v3f){ 0.0f, 0.0f, -0.2f }, player.v );
-   q_identity( player.rot );
+   &tex_norwey,
+   &tex_gradients,
+   &tex_grid,
+   &tex_sky,
+   &tex_cement,
+   &tex_water,
+   &tex_water_surf
+};
 
-   player_transform_update();
-   return 0;
+int main( int argc, char *argv[] )
+{ 
+   vg_init( argc, argv, "Voyager Game Engine" ); 
 }
 
-void vg_register(void)
+static int playermodel( int argc, char const *argv[] )
 {
-   scene_register();
+   if( argc < 1 ) return 0;
+   
+   glmesh old_mesh = player.mdl.mesh;
+
+   if( character_load( &player.mdl, argv[0] ) )
+      mesh_free( &old_mesh );
+
+   return 1;
 }
 
 void vg_start(void)
 {
-   vg_tex2d_init( texture_list, vg_list_size( texture_list ) );
-   
-   road_patch_init( &road_main );
-   road_generate( &road_main );
+   steam_init();
 
    vg_convar_push( (struct vg_convar){
-      .name = "freecam",
+      .name = "fc",
       .data = &freecam,
       .data_type = k_convar_dtype_i32,
       .opt_i32 = { .min=0, .max=1, .clamp=1 },
@@ -102,462 +116,404 @@ void vg_start(void)
    });
 
    vg_convar_push( (struct vg_convar){
-      .name = "debugview",
-      .data = &debugview,
+      .name = "grid",
+      .data = &walk_grid_iterations,
       .data_type = k_convar_dtype_i32,
       .opt_i32 = { .min=0, .max=1, .clamp=0 },
       .persistent = 1
    });
 
    vg_convar_push( (struct vg_convar){
-      .name = "debugsdf",
-      .data = &debugsdf,
+      .name = "fcs",
+      .data = &fc_speed,
+      .data_type = k_convar_dtype_f32,
+      .opt_f32 = { .clamp = 0 },
+      .persistent = 1
+   });
+
+   vg_convar_push( (struct vg_convar){
+      .name = "ledit",
+      .data = &lightedit,
       .data_type = k_convar_dtype_i32,
       .opt_i32 = { .min=0, .max=1, .clamp=1 },
       .persistent = 1
    });
 
    vg_convar_push( (struct vg_convar){
-      .name = "debugroad",
-      .data = &debugroad,
+      .name = "cl_ui",
+      .data = &cl_ui,
       .data_type = k_convar_dtype_i32,
       .opt_i32 = { .min=0, .max=1, .clamp=1 },
       .persistent = 1
    });
 
+   vg_convar_push( (struct vg_convar){
+      .name = "walk_speed",
+      .data = &k_walkspeed,
+      .data_type = k_convar_dtype_f32,
+      .opt_f32 = { .clamp = 0 },
+      .persistent = 1
+   });
+
+   vg_convar_push( (struct vg_convar){
+      .name = "run_speed",
+      .data = &k_runspeed,
+      .data_type = k_convar_dtype_f32,
+      .opt_f32 = { .clamp = 0 },
+      .persistent = 1
+   });
+
+   vg_convar_push( (struct vg_convar){
+      .name = "walk_accel",
+      .data = &k_walk_accel,
+      .data_type = k_convar_dtype_f32,
+      .opt_f32 = { .clamp = 0 },
+      .persistent = 1
+   });
+
+   vg_convar_push( (struct vg_convar){
+      .name = "dt",
+      .data = &ktimestep,
+      .data_type = k_convar_dtype_f32,
+      .opt_f32 = { .clamp = 0 },
+      .persistent = 0
+   });
+
+   vg_convar_push( (struct vg_convar){
+      .name = "debugcam",
+      .data = &sv_debugcam,
+      .data_type = k_convar_dtype_i32,
+      .opt_i32 = { .min=0, .max=1, .clamp=0 },
+      .persistent = 1
+   });
+
+   vg_convar_push( (struct vg_convar){
+      .name = "debugview",
+      .data = &debugview,
+      .data_type = k_convar_dtype_i32,
+      .opt_i32 = { .min=0, .max=1, .clamp=0 },
+      .persistent = 1
+   });
+
        vg_function_push( (struct vg_cmd){
                .name = "reset",
                .function = reset_player
        });
-   
-   v3f lightDir = { 0.1f, 0.8f, 0.2f };
-   v3_normalize( lightDir );
-   
-   scene_init( &world_scene );
-   scene_init( &test_scene );
-   scene_init( &player_scene );
-   model *world = vg_asset_read( "models/free_dev.mdl" );
-   model *test = vg_asset_read( "models/test.mdl" );
-   model *char_dev = vg_asset_read( "models/char_dev.mdl" );
-   scene_add_model( &player_scene, char_dev,
-         submodel_get( char_dev, "joint" ),
-         (v3f){0.0f,0.0f,0.0f}, 0.0f, 1.0f );
-   free(char_dev);
-
-   scene_add_model( &world_scene, world, 
-         submodel_get( world, "terrain" ),
-         (v3f){0.0f,0.0f,0.0f}, 0.0f, 1.0f );
-
-   int id_tree = submodel_get( test, "tree" ),
-       id_groundcover[] = 
-       {
-         submodel_get( test, "bush" ),
-         submodel_get( test, "grass" ),
-         submodel_get( test, "blubber" ),
-         submodel_get( test, "blubber" ),
-         submodel_get( test, "blubber" ),
-         submodel_get( test, "grassthin" )
-       };
-
-   /* Sprinkle some trees in the terrain areas */
-   v3f range;
-   v3_sub( world_scene.bbx[1], world_scene.bbx[0], range );
-
-   for( int i=0; i<8000; i++ )
-   {
-      v3f pos;
 
-      pos[0] = vg_randf();
-      pos[1] = 0.0f;
-      pos[2] = vg_randf();
-      v3_muladd( world_scene.bbx[0], pos, range, pos );
-
-      if( sample_scene_height( &world_scene, pos ) )
-      {
-         scene_add_model( &test_scene, test, 
-               id_tree,
-               pos, vg_randf() * VG_TAUf, vg_randf() * 0.5f + 0.5f );
-      }
-   }
+   vg_tex2d_init( texture_list, vg_list_size( texture_list ) );
+   
+   init_other();
 
-   world_terrain_count = world_scene.indice_count;
+   /* 
+    * If we're in physics test mode we dont need to load anything else, this
+    * parameter is dev only. TODO: dev only cvars that don't ship with the game
+    * when building in release mode.
+    */
 
-   scene_add_model( &world_scene, world, 
-         submodel_get( world, "road" ),
-         (v3f){0.0f,0.0f,0.0f}, 0.0f, 1.0f );
-   world_road_count = world_scene.indice_count-world_terrain_count;
+   if( sv_scene == 0 )
+   {
+      character_load( &player.mdl, "ch_new" );
+      character_init_ragdoll( &player.mdl );
 
-   free( test );
-   free( world );
+      world_load();
 
-#if 0
-   scene_compute_occlusion( &test_scene );
-   scene_shadow_gradient( &test_scene, 1, 0.0f, 1.0f );
-   scene_shadow_sphere( &test_scene, (v3f){ 0.0f,4.0f,0.0f},
-         (v4f){1.0f, 2.0f, 0.0f, 0.0f}, lightDir );
-#endif
+      reset_player( 1, (const char *[]){ "start" } );
+      rb_init( &player.rb );
 
-   scene_upload( &player_scene );
-   scene_upload( &world_scene );
-   scene_upload( &test_scene );
-   
-   reset_player( 0, NULL );
-   player_transform_update();
+      network_init();
+   }
+   else if( sv_scene == 1 )
+   {
+      physics_test_start();
+   }
+   else if( sv_scene == 2 )
+   {
+      anim_test_start();
+   }
 }
 
-v3f head;
+void vg_free(void)
+{
+   network_end();
+   vg_tex2d_free( texture_list, vg_list_size(texture_list) );
+   /* TODO: THE REST OF THE GOD DAMN FREEING STUFF */
+   steam_end();
+}
 
 void vg_update(void)
 {
-   float timestep = 1.0f/60.0f;
+   steam_update();
 
-   if( freecam )
+   if( sv_scene == 0 )
    {
-      m4x3f cam_rot;
-      m4x3_identity( cam_rot );
-      m4x3_rotate_y( cam_rot, -player.look_dir[0] );
-      m4x3_rotate_x( cam_rot, -player.look_dir[1] );
-
-      v3f lookdir = { 0.0f, 0.0f, -1.0f },
-          sidedir = { 1.0f, 0.0f,  0.0f };
-      
-      m4x3_mulv( cam_rot, lookdir, lookdir );
-      m4x3_mulv( cam_rot, sidedir, sidedir );
-      
-      float movespeed = 5.0f;
-      static v2f mouse_last,
-                 view_vel = { 0.0f, 0.0f };
-      static v3f move_vel = { 0.0f, 0.0f, 0.0f };
-
-      if( vg_get_button_down( "primary" ) )
-      {
-         v2_copy( vg_mouse, mouse_last );
-      }
-      else if( vg_get_button( "primary" ) )
+      network_update();
+      player_update();
+      world_update();
+      //traffic_visualize( world.traffic, world.traffic_count );
+      //
+      /* TEMP */
+      if( glfwGetKey( vg_window, GLFW_KEY_J ))
       {
-         v2f delta;
-         v2_sub( vg_mouse, mouse_last, delta );
-         v2_copy( vg_mouse, mouse_last );
-
-         v2_muladds( view_vel, delta, 0.005f, view_vel );
+         v3_copy( player.camera_pos, world.mr_ball.co );
       }
-      
-      v2_muls( view_vel, 0.75f, view_vel );
-      v2_add( view_vel, player.look_dir, player.look_dir );
-      player.look_dir[1] = 
-         vg_clampf( player.look_dir[1], -VG_PIf*0.5f, VG_PIf*0.5f );
-
-      if( vg_get_button( "forward" ) )
-         v3_muladds( move_vel, lookdir, timestep * movespeed, move_vel );
-      if( vg_get_button( "back" ) )
-         v3_muladds( move_vel, lookdir, timestep *-movespeed, move_vel );
-      if( vg_get_button( "left" ) )
-         v3_muladds( move_vel, sidedir, timestep *-movespeed, move_vel );
-      if( vg_get_button( "right" ) )
-         v3_muladds( move_vel, sidedir, timestep * movespeed, move_vel );
-
-      v3_muls( move_vel, 0.75f, move_vel );
-      v3_add( move_vel, player.view, player.view );
-
-      return;
    }
-   
-   if( vg_get_button( "forward" ) )
+   else if( sv_scene == 1 )
    {
-      v3f dir = { 0.0f, 0.0f, -1.0f };
-
-      m3x3_mulv( player.to_world, dir, dir );
-      v3_muladds( player.v, dir, 5.0f * timestep, player.v );
+      physics_test_update();
    }
+   else if( sv_scene == 2 )
+   {
+      anim_test_update();
+   }
+}
 
-   /* Get front and back contact points */
-   v3f contact_front, contact_back, fwd, fwd1, contact_norm, vup;
-
-   m3x3_mulv( player.to_world, (v3f){0.0f,0.0f,-1.0f}, fwd );
-   m3x3_mulv( player.to_world, (v3f){0.03f,0.0f,-1.0f}, fwd1 );
-   m3x3_mulv( player.to_world, (v3f){0.0f,1.0f,0.0f}, vup );
+static void vg_framebuffer_resize( int w, int h )
+{
+   render_fb_resize();
+   gate_fb_resize();
+   water_fb_resize();
+}
 
-   v3_muladds( player.co, fwd, 1.0f, contact_front );
-   v3_muladds( player.co, fwd,-1.0f, contact_back );
-   v3_muladds( player.co, fwd1, 1.0f, contact_norm );
-   
-   sample_scene_height( &world_scene, contact_front );
-   sample_scene_height( &world_scene, contact_back );
-   sample_scene_height( &world_scene, contact_norm );
+static void draw_origin_axis(void)
+{
+   vg_line( (v3f){ 0.0f, 0.0f, 0.0f }, (v3f){ 1.0f, 0.0f, 0.0f }, 0xffff0000 );
+   vg_line( (v3f){ 0.0f, 0.0f, 0.0f }, (v3f){ 0.0f, 1.0f, 0.0f }, 0xff00ff00 );
+   vg_line( (v3f){ 0.0f, 0.0f, 0.0f }, (v3f){ 0.0f, 0.0f, 1.0f }, 0xff0000ff );
+}
 
-   v3f norm;
-   v3f v0, v1;
-   v3_sub( contact_back, contact_norm, v0 );
-   v3_sub( contact_front, contact_norm, v1 );
-   v3_cross( v1, v0, norm );
-   v3_normalize( norm );
+static void render_main_game(void)
+{
+   float speed = freecam? 0.0f: v3_length( player.rb.v );
+   v3f shake = { vg_randf()-0.5f, vg_randf()-0.5f, vg_randf()-0.5f };
+   v3_muls( shake, speed*0.01f, shake );
 
-   v3f gravity = { 0.0f, -9.6f, 0.0f };
-   v3_muladds( player.v, gravity, timestep, player.v );
+   m4x4f world_4x4;
+   m4x3_expand( player.camera_inverse, world_4x4 );
 
-   v3f ground_pos;
-   v3_copy( player.co, ground_pos );
-   sample_scene_height( &world_scene, ground_pos );
+   gpipeline.fov = freecam? 60.0f: 125.0f; /* 120 */
+   m4x4_projection( vg_pv, gpipeline.fov, 
+         (float)vg_window_x / (float)vg_window_y, 
+         0.02f, 2100.0f );
 
-   static int in_air = 1;
+   m4x4_mul( vg_pv, world_4x4, vg_pv );
 
-   if( in_air )
-   {
-      if( ground_pos[1] > player.co[1] )
-         in_air = 0;
-   }
+   glEnable( GL_DEPTH_TEST );
+   
+   /* 
+    * Draw world
+    */
 
-   if( !in_air )
-   {
-      float resistance = v3_dot( norm, player.v );
+   int draw_solid = player.is_dead | freecam;
+   
+   render_world( vg_pv, player.camera );
+   if( draw_solid )
+      draw_player();
+   render_water_texture( player.camera );
 
-      if( resistance >= 0.0f )
-         in_air = 1;
-      else
-      {
-         v3_muladds( player.v, norm, -resistance, player.v );
-      }
-   }
+   glBindFramebuffer( GL_FRAMEBUFFER, 0 );
+   render_water_surface( vg_pv, player.camera );
 
-   /* vg_info( "%.3f | %.3f\n", player.v[1], resistance ); */
-   v3_muladds( player.co, player.v, timestep, player.co );
+   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 );
+   glBindFramebuffer( GL_DRAW_FRAMEBUFFER, gpipeline.fb_background );
+   glBlitFramebuffer( 0,0, vg_window_x, vg_window_y, 
+                      0,0, vg_window_x, vg_window_y,
+                      GL_COLOR_BUFFER_BIT,
+                      GL_LINEAR );
    
-   float slip = 0.0f;
-   float yawamt = 0.0f;
+   /* Clear out the colour buffer, but keep depth */
+   glBindFramebuffer( GL_FRAMEBUFFER, 0 );
+   glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
 
-   if( !in_air )
+   if( !player.is_dead )
+      glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
+   else
+      glClear( GL_COLOR_BUFFER_BIT );
+   
+   if( !draw_solid )
    {
-      player.co[1] = (contact_front[1]+contact_back[1])*0.5f;
+      m4x4_projection( vg_pv, gpipeline.fov, 
+            (float)vg_window_x / (float)vg_window_y, 
+            0.01f, 600.0f );
+      m4x4_mul( vg_pv, world_4x4, vg_pv );
+      draw_player();
+   }
 
-      vg_line( player.co, contact_front, 0xff00ffff );
-      vg_line( player.co, contact_back, 0xff00ffa0 );
+   /* Draw back in the background
+    *
+    * TODO: need to disable alpha write in the terrain shader so this works 
+    *       again.
+    */
+   glEnable(GL_BLEND);
+   glDisable(GL_DEPTH_TEST);
+   glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA);
+   glBlendEquation(GL_FUNC_ADD);
+   
+   shader_blit_use();
+   shader_blit_uTexMain( 0 );
+   glActiveTexture(GL_TEXTURE0);
+   glBindTexture( GL_TEXTURE_2D, gpipeline.rgb_background );
 
-      /* Create the 'travel' vector */
-      v3f travel;
-      v3_sub( contact_front, contact_back, travel );
-      v3_normalize( travel );
+   render_fsquad();
+   glDisable(GL_BLEND);
 
-      /* Apply gravity */
-#if 0
-      float gravity_conversion = -v3_dot( travel, gravity );
-      vel[2] += gravity_conversion * substep;
-#endif
+   /* Other shite */
+   glDisable( GL_DEPTH_TEST );
+   vg_lines_drawall( (float *)vg_pv );
+   glViewport( 0,0, vg_window_x, vg_window_y );
+}
 
-      /* Get localized (rotated) rigidbody forces 
-       *           -z
-       *            ^
-       *           -|-
-       *            |
-       *           +z
-       */
-      
-      v3f vel;
-      m3x3_mulv( player.to_local, player.v, vel );
-
-      /* Calculate local forces */
-      slip = -vel[0] / vel[2];
-      float substep = timestep * 0.2f;
-
-      if( fabsf( slip ) > 1.2f )
-      {
-         slip = vg_signf( slip ) * 1.2f;
-      }
-      
-      for( int i=0; i<5; i++ )
-      {
-         if( fabsf(vel[2]) >= 0.02f*substep )
-            vel[2] += vg_signf( vel[2] ) * -0.02f * substep;
-         if( fabsf(vel[0]) >= 6.0f*substep )
-            vel[0] += vg_signf( vel[0] ) * -6.0f * substep;
-      }
-      
-      m3x3_mulv( player.to_world, vel, player.v );
+void vg_render(void) 
+{
+   glBindFramebuffer( GL_FRAMEBUFFER, 0 );
+   glViewport( 0,0, vg_window_x, vg_window_y );
 
-      if( vg_get_button( "yawl" ) )
-         yawamt =  1.6f * timestep;
-      if( vg_get_button( "yawr" ) )
-         yawamt = -1.6f * timestep;
-      
-      yawamt -= vg_get_axis( "horizontal" ) * 1.6f * timestep;
+   glDisable( GL_DEPTH_TEST );
+   glClearColor( 0.11f, 0.35f, 0.37f, 1.0f );
+   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
 
+#ifndef SR_NETWORK_TEST
+   draw_origin_axis();
+
+   if( sv_scene == 0 )
+   {
+      render_main_game();
    }
-   else
+   else if( sv_scene == 1 )
    {
-      yawamt -= vg_get_axis( "horizontal" ) * 3.6f * timestep;
+      physics_test_render();
    }
+   else if( sv_scene == 2 )
+   {
+      anim_test_render( &tex_characters );
+   }
+#endif
+}
 
-   v4f rotate;
-   q_axis_angle( rotate, vup, yawamt );
-   q_mul( rotate, player.rot, player.rot );
+static void run_light_widget( struct light_widget *lw )
+{
+   struct ui_checkbox c1 = { .data=&lw->enabled };
 
-   player.look_dir[0] = atan2f( player.v[0], -player.v[2] );
+   ui_checkbox( &ui_global_ctx, &c1 );
    
-   /* Creating a skeleton of the player dynamically */
-   float kheight = 1.8f,
-         kleg = 0.6f;
-
-   v2f ac;
-
-   static v3f last_vel = { 0.0f, 0.0f, 0.0f };
-   static v3f bob, bob1;
+   if( lw->enabled )
+   {
+      struct ui_slider_vector 
+         colour = { .min=0.0f, .max=2.0f, .len=3, .data=lw->colour },
+         dir    = { .min=-VG_PIf, .max=VG_PIf, .len=2, .data=lw->dir };
 
-   v3_sub( player.v, last_vel, player.a );
-   v3_copy( player.v, last_vel );
-   v3_add( bob, player.a, bob );
+      ui_slider_vector( &ui_global_ctx, &colour );
+      ui_global_ctx.cursor[1] += 4;
+      ui_slider_vector( &ui_global_ctx, &dir );
+   }
+}
 
-   bob[0] = vg_clampf( bob[0], -0.4f, 1.0f );
-   bob[1] = vg_clampf( bob[1], -0.4f, 1.3f );
-   bob[2] = vg_clampf( bob[2], -0.4f, 1.0f );
+static void run_debug_info(void)
+{
+   char buf[40];
    
-   v3_lerp( bob, (v3f){ 0.0f, 0.0f, 0.0f }, 0.1f, bob );
-   v3_lerp( bob1, bob, 0.1f, bob1 );
+   snprintf( buf, 40, "%.2fm/s", v3_length( player.rb.v ) );
+   gui_text( (ui_px [2]){ 0, 0 }, buf, 1, k_text_align_left );
    
-   /* Head */
-   head[0] =  (-sinf(slip)*0.9f * kheight + bob1[0]*0.6f) * 0.54f;
-   head[1] =  cosf(slip)*0.9f * kheight +-bob1[1]*1.4f;
-   head[2] =  0.0f;
-
-   m4x3_mulv( player.to_world, head, head );
-   v3_copy( head, player.view );
-
-   player_transform_update();
-}
+   snprintf( buf, 40, "%.2f %.2f %.2f m/s", 
+         player.a[0], player.a[1], player.a[2] );
+   gui_text( (ui_px [2]){ 0, 20 }, buf, 1, k_text_align_left );
 
-static void debug_grid( v3f at )
-{
-   v3f base;
-   v3_floor( at, base );
+   snprintf( buf, 40, "pos %.2f %.2f %.2f", 
+         player.rb.co[0], player.rb.co[1], player.rb.co[2] );
+   gui_text( (ui_px [2]){ 0, 40 }, buf, 1, k_text_align_left );
 
-   for( int y=0; y<16; y++ )
+   if( vg_gamepad_ready )
    {
-      vg_line( (v3f){ base[0] - 8, base[1], base[2]+y-8 },
-               (v3f){ base[0] + 8, base[1], base[2]+y-8 },
-               0x40ffffff );
+      for( int i=0; i<6; i++ )
+      {
+         snprintf( buf, 40, "%.2f", vg_gamepad.axes[i] );
+         gui_text( (ui_px [2]){ 0, (i+3)*20 }, buf, 1, k_text_align_left );
+      }
    }
-   for( int x=0; x<16; x++ )
+   else
    {
-      vg_line( (v3f){ base[0]+x-8, base[1], base[2]-8 },
-               (v3f){ base[0]+x-8, base[1], base[2]+8 },
-               0x40ffffff );
+      gui_text( (ui_px [2]){ 0, 60 }, 
+            "Gamepad not ready", 1, k_text_align_left );
    }
 }
 
-void vg_render(void) 
+void vg_ui(void)
 {
-   glViewport( 0,0, vg_window_x, vg_window_y );
-
-   glDisable( GL_DEPTH_TEST );
-   glClearColor( 0.1f, 0.0f, 0.2f, 1.0f );
-   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
-
-   v3f pos_inv;
-   v3_negate( player.view, pos_inv );
-
-   float speed = v3_length( player.v );
-   v3f shake = { vg_randf()-0.5f, vg_randf()-0.5f, vg_randf()-0.5f };
-   v3_muls( shake, speed*0.01f, shake );
-
-   m4x3_identity( world_matrix );
-   m4x3_rotate_x( world_matrix, 
-         freecam? player.look_dir[1]: 0.3f+shake[1]*0.04f );
-   m4x3_rotate_y( world_matrix, player.look_dir[0]+shake[0]*0.02f );
-   m4x3_translate( world_matrix, pos_inv );
-   
-   m4x4f world_4x4;
-   m4x3_expand( world_matrix, world_4x4 );
-
-   m4x4_projection( vg_pv, 
-         freecam? 90.0f: 130.0f,
-         (float)vg_window_x / (float)vg_window_y, 
-         0.01f, 1000.0f );
-   m4x4_mul( vg_pv, world_4x4, vg_pv );
-
-   if( debugroad )
-      draw_road_patch_dev( &road_main );
-
-   vg_line( (v3f){ 0.0f, 0.0f, 0.0f }, (v3f){ 1.0f, 0.0f, 0.0f }, 0xffff0000 );
-   vg_line( (v3f){ 0.0f, 0.0f, 0.0f }, (v3f){ 0.0f, 1.0f, 0.0f }, 0xff00ff00 );
-   vg_line( (v3f){ 0.0f, 0.0f, 0.0f }, (v3f){ 0.0f, 0.0f, 1.0f }, 0xff0000ff );
+   if( lightedit )
+   {
+      ui_global_ctx.cursor[0] = 10;
+      ui_global_ctx.cursor[1] = 10;
+      ui_global_ctx.cursor[2] = 200;
+      ui_global_ctx.cursor[3] = 20;
 
-   v3f board_fwd = { 0.0f, 0.0f, -1.0f };
-   v3f board_side = { 1.0f, 0.0f, 0.0f };
+      struct ub_world_lighting *wl = &gpipeline.ub_world_lighting;
+      struct ui_slider_vector 
+         s5 = { .min=0.0f, .max=2.0f, .len=3, .data=wl->g_ambient_colour };
 
-   m3x3_mulv( player.to_world, board_fwd, board_fwd );
-   m3x3_mulv( player.to_world, board_side, board_side );
+      struct ui_slider
+         s8 = { .min=0.0f, .max=2.0f, .data = &gpipeline.shadow_spread },
+         s9 = { .min=0.0f, .max=25.0f, .data = &gpipeline.shadow_length };
 
-   v3f bnw, bne, bse, bsw;
-   
-   v3_muladds( player.co, board_fwd, 0.75f, bnw );
-   v3_muladds( player.co, board_fwd, -0.75f, bsw );
-   v3_muladds( bnw, board_side,  0.1f, bne );
-   v3_muladds( bnw, board_side, -0.1f, bnw );
-   v3_muladds( bsw, board_side,  0.1f, bse );
-   v3_muladds( bsw, board_side, -0.1f, bsw );
-
-   vg_line( bnw, bne, 0xff00ff00 );
-   vg_line( bne, bse, 0xff00ff00 );
-   vg_line( bse, bsw, 0xff00ff00 );
-   vg_line( bsw, bnw, 0xff00ff00 );
+      for( int i=0; i<3; i++ )
+         run_light_widget( &gpipeline.widgets[i] );
 
-   glEnable( GL_DEPTH_TEST );
+      gui_text( ui_global_ctx.cursor, "Ambient", 1, 0 );
+      ui_global_ctx.cursor[1] += 16;
+      ui_slider_vector( &ui_global_ctx, &s5 );
 
-   SHADER_USE( shader_debug_vcol );
-   m4x3f temp;
-   m4x4f temp1;
+      gui_text( ui_global_ctx.cursor, "Shadows", 1, 0 );
+      ui_global_ctx.cursor[1] += 16;
+      ui_slider( &ui_global_ctx, &s8 );
+      ui_slider( &ui_global_ctx, &s9 );
 
-   vg_tex2d_bind( &tex_grid, 0 );
-   scene_tree_sway = 0.0f;
+      gui_text( ui_global_ctx.cursor, "Misc", 1, 0 );
+      ui_global_ctx.cursor[1] += 16;
+      struct ui_checkbox c1 = {.data = &wl->g_light_preview};
+      ui_checkbox( &ui_global_ctx, &c1 );
 
-   m4x3_identity( temp );
-   m4x3_expand( temp, temp1 );
-       glUniformMatrix4fv( SHADER_UNIFORM( shader_debug_vcol, "uMdl" ), 
-         1, GL_FALSE, (float *)temp1 );
-   scene_draw( &player_scene, -1, 0 );
+      render_update_lighting_ub();
+   }
    
-   m4x3_identity( temp );
-   m4x3_expand( temp, temp1 );
-       glUniformMatrix4fv( SHADER_UNIFORM( shader_debug_vcol, "uMdl" ), 
-         1, GL_FALSE, (float *)temp1 );
+   //glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
+   if( cl_ui )
+   {
+      render_world_routes_ui();
+   }
+   //glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
 
-   vg_tex2d_bind( &tex_norwey, 0 );
-   scene_tree_sway = 0.1f;
-   scene_draw( &test_scene, -1, 0 );
+#if 0
+   static double last_b_press = 0.0;
 
-   vg_tex2d_bind( &tex_grid, 0 );
-   scene_tree_sway = 0.0f;
-   scene_draw( &world_scene, world_terrain_count, 0 );
+   double localtime = vg_time - last_b_press;
 
-   vg_tex2d_bind( &tex_road, 0 );
-   scene_draw( &world_scene, world_road_count, world_terrain_count );
+   world_routes_ui_updatetime( 0, localtime );
+   world_routes_ui_draw( 0, (v4f){ 1.0f,0.0f,1.0f,1.0f}, 9.0f );
 
-   glDisable( GL_DEPTH_TEST );
-}
+   if( glfwGetKey(vg_window,GLFW_KEY_B) )
+      world_routes_ui_notch( 0, localtime );
 
-void vg_ui(void)
-{
-   char buf[20];
+   if( vg_time-last_b_press > 1.0 )
+      if( glfwGetKey(vg_window,GLFW_KEY_N) )
+      {
+         last_b_press = vg_time;
+         world_routes_ui_newseg( 0 );
+      }
 
-   snprintf( buf, 20, "%.2fm/s", v3_length( player.v ) );
-   gui_text( (ui_px [2]){ 0, 0 }, buf, 1, k_text_align_left );
-   
-   snprintf( buf, 20, "%.2f %.2f %.2f m/s", 
-         player.a[0], player.a[1], player.a[2] );
+   static double last_m_press;
+   if( vg_time-last_m_press > 1.0 )
+      if( glfwGetKey( vg_window, GLFW_KEY_M) )
+      {
+         last_m_press = vg_time;
 
-   gui_text( (ui_px [2]){ 0, 20 }, buf, 1, k_text_align_left );
+         vg_info( "start: %u\n",world.routes.routes[0].ui.segment_count );
+         for( int i=0; i<world.routes.routes[0].ui.segment_count; i++ )
+            world_routes_ui_popfirst(0);
 
-   if( vg_gamepad_ready )
-   {
-      for( int i=0; i<6; i++ )
-      {
-         snprintf( buf, 20, "%.2f", vg_gamepad.axes[i] );
-         gui_text( (ui_px [2]){ 0, (i+2)*20 }, buf, 1, k_text_align_left );
+         vg_info( "new: %u\n",world.routes.routes[0].ui.segment_count );
       }
-   }
-   else
-   {
-      gui_text( (ui_px [2]){ 0, 40 }, 
-            "Gamepad not ready", 1, k_text_align_left );
-   }
+#endif
 }
-
-void vg_free(void){}