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