marker improvement
[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
134 /* TODO(ART IMPROVEMENT): animate alpha of this? */
135 shader_model_gate_uColour( (v4f){0.0f,1.0f,0.0f,0.0f} );
136 shader_model_gate_uTime( vg.time*0.25f );
137 shader_model_gate_uInvRes( (v2f){
138 1.0f / (float)vg.window_x,
139 1.0f / (float)vg.window_y });
140
141 glEnable( GL_STENCIL_TEST );
142 glStencilOp( GL_KEEP, GL_KEEP, GL_REPLACE );
143 glStencilFunc( GL_ALWAYS, 1, 0xFF );
144 glStencilMask( 0xFF );
145
146 mesh_bind( &world_global.mesh_gate );
147 mdl_draw_submesh( &world_global.sm_gate_surface );
148
149 glClear( GL_DEPTH_BUFFER_BIT );
150 glStencilFunc( GL_EQUAL, 1, 0xFF );
151 glStencilMask( 0x00 );
152 }
153
154 render_world( world_inside, &gate_camera, layer_depth );
155
156 {
157 glDisable( GL_STENCIL_TEST );
158
159 render_water_texture( world_inside, &gate_camera, layer_depth );
160 render_fb_bind( gpipeline.fb_main );
161
162 glEnable( GL_STENCIL_TEST );
163
164 render_water_surface( world_inside, &gate_camera );
165
166 glStencilMask( 0xFF );
167 glStencilFunc( GL_ALWAYS, 1, 0xFF );
168 glDisable( GL_STENCIL_TEST );
169 }
170
171 return 1;
172 }
173
174 VG_STATIC int gate_intersect_plane( ent_gate *gate,
175 v3f pos, v3f last, v2f where )
176 {
177 v4f surface;
178 q_mulv( gate->q[0], (v3f){0.0f,0.0f,-1.0f}, surface );
179 surface[3] = v3_dot( surface, gate->co[0] );
180
181 v3f v0, c, delta, p0;
182 v3_sub( pos, last, v0 );
183 float l = v3_length( v0 );
184
185 if( l == 0.0f )
186 return 0;
187
188 v3_divs( v0, l, v0 );
189
190 v3_muls( surface, surface[3], c );
191 v3_sub( c, last, delta );
192
193 float d = v3_dot( surface, v0 );
194
195 if( d > 0.00001f ){
196 float t = v3_dot(delta, surface) / d;
197 if( t >= 0.0f && t <= l ){
198 v3f local, rel;
199 v3_muladds( last, v0, t, local );
200 v3_sub( gate->co[0], local, rel );
201
202 where[0] = v3_dot( rel, gate->to_world[0] );
203 where[1] = v3_dot( rel, gate->to_world[1] );
204
205 where[0] /= v3_dot( gate->to_world[0], gate->to_world[0] );
206 where[1] /= v3_dot( gate->to_world[1], gate->to_world[1] );
207
208 return 1;
209 }
210 }
211
212 return 0;
213 }
214
215 VG_STATIC int gate_intersect( ent_gate *gate, v3f pos, v3f last )
216 {
217 v2f xy;
218
219 if( gate_intersect_plane( gate, pos, last, xy ) ){
220 if( fabsf(xy[0]) <= 1.0f && fabsf(xy[1]) <= 1.0f ){
221 return 1;
222 }
223 }
224
225 return 0;
226 }
227
228 /*
229 * Intersect all gates in the world
230 */
231 VG_STATIC ent_gate *world_intersect_gates( world_instance *world,
232 v3f pos, v3f last )
233 {
234 for( u32 i=0; i<mdl_arrcount(&world->ent_gate); i++ ){
235 ent_gate *gate = mdl_arritm( &world->ent_gate, i );
236 if( gate->type == k_gate_type_unlinked )
237 continue;
238
239 if( gate_intersect( gate, pos, last ) ){
240 return gate;
241 }
242 }
243
244 return NULL;
245 }
246
247 #endif /* WORLD_GATE_H */