dont remember
[carveJwlIkooP6JGAAIwe30JlM.git] / world_gate.h
1 #ifndef WORLD_GATE_H
2 #define WORLD_GATE_H
3
4 #define GATE_RENDER_PERFORMANCE
5
6 #include "common.h"
7 #include "model.h"
8 #include "render.h"
9
10 #ifndef GATE_RENDER_PERFORMANCE
11 #include "shaders/gate.h"
12 #else
13 #include "shaders/gatelq.h"
14 #endif
15
16 #include "world_water.h"
17
18
19 typedef struct teleport_gate teleport_gate;
20
21 static struct
22 {
23 struct framebuffer fb;
24 glmesh mdl;
25 }
26 grender =
27 {
28 .fb = {
29 .format = GL_RGB,
30 .div = 1
31 }
32 };
33
34 static void gate_transform_update( teleport_gate *gate )
35 {
36 m4x3f to_local;
37
38 q_m3x3( gate->q[0], gate->to_world );
39 v3_copy( gate->co[0], gate->to_world[3] );
40
41 m4x3_invert_affine( gate->to_world, to_local );
42
43 q_m3x3( gate->q[1], gate->recv_to_world );
44 v3_copy( gate->co[1], gate->recv_to_world[3] );
45 m4x3_mul( gate->recv_to_world, to_local, gate->transport );
46 }
47
48 static void world_gates_init(void)
49 {
50 vg_info( "world_gates_init\n" );
51
52 #ifndef GATE_RENDER_PERFORMANCE
53 shader_gate_register();
54 #else
55 shader_gatelq_register();
56 #endif
57
58 mdl_header *mgate = mdl_load( "models/rs_gate.mdl" );
59
60 vg_acquire_thread_sync();
61 {
62 fb_init( &grender.fb );
63 mdl_unpack_glmesh( mgate, &grender.mdl );
64 }
65 vg_release_thread_sync();
66 }
67
68 static void world_gates_free(void*_)
69 {
70 fb_free( &grender.fb );
71 }
72
73 static void gate_fb_resize(void)
74 {
75 fb_resize( &grender.fb );
76 }
77
78 static int render_gate( teleport_gate *gate, v3f viewpos, m4x3f camera )
79 {
80 v3f viewdir, gatedir;
81 m3x3_mulv( camera, (v3f){0.0f,0.0f,-1.0f}, viewdir );
82 m3x3_mulv( gate->to_world, (v3f){0.0f,0.0f,-1.0f}, gatedir );
83
84 v3f v0;
85 v3_sub( viewpos, gate->co[0], v0 );
86 if( v3_dot(v0, gatedir) >= 0.0f )
87 return 0;
88
89 if( v3_dist( viewpos, gate->co[0] ) > 100.0f )
90 return 0;
91
92 v3f a,b,c,d;
93
94 float sx = gate->dims[0],
95 sy = gate->dims[1];
96 m4x3_mulv( gate->to_world, (v3f){-sx,-sy,0.0f}, a );
97 m4x3_mulv( gate->to_world, (v3f){ sx,-sy,0.0f}, b );
98 m4x3_mulv( gate->to_world, (v3f){ sx, sy,0.0f}, c );
99 m4x3_mulv( gate->to_world, (v3f){-sx, sy,0.0f}, d );
100
101 vg_line( a,b, 0xffffa000 );
102 vg_line( b,c, 0xffffa000 );
103 vg_line( c,d, 0xffffa000 );
104 vg_line( d,a, 0xffffa000 );
105
106 vg_line2( gate->co[0], gate->co[1], 0xff0000ff, 0x00000000 );
107
108 m4x3f cam_new;
109 m4x3_mul( gate->transport, camera, cam_new );
110
111 vg_line_pt3( cam_new[3], 0.3f, 0xff00ff00 );
112
113 m4x3f gate_xform;
114 m4x3_copy( gate->to_world, gate_xform );
115 m4x3_scalev( gate_xform, (v3f){ gate->dims[0], gate->dims[1], 1.0f } );
116
117 m4x3f inverse;
118 m4x3_invert_affine( cam_new, inverse );
119
120 m4x4f view;
121 m4x3_expand( inverse, view );
122
123 v4f surface;
124 m3x3_mulv( gate->recv_to_world, (v3f){0.0f,0.0f,-1.0f}, surface );
125 surface[3] = v3_dot( surface, gate->co[1] );
126
127 m4x4f projection;
128 pipeline_projection( projection, 0.1f, 900.0f );
129
130 m4x3_mulp( inverse, surface, surface );
131 surface[3] = -fabsf(surface[3]);
132 plane_clip_projection( projection, surface );
133
134 m4x4_mul( projection, view, projection );
135
136 #ifndef GATE_RENDER_PERFORMANCE
137 fb_use( &grender.fb );
138 glClearColor( 0.11f, 0.35f, 0.37f, 1.0f );
139 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
140 #else
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 #endif
162
163 render_world( projection, cam_new );
164
165 #ifndef GATE_RENDER_PERFORMANCE
166
167 /*
168 * NOTE: 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 #else
202 glDisable( GL_STENCIL_TEST );
203
204 render_water_texture( cam_new );
205 fb_use( NULL );
206 glEnable( GL_STENCIL_TEST );
207
208 render_water_surface( projection, cam_new );
209
210 glStencilMask( 0xFF );
211 glStencilFunc( GL_ALWAYS, 1, 0xFF );
212 glDisable( GL_STENCIL_TEST );
213 #endif
214
215 return 1;
216 }
217
218 static int gate_intersect( teleport_gate *gate, v3f pos, v3f last )
219 {
220 v4f surface;
221 m3x3_mulv( gate->to_world, (v3f){0.0f,0.0f,-1.0f}, surface );
222 surface[3] = v3_dot( surface, gate->co[0] );
223
224 v3f v0, c, delta, p0;
225 v3_sub( pos, last, v0 );
226 float l = v3_length( v0 );
227 v3_divs( v0, l, v0 );
228
229 v3_muls( surface, surface[3], c );
230 v3_sub( c, last, delta );
231
232 float d = v3_dot(surface, v0);
233
234 if( d > 0.00001f )
235 {
236 float t = v3_dot(delta, surface) / d;
237 if( t >= 0.0f && t <= l )
238 {
239 v3f local, rel;
240 v3_muladds( last, v0, t, local );
241 v3_sub( gate->co[0], local, rel );
242
243 v3f vup, vside;
244 m3x3_mulv( gate->to_world, (v3f){0.0f,1.0f,0.0f}, vup );
245 m3x3_mulv( gate->to_world, (v3f){1.0f,0.0f,0.0f}, vside );
246
247 v2f xy = { v3_dot( rel, vside ), v3_dot( rel, vup ) };
248
249 if( fabsf(xy[0]) <= gate->dims[0] && fabsf(xy[1]) <= gate->dims[1] )
250 {
251 return 1;
252 }
253 }
254 }
255
256 return 0;
257 }
258
259 #endif /* WORLD_GATE_H */