gui icons
[carveJwlIkooP6JGAAIwe30JlM.git] / world_gate.c
1 /*
2 * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #ifndef WORLD_GATE_C
6 #define WORLD_GATE_C
7
8 #include "world.h"
9 #include "world_gate.h"
10
11 #include "skaterift.h"
12 #include "common.h"
13 #include "model.h"
14 #include "entity.h"
15 #include "render.h"
16 #include "camera.h"
17
18 #include "world_water.h"
19
20 /*
21 * Update the transform matrices for gate
22 */
23 VG_STATIC void gate_transform_update( ent_gate *gate ){
24 if( gate->flags & k_ent_gate_flip ){
25 v4f qflip;
26 q_axis_angle( qflip, (v3f){0.0f,1.0f,0.0f}, VG_PIf );
27 q_mul( gate->q[1], qflip, gate->q[1] );
28 }
29
30 m4x3f to_local, recv_to_world;
31
32 q_m3x3( gate->q[0], gate->to_world );
33 v3_copy( gate->co[0], gate->to_world[3] );
34
35 m4x3_invert_affine( gate->to_world, to_local );
36
37 q_m3x3( gate->q[1], recv_to_world );
38 v3_copy( gate->co[1], recv_to_world[3] );
39 m4x3_mul( recv_to_world, to_local, gate->transport );
40 }
41
42 VG_STATIC void world_gates_init(void)
43 {
44 vg_info( "world_gates_init\n" );
45
46 shader_model_gate_register();
47
48 vg_linear_clear( vg_mem.scratch );
49
50 mdl_context mgate;
51 mdl_open( &mgate, "models/rs_gate.mdl", vg_mem.scratch );
52 mdl_load_metadata_block( &mgate, vg_mem.scratch );
53
54 mdl_mesh *surface = mdl_find_mesh( &mgate, "rs_gate" );
55 mdl_submesh *sm = mdl_arritm(&mgate.submeshs,surface->submesh_start);
56 world_gates.sm_surface = *sm;
57
58 const char *names[] = { "rs_gate_marker", "rs_gate_marker.001",
59 "rs_gate_marker.002", "rs_gate_marker.003" };
60
61 for( int i=0; i<4; i++ ){
62 mdl_mesh *marker = mdl_find_mesh( &mgate, names[i] );
63 sm = mdl_arritm( &mgate.submeshs, marker->submesh_start );
64 world_gates.sm_marker[i] = *sm;
65 }
66
67 mdl_async_load_glmesh( &mgate, &world_gates.mesh );
68 mdl_close( &mgate );
69 }
70
71 VG_STATIC void ent_gate_get_mdl_mtx( ent_gate *gate, m4x3f mmdl ){
72 m4x3_copy( gate->to_world, mmdl );
73
74 if( !(gate->flags & k_ent_gate_custom_mesh) ){
75 m3x3_scale( mmdl, (v3f){ gate->dimensions[0],
76 gate->dimensions[1], 1.0f } );
77 }
78 }
79
80 /*
81 * Render the view through a gate
82 */
83 VG_STATIC int render_gate( world_instance *world, world_instance *world_inside,
84 ent_gate *gate, camera *cam, int layer_depth )
85 {
86 v3f viewdir, gatedir;
87 m3x3_mulv( cam->transform, (v3f){0.0f,0.0f,-1.0f}, viewdir );
88 q_mulv( gate->q[0], (v3f){0.0f,0.0f,-1.0f}, gatedir );
89
90 v3f v0;
91 v3_sub( cam->pos, gate->co[0], v0 );
92
93 float dist = v3_dot(v0, gatedir);
94
95 /* Hard cutoff */
96 if( dist > 3.0f )
97 return 0;
98
99 if( v3_dist( cam->pos, gate->co[0] ) > 100.0f )
100 return 0;
101
102 {
103 f32 w = gate->dimensions[0],
104 h = gate->dimensions[1];
105
106 v3f a,b,c,d;
107 m4x3_mulv( gate->to_world, (v3f){-w,-h,0.0f}, a );
108 m4x3_mulv( gate->to_world, (v3f){ w,-h,0.0f}, b );
109 m4x3_mulv( gate->to_world, (v3f){ w, h,0.0f}, c );
110 m4x3_mulv( gate->to_world, (v3f){-w, h,0.0f}, d );
111
112 vg_line( a,b, 0xffffa000 );
113 vg_line( b,c, 0xffffa000 );
114 vg_line( c,d, 0xffffa000 );
115 vg_line( d,a, 0xffffa000 );
116 vg_line( gate->co[0], gate->co[1], 0xff0000ff );
117 }
118
119 /* update gate camera */
120 world_gates.cam.fov = cam->fov;
121 world_gates.cam.nearz = 0.1f;
122 world_gates.cam.farz = 2000.0f;
123
124 m4x3_mul( gate->transport, cam->transform, world_gates.cam.transform );
125 camera_update_view( &world_gates.cam );
126 camera_update_projection( &world_gates.cam );
127
128 /* Add special clipping plane to projection */
129 v4f surface;
130 q_mulv( gate->q[1], (v3f){0.0f,0.0f,-1.0f}, surface );
131 surface[3] = v3_dot( surface, gate->co[1] );
132
133 m4x3_mulp( world_gates.cam.transform_inverse, surface, surface );
134 surface[3] = -fabsf(surface[3]);
135
136 if( dist < -0.5f )
137 m4x4_clip_projection( world_gates.cam.mtx.p, surface );
138
139 /* Ready to draw with new camrea */
140 camera_finalize( &world_gates.cam );
141
142 vg_line_point( world_gates.cam.transform[3], 0.3f, 0xff00ff00 );
143 {
144 shader_model_gate_use();
145 shader_model_gate_uPv( cam->mtx.pv );
146 shader_model_gate_uCam( cam->pos );
147 shader_model_gate_uColour( (v4f){0.0f,1.0f,0.0f,0.0f} );
148 shader_model_gate_uTime( vg.time*0.25f );
149 shader_model_gate_uInvRes( (v2f){
150 1.0f / (float)vg.window_x,
151 1.0f / (float)vg.window_y });
152
153 glEnable( GL_STENCIL_TEST );
154 glStencilOp( GL_KEEP, GL_KEEP, GL_REPLACE );
155 glStencilFunc( GL_ALWAYS, 1, 0xFF );
156 glStencilMask( 0xFF );
157 glDisable( GL_CULL_FACE );
158
159 m4x3f mmdl;
160 ent_gate_get_mdl_mtx( gate, mmdl );
161 shader_model_gate_uMdl( mmdl );
162
163 if( gate->flags & k_ent_gate_custom_mesh ){
164 mesh_bind( &world->mesh_no_collide );
165 for( u32 i=0; i<gate->submesh_count; i++ ){
166 mdl_submesh *sm = mdl_arritm( &world->meta.submeshs,
167 gate->submesh_start+i );
168 mdl_draw_submesh( sm );
169 }
170 }
171 else {
172 mesh_bind( &world_gates.mesh );
173 mdl_draw_submesh( &world_gates.sm_surface );
174 }
175
176 glClear( GL_DEPTH_BUFFER_BIT );
177 glStencilFunc( GL_EQUAL, 1, 0xFF );
178 glStencilMask( 0x00 );
179 glEnable( GL_CULL_FACE );
180 }
181
182 render_world( world_inside, &world_gates.cam, layer_depth );
183
184 {
185 glDisable( GL_STENCIL_TEST );
186
187 render_water_texture( world_inside, &world_gates.cam, layer_depth );
188 render_fb_bind( gpipeline.fb_main, 1 );
189
190 glEnable( GL_STENCIL_TEST );
191
192 render_water_surface( world_inside, &world_gates.cam );
193
194 glStencilMask( 0xFF );
195 glStencilFunc( GL_ALWAYS, 1, 0xFF );
196 glDisable( GL_STENCIL_TEST );
197 }
198
199 return 1;
200 }
201
202 /*
203 * Intersect the plane of a gate with a line segment, plane coordinate result
204 * stored in 'where'
205 */
206 VG_STATIC int gate_intersect_plane( ent_gate *gate,
207 v3f pos, v3f last, v2f where )
208 {
209 v4f surface;
210 q_mulv( gate->q[0], (v3f){0.0f,0.0f,-1.0f}, surface );
211 surface[3] = v3_dot( surface, gate->co[0] );
212
213 v3f v0, c, delta, p0;
214 v3_sub( pos, last, v0 );
215 float l = v3_length( v0 );
216
217 if( l == 0.0f )
218 return 0;
219
220 v3_divs( v0, l, v0 );
221
222 v3_muls( surface, surface[3], c );
223 v3_sub( c, last, delta );
224
225 float d = v3_dot( surface, v0 );
226
227 if( d > 0.00001f ){
228 float t = v3_dot(delta, surface) / d;
229 if( t >= 0.0f && t <= l ){
230 v3f local, rel;
231 v3_muladds( last, v0, t, local );
232 v3_sub( gate->co[0], local, rel );
233
234 where[0] = v3_dot( rel, gate->to_world[0] );
235 where[1] = v3_dot( rel, gate->to_world[1] );
236
237 where[0] /= v3_dot( gate->to_world[0], gate->to_world[0] );
238 where[1] /= v3_dot( gate->to_world[1], gate->to_world[1] );
239
240 return 1;
241 }
242 }
243
244 return 0;
245 }
246
247 /*
248 * Intersect specific gate
249 */
250 VG_STATIC int gate_intersect( ent_gate *gate, v3f pos, v3f last )
251 {
252 v2f xy;
253
254 if( gate_intersect_plane( gate, pos, last, xy ) ){
255 if( (fabsf(xy[0]) <= gate->dimensions[0]) &&
256 (fabsf(xy[1]) <= gate->dimensions[1]) ){
257 return 1;
258 }
259 }
260
261 return 0;
262 }
263
264 /*
265 * Intersect all gates in the world
266 */
267 VG_STATIC ent_gate *world_intersect_gates( world_instance *world,
268 v3f pos, v3f last ){
269 for( u32 i=0; i<mdl_arrcount(&world->ent_gate); i++ ){
270 ent_gate *gate = mdl_arritm( &world->ent_gate, i );
271
272 if( !(gate->flags & k_ent_gate_linked) ) continue;
273 if( gate->flags & k_ent_gate_locked ) continue;
274
275 if( gate->flags & k_ent_gate_nonlocal ){
276 if( world_static.load_state != k_world_loader_none ){
277 continue;
278 }
279 }
280
281 if( gate_intersect( gate, pos, last ) ){
282 return gate;
283 }
284 }
285
286 return NULL;
287 }
288
289 /*
290 * detatches any nonlocal gates
291 */
292 VG_STATIC void world_unlink_nonlocal( world_instance *world ){
293 for( u32 j=0; j<mdl_arrcount(&world->ent_gate); j ++ ){
294 ent_gate *gate = mdl_arritm( &world->ent_gate, j );
295
296 if( gate->flags & k_ent_gate_nonlocal ){
297 gate->flags &= ~k_ent_gate_linked;
298 }
299 }
300 }
301
302 /*
303 * attatches nonlocal gates, to be called from main thread ONLY!
304 */
305 VG_STATIC void world_link_nonlocal_async( void *payload, u32 size ){
306 world_instance *world = payload;
307 u32 world_id = world - world_static.instances;
308
309 for( u32 j=0; j<mdl_arrcount(&world->ent_gate); j ++ ){
310 ent_gate *gate = mdl_arritm( &world->ent_gate, j );
311
312 if( !(gate->flags & k_ent_gate_nonlocal) ) continue;
313 if( gate->flags & k_ent_gate_linked ) continue;
314
315 const char *key = mdl_pstr( &world->meta, gate->key );
316 vg_info( "key: %s\n", key );
317
318 for( u32 i=0; i<vg_list_size(world_static.instances); i++ ){
319 world_instance *other = &world_static.instances[i];
320 if( other == world ) continue;
321 if( other->status != k_world_status_loaded ) continue;
322 vg_info( "Checking world %u for key matches\n", i );
323
324 for( u32 k=0; k<mdl_arrcount( &other->ent_gate ); k++ ){
325 ent_gate *gate2 = mdl_arritm( &other->ent_gate, k );
326
327 if( !(gate2->flags & k_ent_gate_nonlocal) ) continue;
328 if( gate2->flags & k_ent_gate_linked ) continue;
329
330 const char *key2 = mdl_pstr( &other->meta, gate2->key );
331 vg_info( " key2: %s\n", key2 );
332
333 if( strcmp( key, key2 ) ) continue;
334
335 vg_success( "Non-local matching pair '%s' found. (%u:%u)\n",
336 key, world_id, i );
337
338 gate->flags |= k_ent_gate_linked;
339 gate2->flags |= k_ent_gate_linked;
340 gate->target = i;
341 gate2->target = world_id;
342
343 v3_copy( gate->co[0], gate2->co[1] );
344 v3_copy( gate2->co[0], gate->co[1] );
345 v4_copy( gate->q[0], gate2->q[1] );
346 v4_copy( gate2->q[0], gate->q[1] );
347
348 if( world->meta.info.version < 102 ){
349 /* LEGACY BEHAVIOUR: v101
350 * this would flip both the client worlds portal's entrance and
351 * exit. effectively the clients portal would be the opposite
352 * to the hub worlds one. new behaviour is to just flip the
353 * destinations so the rules are consistent in each world.
354 */
355 v4f qflip;
356 q_axis_angle( qflip, (v3f){0.0f,1.0f,0.0f}, VG_PIf );
357 q_mul( gate->q[0], qflip, gate->q[0] );
358 q_mul( gate->q[1], qflip, gate->q[1] );
359 q_mul( gate2->q[1], qflip, gate2->q[1] );
360 }
361
362 gate_transform_update( gate );
363 gate_transform_update( gate2 );
364
365 goto matched;
366 }
367 } matched:;
368 }
369 }
370
371 VG_STATIC void ent_gate_call( world_instance *world, ent_call *call ){
372 u32 index = mdl_entity_id_id( call->id );
373 ent_gate *gate = mdl_arritm( &world->ent_gate, index );
374
375 if( call->function == 0 ){ /* unlock() */
376 gate->flags &= ~k_ent_gate_locked;
377 }
378 else {
379 vg_print_backtrace();
380 vg_error( "Unhandled function id: %u\n", call->function );
381 }
382 }
383
384 #endif /* WORLD_GATE_C */