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. */
37 m4x3f to_world
, recv_to_world
, transport
;
40 static void gate_transform_update( teleport_gate
*gate
)
44 q_m3x3( gate
->q
[0], gate
->to_world
);
45 v3_copy( gate
->co
[0], gate
->to_world
[3] );
47 m4x3_invert_affine( gate
->to_world
, to_local
);
49 q_m3x3( gate
->q
[1], gate
->recv_to_world
);
50 v3_copy( gate
->co
[1], gate
->recv_to_world
[3] );
51 m4x3_mul( gate
->recv_to_world
, to_local
, gate
->transport
);
54 static void gate_register(void)
56 shader_gate_register();
57 shader_gatelq_register();
60 static void gate_init(void)
62 fb_init( &grender
.fb
);
64 mdl_header
*mgate
= mdl_load( "models/rs_gate.mdl" );
65 mdl_unpack_glmesh( mgate
, &grender
.mdl
);
69 static void gate_fb_resize(void)
71 fb_resize( &grender
.fb
);
74 static int render_gate( teleport_gate
*gate
, v3f viewpos
, m4x3f camera
)
77 m3x3_mulv( camera
, (v3f
){0.0f
,0.0f
,-1.0f
}, viewdir
);
78 m3x3_mulv( gate
->to_world
, (v3f
){0.0f
,0.0f
,-1.0f
}, gatedir
);
81 v3_sub( viewpos
, gate
->co
[0], v0
);
82 if( v3_dot(v0
, gatedir
) >= 0.0f
)
85 if( v3_dist( viewpos
, gate
->co
[0] ) > 100.0f
)
90 float sx
= gate
->dims
[0],
92 m4x3_mulv( gate
->to_world
, (v3f
){-sx
,-sy
,0.0f
}, a
);
93 m4x3_mulv( gate
->to_world
, (v3f
){ sx
,-sy
,0.0f
}, b
);
94 m4x3_mulv( gate
->to_world
, (v3f
){ sx
, sy
,0.0f
}, c
);
95 m4x3_mulv( gate
->to_world
, (v3f
){-sx
, sy
,0.0f
}, d
);
97 vg_line( a
,b
, 0xffffa000 );
98 vg_line( b
,c
, 0xffffa000 );
99 vg_line( c
,d
, 0xffffa000 );
100 vg_line( d
,a
, 0xffffa000 );
102 vg_line2( gate
->co
[0], gate
->co
[1], 0xff0000ff, 0x00000000 );
105 m4x3_mul( gate
->transport
, camera
, cam_new
);
107 vg_line_pt3( cam_new
[3], 0.3f
, 0xff00ff00 );
110 m4x3_copy( gate
->to_world
, gate_xform
);
111 m4x3_scalev( gate_xform
, (v3f
){ gate
->dims
[0], gate
->dims
[1], 1.0f
} );
114 m4x3_invert_affine( cam_new
, inverse
);
117 m4x3_expand( inverse
, view
);
120 m3x3_mulv( gate
->recv_to_world
, (v3f
){0.0f
,0.0f
,-1.0f
}, surface
);
121 surface
[3] = v3_dot( surface
, gate
->co
[1] );
124 pipeline_projection( projection
, 0.1f
, 900.0f
);
126 m4x3_mulp( inverse
, surface
, surface
);
127 surface
[3] = -fabsf(surface
[3]);
128 plane_clip_projection( projection
, surface
);
130 m4x4_mul( projection
, view
, projection
);
132 if( grender
.high_qual
)
134 fb_use( &grender
.fb
);
135 glClearColor( 0.11f
, 0.35f
, 0.37f
, 1.0f
);
136 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
141 shader_gatelq_uPv( vg_pv
);
142 shader_gatelq_uMdl( gate_xform
);
143 shader_gatelq_uCam( viewpos
);
144 shader_gatelq_uTime( vg_time
*0.25f
);
145 shader_gatelq_uInvRes( (v2f
){
146 1.0f
/ (float)vg_window_x
,
147 1.0f
/ (float)vg_window_y
});
149 glEnable( GL_STENCIL_TEST
);
150 glStencilOp( GL_KEEP
, GL_KEEP
, GL_REPLACE
);
151 glStencilFunc( GL_ALWAYS
, 1, 0xFF );
152 glStencilMask( 0xFF );
154 mesh_bind( &grender
.mdl
);
155 mesh_draw( &grender
.mdl
);
157 glClear( GL_DEPTH_BUFFER_BIT
);
158 glStencilFunc( GL_EQUAL
, 1, 0xFF );
159 glStencilMask( 0x00 );
162 render_world( projection
, cam_new
);
164 if( grender
.high_qual
)
167 * TODO: Need to find a way to draw a stencil buffer into the water
171 render_water_texture( cam_new
);
172 fb_use( &grender
.fb
);
174 render_water_surface( projection
, cam_new
);
179 shader_gate_uPv( vg_pv
);
180 shader_gate_uMdl( gate_xform
);
182 fb_bindtex( &grender
.fb
, 0 );
184 shader_gate_uCam( viewpos
);
185 shader_gate_uTexMain( 0 );
186 shader_gate_uTexWater( 1 );
187 shader_gate_uTime( vg_time
*0.25f
);
188 shader_gate_uInvRes( (v2f
){
189 1.0f
/ (float)vg_window_x
,
190 1.0f
/ (float)vg_window_y
});
193 glBlendFunc(GL_SRC_ALPHA
,GL_ONE_MINUS_SRC_ALPHA
);
194 glBlendEquation(GL_FUNC_ADD
);
196 mesh_bind( &grender
.mdl
);
197 mesh_draw( &grender
.mdl
);
203 glDisable( GL_STENCIL_TEST
);
205 render_water_texture( cam_new
);
207 glEnable( GL_STENCIL_TEST
);
209 render_water_surface( projection
, cam_new
);
211 glStencilMask( 0xFF );
212 glStencilFunc( GL_ALWAYS
, 1, 0xFF );
213 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
);
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] )