6e9b95f9b8d27fcebe255fe496d1a52b78cda74b
[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 GLuint fb, rgb, rb;
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 };
27
28 struct teleport_gate
29 {
30 v3f co[2];
31 v4f q[2];
32 v2f dims;
33
34 m4x3f to_world, recv_to_world, transport;
35 };
36
37 static void gate_transform_update( teleport_gate *gate )
38 {
39 m4x3f to_local;
40
41 q_m3x3( gate->q[0], gate->to_world );
42 v3_copy( gate->co[0], gate->to_world[3] );
43
44 m4x3_invert_affine( gate->to_world, to_local );
45
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 );
49 }
50
51 static void gate_register(void)
52 {
53 shader_gate_register();
54 shader_gatelq_register();
55 }
56
57 static void gate_init(void)
58 {
59 create_renderbuffer_std( &grender.fb, &grender.rgb, &grender.rb );
60
61 model *mgate = vg_asset_read( "models/rs_gate.mdl" );
62 model_unpack( mgate, &grender.mdl );
63 free( mgate );
64 }
65
66 static void gate_fb_resize(void)
67 {
68 resize_renderbuffer_std( &grender.fb, &grender.rgb, &grender.rb );
69 }
70
71 static void render_gate( teleport_gate *gate, m4x3f camera )
72 {
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 );
77
78 v3f v0;
79 v3_sub( viewpos, gate->co[0], v0 );
80 if( v3_dot(v0, gatedir) >= 0.0f )
81 return;
82
83 if( v3_dist( viewpos, gate->co[0] ) > 100.0f )
84 return;
85
86 v3f a,b,c,d;
87
88 float sx = gate->dims[0],
89 sy = gate->dims[1];
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 );
94
95 vg_line( a,b, 0xffffa000 );
96 vg_line( b,c, 0xffffa000 );
97 vg_line( c,d, 0xffffa000 );
98 vg_line( d,a, 0xffffa000 );
99
100 vg_line2( gate->co[0], gate->co[1], 0xff0000ff, 0x00000000 );
101
102 m4x3f cam_new;
103 m4x3_mul( gate->transport, camera, cam_new );
104
105 vg_line_pt3( cam_new[3], 0.3f, 0xff00ff00 );
106
107 m4x3f gate_xform;
108 m4x3_copy( gate->to_world, gate_xform );
109 m4x3_scalev( gate_xform, (v3f){ gate->dims[0], gate->dims[1], 1.0f } );
110
111 m4x3f inverse;
112 m4x3_invert_affine( cam_new, inverse );
113
114 m4x4f view;
115 m4x3_expand( inverse, view );
116
117 v4f surface;
118 m3x3_mulv( gate->recv_to_world, (v3f){0.0f,0.0f,-1.0f}, surface );
119 surface[3] = v3_dot( surface, gate->co[1] );
120
121 m4x4f projection;
122 pipeline_projection( projection, 0.1f, 900.0f );
123
124 m4x3_mulp( inverse, surface, surface );
125 surface[3] = -fabsf(surface[3]);
126 plane_clip_projection( projection, surface );
127
128 m4x4_mul( projection, view, projection );
129
130 if( grender.high_qual )
131 {
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 );
135 }
136 else
137 {
138 shader_gatelq_use();
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 });
146
147 glEnable( GL_STENCIL_TEST );
148 glStencilOp( GL_KEEP, GL_KEEP, GL_REPLACE );
149 glStencilFunc( GL_ALWAYS, 1, 0xFF );
150 glStencilMask( 0xFF );
151
152 mesh_bind( &grender.mdl );
153 mesh_draw( &grender.mdl );
154
155 glClear( GL_DEPTH_BUFFER_BIT );
156 glStencilFunc( GL_EQUAL, 1, 0xFF );
157 glStencilMask( 0x00 );
158 }
159
160 render_world( projection, cam_new );
161
162 if( grender.high_qual )
163 {
164 /*
165 * TODO: Need to find a way to draw a stencil buffer into the water
166 * rendering
167 */
168
169 render_water_texture( cam_new );
170 glBindFramebuffer( GL_FRAMEBUFFER, grender.fb );
171 render_water_surface( projection );
172 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
173
174 shader_gate_use();
175
176 shader_gate_uPv( vg_pv );
177 shader_gate_uMdl( gate_xform );
178
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 });
188
189 glEnable(GL_BLEND);
190 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
191 glBlendEquation(GL_FUNC_ADD);
192
193 mesh_bind( &grender.mdl );
194 mesh_draw( &grender.mdl );
195
196 glDisable(GL_BLEND);
197 }
198 else
199 {
200 glStencilMask( 0xFF );
201 glStencilFunc( GL_ALWAYS, 1, 0xFF );
202 glDisable( GL_STENCIL_TEST );
203 }
204 }
205
206 static int gate_intersect( teleport_gate *gate, v3f pos, v3f last )
207 {
208 v4f surface;
209 m3x3_mulv( gate->to_world, (v3f){0.0f,0.0f,-1.0f}, surface );
210 surface[3] = v3_dot( surface, gate->co[0] );
211
212 v3f v0, c, delta, p0;
213 v3_sub( pos, last, v0 );
214 float l = v3_length( v0 );
215 v3_divs( v0, l, v0 );
216
217 v3_muls( surface, surface[3], c );
218 v3_sub( c, last, delta );
219
220 float d = v3_dot(surface, v0);
221
222 if( fabsf(d) > 0.00001f )
223 {
224 float t = v3_dot(delta, surface) / d;
225 if( t >= 0.0f && t <= l )
226 {
227 v3f local, rel;
228 v3_muladds( last, v0, t, local );
229 v3_sub( gate->co[0], local, rel );
230
231 v3f vup, vside;
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 );
234
235 v2f xy = { v3_dot( rel, vside ), v3_dot( rel, vup ) };
236
237 if( fabsf(xy[0]) <= gate->dims[0] && fabsf(xy[1]) <= gate->dims[1] )
238 {
239 return 1;
240 }
241 }
242 }
243
244 return 0;
245 }
246
247 #endif