X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=main.c;h=def609d6c915b063db19fba3a135ea68c810261f;hb=dfee9022b3513fddec36f7ea70867ee5961a44da;hp=b397e2e9ec759a7af26589d5df62ef540ce106c6;hpb=3f59dc191c7792947cd2d3f6588be06933fd49f0;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/main.c b/main.c index b397e2e..def609d 100644 --- a/main.c +++ b/main.c @@ -1,84 +1,78 @@ -#define VG_3D -#include "vg/vg.h" +#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; /* Components */ #include "road.h" #include "scene.h" +#include "ik.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 "player.h" + +#include "shaders/blit.h" +#include "shaders/standard.h" +#include "shaders/unlit.h" + +void vg_register(void) +{ + shader_blit_register(); + shader_standard_register(); + shader_unlit_register(); + + terrain_register(); + character_register(); + water_register(); + gate_register(); +} + +vg_tex2d *texture_list[] = +{ + &tex_norwey, + &tex_gradients, + &tex_grid, + &tex_sky, + &tex_cement, + &tex_water, + &tex_water_surf +}; int main( int argc, char *argv[] ) { vg_init( argc, argv, "Voyager Game Engine" ); } -m4x3f world_matrix; -v3f player_pos; -v3f player_head; /* Relative to pos */ -v3f player_vel = { 0.0f, 0.0f, -0.2f }; -float player_yaw; -v2f look_dir; -v3f player_accel; - -float waves[128][128]; - -road_patch road_main; -scene test_scene; -scene world_scene; -scene player_scene; -u32 world_terrain_count, - world_road_count; - -static int reset_player( int argc, char const *argv[] ) +static int playermodel( int argc, char const *argv[] ) { - v3_zero( player_pos ); - v3_copy( (v3f){ 0.0f, 0.0f, -0.2f }, player_vel ); - player_yaw = 0.0f; + if( argc < 1 ) return 0; + + glmesh old_mesh = player.mdl.mesh; - return 0; -} + if( character_load( &player.mdl, argv[0] ) ) + mesh_free( &old_mesh ); -void vg_register(void) -{ - scene_register(); + return 1; } void vg_start(void) { - for( int y=0; y<128; y++ ) - { - for( int x=0; x<128; x++ ) - { - v2f pos = {x,y}; - - waves[x][y] = sinf( pos[0] * 0.1f ) * - cosf( pos[1] * 0.3f ) * 5.0f + x*-0.2f; - } - } - - vg_tex2d_init( texture_list, vg_list_size( texture_list ) ); - - road_patch_init( &road_main ); - road_generate( &road_main ); - vg_convar_push( (struct vg_convar){ .name = "freecam", .data = &freecam, @@ -88,26 +82,34 @@ 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 = "walk_speed", + .data = &k_walkspeed, + .data_type = k_convar_dtype_f32, + .opt_f32 = { .clamp = 0 }, + .persistent = 1 + }); + + vg_convar_push( (struct vg_convar){ + .name = "debugcam", + .data = &sv_debugcam, .data_type = k_convar_dtype_i32, - .opt_i32 = { .min=0, .max=1, .clamp=1 }, + .opt_i32 = { .min=0, .max=1, .clamp=0 }, .persistent = 1 }); vg_convar_push( (struct vg_convar){ - .name = "debugroad", - .data = &debugroad, + .name = "debugview", + .data = &debugview, .data_type = k_convar_dtype_i32, - .opt_i32 = { .min=0, .max=1, .clamp=1 }, + .opt_i32 = { .min=0, .max=1, .clamp=0 }, .persistent = 1 }); @@ -115,570 +117,156 @@ void vg_start(void) .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 ); - } - } - - world_terrain_count = world_scene.indice_count; - - 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; - free( test ); - free( world ); - -#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 - - scene_upload( &player_scene ); - scene_upload( &world_scene ); - scene_upload( &test_scene ); -} - -static float sample_terrain( v3f pos ) -{ - v3f local; - v3_muls( pos, 0.1f, local ); + vg_tex2d_init( texture_list, vg_list_size( texture_list ) ); - v2i ico = { local[0], local[2] }; - for( int i=0; i<2; i++ ) - { - if( ico[i] < 0 ) ico[i] = 0; - if( ico[i] > 126 ) ico[i] = 126; - } + render_init(); + gate_init(); + terrain_init(); + character_init(); - float hse = waves[ico[0]+1][ico[1]], - hsw = waves[ico[0]][ico[1]], - hne = waves[ico[0]+1][ico[1]+1], - hnw = waves[ico[0]][ico[1]+1]; + character_load( &player.mdl, "ch_default" ); + character_init_ragdoll( &player.mdl ); - v3f subcoord; - v3_floor( local, subcoord ); - v3_sub( local, subcoord, subcoord ); + world_load(); - float hs = vg_lerpf( hse, hsw, 1.0f-subcoord[0] ), - hn = vg_lerpf( hne, hnw, 1.0f-subcoord[0] ), - sampleh = vg_lerpf( hs, hn, subcoord[2] ); - - return sampleh*10.0f; + reset_player( 1, (const char *[]){ "tutorial" } ); + player_transform_update(); } -static void draw_terrain(void) +void vg_free(void) { - float sf = 10.0f; - - /* Draw waves - */ - for( int y=0; y<63; y++ ) - { - for( int x=0; x<63; x++ ) - { - v2i pos = {x*2,y*2}; - vg_line( (v3f){ pos[0]*sf, waves[pos[0]][pos[1]]*sf, pos[1]*sf }, - (v3f){ (pos[0]+2)*sf, waves[pos[0]+2][pos[1]]*sf, pos[1]*sf }, - 0xa0ffffff ); - - vg_line( (v3f){ pos[1]*sf, waves[pos[1]][pos[0]]*sf, pos[0]*sf }, - (v3f){ pos[1]*sf, waves[pos[1]][pos[0]+2]*sf, (pos[0]+2)*sf }, - 0xa0ffffff ); - } - } - + vg_tex2d_free( texture_list, vg_list_size(texture_list) ); } -v3f head, butt, knee_r, knee_l, foot_r, foot_l, hand_r, hand_l, - shoulder_r, shoulder_l; - - void vg_update(void) { - float timestep = 1.0f/60.0f; - - if( freecam ) - { - m4x3f cam_rot; - m4x3_identity( cam_rot ); - m4x3_rotate_y( cam_rot, -look_dir[0] ); - m4x3_rotate_x( cam_rot, -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" ) ) - { - v2f delta; - v2_sub( vg_mouse, mouse_last, delta ); - v2_copy( vg_mouse, mouse_last ); - - v2_muladds( view_vel, delta, 0.005f, view_vel ); - } - - v2_muls( view_vel, 0.75f, view_vel ); - v2_add( view_vel, look_dir, look_dir ); - look_dir[1] = vg_clampf( 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_head, player_head ); - - return; - } - - if( vg_get_button( "forward" ) ) - { - v3f accel = { sinf( player_yaw ), 0.0f, -cosf( player_yaw ) }; - v3_muladds( player_vel, accel, 5.0f * timestep, player_vel ); - } - - m4x3f player_transform, - world_to_local, local_to_world; - - m4x3_identity( player_transform ); - m4x3_translate( player_transform, player_pos ); - m4x3_rotate_y( player_transform, -player_yaw ); - - m4x3_identity( world_to_local ); - m4x3_rotate_y( world_to_local, player_yaw ); - - m4x3_identity( local_to_world ); - m4x3_rotate_y( local_to_world, -player_yaw ); - - /* Get front and back contact points */ - v3f contact_front, contact_back, fwd, fwd1, contact_norm; - - m4x3_mulv( local_to_world, (v3f){0.0f,0.0f,-1.0f}, fwd ); - m4x3_mulv( local_to_world, (v3f){0.03f,0.0f,-1.0f}, fwd1 ); - - v3_muladds( player_pos, fwd, 1.0f, contact_front ); - v3_muladds( player_pos, fwd,-1.0f, contact_back ); - v3_muladds( player_pos, fwd1, 1.0f, contact_norm ); - -#if 0 - road_patch_setplayer( &road_main, player_pos ); - sample_road_height( &road_main, contact_front ); - sample_road_height( &road_main, contact_back ); -#else - sample_scene_height( &world_scene, contact_front ); - sample_scene_height( &world_scene, contact_back ); - sample_scene_height( &world_scene, contact_norm ); -#endif - - 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 ); - - v3f gravity = { 0.0f, -9.6f, 0.0f }; - v3_muladds( player_vel, gravity, timestep, player_vel ); - - v3f ground_pos; - v3_copy( player_pos, ground_pos ); - sample_scene_height( &world_scene, ground_pos ); - - static int in_air = 1; - - if( in_air ) - { - if( ground_pos[1] > player_pos[1] ) - in_air = 0; - } - - if( !in_air ) - { - float resistance = v3_dot( norm, player_vel ); - - if( resistance >= 0.0f ) - in_air = 1; - else - { - v3_muladds( player_vel, norm, -resistance, player_vel ); - } - } - - /* vg_info( "%.3f | %.3f\n", player_vel[1], resistance ); */ - v3_muladds( player_pos, player_vel, timestep, player_pos ); - - float slip = 0.0f; - - if( !in_air ) - { - player_pos[1] = (contact_front[1]+contact_back[1])*0.5f; - - vg_line( player_pos, contact_front, 0xff00ffff ); - vg_line( player_pos, contact_back, 0xff00ffa0 ); - - /* Create the 'travel' vector */ - v3f travel; - v3_sub( contact_front, contact_back, travel ); - v3_normalize( travel ); - - /* Apply gravity */ -#if 0 - float gravity_conversion = -v3_dot( travel, gravity ); - vel[2] += gravity_conversion * substep; -#endif - - /* Get localized (rotated) rigidbody forces - * -z - * ^ - * -|- - * | - * +z - */ - - v3f vel; - m4x3_mulv( world_to_local, player_vel, 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; - } - - m4x3_mulv( local_to_world, vel, player_vel ); - - if( vg_get_button( "yawl" ) ) - player_yaw -= 1.6f * timestep; - if( vg_get_button( "yawr" ) ) - player_yaw += 1.6f * timestep; - - player_yaw += vg_get_axis( "horizontal" ) * 1.6f * timestep; - } - else - { - player_yaw += vg_get_axis( "horizontal" ) * 3.6f * timestep; - look_dir[0] = player_yaw; - } - - look_dir[0] = atan2f( player_vel[0], -player_vel[2] ); - - /* 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; - - v3_sub( player_vel, last_vel, player_accel ); - v3_copy( player_vel, last_vel ); - v3_add( bob, player_accel, bob ); - - 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 ); - - v3_lerp( bob, (v3f){ 0.0f, 0.0f, 0.0f }, 0.1f, bob ); - v3_lerp( bob1, bob, 0.1f, bob1 ); - - /* Feet */ - foot_r[0] = 0.0f; - foot_r[1] = 0.0f; - foot_r[2] = 0.3f; - - foot_l[0] = 0.0f; - foot_l[1] = 0.0f; - foot_l[2] = -0.3f; - - /* 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; - - /* Hips */ - butt[0] = -sinf(slip)*0.2f; - butt[1] = cosf(slip); - butt[2] = 0.0f; - v2_normalize(butt); - v2_muls( butt, -0.7f, butt ); - v2_add( head, butt, butt ); - - /* Knees */ - v2_sub( butt, (v2f){0.0f,0.0f}, ac ); - float cl = v2_length( ac ), - d = acosf( (2.0f * kleg*kleg - cl*cl) / 2.0f * kleg*kleg ), - x = atan2f( ac[0], ac[1] ), - ad = (-VG_PIf-d)/2.0f; - - v2_muladds( (v2f){0.0f,0.0f}, - (v2f){ sinf( ad+x ), cosf( ad + x ) }, - kleg, knee_l ); - knee_l[2] = -0.3f; - - v2_copy( knee_l, knee_r ); - knee_r[2] = 0.3f; - - /* shoulders */ - v3_add( (v3f){0.0f,-0.1f,-0.2f}, head, shoulder_r ); - - /* Hands */ - hand_r[0] = sinf( slip ) * 0.1f; - hand_r[1] = -cosf( slip*5.0f ) * 0.5f - 0.5f; - hand_r[2] = -sinf( fabsf(slip) * 2.4f ); - v3_add( shoulder_r, hand_r, hand_r ); - - m4x3_mulv( player_transform, head, head ); - m4x3_mulv( player_transform, butt, butt ); - m4x3_mulv( player_transform, knee_l, knee_l ); - m4x3_mulv( player_transform, knee_r, knee_r ); - m4x3_mulv( player_transform, foot_l, foot_l ); - m4x3_mulv( player_transform, foot_r, foot_r ); - m4x3_mulv( player_transform, hand_r, hand_r ); - m4x3_mulv( player_transform, shoulder_r, shoulder_r ); - - v3_copy( head, player_head ); - + player_update(); } -static void debug_grid( v3f at ) +static void vg_framebuffer_resize( int w, int h ) { - v3f base; - v3_floor( at, base ); - - for( int y=0; y<16; y++ ) - { - 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 x=0; x<16; x++ ) - { - vg_line( (v3f){ base[0]+x-8, base[1], base[2]-8 }, - (v3f){ base[0]+x-8, base[1], base[2]+8 }, - 0x40ffffff ); - } + render_fb_resize(); + gate_fb_resize(); + water_fb_resize(); } void vg_render(void) { + glBindFramebuffer( GL_FRAMEBUFFER, 0 ); 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_head, pos_inv ); + glClearColor( 0.11f, 0.35f, 0.37f, 1.0f ); + glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT ); - float speed = v3_length( player_vel ); + float speed = freecam? 0.0f: 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? look_dir[1]: 0.3f+shake[1]*0.04f ); - m4x3_rotate_y( world_matrix, look_dir[0]+shake[0]*0.02f ); - m4x3_translate( world_matrix, pos_inv ); - m4x4f world_4x4; - m4x3_expand( world_matrix, world_4x4 ); + m4x3_expand( player.camera_inverse, world_4x4 ); - m4x4_projection( vg_pv, - freecam? 90.0f: 130.0f, + gpipeline.fov = freecam? 60.0f: 120.0f; /* 120 */ + m4x4_projection( vg_pv, gpipeline.fov, (float)vg_window_x / (float)vg_window_y, - 0.01f, 1000.0f ); - m4x4_mul( vg_pv, world_4x4, vg_pv ); + 0.025f, 1000.0f ); - if( debugroad ) - draw_road_patch_dev( &road_main ); + m4x4_mul( vg_pv, world_4x4, vg_pv ); 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 board_fwd = { -sinf( player_yaw ), 0.0f, cosf( player_yaw ) }; - v3f board_side = { -board_fwd[2], 0.0f, board_fwd[0] }; - v3f bnw, bne, bse, bsw; - - v3_muladds( player_pos, board_fwd, 0.75f, bnw ); - v3_muladds( player_pos, 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 ); - glEnable( GL_DEPTH_TEST ); + + /* + * Draw world + */ - v3f leg_dr, leg_dl, leg_ar, leg_al; - - v3_sub( foot_l, butt, leg_dl ); - v3_sub( foot_r, butt, leg_dr ); - leg_dl[1] = 0.0f; - leg_dr[1] = 0.0f; - v3_normalize( leg_dl ); - v3_normalize( leg_dr ); - - v3f v0; - v3_sub( butt, knee_l, v0 ); - float leg_pl = atan2f( v0[1], v3_dot( v0, leg_dl ) ); - - v3_sub( knee_l, foot_l, v0 ); - float knee_pl = atan2f( v0[1], v3_dot( v0, leg_dl ) ); + render_world( vg_pv, player.camera ); + render_water_texture( player.camera ); - float leg_yl = -atan2f( leg_dl[2], leg_dl[0] ), - leg_yr = atan2f( leg_dr[2], leg_dr[0] ); + glBindFramebuffer( GL_FRAMEBUFFER, 0 ); + render_water_surface( vg_pv, player.camera ); - SHADER_USE( shader_debug_vcol ); - m4x3f temp; - m4x4f temp1; + vg_tex2d_bind( &tex_water, 1 ); - vg_tex2d_bind( &tex_grid, 0 ); - scene_tree_sway = 0.0f; + for( int i=0; i