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