fixe
[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 m4x3_scalev( 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 mdl_load_mesh_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_close( &mgate );
61
62 vg_acquire_thread_sync();
63 {
64 mdl_unpack_glmesh( &mgate, &world_global.mesh_gate );
65 }
66 vg_release_thread_sync();
67 }
68
69 VG_STATIC int render_gate( world_instance *world_inside,
70 ent_gate *gate, camera *cam, int layer_depth )
71 {
72 v3f viewdir, gatedir;
73 m3x3_mulv( cam->transform, (v3f){0.0f,0.0f,-1.0f}, viewdir );
74 q_mulv( gate->q[0], (v3f){0.0f,0.0f,-1.0f}, gatedir );
75
76 v3f v0;
77 v3_sub( cam->pos, gate->co[0], v0 );
78
79 float dist = v3_dot(v0, gatedir);
80
81 /* Hard cutoff */
82 if( dist > 3.0f )
83 return 0;
84
85 if( v3_dist( cam->pos, gate->co[0] ) > 100.0f )
86 return 0;
87
88 {
89 v3f a,b,c,d;
90
91 m4x3_mulv( gate->to_world, (v3f){-1.0f,-1.0f,0.0f}, a );
92 m4x3_mulv( gate->to_world, (v3f){ 1.0f,-1.0f,0.0f}, b );
93 m4x3_mulv( gate->to_world, (v3f){ 1.0f, 1.0f,0.0f}, c );
94 m4x3_mulv( gate->to_world, (v3f){-1.0f, 1.0f,0.0f}, d );
95
96 vg_line( a,b, 0xffffa000 );
97 vg_line( b,c, 0xffffa000 );
98 vg_line( c,d, 0xffffa000 );
99 vg_line( d,a, 0xffffa000 );
100
101 vg_line2( gate->co[0], gate->co[1], 0xff0000ff, 0x00000000 );
102 }
103
104 /* update gate camera */
105 gate_camera.fov = cam->fov;
106 gate_camera.nearz = 0.1f;
107 gate_camera.farz = 2000.0f;
108
109 m4x3_mul( gate->transport, cam->transform, gate_camera.transform );
110 camera_update_view( &gate_camera );
111 camera_update_projection( &gate_camera );
112
113 /* Add special clipping plane to projection */
114 v4f surface;
115 q_mulv( gate->q[1], (v3f){0.0f,0.0f,-1.0f}, surface );
116 surface[3] = v3_dot( surface, gate->co[1] );
117
118 m4x3_mulp( gate_camera.transform_inverse, surface, surface );
119 surface[3] = -fabsf(surface[3]);
120
121 if( dist < -0.5f )
122 m4x4_clip_projection( gate_camera.mtx.p, surface );
123
124 /* Ready to draw with new camrea */
125 camera_finalize( &gate_camera );
126
127 vg_line_pt3( gate_camera.transform[3], 0.3f, 0xff00ff00 );
128 {
129 shader_model_gate_use();
130 shader_model_gate_uPv( cam->mtx.pv );
131 shader_model_gate_uMdl( gate->to_world );
132 shader_model_gate_uCam( cam->pos );
133 shader_model_gate_uColour( (v4f){0.0f,1.0f,0.0f,0.0f} );
134 shader_model_gate_uTime( vg.time*0.25f );
135 shader_model_gate_uInvRes( (v2f){
136 1.0f / (float)vg.window_x,
137 1.0f / (float)vg.window_y });
138
139 glEnable( GL_STENCIL_TEST );
140 glStencilOp( GL_KEEP, GL_KEEP, GL_REPLACE );
141 glStencilFunc( GL_ALWAYS, 1, 0xFF );
142 glStencilMask( 0xFF );
143
144 mesh_bind( &world_global.mesh_gate );
145 mdl_draw_submesh( &world_global.sm_gate_surface );
146
147 glClear( GL_DEPTH_BUFFER_BIT );
148 glStencilFunc( GL_EQUAL, 1, 0xFF );
149 glStencilMask( 0x00 );
150 }
151
152 render_world( world_inside, &gate_camera, layer_depth );
153
154 {
155 glDisable( GL_STENCIL_TEST );
156
157 render_water_texture( world_inside, &gate_camera, layer_depth );
158 render_fb_bind( gpipeline.fb_main );
159
160 glEnable( GL_STENCIL_TEST );
161
162 render_water_surface( world_inside, &gate_camera );
163
164 glStencilMask( 0xFF );
165 glStencilFunc( GL_ALWAYS, 1, 0xFF );
166 glDisable( GL_STENCIL_TEST );
167 }
168
169 return 1;
170 }
171
172 VG_STATIC int gate_intersect_plane( ent_gate *gate,
173 v3f pos, v3f last, v2f where )
174 {
175 v4f surface;
176 q_mulv( gate->q[0], (v3f){0.0f,0.0f,-1.0f}, surface );
177 surface[3] = v3_dot( surface, gate->co[0] );
178
179 v3f v0, c, delta, p0;
180 v3_sub( pos, last, v0 );
181 float l = v3_length( v0 );
182
183 if( l == 0.0f )
184 return 0;
185
186 v3_divs( v0, l, v0 );
187
188 v3_muls( surface, surface[3], c );
189 v3_sub( c, last, delta );
190
191 float d = v3_dot( surface, v0 );
192
193 if( d > 0.00001f ){
194 float t = v3_dot(delta, surface) / d;
195 if( t >= 0.0f && t <= l ){
196 v3f local, rel;
197 v3_muladds( last, v0, t, local );
198 v3_sub( gate->co[0], local, rel );
199
200 where[0] = v3_dot( rel, gate->to_world[0] );
201 where[1] = v3_dot( rel, gate->to_world[1] );
202
203 where[0] /= v3_dot( gate->to_world[0], gate->to_world[0] );
204 where[1] /= v3_dot( gate->to_world[1], gate->to_world[1] );
205
206 return 1;
207 }
208 }
209
210 return 0;
211 }
212
213 VG_STATIC int gate_intersect( ent_gate *gate, v3f pos, v3f last )
214 {
215 v2f xy;
216
217 if( gate_intersect_plane( gate, pos, last, xy ) ){
218 if( fabsf(xy[0]) <= 1.0f && fabsf(xy[1]) <= 1.0f ){
219 return 1;
220 }
221 }
222
223 return 0;
224 }
225
226 /*
227 * Intersect all gates in the world
228 */
229 VG_STATIC ent_gate *world_intersect_gates( world_instance *world,
230 v3f pos, v3f last )
231 {
232 for( u32 i=0; i<mdl_arrcount(&world->ent_gate); i++ ){
233 ent_gate *gate = mdl_arritm( &world->ent_gate, i );
234 if( gate->type == k_gate_type_unlinked )
235 continue;
236
237 if( gate_intersect( gate, pos, last ) ){
238 return gate;
239 }
240 }
241
242 return NULL;
243 }
244
245 #endif /* WORLD_GATE_H */