X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=world_gate.h;h=2febafaaa0434f46a249ec2036cb686532e569ca;hb=2673c575386c604fc2c0603dba2480eda05cf97a;hp=d92537eb1566fada932ddb4c4dc121828f776eaf;hpb=aa4c26eae2208872824e0eb5b71bc05c16d43242;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/world_gate.h b/world_gate.h index d92537e..2febafa 100644 --- a/world_gate.h +++ b/world_gate.h @@ -7,24 +7,28 @@ #include "common.h" #include "model.h" +#include "entity.h" #include "render.h" #include "camera.h" #include "shaders/model_gate.h" #include "world_water.h" -VG_STATIC void gate_transform_update( teleport_gate *gate ) +VG_STATIC void gate_transform_update( ent_gate *gate ) { - m4x3f to_local; + m4x3f to_local, recv_to_world; q_m3x3( gate->q[0], gate->to_world ); v3_copy( gate->co[0], gate->to_world[3] ); m4x3_invert_affine( gate->to_world, to_local ); - q_m3x3( gate->q[1], gate->recv_to_world ); - v3_copy( gate->co[1], gate->recv_to_world[3] ); - m4x3_mul( gate->recv_to_world, to_local, gate->transport ); + q_m3x3( gate->q[1], recv_to_world ); + v3_copy( gate->co[1], recv_to_world[3] ); + m4x3_mul( recv_to_world, to_local, gate->transport ); + + m4x3_scalev( gate->to_world, (v3f){ gate->dimensions[0], + gate->dimensions[1], 1.0f } ); } VG_STATIC void world_gates_init(void) @@ -34,20 +38,40 @@ VG_STATIC void world_gates_init(void) shader_model_gate_register(); vg_linear_clear( vg_mem.scratch ); - mdl_context *mgate = mdl_load_full( vg_mem.scratch, "models/rs_gate.mdl" ); + + mdl_context mgate; + mdl_open( &mgate, "models/rs_gate.mdl", vg_mem.scratch ); + mdl_load_metadata_block( &mgate, vg_mem.scratch ); + mdl_load_mesh_block( &mgate, vg_mem.scratch ); + + mdl_mesh *surface = mdl_find_mesh( &mgate, "rs_gate" ); + mdl_submesh *sm = mdl_arritm(&mgate.submeshs,surface->submesh_start); + world_global.sm_gate_surface = *sm; + + const char *names[] = { "rs_gate_marker", "rs_gate_marker.001", + "rs_gate_marker.002", "rs_gate_marker.003" }; + + for( int i=0; i<4; i++ ){ + mdl_mesh *marker = mdl_find_mesh( &mgate, names[i] ); + sm = mdl_arritm( &mgate.submeshs, marker->submesh_start ); + world_global.sm_gate_marker[i] = *sm; + } + + mdl_close( &mgate ); vg_acquire_thread_sync(); { - mdl_unpack_glmesh( mgate, &world.mesh_gate_surface ); + mdl_unpack_glmesh( &mgate, &world_global.mesh_gate ); } vg_release_thread_sync(); } -VG_STATIC int render_gate( teleport_gate *gate, camera *cam ) +VG_STATIC int render_gate( world_instance *world_inside, + ent_gate *gate, camera *cam, int layer_depth ) { v3f viewdir, gatedir; 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 ); + q_mulv( gate->q[0], (v3f){0.0f,0.0f,-1.0f}, gatedir ); v3f v0; v3_sub( cam->pos, gate->co[0], v0 ); @@ -64,12 +88,10 @@ VG_STATIC int render_gate( teleport_gate *gate, camera *cam ) { v3f a,b,c,d; - 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 ); + m4x3_mulv( gate->to_world, (v3f){-1.0f,-1.0f,0.0f}, a ); + m4x3_mulv( gate->to_world, (v3f){ 1.0f,-1.0f,0.0f}, b ); + m4x3_mulv( gate->to_world, (v3f){ 1.0f, 1.0f,0.0f}, c ); + m4x3_mulv( gate->to_world, (v3f){-1.0f, 1.0f,0.0f}, d ); vg_line( a,b, 0xffffa000 ); vg_line( b,c, 0xffffa000 ); @@ -80,39 +102,35 @@ VG_STATIC int render_gate( teleport_gate *gate, camera *cam ) } /* update gate camera */ - static camera gate_view; - gate_view.fov = cam->fov; - gate_view.nearz = 0.1f; - gate_view.farz = 2000.0f; + gate_camera.fov = cam->fov; + gate_camera.nearz = 0.1f; + gate_camera.farz = 2000.0f; - m4x3_mul( gate->transport, cam->transform, gate_view.transform ); - camera_update_view( &gate_view ); - camera_update_projection( &gate_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 ); + q_mulv( gate->q[1], (v3f){0.0f,0.0f,-1.0f}, surface ); surface[3] = v3_dot( surface, gate->co[1] ); - m4x3_mulp( gate_view.transform_inverse, surface, surface ); + m4x3_mulp( gate_camera.transform_inverse, surface, surface ); surface[3] = -fabsf(surface[3]); if( dist < -0.5f ) - m4x4_clip_projection( gate_view.mtx.p, surface ); + m4x4_clip_projection( gate_camera.mtx.p, surface ); /* Ready to draw with new camrea */ - camera_finalize( &gate_view ); + camera_finalize( &gate_camera ); - vg_line_pt3( gate_view.transform[3], 0.3f, 0xff00ff00 ); + 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_uMdl( gate->to_world ); shader_model_gate_uCam( cam->pos ); + shader_model_gate_uColour( (v4f){0.0f,1.0f,0.0f,0.0f} ); shader_model_gate_uTime( vg.time*0.25f ); shader_model_gate_uInvRes( (v2f){ 1.0f / (float)vg.window_x, @@ -123,25 +141,25 @@ VG_STATIC int render_gate( teleport_gate *gate, camera *cam ) glStencilFunc( GL_ALWAYS, 1, 0xFF ); glStencilMask( 0xFF ); - mesh_bind( &world.mesh_gate_surface ); - mesh_draw( &world.mesh_gate_surface ); + mesh_bind( &world_global.mesh_gate ); + mdl_draw_submesh( &world_global.sm_gate_surface ); glClear( GL_DEPTH_BUFFER_BIT ); glStencilFunc( GL_EQUAL, 1, 0xFF ); glStencilMask( 0x00 ); } - render_world( &gate_view ); + render_world( world_inside, &gate_camera, layer_depth ); { glDisable( GL_STENCIL_TEST ); - render_water_texture( &gate_view ); + render_water_texture( world_inside, &gate_camera, layer_depth ); render_fb_bind( gpipeline.fb_main ); glEnable( GL_STENCIL_TEST ); - render_water_surface( &gate_view ); + render_water_surface( world_inside, &gate_camera ); glStencilMask( 0xFF ); glStencilFunc( GL_ALWAYS, 1, 0xFF ); @@ -151,11 +169,11 @@ VG_STATIC int render_gate( teleport_gate *gate, camera *cam ) return 1; } -VG_STATIC int gate_intersect_plane( teleport_gate *gate, v3f pos, v3f last, - v2f where ) +VG_STATIC int gate_intersect_plane( ent_gate *gate, + v3f pos, v3f last, v2f where ) { v4f surface; - m3x3_mulv( gate->to_world, (v3f){0.0f,0.0f,-1.0f}, surface ); + q_mulv( gate->q[0], (v3f){0.0f,0.0f,-1.0f}, surface ); surface[3] = v3_dot( surface, gate->co[0] ); v3f v0, c, delta, p0; @@ -172,21 +190,18 @@ VG_STATIC int gate_intersect_plane( teleport_gate *gate, v3f pos, v3f last, float d = v3_dot( surface, v0 ); - if( d > 0.00001f ) - { + if( d > 0.00001f ){ float t = v3_dot(delta, surface) / d; - if( t >= 0.0f && t <= l ) - { + if( t >= 0.0f && t <= l ){ v3f local, rel; v3_muladds( last, v0, t, local ); v3_sub( gate->co[0], local, rel ); - v3f vup, vside; - 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 ); + where[0] = v3_dot( rel, gate->to_world[0] ); + where[1] = v3_dot( rel, gate->to_world[1] ); - where[0] = v3_dot( rel, vside ); - where[1] = v3_dot( rel, vup ); + where[0] /= v3_dot( gate->to_world[0], gate->to_world[0] ); + where[1] /= v3_dot( gate->to_world[1], gate->to_world[1] ); return 1; } @@ -195,14 +210,12 @@ VG_STATIC int gate_intersect_plane( teleport_gate *gate, v3f pos, v3f last, return 0; } -VG_STATIC int gate_intersect( teleport_gate *gate, v3f pos, v3f last ) +VG_STATIC int gate_intersect( ent_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] ) - { + if( gate_intersect_plane( gate, pos, last, xy ) ){ + if( fabsf(xy[0]) <= 1.0f && fabsf(xy[1]) <= 1.0f ){ return 1; } } @@ -213,15 +226,17 @@ VG_STATIC int gate_intersect( teleport_gate *gate, v3f pos, v3f last ) /* * Intersect all gates in the world */ -VG_STATIC teleport_gate *world_intersect_gates( v3f pos, v3f last ) +VG_STATIC ent_gate *world_intersect_gates( world_instance *world, + v3f pos, v3f last ) { - for( int i=0; igate; + for( u32 i=0; ient_gate); i++ ){ + ent_gate *gate = mdl_arritm( &world->ent_gate, i ); + if( gate->type == k_gate_type_unlinked ) + continue; - if( gate_intersect( gate, pos, last ) ) + if( gate_intersect( gate, pos, last ) ){ return gate; + } } return NULL;