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