framebuffer formalitites
[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 if( v3_dot(v0, gatedir) >= 0.0f )
56 return 0;
57
58 if( v3_dist( cam->pos, gate->co[0] ) > 100.0f )
59 return 0;
60
61 {
62 v3f a,b,c,d;
63
64 float sx = gate->dims[0],
65 sy = gate->dims[1];
66 m4x3_mulv( gate->to_world, (v3f){-sx,-sy,0.0f}, a );
67 m4x3_mulv( gate->to_world, (v3f){ sx,-sy,0.0f}, b );
68 m4x3_mulv( gate->to_world, (v3f){ sx, sy,0.0f}, c );
69 m4x3_mulv( gate->to_world, (v3f){-sx, sy,0.0f}, d );
70
71 vg_line( a,b, 0xffffa000 );
72 vg_line( b,c, 0xffffa000 );
73 vg_line( c,d, 0xffffa000 );
74 vg_line( d,a, 0xffffa000 );
75
76 vg_line2( gate->co[0], gate->co[1], 0xff0000ff, 0x00000000 );
77 }
78
79 /* update gate camera */
80 static camera gate_view;
81 gate_view.fov = cam->fov;
82 gate_view.nearz = 0.1f;
83 gate_view.farz = 900.0f;
84
85 m4x3_mul( gate->transport, cam->transform, gate_view.transform );
86 camera_update_view( &gate_view );
87 camera_update_projection( &gate_view );
88
89 /* Add special clipping plane to projection */
90 v4f surface;
91 m3x3_mulv( gate->recv_to_world, (v3f){0.0f,0.0f,-1.0f}, surface );
92 surface[3] = v3_dot( surface, gate->co[1] );
93
94 m4x3_mulp( gate_view.transform_inverse, surface, surface );
95 surface[3] = -fabsf(surface[3]);
96 m4x4_clip_projection( gate_view.mtx.p, surface );
97
98 /* Ready to draw with new camrea */
99 camera_finalize( &gate_view );
100
101 vg_line_pt3( gate_view.transform[3], 0.3f, 0xff00ff00 );
102 {
103 m4x3f gate_xform;
104 m4x3_copy( gate->to_world, gate_xform );
105 m4x3_scalev( gate_xform, (v3f){ gate->dims[0], gate->dims[1], 1.0f } );
106
107 shader_gatelq_use();
108 shader_gatelq_uPv( cam->mtx.pv );
109 shader_gatelq_uMdl( gate_xform );
110 shader_gatelq_uCam( cam->pos );
111 shader_gatelq_uTime( vg.time*0.25f );
112 shader_gatelq_uInvRes( (v2f){
113 1.0f / (float)vg.window_x,
114 1.0f / (float)vg.window_y });
115
116 glEnable( GL_STENCIL_TEST );
117 glStencilOp( GL_KEEP, GL_KEEP, GL_REPLACE );
118 glStencilFunc( GL_ALWAYS, 1, 0xFF );
119 glStencilMask( 0xFF );
120
121 mesh_bind( &world.mesh_gate_surface );
122 mesh_draw( &world.mesh_gate_surface );
123
124 glClear( GL_DEPTH_BUFFER_BIT );
125 glStencilFunc( GL_EQUAL, 1, 0xFF );
126 glStencilMask( 0x00 );
127 }
128
129 render_world( &gate_view );
130
131 {
132 glDisable( GL_STENCIL_TEST );
133
134 render_water_texture( &gate_view );
135 render_fb_bind( gpipeline.fb_main );
136
137 glEnable( GL_STENCIL_TEST );
138
139 render_water_surface( &gate_view );
140
141 glStencilMask( 0xFF );
142 glStencilFunc( GL_ALWAYS, 1, 0xFF );
143 glDisable( GL_STENCIL_TEST );
144 }
145
146 return 1;
147 }
148
149 VG_STATIC int gate_intersect( teleport_gate *gate, v3f pos, v3f last )
150 {
151 v4f surface;
152 m3x3_mulv( gate->to_world, (v3f){0.0f,0.0f,-1.0f}, surface );
153 surface[3] = v3_dot( surface, gate->co[0] );
154
155 v3f v0, c, delta, p0;
156 v3_sub( pos, last, v0 );
157 float l = v3_length( v0 );
158
159 if( l == 0.0f )
160 return 0;
161
162 v3_divs( v0, l, v0 );
163
164 v3_muls( surface, surface[3], c );
165 v3_sub( c, last, delta );
166
167 float d = v3_dot( surface, v0 );
168
169 if( d > 0.00001f )
170 {
171 float t = v3_dot(delta, surface) / d;
172 if( t >= 0.0f && t <= l )
173 {
174 v3f local, rel;
175 v3_muladds( last, v0, t, local );
176 v3_sub( gate->co[0], local, rel );
177
178 v3f vup, vside;
179 m3x3_mulv( gate->to_world, (v3f){0.0f,1.0f,0.0f}, vup );
180 m3x3_mulv( gate->to_world, (v3f){1.0f,0.0f,0.0f}, vside );
181
182 v2f xy = { v3_dot( rel, vside ), v3_dot( rel, vup ) };
183
184 if( fabsf(xy[0]) <= gate->dims[0] && fabsf(xy[1]) <= gate->dims[1] )
185 {
186 return 1;
187 }
188 }
189 }
190
191 return 0;
192 }
193
194 #endif /* WORLD_GATE_H */