whole
[carveJwlIkooP6JGAAIwe30JlM.git] / gate.h
1 #ifndef GATE_H
2 #define GATE_H
3
4 #include "common.h"
5 #include "model.h"
6 #include "render.h"
7 #include "shaders/gate.h"
8 #include "shaders/gatelq.h"
9 #include "water.h"
10
11 typedef struct teleport_gate teleport_gate;
12
13 static struct
14 {
15 struct framebuffer fb;
16 glmesh mdl;
17
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. */
21 }
22 grender =
23 {
24 .high_qual = 0,
25 .fb = {
26 .format = GL_RGB,
27 .div = 1
28 }
29 };
30
31 struct teleport_gate
32 {
33 v3f co[2];
34 v4f q[2];
35 v2f dims;
36
37 m4x3f to_world, recv_to_world, transport;
38 };
39
40 static void gate_transform_update( teleport_gate *gate )
41 {
42 m4x3f to_local;
43
44 q_m3x3( gate->q[0], gate->to_world );
45 v3_copy( gate->co[0], gate->to_world[3] );
46
47 m4x3_invert_affine( gate->to_world, to_local );
48
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 );
52 }
53
54 static void gate_register(void)
55 {
56 shader_gate_register();
57 shader_gatelq_register();
58 }
59
60 static void gate_init(void)
61 {
62 fb_init( &grender.fb );
63
64 mdl_header *mgate = mdl_load( "models/rs_gate.mdl" );
65 mdl_unpack_glmesh( mgate, &grender.mdl );
66 free( mgate );
67 }
68
69 static void gate_fb_resize(void)
70 {
71 fb_resize( &grender.fb );
72 }
73
74 static int render_gate( teleport_gate *gate, m4x3f camera )
75 {
76 v3f viewpos, viewdir, gatedir;
77 v3_copy( camera[3], viewpos );
78 m3x3_mulv( camera, (v3f){0.0f,0.0f,-1.0f}, viewdir );
79 m3x3_mulv( gate->to_world, (v3f){0.0f,0.0f,-1.0f}, gatedir );
80
81 v3f v0;
82 v3_sub( viewpos, gate->co[0], v0 );
83 if( v3_dot(v0, gatedir) >= 0.0f )
84 return 0;
85
86 if( v3_dist( viewpos, gate->co[0] ) > 100.0f )
87 return 0;
88
89 v3f a,b,c,d;
90
91 float sx = gate->dims[0],
92 sy = gate->dims[1];
93 m4x3_mulv( gate->to_world, (v3f){-sx,-sy,0.0f}, a );
94 m4x3_mulv( gate->to_world, (v3f){ sx,-sy,0.0f}, b );
95 m4x3_mulv( gate->to_world, (v3f){ sx, sy,0.0f}, c );
96 m4x3_mulv( gate->to_world, (v3f){-sx, sy,0.0f}, d );
97
98 vg_line( a,b, 0xffffa000 );
99 vg_line( b,c, 0xffffa000 );
100 vg_line( c,d, 0xffffa000 );
101 vg_line( d,a, 0xffffa000 );
102
103 vg_line2( gate->co[0], gate->co[1], 0xff0000ff, 0x00000000 );
104
105 m4x3f cam_new;
106 m4x3_mul( gate->transport, camera, cam_new );
107
108 vg_line_pt3( cam_new[3], 0.3f, 0xff00ff00 );
109
110 m4x3f gate_xform;
111 m4x3_copy( gate->to_world, gate_xform );
112 m4x3_scalev( gate_xform, (v3f){ gate->dims[0], gate->dims[1], 1.0f } );
113
114 m4x3f inverse;
115 m4x3_invert_affine( cam_new, inverse );
116
117 m4x4f view;
118 m4x3_expand( inverse, view );
119
120 v4f surface;
121 m3x3_mulv( gate->recv_to_world, (v3f){0.0f,0.0f,-1.0f}, surface );
122 surface[3] = v3_dot( surface, gate->co[1] );
123
124 m4x4f projection;
125 pipeline_projection( projection, 0.1f, 900.0f );
126
127 m4x3_mulp( inverse, surface, surface );
128 surface[3] = -fabsf(surface[3]);
129 plane_clip_projection( projection, surface );
130
131 m4x4_mul( projection, view, projection );
132
133 if( grender.high_qual )
134 {
135 fb_use( &grender.fb );
136 glClearColor( 0.11f, 0.35f, 0.37f, 1.0f );
137 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
138 }
139 else
140 {
141 shader_gatelq_use();
142 shader_gatelq_uPv( vg_pv );
143 shader_gatelq_uMdl( gate_xform );
144 shader_gatelq_uCam( viewpos );
145 shader_gatelq_uTime( vg_time*0.25f );
146 shader_gatelq_uInvRes( (v2f){
147 1.0f / (float)vg_window_x,
148 1.0f / (float)vg_window_y });
149
150 glEnable( GL_STENCIL_TEST );
151 glStencilOp( GL_KEEP, GL_KEEP, GL_REPLACE );
152 glStencilFunc( GL_ALWAYS, 1, 0xFF );
153 glStencilMask( 0xFF );
154
155 mesh_bind( &grender.mdl );
156 mesh_draw( &grender.mdl );
157
158 glClear( GL_DEPTH_BUFFER_BIT );
159 glStencilFunc( GL_EQUAL, 1, 0xFF );
160 glStencilMask( 0x00 );
161 }
162
163 render_world( projection, cam_new );
164
165 if( grender.high_qual )
166 {
167 /*
168 * TODO: Need to find a way to draw a stencil buffer into the water
169 * rendering
170 */
171
172 render_water_texture( cam_new );
173 fb_use( &grender.fb );
174
175 render_water_surface( projection, cam_new );
176 fb_use( NULL );
177
178 shader_gate_use();
179
180 shader_gate_uPv( vg_pv );
181 shader_gate_uMdl( gate_xform );
182
183 fb_bindtex( &grender.fb, 0 );
184
185 shader_gate_uCam( viewpos );
186 shader_gate_uTexMain( 0 );
187 shader_gate_uTexWater( 1 );
188 shader_gate_uTime( vg_time*0.25f );
189 shader_gate_uInvRes( (v2f){
190 1.0f / (float)vg_window_x,
191 1.0f / (float)vg_window_y });
192
193 glEnable(GL_BLEND);
194 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
195 glBlendEquation(GL_FUNC_ADD);
196
197 mesh_bind( &grender.mdl );
198 mesh_draw( &grender.mdl );
199
200 glDisable(GL_BLEND);
201 }
202 else
203 {
204 glDisable( GL_STENCIL_TEST );
205
206 render_water_texture( cam_new );
207 fb_use( NULL );
208 glEnable( GL_STENCIL_TEST );
209
210 render_water_surface( projection, cam_new );
211
212 glStencilMask( 0xFF );
213 glStencilFunc( GL_ALWAYS, 1, 0xFF );
214 glDisable( GL_STENCIL_TEST );
215 }
216
217 return 1;
218 }
219
220 static int gate_intersect( teleport_gate *gate, v3f pos, v3f last )
221 {
222 v4f surface;
223 m3x3_mulv( gate->to_world, (v3f){0.0f,0.0f,-1.0f}, surface );
224 surface[3] = v3_dot( surface, gate->co[0] );
225
226 v3f v0, c, delta, p0;
227 v3_sub( pos, last, v0 );
228 float l = v3_length( v0 );
229 v3_divs( v0, l, v0 );
230
231 v3_muls( surface, surface[3], c );
232 v3_sub( c, last, delta );
233
234 float d = v3_dot(surface, v0);
235
236 if( d > 0.00001f )
237 {
238 float t = v3_dot(delta, surface) / d;
239 if( t >= 0.0f && t <= l )
240 {
241 v3f local, rel;
242 v3_muladds( last, v0, t, local );
243 v3_sub( gate->co[0], local, rel );
244
245 v3f vup, vside;
246 m3x3_mulv( gate->to_world, (v3f){0.0f,1.0f,0.0f}, vup );
247 m3x3_mulv( gate->to_world, (v3f){1.0f,0.0f,0.0f}, vside );
248
249 v2f xy = { v3_dot( rel, vside ), v3_dot( rel, vup ) };
250
251 if( fabsf(xy[0]) <= gate->dims[0] && fabsf(xy[1]) <= gate->dims[1] )
252 {
253 return 1;
254 }
255 }
256 }
257
258 return 0;
259 }
260
261 #endif