8 #include "shaders/gate.h"
9 #include "shaders/gatelq.h"
12 typedef struct teleport_gate teleport_gate
;
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. */
34 m4x3f to_world
, recv_to_world
, transport
;
37 static void gate_transform_update( teleport_gate
*gate
)
41 q_m3x3( gate
->q
[0], gate
->to_world
);
42 v3_copy( gate
->co
[0], gate
->to_world
[3] );
44 m4x3_invert_affine( gate
->to_world
, to_local
);
46 q_m3x3( gate
->q
[1], gate
->recv_to_world
);
47 v3_copy( gate
->co
[1], gate
->recv_to_world
[3] );
48 m4x3_mul( gate
->recv_to_world
, to_local
, gate
->transport
);
51 static void gate_register(void)
53 shader_gate_register();
54 shader_gatelq_register();
57 static void gate_init(void)
59 create_renderbuffer_std( &grender
.fb
, &grender
.rgb
, &grender
.rb
);
61 model
*mgate
= vg_asset_read( "models/rs_gate.mdl" );
62 model_unpack( mgate
, &grender
.mdl
);
66 static void gate_fb_resize(void)
68 resize_renderbuffer_std( &grender
.fb
, &grender
.rgb
, &grender
.rb
);
71 static void render_gate( teleport_gate
*gate
, m4x3f camera
)
73 v3f viewpos
, viewdir
, gatedir
;
74 v3_copy( camera
[3], viewpos
);
75 m3x3_mulv( camera
, (v3f
){0.0f
,0.0f
,-1.0f
}, viewdir
);
76 m3x3_mulv( gate
->to_world
, (v3f
){0.0f
,0.0f
,-1.0f
}, gatedir
);
79 v3_sub( viewpos
, gate
->co
[0], v0
);
80 if( v3_dot(v0
, gatedir
) >= 0.0f
)
83 if( v3_dist( viewpos
, gate
->co
[0] ) > 100.0f
)
88 float sx
= gate
->dims
[0],
90 m4x3_mulv( gate
->to_world
, (v3f
){-sx
,-sy
,0.0f
}, a
);
91 m4x3_mulv( gate
->to_world
, (v3f
){ sx
,-sy
,0.0f
}, b
);
92 m4x3_mulv( gate
->to_world
, (v3f
){ sx
, sy
,0.0f
}, c
);
93 m4x3_mulv( gate
->to_world
, (v3f
){-sx
, sy
,0.0f
}, d
);
95 vg_line( a
,b
, 0xffffa000 );
96 vg_line( b
,c
, 0xffffa000 );
97 vg_line( c
,d
, 0xffffa000 );
98 vg_line( d
,a
, 0xffffa000 );
100 vg_line2( gate
->co
[0], gate
->co
[1], 0xff0000ff, 0x00000000 );
103 m4x3_mul( gate
->transport
, camera
, cam_new
);
105 vg_line_pt3( cam_new
[3], 0.3f
, 0xff00ff00 );
108 m4x3_copy( gate
->to_world
, gate_xform
);
109 m4x3_scalev( gate_xform
, (v3f
){ gate
->dims
[0], gate
->dims
[1], 1.0f
} );
112 m4x3_invert_affine( cam_new
, inverse
);
115 m4x3_expand( inverse
, view
);
118 m3x3_mulv( gate
->recv_to_world
, (v3f
){0.0f
,0.0f
,-1.0f
}, surface
);
119 surface
[3] = v3_dot( surface
, gate
->co
[1] );
122 pipeline_projection( projection
, 0.1f
, 900.0f
);
124 m4x3_mulp( inverse
, surface
, surface
);
125 surface
[3] = -fabsf(surface
[3]);
126 plane_clip_projection( projection
, surface
);
128 m4x4_mul( projection
, view
, projection
);
130 if( grender
.high_qual
)
132 glBindFramebuffer( GL_FRAMEBUFFER
, grender
.fb
);
133 glClearColor( 0.11f
, 0.35f
, 0.37f
, 1.0f
);
134 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
139 shader_gatelq_uPv( vg_pv
);
140 shader_gatelq_uMdl( gate_xform
);
141 shader_gatelq_uCam( viewpos
);
142 shader_gatelq_uTime( vg_time
*0.25f
);
143 shader_gatelq_uInvRes( (v2f
){
144 1.0f
/ (float)vg_window_x
,
145 1.0f
/ (float)vg_window_y
});
147 glEnable( GL_STENCIL_TEST
);
148 glStencilOp( GL_KEEP
, GL_KEEP
, GL_REPLACE
);
149 glStencilFunc( GL_ALWAYS
, 1, 0xFF );
150 glStencilMask( 0xFF );
152 mesh_bind( &grender
.mdl
);
153 mesh_draw( &grender
.mdl
);
155 glClear( GL_DEPTH_BUFFER_BIT
);
156 glStencilFunc( GL_EQUAL
, 1, 0xFF );
157 glStencilMask( 0x00 );
160 render_world( projection
, cam_new
);
162 if( grender
.high_qual
)
165 * TODO: Need to find a way to draw a stencil buffer into the water
169 render_water_texture( cam_new
);
170 glBindFramebuffer( GL_FRAMEBUFFER
, grender
.fb
);
171 render_water_surface( projection
);
172 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
176 shader_gate_uPv( vg_pv
);
177 shader_gate_uMdl( gate_xform
);
179 glActiveTexture( GL_TEXTURE0
);
180 glBindTexture( GL_TEXTURE_2D
, grender
.rgb
);
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 glStencilMask( 0xFF );
201 glStencilFunc( GL_ALWAYS
, 1, 0xFF );
202 glDisable( GL_STENCIL_TEST
);
206 static int gate_intersect( teleport_gate
*gate
, v3f pos
, v3f last
)
209 m3x3_mulv( gate
->to_world
, (v3f
){0.0f
,0.0f
,-1.0f
}, surface
);
210 surface
[3] = v3_dot( surface
, gate
->co
[0] );
212 v3f v0
, c
, delta
, p0
;
213 v3_sub( pos
, last
, v0
);
214 float l
= v3_length( v0
);
215 v3_divs( v0
, l
, v0
);
217 v3_muls( surface
, surface
[3], c
);
218 v3_sub( c
, last
, delta
);
220 float d
= v3_dot(surface
, v0
);
222 if( fabsf(d
) > 0.00001f
)
224 float t
= v3_dot(delta
, surface
) / d
;
225 if( t
>= 0.0f
&& t
<= l
)
228 v3_muladds( last
, v0
, t
, local
);
229 v3_sub( gate
->co
[0], local
, rel
);
232 m3x3_mulv( gate
->to_world
, (v3f
){0.0f
,1.0f
,0.0f
}, vup
);
233 m3x3_mulv( gate
->to_world
, (v3f
){1.0f
,0.0f
,0.0f
}, vside
);
235 v2f xy
= { v3_dot( rel
, vside
), v3_dot( rel
, vup
) };
237 if( fabsf(xy
[0]) <= gate
->dims
[0] && fabsf(xy
[1]) <= gate
->dims
[1] )