8 #include "shaders/gate.h"
9 #include "shaders/gatelq.h"
12 typedef struct teleport_gate teleport_gate
;
16 struct framebuffer fb
;
19 int high_qual
; /* If in high performance mode, we don't use RT's, and
20 instead use stencil buffers.
21 There is therefore no heat warp effect. */
38 m4x3f to_world
, recv_to_world
, transport
;
41 static void gate_transform_update( teleport_gate
*gate
)
45 q_m3x3( gate
->q
[0], gate
->to_world
);
46 v3_copy( gate
->co
[0], gate
->to_world
[3] );
48 m4x3_invert_affine( gate
->to_world
, to_local
);
50 q_m3x3( gate
->q
[1], gate
->recv_to_world
);
51 v3_copy( gate
->co
[1], gate
->recv_to_world
[3] );
52 m4x3_mul( gate
->recv_to_world
, to_local
, gate
->transport
);
55 static void gate_register(void)
57 shader_gate_register();
58 shader_gatelq_register();
61 static void gate_init(void)
63 fb_init( &grender
.fb
);
65 mdl_header
*mgate
= mdl_load( "models/rs_gate.mdl" );
66 mdl_unpack_glmesh( mgate
, &grender
.mdl
);
70 static void gate_fb_resize(void)
72 fb_resize( &grender
.fb
);
75 static void render_gate( teleport_gate
*gate
, m4x3f camera
)
77 v3f viewpos
, viewdir
, gatedir
;
78 v3_copy( camera
[3], viewpos
);
79 m3x3_mulv( camera
, (v3f
){0.0f
,0.0f
,-1.0f
}, viewdir
);
80 m3x3_mulv( gate
->to_world
, (v3f
){0.0f
,0.0f
,-1.0f
}, gatedir
);
83 v3_sub( viewpos
, gate
->co
[0], v0
);
84 if( v3_dot(v0
, gatedir
) >= 0.0f
)
87 if( v3_dist( viewpos
, gate
->co
[0] ) > 100.0f
)
92 float sx
= gate
->dims
[0],
94 m4x3_mulv( gate
->to_world
, (v3f
){-sx
,-sy
,0.0f
}, a
);
95 m4x3_mulv( gate
->to_world
, (v3f
){ sx
,-sy
,0.0f
}, b
);
96 m4x3_mulv( gate
->to_world
, (v3f
){ sx
, sy
,0.0f
}, c
);
97 m4x3_mulv( gate
->to_world
, (v3f
){-sx
, sy
,0.0f
}, d
);
99 vg_line( a
,b
, 0xffffa000 );
100 vg_line( b
,c
, 0xffffa000 );
101 vg_line( c
,d
, 0xffffa000 );
102 vg_line( d
,a
, 0xffffa000 );
104 vg_line2( gate
->co
[0], gate
->co
[1], 0xff0000ff, 0x00000000 );
107 m4x3_mul( gate
->transport
, camera
, cam_new
);
109 vg_line_pt3( cam_new
[3], 0.3f
, 0xff00ff00 );
112 m4x3_copy( gate
->to_world
, gate_xform
);
113 m4x3_scalev( gate_xform
, (v3f
){ gate
->dims
[0], gate
->dims
[1], 1.0f
} );
116 m4x3_invert_affine( cam_new
, inverse
);
119 m4x3_expand( inverse
, view
);
122 m3x3_mulv( gate
->recv_to_world
, (v3f
){0.0f
,0.0f
,-1.0f
}, surface
);
123 surface
[3] = v3_dot( surface
, gate
->co
[1] );
126 pipeline_projection( projection
, 0.1f
, 900.0f
);
128 m4x3_mulp( inverse
, surface
, surface
);
129 surface
[3] = -fabsf(surface
[3]);
130 plane_clip_projection( projection
, surface
);
132 m4x4_mul( projection
, view
, projection
);
134 if( grender
.high_qual
)
136 fb_use( &grender
.fb
);
137 glClearColor( 0.11f
, 0.35f
, 0.37f
, 1.0f
);
138 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
143 shader_gatelq_uPv( vg_pv
);
144 shader_gatelq_uMdl( gate_xform
);
145 shader_gatelq_uCam( viewpos
);
146 shader_gatelq_uTime( vg_time
*0.25f
);
147 shader_gatelq_uInvRes( (v2f
){
148 1.0f
/ (float)vg_window_x
,
149 1.0f
/ (float)vg_window_y
});
151 glEnable( GL_STENCIL_TEST
);
152 glStencilOp( GL_KEEP
, GL_KEEP
, GL_REPLACE
);
153 glStencilFunc( GL_ALWAYS
, 1, 0xFF );
154 glStencilMask( 0xFF );
156 mesh_bind( &grender
.mdl
);
157 mesh_draw( &grender
.mdl
);
159 glClear( GL_DEPTH_BUFFER_BIT
);
160 glStencilFunc( GL_EQUAL
, 1, 0xFF );
161 glStencilMask( 0x00 );
164 render_world( projection
, cam_new
);
166 if( grender
.high_qual
)
169 * TODO: Need to find a way to draw a stencil buffer into the water
173 render_water_texture( cam_new
);
174 fb_use( &grender
.fb
);
176 render_water_surface( projection
, cam_new
);
181 shader_gate_uPv( vg_pv
);
182 shader_gate_uMdl( gate_xform
);
184 fb_bindtex( &grender
.fb
, 0 );
186 shader_gate_uCam( viewpos
);
187 shader_gate_uTexMain( 0 );
188 shader_gate_uTexWater( 1 );
189 shader_gate_uTime( vg_time
*0.25f
);
190 shader_gate_uInvRes( (v2f
){
191 1.0f
/ (float)vg_window_x
,
192 1.0f
/ (float)vg_window_y
});
195 glBlendFunc(GL_SRC_ALPHA
,GL_ONE_MINUS_SRC_ALPHA
);
196 glBlendEquation(GL_FUNC_ADD
);
198 mesh_bind( &grender
.mdl
);
199 mesh_draw( &grender
.mdl
);
205 glDisable( GL_STENCIL_TEST
);
207 render_water_texture( cam_new
);
209 glEnable( GL_STENCIL_TEST
);
211 render_water_surface( projection
, cam_new
);
213 glStencilMask( 0xFF );
214 glStencilFunc( GL_ALWAYS
, 1, 0xFF );
215 glDisable( GL_STENCIL_TEST
);
219 static int gate_intersect( teleport_gate
*gate
, v3f pos
, v3f last
)
222 m3x3_mulv( gate
->to_world
, (v3f
){0.0f
,0.0f
,-1.0f
}, surface
);
223 surface
[3] = v3_dot( surface
, gate
->co
[0] );
225 v3f v0
, c
, delta
, p0
;
226 v3_sub( pos
, last
, v0
);
227 float l
= v3_length( v0
);
228 v3_divs( v0
, l
, v0
);
230 v3_muls( surface
, surface
[3], c
);
231 v3_sub( c
, last
, delta
);
233 float d
= v3_dot(surface
, v0
);
235 if( fabsf(d
) > 0.00001f
)
237 float t
= v3_dot(delta
, surface
) / d
;
238 if( t
>= 0.0f
&& t
<= l
)
241 v3_muladds( last
, v0
, t
, local
);
242 v3_sub( gate
->co
[0], local
, rel
);
245 m3x3_mulv( gate
->to_world
, (v3f
){0.0f
,1.0f
,0.0f
}, vup
);
246 m3x3_mulv( gate
->to_world
, (v3f
){1.0f
,0.0f
,0.0f
}, vside
);
248 v2f xy
= { v3_dot( rel
, vside
), v3_dot( rel
, vup
) };
250 if( fabsf(xy
[0]) <= gate
->dims
[0] && fabsf(xy
[1]) <= gate
->dims
[1] )