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 m3x3_scale( 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
);
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
;
50 const char *names
[] = { "rs_gate_marker", "rs_gate_marker.001",
51 "rs_gate_marker.002", "rs_gate_marker.003" };
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
;
59 mdl_async_load_glmesh( &mgate
, &world_global
.mesh_gate
);
63 VG_STATIC
int render_gate( world_instance
*world_inside
,
64 ent_gate
*gate
, camera
*cam
, int layer_depth
)
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
);
71 v3_sub( cam
->pos
, gate
->co
[0], v0
);
73 float dist
= v3_dot(v0
, gatedir
);
79 if( v3_dist( cam
->pos
, gate
->co
[0] ) > 100.0f
)
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
);
90 vg_line( a
,b
, 0xffffa000 );
91 vg_line( b
,c
, 0xffffa000 );
92 vg_line( c
,d
, 0xffffa000 );
93 vg_line( d
,a
, 0xffffa000 );
95 vg_line2( gate
->co
[0], gate
->co
[1], 0xff0000ff, 0x00000000 );
98 /* update gate camera */
99 gate_camera
.fov
= cam
->fov
;
100 gate_camera
.nearz
= 0.1f
;
101 gate_camera
.farz
= 2000.0f
;
103 m4x3_mul( gate
->transport
, cam
->transform
, gate_camera
.transform
);
104 camera_update_view( &gate_camera
);
105 camera_update_projection( &gate_camera
);
107 /* Add special clipping plane to projection */
109 q_mulv( gate
->q
[1], (v3f
){0.0f
,0.0f
,-1.0f
}, surface
);
110 surface
[3] = v3_dot( surface
, gate
->co
[1] );
112 m4x3_mulp( gate_camera
.transform_inverse
, surface
, surface
);
113 surface
[3] = -fabsf(surface
[3]);
116 m4x4_clip_projection( gate_camera
.mtx
.p
, surface
);
118 /* Ready to draw with new camrea */
119 camera_finalize( &gate_camera
);
121 vg_line_pt3( gate_camera
.transform
[3], 0.3f
, 0xff00ff00 );
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
});
133 glEnable( GL_STENCIL_TEST
);
134 glStencilOp( GL_KEEP
, GL_KEEP
, GL_REPLACE
);
135 glStencilFunc( GL_ALWAYS
, 1, 0xFF );
136 glStencilMask( 0xFF );
138 mesh_bind( &world_global
.mesh_gate
);
139 mdl_draw_submesh( &world_global
.sm_gate_surface
);
141 glClear( GL_DEPTH_BUFFER_BIT
);
142 glStencilFunc( GL_EQUAL
, 1, 0xFF );
143 glStencilMask( 0x00 );
146 render_world( world_inside
, &gate_camera
, layer_depth
);
149 glDisable( GL_STENCIL_TEST
);
151 render_water_texture( world_inside
, &gate_camera
, layer_depth
);
152 render_fb_bind( gpipeline
.fb_main
, 1 );
154 glEnable( GL_STENCIL_TEST
);
156 render_water_surface( world_inside
, &gate_camera
);
158 glStencilMask( 0xFF );
159 glStencilFunc( GL_ALWAYS
, 1, 0xFF );
160 glDisable( GL_STENCIL_TEST
);
166 VG_STATIC
int gate_intersect_plane( ent_gate
*gate
,
167 v3f pos
, v3f last
, v2f where
)
170 q_mulv( gate
->q
[0], (v3f
){0.0f
,0.0f
,-1.0f
}, surface
);
171 surface
[3] = v3_dot( surface
, gate
->co
[0] );
173 v3f v0
, c
, delta
, p0
;
174 v3_sub( pos
, last
, v0
);
175 float l
= v3_length( v0
);
180 v3_divs( v0
, l
, v0
);
182 v3_muls( surface
, surface
[3], c
);
183 v3_sub( c
, last
, delta
);
185 float d
= v3_dot( surface
, v0
);
188 float t
= v3_dot(delta
, surface
) / d
;
189 if( t
>= 0.0f
&& t
<= l
){
191 v3_muladds( last
, v0
, t
, local
);
192 v3_sub( gate
->co
[0], local
, rel
);
194 where
[0] = v3_dot( rel
, gate
->to_world
[0] );
195 where
[1] = v3_dot( rel
, gate
->to_world
[1] );
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] );
207 VG_STATIC
int gate_intersect( ent_gate
*gate
, v3f pos
, v3f last
)
211 if( gate_intersect_plane( gate
, pos
, last
, xy
) ){
212 if( fabsf(xy
[0]) <= 1.0f
&& fabsf(xy
[1]) <= 1.0f
){
221 * Intersect all gates in the world
223 VG_STATIC ent_gate
*world_intersect_gates( world_instance
*world
,
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
)
232 if( gate_intersect( gate
, pos
, last
) ){
240 #endif /* WORLD_GATE_H */