another api change
[carveJwlIkooP6JGAAIwe30JlM.git] / world_gate.h
1 /*
2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #ifndef WORLD_GATE_H
6 #define WORLD_GATE_H
7
8 #include "common.h"
9 #include "model.h"
10 #include "render.h"
11 #include "camera.h"
12
13 #include "shaders/gatelq.h"
14 #include "world_water.h"
15
16
17 VG_STATIC void gate_transform_update( teleport_gate *gate )
18 {
19 m4x3f to_local;
20
21 q_m3x3( gate->q[0], gate->to_world );
22 v3_copy( gate->co[0], gate->to_world[3] );
23
24 m4x3_invert_affine( gate->to_world, to_local );
25
26 q_m3x3( gate->q[1], gate->recv_to_world );
27 v3_copy( gate->co[1], gate->recv_to_world[3] );
28 m4x3_mul( gate->recv_to_world, to_local, gate->transport );
29 }
30
31 VG_STATIC void world_gates_init(void)
32 {
33 vg_info( "world_gates_init\n" );
34
35 shader_gatelq_register();
36
37 vg_linear_clear( vg_mem.scratch );
38 mdl_context *mgate = mdl_load_full( vg_mem.scratch, "models/rs_gate.mdl" );
39
40 vg_acquire_thread_sync();
41 {
42 mdl_unpack_glmesh( mgate, &world.mesh_gate_surface );
43 }
44 vg_release_thread_sync();
45 }
46
47 VG_STATIC int render_gate( teleport_gate *gate, camera *cam )
48 {
49 v3f viewdir, gatedir;
50 m3x3_mulv( cam->transform, (v3f){0.0f,0.0f,-1.0f}, viewdir );
51 m3x3_mulv( gate->to_world, (v3f){0.0f,0.0f,-1.0f}, gatedir );
52
53 v3f v0;
54 v3_sub( cam->pos, gate->co[0], v0 );
55
56 float dist = v3_dot(v0, gatedir);
57
58 /* Hard cutoff */
59 if( dist > 3.0f )
60 return 0;
61
62 if( v3_dist( cam->pos, gate->co[0] ) > 100.0f )
63 return 0;
64
65 {
66 v3f a,b,c,d;
67
68 float sx = gate->dims[0],
69 sy = gate->dims[1];
70 m4x3_mulv( gate->to_world, (v3f){-sx,-sy,0.0f}, a );
71 m4x3_mulv( gate->to_world, (v3f){ sx,-sy,0.0f}, b );
72 m4x3_mulv( gate->to_world, (v3f){ sx, sy,0.0f}, c );
73 m4x3_mulv( gate->to_world, (v3f){-sx, sy,0.0f}, d );
74
75 vg_line( a,b, 0xffffa000 );
76 vg_line( b,c, 0xffffa000 );
77 vg_line( c,d, 0xffffa000 );
78 vg_line( d,a, 0xffffa000 );
79
80 vg_line2( gate->co[0], gate->co[1], 0xff0000ff, 0x00000000 );
81 }
82
83 /* update gate camera */
84 static camera gate_view;
85 gate_view.fov = cam->fov;
86 gate_view.nearz = 0.1f;
87 gate_view.farz = 2000.0f;
88
89 m4x3_mul( gate->transport, cam->transform, gate_view.transform );
90 camera_update_view( &gate_view );
91 camera_update_projection( &gate_view );
92
93 /* Add special clipping plane to projection */
94 v4f surface;
95 m3x3_mulv( gate->recv_to_world, (v3f){0.0f,0.0f,-1.0f}, surface );
96 surface[3] = v3_dot( surface, gate->co[1] );
97
98 m4x3_mulp( gate_view.transform_inverse, surface, surface );
99 surface[3] = -fabsf(surface[3]);
100
101 if( dist < -0.5f )
102 m4x4_clip_projection( gate_view.mtx.p, surface );
103
104 /* Ready to draw with new camrea */
105 camera_finalize( &gate_view );
106
107 vg_line_pt3( gate_view.transform[3], 0.3f, 0xff00ff00 );
108 {
109 m4x3f gate_xform;
110 m4x3_copy( gate->to_world, gate_xform );
111 m4x3_scalev( gate_xform, (v3f){ gate->dims[0], gate->dims[1], 1.0f } );
112
113 shader_gatelq_use();
114 shader_gatelq_uPv( cam->mtx.pv );
115 shader_gatelq_uMdl( gate_xform );
116 shader_gatelq_uCam( cam->pos );
117 shader_gatelq_uTime( vg.time*0.25f );
118 shader_gatelq_uInvRes( (v2f){
119 1.0f / (float)vg.window_x,
120 1.0f / (float)vg.window_y });
121
122 glEnable( GL_STENCIL_TEST );
123 glStencilOp( GL_KEEP, GL_KEEP, GL_REPLACE );
124 glStencilFunc( GL_ALWAYS, 1, 0xFF );
125 glStencilMask( 0xFF );
126
127 mesh_bind( &world.mesh_gate_surface );
128 mesh_draw( &world.mesh_gate_surface );
129
130 glClear( GL_DEPTH_BUFFER_BIT );
131 glStencilFunc( GL_EQUAL, 1, 0xFF );
132 glStencilMask( 0x00 );
133 }
134
135 render_world( &gate_view );
136
137 {
138 glDisable( GL_STENCIL_TEST );
139
140 render_water_texture( &gate_view );
141 render_fb_bind( gpipeline.fb_main );
142
143 glEnable( GL_STENCIL_TEST );
144
145 render_water_surface( &gate_view );
146
147 glStencilMask( 0xFF );
148 glStencilFunc( GL_ALWAYS, 1, 0xFF );
149 glDisable( GL_STENCIL_TEST );
150 }
151
152 return 1;
153 }
154
155 VG_STATIC int gate_intersect_plane( teleport_gate *gate, v3f pos, v3f last,
156 v2f where )
157 {
158 v4f surface;
159 m3x3_mulv( gate->to_world, (v3f){0.0f,0.0f,-1.0f}, surface );
160 surface[3] = v3_dot( surface, gate->co[0] );
161
162 v3f v0, c, delta, p0;
163 v3_sub( pos, last, v0 );
164 float l = v3_length( v0 );
165
166 if( l == 0.0f )
167 return 0;
168
169 v3_divs( v0, l, v0 );
170
171 v3_muls( surface, surface[3], c );
172 v3_sub( c, last, delta );
173
174 float d = v3_dot( surface, v0 );
175
176 if( d > 0.00001f )
177 {
178 float t = v3_dot(delta, surface) / d;
179 if( t >= 0.0f && t <= l )
180 {
181 v3f local, rel;
182 v3_muladds( last, v0, t, local );
183 v3_sub( gate->co[0], local, rel );
184
185 v3f vup, vside;
186 m3x3_mulv( gate->to_world, (v3f){0.0f,1.0f,0.0f}, vup );
187 m3x3_mulv( gate->to_world, (v3f){1.0f,0.0f,0.0f}, vside );
188
189 where[0] = v3_dot( rel, vside );
190 where[1] = v3_dot( rel, vup );
191
192 return 1;
193 }
194 }
195
196 return 0;
197 }
198
199 VG_STATIC int gate_intersect( teleport_gate *gate, v3f pos, v3f last )
200 {
201 v2f xy;
202
203 if( gate_intersect_plane( gate, pos, last, xy ) )
204 {
205 if( fabsf(xy[0]) <= gate->dims[0] && fabsf(xy[1]) <= gate->dims[1] )
206 {
207 return 1;
208 }
209 }
210
211 return 0;
212 }
213
214 /*
215 * Intersect all gates in the world
216 */
217 VG_STATIC teleport_gate *world_intersect_gates( v3f pos, v3f last )
218 {
219 for( int i=0; i<world.gate_count; i++ )
220 {
221 struct route_gate *rg = &world.gates[i];
222 teleport_gate *gate = &rg->gate;
223
224 if( gate_intersect( gate, pos, last ) )
225 return gate;
226 }
227
228 return NULL;
229 }
230
231 #endif /* WORLD_GATE_H */