2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
14 #include "shaders/model_gate.h"
15 #include "world_water.h"
17 VG_STATIC
void gate_transform_update( ent_gate
*gate
)
19 m4x3f to_local
, recv_to_world
;
21 q_m3x3( gate
->q
[0], gate
->to_world
);
22 v3_copy( gate
->co
[0], gate
->to_world
[3] );
24 m4x3_invert_affine( gate
->to_world
, to_local
);
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
);
30 m4x3_scalev( gate
->to_world
, (v3f
){ gate
->dimensions
[0],
31 gate
->dimensions
[1], 1.0f
} );
34 VG_STATIC
void world_gates_init(void)
36 vg_info( "world_gates_init\n" );
38 shader_model_gate_register();
40 vg_linear_clear( vg_mem
.scratch
);
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
);
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
;
51 const char *names
[] = { "rs_gate_marker", "rs_gate_marker.001",
52 "rs_gate_marker.002", "rs_gate_marker.003" };
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
;
62 vg_acquire_thread_sync();
64 mdl_unpack_glmesh( &mgate
, &world_global
.mesh_gate
);
66 vg_release_thread_sync();
69 VG_STATIC
int render_gate( world_instance
*world_inside
,
70 ent_gate
*gate
, camera
*cam
, int layer_depth
)
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
);
77 v3_sub( cam
->pos
, gate
->co
[0], v0
);
79 float dist
= v3_dot(v0
, gatedir
);
85 if( v3_dist( cam
->pos
, gate
->co
[0] ) > 100.0f
)
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
);
96 vg_line( a
,b
, 0xffffa000 );
97 vg_line( b
,c
, 0xffffa000 );
98 vg_line( c
,d
, 0xffffa000 );
99 vg_line( d
,a
, 0xffffa000 );
101 vg_line2( gate
->co
[0], gate
->co
[1], 0xff0000ff, 0x00000000 );
104 /* update gate camera */
105 gate_camera
.fov
= cam
->fov
;
106 gate_camera
.nearz
= 0.1f
;
107 gate_camera
.farz
= 2000.0f
;
109 m4x3_mul( gate
->transport
, cam
->transform
, gate_camera
.transform
);
110 camera_update_view( &gate_camera
);
111 camera_update_projection( &gate_camera
);
113 /* Add special clipping plane to projection */
115 q_mulv( gate
->q
[1], (v3f
){0.0f
,0.0f
,-1.0f
}, surface
);
116 surface
[3] = v3_dot( surface
, gate
->co
[1] );
118 m4x3_mulp( gate_camera
.transform_inverse
, surface
, surface
);
119 surface
[3] = -fabsf(surface
[3]);
122 m4x4_clip_projection( gate_camera
.mtx
.p
, surface
);
124 /* Ready to draw with new camrea */
125 camera_finalize( &gate_camera
);
127 vg_line_pt3( gate_camera
.transform
[3], 0.3f
, 0xff00ff00 );
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
);
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
});
141 glEnable( GL_STENCIL_TEST
);
142 glStencilOp( GL_KEEP
, GL_KEEP
, GL_REPLACE
);
143 glStencilFunc( GL_ALWAYS
, 1, 0xFF );
144 glStencilMask( 0xFF );
146 mesh_bind( &world_global
.mesh_gate
);
147 mdl_draw_submesh( &world_global
.sm_gate_surface
);
149 glClear( GL_DEPTH_BUFFER_BIT
);
150 glStencilFunc( GL_EQUAL
, 1, 0xFF );
151 glStencilMask( 0x00 );
154 render_world( world_inside
, &gate_camera
, layer_depth
);
157 glDisable( GL_STENCIL_TEST
);
159 render_water_texture( world_inside
, &gate_camera
, layer_depth
);
160 render_fb_bind( gpipeline
.fb_main
);
162 glEnable( GL_STENCIL_TEST
);
164 render_water_surface( world_inside
, &gate_camera
);
166 glStencilMask( 0xFF );
167 glStencilFunc( GL_ALWAYS
, 1, 0xFF );
168 glDisable( GL_STENCIL_TEST
);
174 VG_STATIC
int gate_intersect_plane( ent_gate
*gate
,
175 v3f pos
, v3f last
, v2f where
)
178 q_mulv( gate
->q
[0], (v3f
){0.0f
,0.0f
,-1.0f
}, surface
);
179 surface
[3] = v3_dot( surface
, gate
->co
[0] );
181 v3f v0
, c
, delta
, p0
;
182 v3_sub( pos
, last
, v0
);
183 float l
= v3_length( v0
);
188 v3_divs( v0
, l
, v0
);
190 v3_muls( surface
, surface
[3], c
);
191 v3_sub( c
, last
, delta
);
193 float d
= v3_dot( surface
, v0
);
196 float t
= v3_dot(delta
, surface
) / d
;
197 if( t
>= 0.0f
&& t
<= l
){
199 v3_muladds( last
, v0
, t
, local
);
200 v3_sub( gate
->co
[0], local
, rel
);
202 where
[0] = v3_dot( rel
, gate
->to_world
[0] );
203 where
[1] = v3_dot( rel
, gate
->to_world
[1] );
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] );
215 VG_STATIC
int gate_intersect( ent_gate
*gate
, v3f pos
, v3f last
)
219 if( gate_intersect_plane( gate
, pos
, last
, xy
) ){
220 if( fabsf(xy
[0]) <= 1.0f
&& fabsf(xy
[1]) <= 1.0f
){
229 * Intersect all gates in the world
231 VG_STATIC ent_gate
*world_intersect_gates( world_instance
*world
,
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
)
239 if( gate_intersect( gate
, pos
, last
) ){
247 #endif /* WORLD_GATE_H */