X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=world_gate.h;h=b6942519c4fe93059a9f881284dcd51db9a799d3;hb=34a8df54eb962f3ad2e036355041f5bc5cabe5a0;hp=89945b94a031a16b467db081f5b18503c4cb44ec;hpb=b9dedb4dd2a1e94ae76a3986716ee3c57e568213;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/world_gate.h b/world_gate.h index 89945b9..b694251 100644 --- a/world_gate.h +++ b/world_gate.h @@ -1,37 +1,19 @@ +/* + * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved + */ + #ifndef WORLD_GATE_H #define WORLD_GATE_H -#define GATE_RENDER_PERFORMANCE - #include "common.h" #include "model.h" #include "render.h" +#include "camera.h" -#ifndef GATE_RENDER_PERFORMANCE - #include "shaders/gate.h" -#else - #include "shaders/gatelq.h" -#endif - +#include "shaders/model_gate.h" #include "world_water.h" - -typedef struct teleport_gate teleport_gate; - -static struct -{ - struct framebuffer fb; - glmesh mdl; -} -grender = -{ - .fb = { - .format = GL_RGB, - .div = 1 - } -}; - -static void gate_transform_update( teleport_gate *gate ) +VG_STATIC void gate_transform_update( teleport_gate *gate ) { m4x3f to_local; @@ -45,177 +27,132 @@ static void gate_transform_update( teleport_gate *gate ) m4x3_mul( gate->recv_to_world, to_local, gate->transport ); } -static void world_gates_init(void) +VG_STATIC void world_gates_init(void) { vg_info( "world_gates_init\n" ); -#ifndef GATE_RENDER_PERFORMANCE - shader_gate_register(); -#else - shader_gatelq_register(); -#endif + shader_model_gate_register(); - mdl_header *mgate = mdl_load( "models/rs_gate.mdl" ); + vg_linear_clear( vg_mem.scratch ); + mdl_context *mgate = mdl_load_full( vg_mem.scratch, "models/rs_gate.mdl" ); vg_acquire_thread_sync(); { - fb_init( &grender.fb ); - mdl_unpack_glmesh( mgate, &grender.mdl ); + mdl_unpack_glmesh( mgate, &world_global.mesh_gate_surface ); } vg_release_thread_sync(); } -static void world_gates_free(void*_) -{ - fb_free( &grender.fb ); -} - -static void gate_fb_resize(void) -{ - fb_resize( &grender.fb ); -} - -static int render_gate( teleport_gate *gate, v3f viewpos, m4x3f camera ) +VG_STATIC int render_gate( world_instance *world_inside, + teleport_gate *gate, camera *cam ) { v3f viewdir, gatedir; - m3x3_mulv( camera, (v3f){0.0f,0.0f,-1.0f}, viewdir ); + m3x3_mulv( cam->transform, (v3f){0.0f,0.0f,-1.0f}, viewdir ); m3x3_mulv( gate->to_world, (v3f){0.0f,0.0f,-1.0f}, gatedir ); v3f v0; - v3_sub( viewpos, gate->co[0], v0 ); - if( v3_dot(v0, gatedir) >= 0.0f ) - return 0; + v3_sub( cam->pos, gate->co[0], v0 ); + + float dist = v3_dot(v0, gatedir); - if( v3_dist( viewpos, gate->co[0] ) > 100.0f ) + /* Hard cutoff */ + if( dist > 3.0f ) return 0; - v3f a,b,c,d; + if( v3_dist( cam->pos, gate->co[0] ) > 100.0f ) + return 0; - float sx = gate->dims[0], - sy = gate->dims[1]; - m4x3_mulv( gate->to_world, (v3f){-sx,-sy,0.0f}, a ); - m4x3_mulv( gate->to_world, (v3f){ sx,-sy,0.0f}, b ); - m4x3_mulv( gate->to_world, (v3f){ sx, sy,0.0f}, c ); - m4x3_mulv( gate->to_world, (v3f){-sx, sy,0.0f}, d ); + { + v3f a,b,c,d; - vg_line( a,b, 0xffffa000 ); - vg_line( b,c, 0xffffa000 ); - vg_line( c,d, 0xffffa000 ); - vg_line( d,a, 0xffffa000 ); + float sx = gate->dims[0], + sy = gate->dims[1]; + m4x3_mulv( gate->to_world, (v3f){-sx,-sy,0.0f}, a ); + m4x3_mulv( gate->to_world, (v3f){ sx,-sy,0.0f}, b ); + m4x3_mulv( gate->to_world, (v3f){ sx, sy,0.0f}, c ); + m4x3_mulv( gate->to_world, (v3f){-sx, sy,0.0f}, d ); - vg_line2( gate->co[0], gate->co[1], 0xff0000ff, 0x00000000 ); + vg_line( a,b, 0xffffa000 ); + vg_line( b,c, 0xffffa000 ); + vg_line( c,d, 0xffffa000 ); + vg_line( d,a, 0xffffa000 ); - m4x3f cam_new; - m4x3_mul( gate->transport, camera, cam_new ); - - vg_line_pt3( cam_new[3], 0.3f, 0xff00ff00 ); + vg_line2( gate->co[0], gate->co[1], 0xff0000ff, 0x00000000 ); + } - m4x3f gate_xform; - m4x3_copy( gate->to_world, gate_xform ); - m4x3_scalev( gate_xform, (v3f){ gate->dims[0], gate->dims[1], 1.0f } ); - - m4x3f inverse; - m4x3_invert_affine( cam_new, inverse ); + /* update gate camera */ + gate_camera.fov = cam->fov; + gate_camera.nearz = 0.1f; + gate_camera.farz = 2000.0f; - m4x4f view; - m4x3_expand( inverse, view ); + m4x3_mul( gate->transport, cam->transform, gate_camera.transform ); + camera_update_view( &gate_camera ); + camera_update_projection( &gate_camera ); + /* Add special clipping plane to projection */ v4f surface; m3x3_mulv( gate->recv_to_world, (v3f){0.0f,0.0f,-1.0f}, surface ); surface[3] = v3_dot( surface, gate->co[1] ); - - m4x4f projection; - pipeline_projection( projection, 0.1f, 900.0f ); - m4x3_mulp( inverse, surface, surface ); + m4x3_mulp( gate_camera.transform_inverse, surface, surface ); surface[3] = -fabsf(surface[3]); - plane_clip_projection( projection, surface ); - - m4x4_mul( projection, view, projection ); - -#ifndef GATE_RENDER_PERFORMANCE - fb_use( &grender.fb ); - glClearColor( 0.11f, 0.35f, 0.37f, 1.0f ); - glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT ); -#else - shader_gatelq_use(); - shader_gatelq_uPv( vg_pv ); - shader_gatelq_uMdl( gate_xform ); - shader_gatelq_uCam( viewpos ); - shader_gatelq_uTime( vg_time*0.25f ); - shader_gatelq_uInvRes( (v2f){ - 1.0f / (float)vg_window_x, - 1.0f / (float)vg_window_y }); + + if( dist < -0.5f ) + m4x4_clip_projection( gate_camera.mtx.p, surface ); + + /* Ready to draw with new camrea */ + camera_finalize( &gate_camera ); + + vg_line_pt3( gate_camera.transform[3], 0.3f, 0xff00ff00 ); + { + m4x3f gate_xform; + m4x3_copy( gate->to_world, gate_xform ); + m4x3_scalev( gate_xform, (v3f){ gate->dims[0], gate->dims[1], 1.0f } ); + + shader_model_gate_use(); + shader_model_gate_uPv( cam->mtx.pv ); + shader_model_gate_uMdl( gate_xform ); + shader_model_gate_uCam( cam->pos ); + shader_model_gate_uTime( vg.time*0.25f ); + shader_model_gate_uInvRes( (v2f){ + 1.0f / (float)vg.window_x, + 1.0f / (float)vg.window_y }); glEnable( GL_STENCIL_TEST ); glStencilOp( GL_KEEP, GL_KEEP, GL_REPLACE ); glStencilFunc( GL_ALWAYS, 1, 0xFF ); glStencilMask( 0xFF ); - mesh_bind( &grender.mdl ); - mesh_draw( &grender.mdl ); + mesh_bind( &world_global.mesh_gate_surface ); + mesh_draw( &world_global.mesh_gate_surface ); glClear( GL_DEPTH_BUFFER_BIT ); glStencilFunc( GL_EQUAL, 1, 0xFF ); glStencilMask( 0x00 ); -#endif - - render_world( projection, cam_new ); - -#ifndef GATE_RENDER_PERFORMANCE - - /* - * NOTE: Need to find a way to draw a stencil buffer into the water - * rendering - */ - - render_water_texture( cam_new ); - fb_use( &grender.fb ); - - render_water_surface( projection, cam_new ); - fb_use( NULL ); - - shader_gate_use(); - - shader_gate_uPv( vg_pv ); - shader_gate_uMdl( gate_xform ); - - fb_bindtex( &grender.fb, 0 ); - - shader_gate_uCam( viewpos ); - shader_gate_uTexMain( 0 ); - shader_gate_uTexWater( 1 ); - shader_gate_uTime( vg_time*0.25f ); - shader_gate_uInvRes( (v2f){ - 1.0f / (float)vg_window_x, - 1.0f / (float)vg_window_y }); - - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); - glBlendEquation(GL_FUNC_ADD); + } - mesh_bind( &grender.mdl ); - mesh_draw( &grender.mdl ); + render_world( world_inside, &gate_camera ); - glDisable(GL_BLEND); -#else + { glDisable( GL_STENCIL_TEST ); - render_water_texture( cam_new ); - fb_use( NULL ); + render_water_texture( world_inside, &gate_camera ); + render_fb_bind( gpipeline.fb_main ); + glEnable( GL_STENCIL_TEST ); - render_water_surface( projection, cam_new ); + render_water_surface( world_inside, &gate_camera ); glStencilMask( 0xFF ); glStencilFunc( GL_ALWAYS, 1, 0xFF ); glDisable( GL_STENCIL_TEST ); -#endif + } return 1; } -static int gate_intersect( teleport_gate *gate, v3f pos, v3f last ) +VG_STATIC int gate_intersect_plane( teleport_gate *gate, v3f pos, v3f last, + v2f where ) { v4f surface; m3x3_mulv( gate->to_world, (v3f){0.0f,0.0f,-1.0f}, surface ); @@ -224,12 +161,16 @@ static int gate_intersect( teleport_gate *gate, v3f pos, v3f last ) v3f v0, c, delta, p0; v3_sub( pos, last, v0 ); float l = v3_length( v0 ); + + if( l == 0.0f ) + return 0; + v3_divs( v0, l, v0 ); v3_muls( surface, surface[3], c ); v3_sub( c, last, delta ); - float d = v3_dot(surface, v0); + float d = v3_dot( surface, v0 ); if( d > 0.00001f ) { @@ -244,16 +185,73 @@ static int gate_intersect( teleport_gate *gate, v3f pos, v3f last ) m3x3_mulv( gate->to_world, (v3f){0.0f,1.0f,0.0f}, vup ); m3x3_mulv( gate->to_world, (v3f){1.0f,0.0f,0.0f}, vside ); - v2f xy = { v3_dot( rel, vside ), v3_dot( rel, vup ) }; + where[0] = v3_dot( rel, vside ); + where[1] = v3_dot( rel, vup ); - if( fabsf(xy[0]) <= gate->dims[0] && fabsf(xy[1]) <= gate->dims[1] ) - { - return 1; - } + return 1; + } + } + + return 0; +} + +VG_STATIC int gate_intersect( teleport_gate *gate, v3f pos, v3f last ) +{ + v2f xy; + + if( gate_intersect_plane( gate, pos, last, xy ) ) + { + if( fabsf(xy[0]) <= gate->dims[0] && fabsf(xy[1]) <= gate->dims[1] ) + { + return 1; } } return 0; } +struct gate_hit +{ + struct nonlocal_gate *nonlocal; + struct route_gate *route; + teleport_gate *gate; +}; + +/* + * Intersect all gates in the world + */ +VG_STATIC int world_intersect_gates( world_instance *world, + v3f pos, v3f last, struct gate_hit *hit ) +{ + for( int i=0; igate_count; i++ ) + { + struct route_gate *rg = &world->gates[i]; + teleport_gate *gate = &rg->gate; + + if( gate_intersect( gate, pos, last ) ) + { + hit->gate = gate; + hit->nonlocal = NULL; + hit->route = rg; + return 1; + } + } + + for( int i=0; inonlocalgate_count; i++ ) + { + struct nonlocal_gate *nlg = &world->nonlocal_gates[i]; + teleport_gate *gate = &nlg->gate; + + if( gate_intersect( gate, pos, last ) ) + { + hit->gate = gate; + hit->nonlocal = nlg; + hit->route = NULL; + return 1; + } + } + + return 0; +} + #endif /* WORLD_GATE_H */