well yeah i guess
[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
12 #include "shaders/gatelq.h"
13 #include "world_water.h"
14
15
16 VG_STATIC void gate_transform_update( teleport_gate *gate )
17 {
18 m4x3f to_local;
19
20 q_m3x3( gate->q[0], gate->to_world );
21 v3_copy( gate->co[0], gate->to_world[3] );
22
23 m4x3_invert_affine( gate->to_world, to_local );
24
25 q_m3x3( gate->q[1], gate->recv_to_world );
26 v3_copy( gate->co[1], gate->recv_to_world[3] );
27 m4x3_mul( gate->recv_to_world, to_local, gate->transport );
28 }
29
30 VG_STATIC void world_gates_init(void)
31 {
32 vg_info( "world_gates_init\n" );
33
34 shader_gatelq_register();
35
36 vg_linear_clear( vg_mem.scratch );
37 mdl_context *mgate = mdl_load_full( vg_mem.scratch, "models/rs_gate.mdl" );
38
39 vg_acquire_thread_sync();
40 {
41 mdl_unpack_glmesh( mgate, &world.mesh_gate_surface );
42 }
43 vg_release_thread_sync();
44 }
45
46 VG_STATIC int render_gate( teleport_gate *gate, v3f viewpos, m4x3f camera )
47 {
48 v3f viewdir, gatedir;
49 m3x3_mulv( camera, (v3f){0.0f,0.0f,-1.0f}, viewdir );
50 m3x3_mulv( gate->to_world, (v3f){0.0f,0.0f,-1.0f}, gatedir );
51
52 v3f v0;
53 v3_sub( viewpos, gate->co[0], v0 );
54 if( v3_dot(v0, gatedir) >= 0.0f )
55 return 0;
56
57 if( v3_dist( viewpos, gate->co[0] ) > 100.0f )
58 return 0;
59
60 v3f a,b,c,d;
61
62 float sx = gate->dims[0],
63 sy = gate->dims[1];
64 m4x3_mulv( gate->to_world, (v3f){-sx,-sy,0.0f}, a );
65 m4x3_mulv( gate->to_world, (v3f){ sx,-sy,0.0f}, b );
66 m4x3_mulv( gate->to_world, (v3f){ sx, sy,0.0f}, c );
67 m4x3_mulv( gate->to_world, (v3f){-sx, sy,0.0f}, d );
68
69 vg_line( a,b, 0xffffa000 );
70 vg_line( b,c, 0xffffa000 );
71 vg_line( c,d, 0xffffa000 );
72 vg_line( d,a, 0xffffa000 );
73
74 vg_line2( gate->co[0], gate->co[1], 0xff0000ff, 0x00000000 );
75
76 m4x3f cam_new;
77 m4x3_mul( gate->transport, camera, cam_new );
78
79 vg_line_pt3( cam_new[3], 0.3f, 0xff00ff00 );
80
81 m4x3f gate_xform;
82 m4x3_copy( gate->to_world, gate_xform );
83 m4x3_scalev( gate_xform, (v3f){ gate->dims[0], gate->dims[1], 1.0f } );
84
85 m4x3f inverse;
86 m4x3_invert_affine( cam_new, inverse );
87
88 m4x4f view;
89 m4x3_expand( inverse, view );
90
91 v4f surface;
92 m3x3_mulv( gate->recv_to_world, (v3f){0.0f,0.0f,-1.0f}, surface );
93 surface[3] = v3_dot( surface, gate->co[1] );
94
95 m4x4f projection;
96 pipeline_projection( projection, 0.1f, 900.0f );
97
98 m4x3_mulp( inverse, surface, surface );
99 surface[3] = -fabsf(surface[3]);
100 plane_clip_projection( projection, surface );
101
102 m4x4_mul( projection, view, projection );
103
104 {
105 shader_gatelq_use();
106 shader_gatelq_uPv( vg.pv );
107 shader_gatelq_uMdl( gate_xform );
108 shader_gatelq_uCam( viewpos );
109 shader_gatelq_uTime( vg.time*0.25f );
110 shader_gatelq_uInvRes( (v2f){
111 1.0f / (float)vg.window_x,
112 1.0f / (float)vg.window_y });
113
114 glEnable( GL_STENCIL_TEST );
115 glStencilOp( GL_KEEP, GL_KEEP, GL_REPLACE );
116 glStencilFunc( GL_ALWAYS, 1, 0xFF );
117 glStencilMask( 0xFF );
118
119 mesh_bind( &world.mesh_gate_surface );
120 mesh_draw( &world.mesh_gate_surface );
121
122 glClear( GL_DEPTH_BUFFER_BIT );
123 glStencilFunc( GL_EQUAL, 1, 0xFF );
124 glStencilMask( 0x00 );
125 }
126
127 render_world( projection, cam_new );
128
129 {
130 glDisable( GL_STENCIL_TEST );
131
132 render_water_texture( cam_new );
133 fb_use( NULL );
134 glEnable( GL_STENCIL_TEST );
135
136 render_water_surface( projection, cam_new );
137
138 glStencilMask( 0xFF );
139 glStencilFunc( GL_ALWAYS, 1, 0xFF );
140 glDisable( GL_STENCIL_TEST );
141 }
142
143 return 1;
144 }
145
146 VG_STATIC int gate_intersect( teleport_gate *gate, v3f pos, v3f last )
147 {
148 v4f surface;
149 m3x3_mulv( gate->to_world, (v3f){0.0f,0.0f,-1.0f}, surface );
150 surface[3] = v3_dot( surface, gate->co[0] );
151
152 v3f v0, c, delta, p0;
153 v3_sub( pos, last, v0 );
154 float l = v3_length( v0 );
155 v3_divs( v0, l, v0 );
156
157 v3_muls( surface, surface[3], c );
158 v3_sub( c, last, delta );
159
160 float d = v3_dot(surface, v0);
161
162 if( d > 0.00001f )
163 {
164 float t = v3_dot(delta, surface) / d;
165 if( t >= 0.0f && t <= l )
166 {
167 v3f local, rel;
168 v3_muladds( last, v0, t, local );
169 v3_sub( gate->co[0], local, rel );
170
171 v3f vup, vside;
172 m3x3_mulv( gate->to_world, (v3f){0.0f,1.0f,0.0f}, vup );
173 m3x3_mulv( gate->to_world, (v3f){1.0f,0.0f,0.0f}, vside );
174
175 v2f xy = { v3_dot( rel, vside ), v3_dot( rel, vup ) };
176
177 if( fabsf(xy[0]) <= gate->dims[0] && fabsf(xy[1]) <= gate->dims[1] )
178 {
179 return 1;
180 }
181 }
182 }
183
184 return 0;
185 }
186
187 #endif /* WORLD_GATE_H */