semantics and world reloading
[carveJwlIkooP6JGAAIwe30JlM.git] / world_gate.h
1 /*
2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #ifndef WORLD_GATE_H
6 #define WORLD_GATE_H
7
8 #include "skaterift.h"
9 #include "common.h"
10 #include "model.h"
11 #include "entity.h"
12 #include "render.h"
13 #include "camera.h"
14
15 #include "shaders/model_gate.h"
16 #include "world_water.h"
17
18 VG_STATIC void gate_transform_update( ent_gate *gate )
19 {
20 m4x3f to_local, recv_to_world;
21
22 q_m3x3( gate->q[0], gate->to_world );
23 v3_copy( gate->co[0], gate->to_world[3] );
24
25 m4x3_invert_affine( gate->to_world, to_local );
26
27 q_m3x3( gate->q[1], recv_to_world );
28 v3_copy( gate->co[1], recv_to_world[3] );
29 m4x3_mul( recv_to_world, to_local, gate->transport );
30
31 m3x3_scale( gate->to_world, (v3f){ gate->dimensions[0],
32 gate->dimensions[1], 1.0f } );
33 }
34
35 VG_STATIC void world_gates_init(void)
36 {
37 vg_info( "world_gates_init\n" );
38
39 shader_model_gate_register();
40
41 vg_linear_clear( vg_mem.scratch );
42
43 mdl_context mgate;
44 mdl_open( &mgate, "models/rs_gate.mdl", vg_mem.scratch );
45 mdl_load_metadata_block( &mgate, vg_mem.scratch );
46
47 mdl_mesh *surface = mdl_find_mesh( &mgate, "rs_gate" );
48 mdl_submesh *sm = mdl_arritm(&mgate.submeshs,surface->submesh_start);
49 world_global.sm_gate_surface = *sm;
50
51 const char *names[] = { "rs_gate_marker", "rs_gate_marker.001",
52 "rs_gate_marker.002", "rs_gate_marker.003" };
53
54 for( int i=0; i<4; i++ ){
55 mdl_mesh *marker = mdl_find_mesh( &mgate, names[i] );
56 sm = mdl_arritm( &mgate.submeshs, marker->submesh_start );
57 world_global.sm_gate_marker[i] = *sm;
58 }
59
60 mdl_async_load_glmesh( &mgate, &world_global.mesh_gate );
61 mdl_close( &mgate );
62 }
63
64 VG_STATIC int render_gate( world_instance *world_inside,
65 ent_gate *gate, camera *cam, int layer_depth )
66 {
67 v3f viewdir, gatedir;
68 m3x3_mulv( cam->transform, (v3f){0.0f,0.0f,-1.0f}, viewdir );
69 q_mulv( gate->q[0], (v3f){0.0f,0.0f,-1.0f}, gatedir );
70
71 v3f v0;
72 v3_sub( cam->pos, gate->co[0], v0 );
73
74 float dist = v3_dot(v0, gatedir);
75
76 /* Hard cutoff */
77 if( dist > 3.0f )
78 return 0;
79
80 if( v3_dist( cam->pos, gate->co[0] ) > 100.0f )
81 return 0;
82
83 {
84 v3f a,b,c,d;
85
86 m4x3_mulv( gate->to_world, (v3f){-1.0f,-1.0f,0.0f}, a );
87 m4x3_mulv( gate->to_world, (v3f){ 1.0f,-1.0f,0.0f}, b );
88 m4x3_mulv( gate->to_world, (v3f){ 1.0f, 1.0f,0.0f}, c );
89 m4x3_mulv( gate->to_world, (v3f){-1.0f, 1.0f,0.0f}, d );
90
91 vg_line( a,b, 0xffffa000 );
92 vg_line( b,c, 0xffffa000 );
93 vg_line( c,d, 0xffffa000 );
94 vg_line( d,a, 0xffffa000 );
95
96 vg_line2( gate->co[0], gate->co[1], 0xff0000ff, 0x00000000 );
97 }
98
99 /* update gate camera */
100 gate_camera.fov = cam->fov;
101 gate_camera.nearz = 0.1f;
102 gate_camera.farz = 2000.0f;
103
104 m4x3_mul( gate->transport, cam->transform, gate_camera.transform );
105 camera_update_view( &gate_camera );
106 camera_update_projection( &gate_camera );
107
108 /* Add special clipping plane to projection */
109 v4f surface;
110 q_mulv( gate->q[1], (v3f){0.0f,0.0f,-1.0f}, surface );
111 surface[3] = v3_dot( surface, gate->co[1] );
112
113 m4x3_mulp( gate_camera.transform_inverse, surface, surface );
114 surface[3] = -fabsf(surface[3]);
115
116 if( dist < -0.5f )
117 m4x4_clip_projection( gate_camera.mtx.p, surface );
118
119 /* Ready to draw with new camrea */
120 camera_finalize( &gate_camera );
121
122 vg_line_pt3( gate_camera.transform[3], 0.3f, 0xff00ff00 );
123 {
124 shader_model_gate_use();
125 shader_model_gate_uPv( cam->mtx.pv );
126 shader_model_gate_uMdl( gate->to_world );
127 shader_model_gate_uCam( cam->pos );
128 shader_model_gate_uColour( (v4f){0.0f,1.0f,0.0f,0.0f} );
129 shader_model_gate_uTime( vg.time*0.25f );
130 shader_model_gate_uInvRes( (v2f){
131 1.0f / (float)vg.window_x,
132 1.0f / (float)vg.window_y });
133
134 glEnable( GL_STENCIL_TEST );
135 glStencilOp( GL_KEEP, GL_KEEP, GL_REPLACE );
136 glStencilFunc( GL_ALWAYS, 1, 0xFF );
137 glStencilMask( 0xFF );
138
139 mesh_bind( &world_global.mesh_gate );
140 mdl_draw_submesh( &world_global.sm_gate_surface );
141
142 glClear( GL_DEPTH_BUFFER_BIT );
143 glStencilFunc( GL_EQUAL, 1, 0xFF );
144 glStencilMask( 0x00 );
145 }
146
147 render_world( world_inside, &gate_camera, layer_depth );
148
149 {
150 glDisable( GL_STENCIL_TEST );
151
152 render_water_texture( world_inside, &gate_camera, layer_depth );
153 render_fb_bind( gpipeline.fb_main, 1 );
154
155 glEnable( GL_STENCIL_TEST );
156
157 render_water_surface( world_inside, &gate_camera );
158
159 glStencilMask( 0xFF );
160 glStencilFunc( GL_ALWAYS, 1, 0xFF );
161 glDisable( GL_STENCIL_TEST );
162 }
163
164 return 1;
165 }
166
167 VG_STATIC int gate_intersect_plane( ent_gate *gate,
168 v3f pos, v3f last, v2f where )
169 {
170 v4f surface;
171 q_mulv( gate->q[0], (v3f){0.0f,0.0f,-1.0f}, surface );
172 surface[3] = v3_dot( surface, gate->co[0] );
173
174 v3f v0, c, delta, p0;
175 v3_sub( pos, last, v0 );
176 float l = v3_length( v0 );
177
178 if( l == 0.0f )
179 return 0;
180
181 v3_divs( v0, l, v0 );
182
183 v3_muls( surface, surface[3], c );
184 v3_sub( c, last, delta );
185
186 float d = v3_dot( surface, v0 );
187
188 if( d > 0.00001f ){
189 float t = v3_dot(delta, surface) / d;
190 if( t >= 0.0f && t <= l ){
191 v3f local, rel;
192 v3_muladds( last, v0, t, local );
193 v3_sub( gate->co[0], local, rel );
194
195 where[0] = v3_dot( rel, gate->to_world[0] );
196 where[1] = v3_dot( rel, gate->to_world[1] );
197
198 where[0] /= v3_dot( gate->to_world[0], gate->to_world[0] );
199 where[1] /= v3_dot( gate->to_world[1], gate->to_world[1] );
200
201 return 1;
202 }
203 }
204
205 return 0;
206 }
207
208 VG_STATIC int gate_intersect( ent_gate *gate, v3f pos, v3f last )
209 {
210 v2f xy;
211
212 if( gate_intersect_plane( gate, pos, last, xy ) ){
213 if( fabsf(xy[0]) <= 1.0f && fabsf(xy[1]) <= 1.0f ){
214 return 1;
215 }
216 }
217
218 return 0;
219 }
220
221 /*
222 * Intersect all gates in the world
223 */
224 VG_STATIC ent_gate *world_intersect_gates( world_instance *world,
225 v3f pos, v3f last )
226 {
227 for( u32 i=0; i<mdl_arrcount(&world->ent_gate); i++ ){
228 ent_gate *gate = mdl_arritm( &world->ent_gate, i );
229 if( gate->type == k_gate_type_unlinked ||
230 gate->type == k_gate_type_nonlocal_unlinked )
231 continue;
232
233 if( gate->type == k_gate_type_nonlocel ){
234 if( skaterift.async_op == k_async_op_world_loading ||
235 skaterift.async_op == k_async_op_world_preloading ){
236 continue;
237 }
238 }
239
240 if( gate_intersect( gate, pos, last ) ){
241 return gate;
242 }
243 }
244
245 return NULL;
246 }
247
248 #endif /* WORLD_GATE_H */