/*
- * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
+ * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
*/
#ifndef WORLD_GATE_H
#define WORLD_GATE_H
-#include "common.h"
-#include "model.h"
-#include "render.h"
+#include "camera.h"
+#include "world.h"
+#include "shaders/model_gate.h"
-#include "shaders/gatelq.h"
-#include "world_water.h"
+struct world_gates{
+ glmesh mesh;
+ mdl_submesh sm_surface, sm_marker[4];
+ camera cam;
-
-VG_STATIC void gate_transform_update( teleport_gate *gate )
-{
- m4x3f to_local;
-
- 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 );
-}
-
-VG_STATIC void world_gates_init(void)
-{
- vg_info( "world_gates_init\n" );
-
- shader_gatelq_register();
-
- vg_linear_clear( vg_mem.scratch );
- mdl_context *mgate = mdl_load_full( vg_mem.scratch, "models/rs_gate.mdl" );
-
- vg_acquire_thread_sync();
- {
- mdl_unpack_glmesh( mgate, &world.mesh_gate_surface );
- }
- vg_release_thread_sync();
-}
-
-VG_STATIC int render_gate( teleport_gate *gate, v3f viewpos, m4x3f camera )
-{
- v3f viewdir, gatedir;
- m3x3_mulv( camera, (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;
-
- if( v3_dist( viewpos, gate->co[0] ) > 100.0f )
- return 0;
-
- 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 );
-
- vg_line( a,b, 0xffffa000 );
- vg_line( b,c, 0xffffa000 );
- vg_line( c,d, 0xffffa000 );
- vg_line( d,a, 0xffffa000 );
-
- vg_line2( gate->co[0], gate->co[1], 0xff0000ff, 0x00000000 );
-
- m4x3f cam_new;
- m4x3_mul( gate->transport, camera, cam_new );
-
- vg_line_pt3( cam_new[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 } );
-
- m4x3f inverse;
- m4x3_invert_affine( cam_new, inverse );
-
- m4x4f view;
- m4x3_expand( inverse, view );
-
- 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 );
- surface[3] = -fabsf(surface[3]);
- plane_clip_projection( projection, surface );
-
- m4x4_mul( projection, view, projection );
-
- {
- 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 });
-
- glEnable( GL_STENCIL_TEST );
- glStencilOp( GL_KEEP, GL_KEEP, GL_REPLACE );
- glStencilFunc( GL_ALWAYS, 1, 0xFF );
- glStencilMask( 0xFF );
-
- mesh_bind( &world.mesh_gate_surface );
- mesh_draw( &world.mesh_gate_surface );
-
- glClear( GL_DEPTH_BUFFER_BIT );
- glStencilFunc( GL_EQUAL, 1, 0xFF );
- glStencilMask( 0x00 );
- }
-
- render_world( projection, cam_new );
-
- {
- glDisable( GL_STENCIL_TEST );
-
- render_water_texture( cam_new );
- fb_use( NULL );
- glEnable( GL_STENCIL_TEST );
-
- render_water_surface( projection, cam_new );
-
- glStencilMask( 0xFF );
- glStencilFunc( GL_ALWAYS, 1, 0xFF );
- glDisable( GL_STENCIL_TEST );
- }
-
- return 1;
+ v3f userportal_co;
}
+static world_gates;
-VG_STATIC int gate_intersect( teleport_gate *gate, v3f pos, v3f last )
-{
- v4f surface;
- m3x3_mulv( gate->to_world, (v3f){0.0f,0.0f,-1.0f}, surface );
- surface[3] = v3_dot( surface, gate->co[0] );
-
- v3f v0, c, delta, p0;
- v3_sub( pos, last, v0 );
- float l = v3_length( v0 );
- v3_divs( v0, l, v0 );
-
- v3_muls( surface, surface[3], c );
- v3_sub( c, last, delta );
+static void world_gates_init(void);
+static void gate_transform_update( ent_gate *gate );
+static int render_gate( world_instance *world, world_instance *world_inside,
+ ent_gate *gate, camera *cam );
- float d = v3_dot(surface, v0);
+static int gate_intersect( ent_gate *gate, v3f pos, v3f last );
+static u32 world_intersect_gates( world_instance *world, v3f pos, v3f last );
- if( d > 0.00001f )
- {
- float t = v3_dot(delta, surface) / d;
- if( t >= 0.0f && t <= l )
- {
- v3f local, rel;
- v3_muladds( last, v0, t, local );
- v3_sub( gate->co[0], local, rel );
+static void ent_gate_call( world_instance *world, ent_call *call );
+static void ent_gate_get_mdl_mtx( ent_gate *gate, m4x3f mmdl );
- 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 );
-
- v2f xy = { v3_dot( rel, vside ), v3_dot( rel, vup ) };
-
- if( fabsf(xy[0]) <= gate->dims[0] && fabsf(xy[1]) <= gate->dims[1] )
- {
- return 1;
- }
- }
- }
-
- return 0;
-}
+static void world_link_nonlocal_async( void *payload, u32 size );
+static void world_unlink_nonlocal( world_instance *world );
+static void render_gate_unlinked( world_instance *world,
+ ent_gate *gate, camera *cam );
#endif /* WORLD_GATE_H */