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