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