X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;ds=sidebyside;f=main.c;h=def609d6c915b063db19fba3a135ea68c810261f;hb=dfee9022b3513fddec36f7ea70867ee5961a44da;hp=e34ca5fff6b823dd7ec08f2cf7b3b81ccf066e7c;hpb=04a309d5d25508cc47e796e84fad6703b21fe15a;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/main.c b/main.c index e34ca5f..def609d 100644 --- a/main.c +++ b/main.c @@ -1,98 +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" -int main( int argc, char *argv[] ) -{ - vg_init( argc, argv, "Voyager Game Engine" ); -} - -m4x3f world_matrix; - -#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 - -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_unlit_register(); + + terrain_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) +vg_tex2d *texture_list[] = { - q_m3x3( player.rot, player.to_world ); - v3_copy( player.co, player.to_world[3] ); + &tex_norwey, + &tex_gradients, + &tex_grid, + &tex_sky, + &tex_cement, + &tex_water, + &tex_water_surf +}; - v4f inv; - q_inv( player.rot, inv ); - q_m3x3( inv, player.to_local ); - v3_negate( player.co, player.to_local[3] ); +int main( int argc, char *argv[] ) +{ + vg_init( argc, argv, "Voyager Game Engine" ); } -static int reset_player( int argc, char const *argv[] ) +static int playermodel( int argc, char const *argv[] ) { - v3_zero( player.co ); - v3_copy( (v3f){ 0.0f, 0.0f, -0.2f }, player.v ); - q_identity( player.rot ); + if( argc < 1 ) return 0; + + glmesh old_mesh = player.mdl.mesh; - player_transform_update(); - 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) { - 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, @@ -102,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 }); @@ -129,408 +117,128 @@ 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; + vg_tex2d_init( texture_list, vg_list_size( texture_list ) ); - 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; + render_init(); + gate_init(); + terrain_init(); + character_init(); - free( test ); - free( world ); + character_load( &player.mdl, "ch_default" ); + character_init_ragdoll( &player.mdl ); -#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 + world_load(); - scene_upload( &player_scene ); - scene_upload( &world_scene ); - scene_upload( &test_scene ); - - reset_player( 0, NULL ); + reset_player( 1, (const char *[]){ "tutorial" } ); player_transform_update(); } -v3f head; +void vg_free(void) +{ + vg_tex2d_free( texture_list, vg_list_size(texture_list) ); +} void vg_update(void) { - float timestep = 1.0f/60.0f; - - if( freecam ) - { - 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" ) ) - { - 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, 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" ) ) - { - 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 ); - } - - /* 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 ); - - 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 ); - - 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.v, gravity, timestep, player.v ); - - v3f ground_pos; - v3_copy( player.co, ground_pos ); - sample_scene_height( &world_scene, ground_pos ); - - static int in_air = 1; - - if( in_air ) - { - if( ground_pos[1] > player.co[1] ) - in_air = 0; - } - - if( !in_air ) - { - float resistance = v3_dot( norm, player.v ); - - if( resistance >= 0.0f ) - in_air = 1; - else - { - v3_muladds( player.v, norm, -resistance, player.v ); - } - } - - /* vg_info( "%.3f | %.3f\n", player.v[1], resistance ); */ - v3_muladds( player.co, player.v, timestep, player.co ); - - float slip = 0.0f; - float yawamt = 0.0f; - - if( !in_air ) - { - player.co[1] = (contact_front[1]+contact_back[1])*0.5f; - - vg_line( player.co, contact_front, 0xff00ffff ); - vg_line( player.co, 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; - 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 ); - - 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; - - } - else - { - yawamt -= vg_get_axis( "horizontal" ) * 3.6f * timestep; - } - - v4f rotate; - q_axis_angle( rotate, vup, yawamt ); - q_mul( rotate, player.rot, player.rot ); - - player.look_dir[0] = atan2f( player.v[0], -player.v[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.v, last_vel, player.a ); - v3_copy( player.v, last_vel ); - v3_add( bob, player.a, 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 ); - - /* 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(); + 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 ); + glClearColor( 0.11f, 0.35f, 0.37f, 1.0f ); + glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT ); - v3f pos_inv; - v3_negate( player.view, pos_inv ); - - float speed = v3_length( player.v ); + 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? 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 ); + 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 = { 0.0f, 0.0f, -1.0f }; - v3f board_side = { 1.0f, 0.0f, 0.0f }; - - m3x3_mulv( player.to_world, board_fwd, board_fwd ); - m3x3_mulv( player.to_world, board_side, board_side ); - - v3f bnw, bne, bse, bsw; + glEnable( GL_DEPTH_TEST ); - 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 ); + /* + * Draw world + */ - glEnable( GL_DEPTH_TEST ); + render_world( vg_pv, player.camera ); + render_water_texture( player.camera ); - SHADER_USE( shader_debug_vcol ); - m4x3f temp; - m4x4f temp1; + glBindFramebuffer( GL_FRAMEBUFFER, 0 ); + render_water_surface( vg_pv, player.camera ); - vg_tex2d_bind( &tex_grid, 0 ); - scene_tree_sway = 0.0f; + vg_tex2d_bind( &tex_water, 1 ); - 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 ); - - m4x3_identity( temp ); - m4x3_expand( temp, temp1 ); - glUniformMatrix4fv( SHADER_UNIFORM( shader_debug_vcol, "uMdl" ), - 1, GL_FALSE, (float *)temp1 ); + for( int i=0; i