7 #include "shaders/gate.h"
8 #include "shaders/gatelq.h"
9 #include "world_water.h"
11 typedef struct teleport_gate teleport_gate
;
15 struct framebuffer fb
;
18 int high_qual
; /* If in high performance mode, we don't use RT's, and
19 instead use stencil buffers.
20 There is therefore no heat warp effect. */
31 static void gate_transform_update( teleport_gate
*gate
)
35 q_m3x3( gate
->q
[0], gate
->to_world
);
36 v3_copy( gate
->co
[0], gate
->to_world
[3] );
38 m4x3_invert_affine( gate
->to_world
, to_local
);
40 q_m3x3( gate
->q
[1], gate
->recv_to_world
);
41 v3_copy( gate
->co
[1], gate
->recv_to_world
[3] );
42 m4x3_mul( gate
->recv_to_world
, to_local
, gate
->transport
);
45 static void world_gates_init(void)
47 vg_info( "world_gates_init\n" );
48 shader_gate_register();
49 shader_gatelq_register();
51 mdl_header
*mgate
= mdl_load( "models/rs_gate.mdl" );
53 vg_acquire_thread_sync();
55 fb_init( &grender
.fb
);
56 mdl_unpack_glmesh( mgate
, &grender
.mdl
);
58 vg_release_thread_sync();
61 static void world_gates_free(void*_
)
63 fb_free( &grender
.fb
);
66 static void gate_fb_resize(void)
68 fb_resize( &grender
.fb
);
71 static int render_gate( teleport_gate
*gate
, v3f viewpos
, m4x3f camera
)
74 m3x3_mulv( camera
, (v3f
){0.0f
,0.0f
,-1.0f
}, viewdir
);
75 m3x3_mulv( gate
->to_world
, (v3f
){0.0f
,0.0f
,-1.0f
}, gatedir
);
78 v3_sub( viewpos
, gate
->co
[0], v0
);
79 if( v3_dot(v0
, gatedir
) >= 0.0f
)
82 if( v3_dist( viewpos
, gate
->co
[0] ) > 100.0f
)
87 float sx
= gate
->dims
[0],
89 m4x3_mulv( gate
->to_world
, (v3f
){-sx
,-sy
,0.0f
}, a
);
90 m4x3_mulv( gate
->to_world
, (v3f
){ sx
,-sy
,0.0f
}, b
);
91 m4x3_mulv( gate
->to_world
, (v3f
){ sx
, sy
,0.0f
}, c
);
92 m4x3_mulv( gate
->to_world
, (v3f
){-sx
, sy
,0.0f
}, d
);
94 vg_line( a
,b
, 0xffffa000 );
95 vg_line( b
,c
, 0xffffa000 );
96 vg_line( c
,d
, 0xffffa000 );
97 vg_line( d
,a
, 0xffffa000 );
99 vg_line2( gate
->co
[0], gate
->co
[1], 0xff0000ff, 0x00000000 );
102 m4x3_mul( gate
->transport
, camera
, cam_new
);
104 vg_line_pt3( cam_new
[3], 0.3f
, 0xff00ff00 );
107 m4x3_copy( gate
->to_world
, gate_xform
);
108 m4x3_scalev( gate_xform
, (v3f
){ gate
->dims
[0], gate
->dims
[1], 1.0f
} );
111 m4x3_invert_affine( cam_new
, inverse
);
114 m4x3_expand( inverse
, view
);
117 m3x3_mulv( gate
->recv_to_world
, (v3f
){0.0f
,0.0f
,-1.0f
}, surface
);
118 surface
[3] = v3_dot( surface
, gate
->co
[1] );
121 pipeline_projection( projection
, 0.1f
, 900.0f
);
123 m4x3_mulp( inverse
, surface
, surface
);
124 surface
[3] = -fabsf(surface
[3]);
125 plane_clip_projection( projection
, surface
);
127 m4x4_mul( projection
, view
, projection
);
129 if( grender
.high_qual
)
131 fb_use( &grender
.fb
);
132 glClearColor( 0.11f
, 0.35f
, 0.37f
, 1.0f
);
133 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
138 shader_gatelq_uPv( vg_pv
);
139 shader_gatelq_uMdl( gate_xform
);
140 shader_gatelq_uCam( viewpos
);
141 shader_gatelq_uTime( vg_time
*0.25f
);
142 shader_gatelq_uInvRes( (v2f
){
143 1.0f
/ (float)vg_window_x
,
144 1.0f
/ (float)vg_window_y
});
146 glEnable( GL_STENCIL_TEST
);
147 glStencilOp( GL_KEEP
, GL_KEEP
, GL_REPLACE
);
148 glStencilFunc( GL_ALWAYS
, 1, 0xFF );
149 glStencilMask( 0xFF );
151 mesh_bind( &grender
.mdl
);
152 mesh_draw( &grender
.mdl
);
154 glClear( GL_DEPTH_BUFFER_BIT
);
155 glStencilFunc( GL_EQUAL
, 1, 0xFF );
156 glStencilMask( 0x00 );
159 render_world( projection
, cam_new
);
161 if( grender
.high_qual
)
164 * TODO: Need to find a way to draw a stencil buffer into the water
168 render_water_texture( cam_new
);
169 fb_use( &grender
.fb
);
171 render_water_surface( projection
, cam_new
);
176 shader_gate_uPv( vg_pv
);
177 shader_gate_uMdl( gate_xform
);
179 fb_bindtex( &grender
.fb
, 0 );
181 shader_gate_uCam( viewpos
);
182 shader_gate_uTexMain( 0 );
183 shader_gate_uTexWater( 1 );
184 shader_gate_uTime( vg_time
*0.25f
);
185 shader_gate_uInvRes( (v2f
){
186 1.0f
/ (float)vg_window_x
,
187 1.0f
/ (float)vg_window_y
});
190 glBlendFunc(GL_SRC_ALPHA
,GL_ONE_MINUS_SRC_ALPHA
);
191 glBlendEquation(GL_FUNC_ADD
);
193 mesh_bind( &grender
.mdl
);
194 mesh_draw( &grender
.mdl
);
200 glDisable( GL_STENCIL_TEST
);
202 render_water_texture( cam_new
);
204 glEnable( GL_STENCIL_TEST
);
206 render_water_surface( projection
, cam_new
);
208 glStencilMask( 0xFF );
209 glStencilFunc( GL_ALWAYS
, 1, 0xFF );
210 glDisable( GL_STENCIL_TEST
);
216 static int gate_intersect( teleport_gate
*gate
, v3f pos
, v3f last
)
219 m3x3_mulv( gate
->to_world
, (v3f
){0.0f
,0.0f
,-1.0f
}, surface
);
220 surface
[3] = v3_dot( surface
, gate
->co
[0] );
222 v3f v0
, c
, delta
, p0
;
223 v3_sub( pos
, last
, v0
);
224 float l
= v3_length( v0
);
225 v3_divs( v0
, l
, v0
);
227 v3_muls( surface
, surface
[3], c
);
228 v3_sub( c
, last
, delta
);
230 float d
= v3_dot(surface
, v0
);
234 float t
= v3_dot(delta
, surface
) / d
;
235 if( t
>= 0.0f
&& t
<= l
)
238 v3_muladds( last
, v0
, t
, local
);
239 v3_sub( gate
->co
[0], local
, rel
);
242 m3x3_mulv( gate
->to_world
, (v3f
){0.0f
,1.0f
,0.0f
}, vup
);
243 m3x3_mulv( gate
->to_world
, (v3f
){1.0f
,0.0f
,0.0f
}, vside
);
245 v2f xy
= { v3_dot( rel
, vside
), v3_dot( rel
, vup
) };
247 if( fabsf(xy
[0]) <= gate
->dims
[0] && fabsf(xy
[1]) <= gate
->dims
[1] )
257 #endif /* WORLD_GATE_H */