CHGICKEN
[carveJwlIkooP6JGAAIwe30JlM.git] / main.c
diff --git a/main.c b/main.c
index 57728587415bb3237d8a9a668c5459fa6cdaff7a..aef6dabf24c49d8e46008c94410a79ed1a449f5e 100644 (file)
--- a/main.c
+++ b/main.c
@@ -31,13 +31,14 @@ static int replay_buffer_frame = 0;
 #include "road.h"
 #include "scene.h"
 #include "ik.h"
-#include "character.h"
 #include "terrain.h"
+#include "character.h"
 #include "ragdoll.h"
 #include "rigidbody.h"
 #include "render.h"
 #include "gate.h"
 #include "water.h"
+#include "world.h"
 
 #include "shaders/blit.h"
 #include "shaders/standard.h"
@@ -110,15 +111,6 @@ static struct gplayer
 }
 player;
 
-static struct gworld
-{
-   scene geo;
-   submodel sm_road, sm_terrain;
-   glmesh skybox;
-
-   v3f tutorial;
-}
-world;
 
 static struct grender
 {
@@ -129,6 +121,7 @@ static struct grender
 }
 render;
 
+#if 0
 rigidbody mr_box = {
    .bbx = {{ -1.0f, -0.25f, -0.25f }, { 1.0f, 0.25f, 0.25f }}
 };
@@ -145,6 +138,7 @@ gate_b = {
    .co = { -8.0f, -3.0f, -17.0f },
    .q = { 0.0f, 0.0f, 0.0f, 1.0f }
 };
+#endif
 
 static void player_transform_update(void)
 {
@@ -192,42 +186,6 @@ static int playermodel( int argc, char const *argv[] )
    return 1;
 }
 
-static void create_renderbuffer_std( GLuint *fb, GLuint *rgb, GLuint *rb )
-{
-   glGenFramebuffers( 1, fb );
-   glBindFramebuffer( GL_FRAMEBUFFER, *fb );
-
-   glGenTextures( 1, rgb );
-   glBindTexture( GL_TEXTURE_2D, *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, *rgb, 0);
-
-   /* TODO: Check for DEPTH32f availiblity and use if possible */
-
-   glGenRenderbuffers( 1, rb );
-   glBindRenderbuffer( GL_RENDERBUFFER, *rb );
-   glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 
-         vg_window_x, vg_window_y );
-
-   glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
-         GL_RENDERBUFFER, *rb );
-}
-
-static void resize_renderbuffer_std( GLuint *fb, GLuint *rgb, GLuint *rb )
-{
-   glBindTexture( GL_TEXTURE_2D, *rgb );
-   glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, vg_window_x, vg_window_y, 0,
-         GL_RGB, GL_UNSIGNED_BYTE, NULL );
-
-   glBindRenderbuffer( GL_RENDERBUFFER, *rb );
-   glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 
-         vg_window_x, vg_window_y );
-}
 
 void vg_start(void)
 {
@@ -235,9 +193,11 @@ void vg_start(void)
 
    vg_tex2d_init( texture_list, vg_list_size( texture_list ) );
 
+#if 0
    rb_init( &mr_box );
    rb_init( &mrs_box );
    mrs_box.co[2] += 2.0f;
+#endif
 
    vg_convar_push( (struct vg_convar){
       .name = "frame",
@@ -322,58 +282,7 @@ void vg_start(void)
    character_load( &player.mdl, "ch_default" );
    character_init_ragdoll( &player.mdl );
    
-   /* Setup scene */
-   scene_init( &world.geo );
-   model *mworld = vg_asset_read( "models/mp_dev.mdl" );
-
-   scene_add_model( &world.geo, mworld, submodel_get( mworld, "mp_dev" ),
-         (v3f){0.0f,0.0f,0.0f}, 0.0f, 1.0f );
-   scene_copy_slice( &world.geo, &world.sm_road );
-
-   scene_add_model( &world.geo, mworld, submodel_get( mworld, "terrain" ),
-         (v3f){0.0f,0.0f,0.0f}, 0.0f, 1.0f );
-   scene_copy_slice( &world.geo, &world.sm_terrain );
-
-   v3_copy( model_marker_get( mworld, "mp_dev_tutorial" )->co, world.tutorial );
-
-
-   /* GATE DEV */
-   {
-      model_marker *ga = model_marker_get(mworld,"gate_a"),
-                   *gb = model_marker_get(mworld,"gate_a_recv");
-
-      v3_copy( ga->co, gate_a.co );
-      v3_copy( gb->co, gate_b.co );
-      v4_copy( ga->q, gate_a.q );
-      v4_copy( gb->q, gate_b.q );
-      v2_copy( ga->s, gate_a.dims );
-      v2_copy( gb->s, gate_b.dims );
-
-      gate_a.other = &gate_b;
-      gate_b.other = &gate_a;
-
-      gate_transform_update( &gate_a );
-      gate_transform_update( &gate_b );
-   }
-
-   /* WATER DEV */
-   {
-      glmesh surf;
-      submodel *sm = submodel_get(mworld,"mp_dev_water");
-      model_unpack_submodel( mworld, &surf, sm );
-            
-      water_init( create_renderbuffer_std );
-      water_set_surface( &surf, sm->pivot[1] );
-   }
-   {
-      model *msky = vg_asset_read("models/rs_skydome.mdl");
-      model_unpack( msky, &world.skybox );
-      free(msky);
-   }
-
-   free( mworld );
-   scene_upload( &world.geo );
-   bvh_create( &world.geo );
+   world_init_default();
 
    reset_player( 1, (const char *[]){ "tutorial" } );
    player_transform_update();
@@ -394,6 +303,7 @@ void vg_start(void)
          render.rgb_background, 0);
 
    gate_init( create_renderbuffer_std );
+   terrain_init();
 
    {
       float quad[] = { 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f,
@@ -427,7 +337,7 @@ static void player_freecam(void)
    m4x3_mulv( cam_rot, lookdir, lookdir );
    m4x3_mulv( cam_rot, sidedir, sidedir );
    
-   float movespeed = 5.0f;
+   float movespeed = 15.0f;
    static v2f mouse_last,
               view_vel = { 0.0f, 0.0f };
 
@@ -468,11 +378,6 @@ static void apply_gravity( v3f vel, float const timestep )
    v3_muladds( vel, gravity, timestep, vel );
 }
 
-static int ray_hit_is_ramp( ray_hit *hit )
-{
-   return hit->tri[0] < world.sm_road.vertex_count;
-}
-
 static void player_start_air(void)
 {
    player.in_air = 1;
@@ -526,7 +431,7 @@ static void player_start_air(void)
          contact.dist = v3_length( vdir );
          v3_divs( vdir, contact.dist, vdir);
 
-         if( bvh_raycast( &world.geo, pco1, vdir, &contact ))
+         if( ray_world( pco1, vdir, &contact ))
          {
             float land_delta = v3_dot( pv, contact.normal );
             u32 scolour = (u8)(vg_minf(-land_delta * 2.0f, 255.0f));
@@ -571,9 +476,12 @@ static int sample_if_resistant( v3f pos )
 {
    v3f ground;
    v3_copy( pos, ground );
+   ground[1] += 4.0f;
    
    ray_hit hit;
-   if( bvh_scene_sample( &world.geo, ground, &hit ) )
+   hit.dist = INFINITY;
+
+   if( ray_world( ground, (v3f){0.0f,-1.0f,0.0f}, &hit ))
    {
       v3f angle;
       v3_copy( player.v, angle );
@@ -582,7 +490,7 @@ static int sample_if_resistant( v3f pos )
 
       if( resistance < 0.25f )
       {
-         v3_copy( ground, pos );
+         v3_copy( hit.pos, pos );
          return 1;
       }
    }
@@ -748,11 +656,13 @@ static void player_physics_air(void)
 
    v3f ground_pos;
    v3_copy( player.co, ground_pos );
+   ground_pos[1] += 4.0f;
    
    ray_hit hit;
-   if( bvh_scene_sample( &world.geo, ground_pos, &hit ) )
+   hit.dist = INFINITY;
+   if( ray_world( ground_pos, (v3f){0.0f,-1.0f,0.0f}, &hit ))
    {
-      if( ground_pos[1] > player.co[1] )
+      if( hit.pos[1] > player.co[1] )
       {
          player.in_air = 0;
          
@@ -796,7 +706,7 @@ static void player_physics_air(void)
       v3_divs( vdir, contact.dist, vdir);
       
       float orig_dist = contact.dist;
-      if( bvh_raycast( &world.geo, pco1, vdir, &contact ))
+      if( ray_world( pco1, vdir, &contact ))
       {
          v3f localup;
          m3x3_mulv( player.to_world, (v3f){0.0f,1.0f,0.0f}, localup );
@@ -905,6 +815,7 @@ static void player_update(void)
 
    player.iY = 0.0f; /* temp */
 
+#if 0
    /* GATE COLLISION */
    if( gate_intersect( &gate_a, player.co, prevco ) )
    {
@@ -922,6 +833,7 @@ static void player_update(void)
       m3x3_q( transport, transport_rotation );
       q_mul( transport_rotation, player.rot, player.rot );
    }
+#endif
 
    /* Camera and character */
    player_transform_update();
@@ -956,8 +868,9 @@ void vg_update(void)
       character_debug_ragdoll( &player.mdl );
 
       if( player.is_dead )
-         character_ragdoll_iter( &player.mdl, &world.geo );
+         character_ragdoll_iter( &player.mdl );
 
+#if 0
       rb_build_manifold( &mr_box, &world.geo );
       rb_build_manifold( &mrs_box, &world.geo );
       rb_constraint_manifold( &mr_box );
@@ -971,6 +884,7 @@ void vg_update(void)
 
       rb_update_transform( &mr_box );
       rb_update_transform( &mrs_box );
+#endif
 
       clock = 0;
    }
@@ -1193,40 +1107,8 @@ static void vg_framebuffer_resize( int w, int h )
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, 
          GL_RGB, GL_UNSIGNED_BYTE, NULL );
 
-   gate_fb_resize( resize_renderbuffer_std );
-   water_fb_resize( resize_renderbuffer_std );
-}
-
-static void render_world( m4x4f projection )
-{
-   m4x3f identity_matrix;
-   m4x3_identity( identity_matrix );
-
-   shader_unlit_use();
-   shader_unlit_uPv( projection );
-   shader_unlit_uTexMain( 0 );
-   vg_tex2d_bind( &tex_sky, 0 );
-   
-   glDepthMask(GL_FALSE);
-   glDisable(GL_DEPTH_TEST);
-
-   mesh_bind( &world.skybox );
-   mesh_draw( &world.skybox );
-
-   glEnable(GL_DEPTH_TEST);
-   glDepthMask(GL_TRUE);
-
-   
-   shader_standard_use();
-   shader_standard_uPv( projection );
-   shader_standard_uMdl( identity_matrix );
-
-   vg_tex2d_bind( &tex_grid, 0 );
-   shader_standard_uTexMain( 0 );
-   shader_standard_uColour( (v4f){0.4f,0.4f,0.4f,1.0f} );
-
-   scene_bind( &world.geo );
-   scene_draw( &world.geo );
+   gate_fb_resize();
+   water_fb_resize();
 }
 
 void vg_render(void) 
@@ -1308,7 +1190,7 @@ void vg_render(void)
    gpipeline.fov = freecam? 60.0f: 120.0f;
    m4x4_projection( vg_pv, gpipeline.fov, 
          (float)vg_window_x / (float)vg_window_y, 
-         0.1f, 1000.0f );
+         0.025f, 1000.0f );
 
    m4x4_mul( vg_pv, world_4x4, vg_pv );
 
@@ -1325,14 +1207,16 @@ void vg_render(void)
    m4x3f cam_transform;
    m4x3_invert_affine( world_matrix, cam_transform );
 
-   render_world( vg_pv );
+   render_world( vg_pv, cam_transform );
    render_water_texture( cam_transform );
 
    glBindFramebuffer( GL_FRAMEBUFFER, 0 );
    render_water_surface( vg_pv );
 
+#if 0
    vg_tex2d_bind( &tex_water, 1 );
    render_gate( &gate_a, cam_transform );
+#endif
 
    
    /* Copy the RGB of what we have into the background buffer */