moved some stuff
[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 #define GATE_RENDER_PERFORMANCE
9
10 #include "common.h"
11 #include "model.h"
12 #include "render.h"
13
14 #ifndef GATE_RENDER_PERFORMANCE
15 #include "shaders/gate.h"
16 #else
17 #include "shaders/gatelq.h"
18 #endif
19
20 #include "world_water.h"
21
22
23 static struct
24 {
25 struct framebuffer fb;
26 glmesh mdl;
27 }
28 grender =
29 {
30 .fb = {
31 .format = GL_RGB,
32 .div = 1
33 }
34 };
35
36 static void gate_transform_update( teleport_gate *gate )
37 {
38 m4x3f to_local;
39
40 q_m3x3( gate->q[0], gate->to_world );
41 v3_copy( gate->co[0], gate->to_world[3] );
42
43 m4x3_invert_affine( gate->to_world, to_local );
44
45 q_m3x3( gate->q[1], gate->recv_to_world );
46 v3_copy( gate->co[1], gate->recv_to_world[3] );
47 m4x3_mul( gate->recv_to_world, to_local, gate->transport );
48 }
49
50 static void world_gates_init(void)
51 {
52 vg_info( "world_gates_init\n" );
53
54 #ifndef GATE_RENDER_PERFORMANCE
55 shader_gate_register();
56 #else
57 shader_gatelq_register();
58 #endif
59
60 mdl_header *mgate = mdl_load( "models/rs_gate.mdl" );
61
62 vg_acquire_thread_sync();
63 {
64 fb_init( &grender.fb );
65 mdl_unpack_glmesh( mgate, &grender.mdl );
66 }
67 vg_release_thread_sync();
68 }
69
70 static void world_gates_free(void*_)
71 {
72 fb_free( &grender.fb );
73 }
74
75 static void gate_fb_resize(void)
76 {
77 fb_resize( &grender.fb );
78 }
79
80 static int render_gate( teleport_gate *gate, v3f viewpos, m4x3f camera )
81 {
82 v3f viewdir, gatedir;
83 m3x3_mulv( camera, (v3f){0.0f,0.0f,-1.0f}, viewdir );
84 m3x3_mulv( gate->to_world, (v3f){0.0f,0.0f,-1.0f}, gatedir );
85
86 v3f v0;
87 v3_sub( viewpos, gate->co[0], v0 );
88 if( v3_dot(v0, gatedir) >= 0.0f )
89 return 0;
90
91 if( v3_dist( viewpos, gate->co[0] ) > 100.0f )
92 return 0;
93
94 v3f a,b,c,d;
95
96 float sx = gate->dims[0],
97 sy = gate->dims[1];
98 m4x3_mulv( gate->to_world, (v3f){-sx,-sy,0.0f}, a );
99 m4x3_mulv( gate->to_world, (v3f){ sx,-sy,0.0f}, b );
100 m4x3_mulv( gate->to_world, (v3f){ sx, sy,0.0f}, c );
101 m4x3_mulv( gate->to_world, (v3f){-sx, sy,0.0f}, d );
102
103 vg_line( a,b, 0xffffa000 );
104 vg_line( b,c, 0xffffa000 );
105 vg_line( c,d, 0xffffa000 );
106 vg_line( d,a, 0xffffa000 );
107
108 vg_line2( gate->co[0], gate->co[1], 0xff0000ff, 0x00000000 );
109
110 m4x3f cam_new;
111 m4x3_mul( gate->transport, camera, cam_new );
112
113 vg_line_pt3( cam_new[3], 0.3f, 0xff00ff00 );
114
115 m4x3f gate_xform;
116 m4x3_copy( gate->to_world, gate_xform );
117 m4x3_scalev( gate_xform, (v3f){ gate->dims[0], gate->dims[1], 1.0f } );
118
119 m4x3f inverse;
120 m4x3_invert_affine( cam_new, inverse );
121
122 m4x4f view;
123 m4x3_expand( inverse, view );
124
125 v4f surface;
126 m3x3_mulv( gate->recv_to_world, (v3f){0.0f,0.0f,-1.0f}, surface );
127 surface[3] = v3_dot( surface, gate->co[1] );
128
129 m4x4f projection;
130 pipeline_projection( projection, 0.1f, 900.0f );
131
132 m4x3_mulp( inverse, surface, surface );
133 surface[3] = -fabsf(surface[3]);
134 plane_clip_projection( projection, surface );
135
136 m4x4_mul( projection, view, projection );
137
138 #ifndef GATE_RENDER_PERFORMANCE
139 fb_use( &grender.fb );
140 glClearColor( 0.11f, 0.35f, 0.37f, 1.0f );
141 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
142 #else
143 shader_gatelq_use();
144 shader_gatelq_uPv( vg.pv );
145 shader_gatelq_uMdl( gate_xform );
146 shader_gatelq_uCam( viewpos );
147 shader_gatelq_uTime( vg.time*0.25f );
148 shader_gatelq_uInvRes( (v2f){
149 1.0f / (float)vg.window_x,
150 1.0f / (float)vg.window_y });
151
152 glEnable( GL_STENCIL_TEST );
153 glStencilOp( GL_KEEP, GL_KEEP, GL_REPLACE );
154 glStencilFunc( GL_ALWAYS, 1, 0xFF );
155 glStencilMask( 0xFF );
156
157 mesh_bind( &grender.mdl );
158 mesh_draw( &grender.mdl );
159
160 glClear( GL_DEPTH_BUFFER_BIT );
161 glStencilFunc( GL_EQUAL, 1, 0xFF );
162 glStencilMask( 0x00 );
163 #endif
164
165 render_world( projection, cam_new );
166
167 #ifndef GATE_RENDER_PERFORMANCE
168
169 /*
170 * NOTE: Need to find a way to draw a stencil buffer into the water
171 * rendering
172 */
173
174 render_water_texture( cam_new );
175 fb_use( &grender.fb );
176
177 render_water_surface( projection, cam_new );
178 fb_use( NULL );
179
180 shader_gate_use();
181
182 shader_gate_uPv( vg_pv );
183 shader_gate_uMdl( gate_xform );
184
185 fb_bindtex( &grender.fb, 0 );
186
187 shader_gate_uCam( viewpos );
188 shader_gate_uTexMain( 0 );
189 shader_gate_uTexWater( 1 );
190 shader_gate_uTime( vg_time*0.25f );
191 shader_gate_uInvRes( (v2f){
192 1.0f / (float)vg_window_x,
193 1.0f / (float)vg_window_y });
194
195 glEnable(GL_BLEND);
196 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
197 glBlendEquation(GL_FUNC_ADD);
198
199 mesh_bind( &grender.mdl );
200 mesh_draw( &grender.mdl );
201
202 glDisable(GL_BLEND);
203 #else
204 glDisable( GL_STENCIL_TEST );
205
206 render_water_texture( cam_new );
207 fb_use( NULL );
208 glEnable( GL_STENCIL_TEST );
209
210 render_water_surface( projection, cam_new );
211
212 glStencilMask( 0xFF );
213 glStencilFunc( GL_ALWAYS, 1, 0xFF );
214 glDisable( GL_STENCIL_TEST );
215 #endif
216
217 return 1;
218 }
219
220 static int gate_intersect( teleport_gate *gate, v3f pos, v3f last )
221 {
222 v4f surface;
223 m3x3_mulv( gate->to_world, (v3f){0.0f,0.0f,-1.0f}, surface );
224 surface[3] = v3_dot( surface, gate->co[0] );
225
226 v3f v0, c, delta, p0;
227 v3_sub( pos, last, v0 );
228 float l = v3_length( v0 );
229 v3_divs( v0, l, v0 );
230
231 v3_muls( surface, surface[3], c );
232 v3_sub( c, last, delta );
233
234 float d = v3_dot(surface, v0);
235
236 if( d > 0.00001f )
237 {
238 float t = v3_dot(delta, surface) / d;
239 if( t >= 0.0f && t <= l )
240 {
241 v3f local, rel;
242 v3_muladds( last, v0, t, local );
243 v3_sub( gate->co[0], local, rel );
244
245 v3f vup, vside;
246 m3x3_mulv( gate->to_world, (v3f){0.0f,1.0f,0.0f}, vup );
247 m3x3_mulv( gate->to_world, (v3f){1.0f,0.0f,0.0f}, vside );
248
249 v2f xy = { v3_dot( rel, vside ), v3_dot( rel, vup ) };
250
251 if( fabsf(xy[0]) <= gate->dims[0] && fabsf(xy[1]) <= gate->dims[1] )
252 {
253 return 1;
254 }
255 }
256 }
257
258 return 0;
259 }
260
261 #endif /* WORLD_GATE_H */