Fix major overstep with last commit
[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 void 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 vg_acquire_thread_sync();
54 {
55 fb_init( &grender.fb );
56 mdl_unpack_glmesh( mgate, &grender.mdl );
57 }
58 vg_release_thread_sync();
59 }
60
61 static void world_gates_free(void*_)
62 {
63 fb_free( &grender.fb );
64 }
65
66 static void gate_fb_resize(void)
67 {
68 fb_resize( &grender.fb );
69 }
70
71 static int render_gate( teleport_gate *gate, v3f viewpos, m4x3f camera )
72 {
73 v3f viewdir, gatedir;
74 m3x3_mulv( camera, (v3f){0.0f,0.0f,-1.0f}, viewdir );
75 m3x3_mulv( gate->to_world, (v3f){0.0f,0.0f,-1.0f}, gatedir );
76
77 v3f v0;
78 v3_sub( viewpos, gate->co[0], v0 );
79 if( v3_dot(v0, gatedir) >= 0.0f )
80 return 0;
81
82 if( v3_dist( viewpos, gate->co[0] ) > 100.0f )
83 return 0;
84
85 v3f a,b,c,d;
86
87 float sx = gate->dims[0],
88 sy = gate->dims[1];
89 m4x3_mulv( gate->to_world, (v3f){-sx,-sy,0.0f}, a );
90 m4x3_mulv( gate->to_world, (v3f){ sx,-sy,0.0f}, b );
91 m4x3_mulv( gate->to_world, (v3f){ sx, sy,0.0f}, c );
92 m4x3_mulv( gate->to_world, (v3f){-sx, sy,0.0f}, d );
93
94 vg_line( a,b, 0xffffa000 );
95 vg_line( b,c, 0xffffa000 );
96 vg_line( c,d, 0xffffa000 );
97 vg_line( d,a, 0xffffa000 );
98
99 vg_line2( gate->co[0], gate->co[1], 0xff0000ff, 0x00000000 );
100
101 m4x3f cam_new;
102 m4x3_mul( gate->transport, camera, cam_new );
103
104 vg_line_pt3( cam_new[3], 0.3f, 0xff00ff00 );
105
106 m4x3f gate_xform;
107 m4x3_copy( gate->to_world, gate_xform );
108 m4x3_scalev( gate_xform, (v3f){ gate->dims[0], gate->dims[1], 1.0f } );
109
110 m4x3f inverse;
111 m4x3_invert_affine( cam_new, inverse );
112
113 m4x4f view;
114 m4x3_expand( inverse, view );
115
116 v4f surface;
117 m3x3_mulv( gate->recv_to_world, (v3f){0.0f,0.0f,-1.0f}, surface );
118 surface[3] = v3_dot( surface, gate->co[1] );
119
120 m4x4f projection;
121 pipeline_projection( projection, 0.1f, 900.0f );
122
123 m4x3_mulp( inverse, surface, surface );
124 surface[3] = -fabsf(surface[3]);
125 plane_clip_projection( projection, surface );
126
127 m4x4_mul( projection, view, projection );
128
129 if( grender.high_qual )
130 {
131 fb_use( &grender.fb );
132 glClearColor( 0.11f, 0.35f, 0.37f, 1.0f );
133 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
134 }
135 else
136 {
137 shader_gatelq_use();
138 shader_gatelq_uPv( vg_pv );
139 shader_gatelq_uMdl( gate_xform );
140 shader_gatelq_uCam( viewpos );
141 shader_gatelq_uTime( vg_time*0.25f );
142 shader_gatelq_uInvRes( (v2f){
143 1.0f / (float)vg_window_x,
144 1.0f / (float)vg_window_y });
145
146 glEnable( GL_STENCIL_TEST );
147 glStencilOp( GL_KEEP, GL_KEEP, GL_REPLACE );
148 glStencilFunc( GL_ALWAYS, 1, 0xFF );
149 glStencilMask( 0xFF );
150
151 mesh_bind( &grender.mdl );
152 mesh_draw( &grender.mdl );
153
154 glClear( GL_DEPTH_BUFFER_BIT );
155 glStencilFunc( GL_EQUAL, 1, 0xFF );
156 glStencilMask( 0x00 );
157 }
158
159 render_world( projection, cam_new );
160
161 if( grender.high_qual )
162 {
163 /*
164 * TODO: Need to find a way to draw a stencil buffer into the water
165 * rendering
166 */
167
168 render_water_texture( cam_new );
169 fb_use( &grender.fb );
170
171 render_water_surface( projection, cam_new );
172 fb_use( NULL );
173
174 shader_gate_use();
175
176 shader_gate_uPv( vg_pv );
177 shader_gate_uMdl( gate_xform );
178
179 fb_bindtex( &grender.fb, 0 );
180
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 glDisable( GL_STENCIL_TEST );
201
202 render_water_texture( cam_new );
203 fb_use( NULL );
204 glEnable( GL_STENCIL_TEST );
205
206 render_water_surface( projection, cam_new );
207
208 glStencilMask( 0xFF );
209 glStencilFunc( GL_ALWAYS, 1, 0xFF );
210 glDisable( GL_STENCIL_TEST );
211 }
212
213 return 1;
214 }
215
216 static int gate_intersect( teleport_gate *gate, v3f pos, v3f last )
217 {
218 v4f surface;
219 m3x3_mulv( gate->to_world, (v3f){0.0f,0.0f,-1.0f}, surface );
220 surface[3] = v3_dot( surface, gate->co[0] );
221
222 v3f v0, c, delta, p0;
223 v3_sub( pos, last, v0 );
224 float l = v3_length( v0 );
225 v3_divs( v0, l, v0 );
226
227 v3_muls( surface, surface[3], c );
228 v3_sub( c, last, delta );
229
230 float d = v3_dot(surface, v0);
231
232 if( d > 0.00001f )
233 {
234 float t = v3_dot(delta, surface) / d;
235 if( t >= 0.0f && t <= l )
236 {
237 v3f local, rel;
238 v3_muladds( last, v0, t, local );
239 v3_sub( gate->co[0], local, rel );
240
241 v3f vup, vside;
242 m3x3_mulv( gate->to_world, (v3f){0.0f,1.0f,0.0f}, vup );
243 m3x3_mulv( gate->to_world, (v3f){1.0f,0.0f,0.0f}, vside );
244
245 v2f xy = { v3_dot( rel, vside ), v3_dot( rel, vup ) };
246
247 if( fabsf(xy[0]) <= gate->dims[0] && fabsf(xy[1]) <= gate->dims[1] )
248 {
249 return 1;
250 }
251 }
252 }
253
254 return 0;
255 }
256
257 #endif /* WORLD_GATE_H */