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