Repository navigation
Expand file tree
/
Copy pathengine.cpp
More file actions
822 lines (679 loc) · 22.9 KB
/
Copy pathengine.cpp
File metadata and controls
822 lines (679 loc) · 22.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "../grinliz/glutil.h"
#include "../grinliz/glmath.h"
#include "../grinliz/glmatrix.h"
#include "../grinliz/glvector.h"
#include "../grinliz/glgeometry.h"
#include "../Shiny/include/Shiny.h"
#include "../audio/xenoaudio.h"
#include "gpustatemanager.h"
#include "shadermanager.h"
#include "engine.h"
#include "loosequadtree.h"
#include "renderqueue.h"
#include "surface.h"
#include "texture.h"
#include "particle.h"
#include "rendertarget.h"
#include "engineshaders.h"
#include "bolt.h"
#include "../game/layout.h"
#include "../xegame/xegamelimits.h"
/*
XenoEngine-2 has a cleaned up render queue. The sorting of items probably makes the engine
faster even when not instancing. Cleaning up code makes the underlying algorithm clearer.
*/
using namespace grinliz;
#define ENGINE_RENDER_MODELS
#define ENGINE_RENDER_SHADOWS
#define ENGINE_RENDER_MAP
#define ENGINE_RENDER_GLOW
// Debugging:
//#define ENGINE_GLOW_TO_FRAMEBUFFER
//#define ENGINE_RT1_TO_FRAMEBUFFER
//#define ENGINE_DETAILED_PROFILE PROFILE_BLOCK
#define ENGINE_DETAILED_PROFILE(name)
Engine::Engine( Screenport* port, const gamedb::Reader* database, Map* m )
:
screenport( port ),
initZoomDistance( 0 ),
stages( 0xffff ),
map( 0 ),
uiRenderer( GPUDevice::ENGINE )
{
map = m;
temperature = 0;
frame = 0;
if ( map )
spaceTree = new SpaceTree( -0.1f, 3.1f, Max( m->Width(), m->Height() ) );
else
spaceTree = new SpaceTree( -0.1f, 3.1f, 64 );
renderQueue = new RenderQueue();
for( int i=0; i<RT_COUNT; ++i )
renderTarget[i] = 0;
ShaderManager::Instance()->AddDeviceLossHandler( this );
particleSystem = new ParticleSystem();
boltRenderer = new BoltRenderer();
overlay.Init(&uiRenderer);
overlay.SetScale(port->PhysicalWidth(), port->PhysicalHeight(), LAYOUT_VIRTUAL_HEIGHT);
FontSingleton* bridge = FontSingleton::Instance();
overlay.SetText(bridge->TextAtom(false), bridge->TextAtom(true), FontSingleton::Instance());
overlay.SetPixelSnap(false);
}
Engine::~Engine()
{
delete particleSystem;
ShaderManager::Instance()->RemoveDeviceLossHandler( this );
delete renderQueue;
delete spaceTree;
for( int i=0; i<RT_COUNT; ++i )
delete renderTarget[i];
delete boltRenderer;
}
void Engine::DeviceLoss()
{
for( int i=0; i<RT_COUNT; ++i ) {
delete renderTarget[i];
renderTarget[i] = 0;
}
}
void Engine::CameraIso( bool normal, bool sizeToWidth, float width, float height )
{
if ( normal ) {
camera.TiltRotationToQuat( -45.f, -50.f );
MoveCameraHome();
}
else {
float h = 0;
float theta = ToRadian( EL_FOV/2.0f );
float ratio = 0.5f; // FIXME screenport->UIAspectRatio();
if ( sizeToWidth ) {
h = (width) / tanf(theta);
}
else {
h = (height/2) / (tanf(theta)*ratio);
}
camera.TiltRotationToQuat( -90.0f, 0 );
camera.SetPosWC( width/2.0f, h, height/2.0f );
}
}
void Engine::MoveCameraHome()
{
camera.SetPosWC( 1, 25.0f, 1 );
camera.TiltRotationToQuat( -50.f, -45.f );
}
void Engine::CameraLookAt( float x, float z )
{
Vector3F target = { x, 0, z };
Vector3F at = { 0, 0, 0 };
CameraLookingAt( &at );
if ( Equal( at.y, 0.0f, 0.01f )) {
Vector3F delta = target - at;
camera.DeltaPosWC( delta.x, 0, delta.z );
}
}
void Engine::CameraLookAt( const grinliz::Vector3F& c, const grinliz::Vector3F& target )
{
camera.SetPosWC( c );
Vector3F dir = target - c;
dir.Normalize();
camera.SetDir( dir, V3F_UP );
}
void Engine::CameraLookAt( float x, float z, float heightOfCamera, float yRotation, float tilt )
{
camera.SetPosWC( x, heightOfCamera, z );
camera.TiltRotationToQuat( tilt, yRotation );
Vector3F at;
CameraLookingAt( &at );
camera.DeltaPosWC( x-at.x, 0, z-at.z );
}
void Engine::CameraLookingAt( grinliz::Vector3F* at )
{
const Vector3F* eyeDir = camera.EyeDir3();
float h = camera.PosWC().y;
*at = camera.PosWC() - eyeDir[0]*(h/eyeDir[0].y);
}
void Engine::MoveCameraXZ( float x, float z, Vector3F* calc )
{
// Move the camera, but don't change tilt or rotation.
// The distance is based on triangle ratios.
//
const Vector3F* eyeDir = camera.EyeDir3();
Vector3F start = { x, 0.0f, z };
float h = camera.PosWC().y;
Vector3F pos = start + eyeDir[0]*(h/eyeDir[0].y);
if ( calc ) {
*calc = pos;
}
else {
camera.SetPosWC( pos.x, pos.y, pos.z );
}
}
Texture* Engine::GetMiniMapTexture()
{
return TextureManager::Instance()->GetTexture( "miniMap" );
}
void Engine::QueueSet( EngineShaders* engineShaders,
const CDynArray<Model*>& models,
int requiredModelFlag, int excludedModelFlag,
int requiredShaderFlag, int excludedShaderFlag )
{
renderQueue->Clear();
for (int i = 0; i < models.Size(); ++i) {
Model* model = models[i];
int flag = model->Flags();
if ( (( requiredModelFlag & flag ) == requiredModelFlag )
&& (( excludedModelFlag & flag ) == 0 ) )
{
model->Queue( renderQueue, engineShaders, requiredShaderFlag, excludedShaderFlag );
}
}
}
void Engine::Draw(U32 deltaTime, const Bolt* bolts, int nBolts, IUITracker* tracker)
{
PROFILE_FUNC();
++frame;
for (int i = 0; i < NUM_MODEL_DRAW_CALLS; ++i)
modelDrawCalls[i] = 0;
// Engine should probably have a re-size.
if (overlay.PixelWidth() != screenport->PhysicalWidth() || overlay.PixelHeight() != screenport->PhysicalHeight()) {
overlay.SetScale(screenport->PhysicalWidth(), screenport->PhysicalHeight(), LAYOUT_VIRTUAL_HEIGHT);
}
// -------- Camera & Frustum -------- //
screenport->SetView(camera.ViewMatrix(), GPUDevice::Instance()); // Draw the camera
Rectangle3F particleClip;
particleClip.Set(camera.PosWC().x - EL_FAR, -10.0f, camera.PosWC().z - EL_FAR,
camera.PosWC().x + EL_FAR, 100.0f, camera.PosWC().z + EL_FAR);
particleSystem->SetClip(particleClip);
if (XenoAudio::Instance()) {
Vector3F lookingAt = { 0, 0, 0 };
this->CameraLookingAt(&lookingAt);
const Vector3F* camDir = camera.EyeDir3();
Vector3F dir = camDir[0];
dir.y = 0;
XenoAudio::Instance()->SetListener(lookingAt, dir);
}
#ifdef DEBUG
#if 0 // turn back on if camera problems
{
Vector3F at;
CameraLookingAt(&at);
//GLOUTPUT(( "View set. Camera at (%.1f,%.1f,%.1f) looking at (%.1f,%.1f,%.1f)\n",
// camera.PosWC().x, camera.PosWC().y, camera.PosWC().z,
// at.x, at.y, at.z ));
if (map) {
Rectangle2I b = map->Bounds();
b.Outset(20);
if (!b.Contains((int)at.x, (int)at.z)) {
GLASSERT(0); // looking at nothing.
}
}
}
#endif
#endif
// Compute the frustum planes and query the tree.
Plane planes[6];
CalcFrustumPlanes(planes);
// Get the working set of models.
modelCache.Clear();
spaceTree->Query(&modelCache, planes, 6, 0, true, 0, Model::MODEL_INVISIBLE);
// Track the UI relevant ones:
trackedModels.Clear();
for (int i = 0; i < modelCache.Size(); ++i) {
Model* m = modelCache[i];
if (m->Flags() & Model::MODEL_UI_TRACK) {
trackedModels.Push(m);
}
}
if ( map && (stages & STAGE_VOXEL) ) {
ENGINE_DETAILED_PROFILE(MapPrep);
map->PrepVoxels(spaceTree, &modelCache, planes);
map->PrepGrid( spaceTree );
}
GPUDevice* device = GPUDevice::Instance();
Color4F ambient, diffuse, shadow;
Vector4F dir;
lighting.Query( &diffuse, &ambient, &shadow, temperature, &dir );
device->ambient = ambient;
device->directionWC = dir;
device->diffuse = diffuse;
EngineShaders engineShaders;
{
int flag = ShaderManager::LIGHTING;
LightShader light( flag );
LightShader em( flag );
em.SetShaderFlag( ShaderManager::EMISSIVE );
LightShader blend( flag, BLEND_NORMAL );
engineShaders.Push( EngineShaders::LIGHT, light );
engineShaders.Push( EngineShaders::EMISSIVE, em );
engineShaders.Push( EngineShaders::BLEND, blend );
}
Rectangle2I mapBounds( 0, 0, MAX_MAP_SIZE-1, MAX_MAP_SIZE-1 );
if ( map ) {
mapBounds = map->Bounds();
}
// ----------- Render Passess ---------- //
#ifdef ENGINE_RENDER_GLOW
if (stages & STAGE_GLOW) {
ENGINE_DETAILED_PROFILE(Glow);
if (!renderTarget[RT_LIGHTS]) {
renderTarget[RT_LIGHTS] = new RenderTarget(screenport->PhysicalWidth(), screenport->PhysicalHeight(), true);
}
renderTarget[RT_LIGHTS]->SetActive(true, this);
renderTarget[RT_LIGHTS]->Clear();
renderTarget[RT_LIGHTS]->screenport->SetPerspective(GPUDevice::Instance());
// ---------- Pass 1 -----------
{
// Tweak the shaders for glow-only rendering.
FlatShader emEx;
//emEx.SetColor(1, 0, 0);
emEx.SetShaderFlag(ShaderManager::EMISSIVE); // Interpret alpha as emissive.
emEx.SetShaderFlag(ShaderManager::EMISSIVE_EXCLUSIVE); // Render emmissive colors or black.
// Replace all kinds of lighting with emissive:
engineShaders.PushAll(emEx);
QueueSet(&engineShaders, modelCache, 0, 0, 0, 0);
int dc = device->DrawCalls();
if (map) map->Submit(&emEx);
if (map && (stages & STAGE_VOXEL)) map->DrawVoxels(&emEx, 0);
renderQueue->Submit(0, 0, 0);
modelDrawCalls[GLOW_EMISSIVE] = dc - device->DrawCalls();
engineShaders.PopAll();
}
renderTarget[RT_LIGHTS]->SetActive(false, this);
}
#endif
if ( map ) {
ENGINE_DETAILED_PROFILE(Map);
#ifdef ENGINE_RENDER_MAP
// Draw shadows to stencil buffer.
float shadowAmount = 1.0f;
Color3F shadow, lighted;
static const Vector3F groundNormal = { 0, 1, 0 };
lighting.CalcLight( groundNormal, 1.0f, &lighted, &shadow, temperature );
#ifdef ENGINE_RENDER_SHADOWS
if ( shadowAmount > 0.0f && (stages & STAGE_SHADOW) ) {
FlatShader shadowShader;
shadowShader.SetStencilMode( STENCIL_WRITE );
shadowShader.SetDepthTest( false ); // flat plane. 1st pass.
shadowShader.SetDepthWrite( false );
shadowShader.SetColorWrite( false );
engineShaders.PushAll( shadowShader );
QueueSet( &engineShaders, modelCache, 0, Model::MODEL_NO_SHADOW, 0, EngineShaders::BLEND );
Matrix4 shadowMatrix;
shadowMatrix.SetCol(1,
-lighting.direction.x / lighting.direction.y,
0.0f,
-lighting.direction.z / lighting.direction.y,
0);
{
modelDrawCalls[SHADOW] = device->DrawCalls();
if ( map && (stages && STAGE_VOXEL) ) {
map->DrawVoxels( &shadowShader, &shadowMatrix );
}
renderQueue->Submit( 0, 0, &shadowMatrix );
modelDrawCalls[SHADOW] = device->DrawCalls() - modelDrawCalls[SHADOW];
}
engineShaders.PopAll();
map->Draw3D( shadow, STENCIL_SET, true );
}
map->Draw3D( lighted, STENCIL_CLEAR, true );
#else
map->Draw3D( lighted, STENCIL_OFF, true );
#endif
map->DrawOverlay(0);
#endif
}
// -------- Models ---------- //
#ifdef ENGINE_RENDER_MODELS
{
ENGINE_DETAILED_PROFILE(Models);
if ( map && (stages & STAGE_VOXEL) ) {
GPUState state;
engineShaders.GetState( EngineShaders::LIGHT, 0, &state );
state.SetShaderFlag( ShaderManager::SATURATION );
map->DrawVoxels( &state, 0 );
}
QueueSet( &engineShaders, modelCache, 0, 0, 0, 0 );
{
modelDrawCalls[MODELS] = device->DrawCalls();
renderQueue->Submit( 0, 0, 0 );
modelDrawCalls[MODELS] = device->DrawCalls() - modelDrawCalls[MODELS];
}
}
#endif
engineShaders.PopAll();
renderQueue->Clear();
if ( map ) {
map->DrawOverlay(1);
}
// --------- Composite Glow -------- //
#ifdef ENGINE_RENDER_GLOW
if ( stages & STAGE_GLOW ) {
ENGINE_DETAILED_PROFILE(CompositeGlow);
Blur();
screenport->SetUI(GPUDevice::Instance());
#endif
#ifdef ENGINE_GLOW_TO_FRAMEBUFFER
CompositingShader shader( BLEND_NONE );
Vector3F p0 = { 0, (float)screenport->PhysicalHeight(), 0 };
Vector3F p1 = { (float)screenport->PhysicalWidth(), 0, 0 };
device->DrawQuad( shader, renderTarget[RT_LIGHTS]->GetTexture(), p0, p1 );
#elif defined(ENGINE_RT1_TO_FRAMEBUFFER)
CompositingShader shader( BLEND_NONE );
Vector3F p0 = { 0, (float)screenport->PhysicalHeight(), 0 };
Vector3F p1 = { (float)screenport->PhysicalWidth(), 0, 0 };
device->DrawQuad( shader, renderTarget[RT_BLUR_1]->GetTexture(), p0, p1 );
#else
#ifdef EL_USE_MRT_BLUR
CompositingShader shader( BLEND_ADD );
for( int i=0; i<BLUR_COUNT; ++i ) {
shader.SetColor( lighting.glow.x, lighting.glow.y, lighting.glow.z, 0 );
Vector3F p0 = { 0, (float)screenport->PhysicalHeight(), 0 };
Vector3F p1 = { (float)screenport->PhysicalWidth(), 0, 0 };
device->DrawQuad( shader, renderTarget[RT_BLUR_0+i]->GetTexture(), p0, p1 );
}
#else
shader.SetTexture0( renderTarget[RT_BLUR_Y]->GetTexture() );
shader.SetColor( lighting.glow.r, lighting.glow.g, lighting.glow.b, 0 );
Vector3F p0 = { 0, screenport->UIHeight(), 0 };
Vector3F p1 = { screenport->UIWidth(), 0, 0 };
shader.DrawQuad( p0, p1 );
#endif
#endif
screenport->SetPerspective(GPUDevice::Instance());
screenport->SetView( camera.ViewMatrix(), GPUDevice::Instance() ); // Draw the camera
}
// ------ Particle system ------------- //
if ( stages & STAGE_BOLT ) {
boltRenderer->DrawAll( bolts, nBolts, this );
}
particleSystem->Update( deltaTime, &camera );
if ( stages & STAGE_PARTICLE ) {
particleSystem->Draw();
}
// ---- Debugging ---
{
DrawDebugLines( deltaTime );
}
// Overlay elements
if (tracker) {
tracker->UpdateUIElements(trackedModels.Mem(), trackedModels.Size());
}
screenport->SetUI(GPUDevice::Instance());
overlay.Render();
screenport->SetPerspective(GPUDevice::Instance());
}
void Engine::Blur()
{
ENGINE_DETAILED_PROFILE(Blur);
GPUDevice* device = GPUDevice::Instance();
#ifdef EL_USE_MRT_BLUR
for( int i=0; i<BLUR_COUNT; ++i ) {
if ( !renderTarget[i+RT_BLUR_0] ) {
int shift = i+1;
renderTarget[i+RT_BLUR_0] = new RenderTarget( screenport->PhysicalWidth()>>shift, screenport->PhysicalHeight()>>shift, false );
}
}
for( int i=0; i<BLUR_COUNT; ++i ) {
renderTarget[i+RT_BLUR_0]->SetActive( true, this );
renderTarget[i + RT_BLUR_0]->Clear();
renderTarget[i+RT_BLUR_0]->screenport->SetUI(GPUDevice::Instance());
//int shift = i+1;
FlatShader shader;
shader.SetDepthTest(false);
shader.SetDepthWrite(false);
Vector3F p0 = { 0, (float)screenport->PhysicalHeight(), 0 };
Vector3F p1 = { (float)screenport->PhysicalWidth(), 0, 0 };
device->DrawQuad( shader, renderTarget[i+RT_BLUR_0-1]->GetTexture(), p0, p1 );
renderTarget[i+RT_BLUR_0]->SetActive( false, this );
}
#else
if ( !renderTarget[RT_BLUR_X] ) {
renderTarget[RT_BLUR_X] = new RenderTarget( screenport->PhysicalWidth(), screenport->PhysicalHeight(), false );
}
if ( !renderTarget[RT_BLUR_Y] ) {
renderTarget[RT_BLUR_Y] = new RenderTarget( screenport->PhysicalWidth(), screenport->PhysicalHeight(), false );
}
// --- x ---- //
renderTarget[RT_BLUR_X]->SetActive( true, this );
renderTarget[RT_BLUR_X]->screenport->SetUI();
{
FlatShader shader;
shader.SetTexture0( renderTarget[RT_LIGHTS]->GetTexture() );
shader.SetShaderFlag( ShaderManager::BLUR );
shader.SetRadius( lighting.glowRadius );
Vector3F p0 = { 0, screenport->UIHeight(), 0 };
Vector3F p1 = { screenport->UIWidth(), 0, 0 };
shader.DrawQuad( p0, p1 );
}
renderTarget[RT_BLUR_X]->SetActive( false, this );
// --- y ---- //
renderTarget[RT_BLUR_Y]->SetActive( true, this );
renderTarget[RT_BLUR_Y]->screenport->SetUI();
{
FlatShader shader;
shader.SetTexture0( renderTarget[RT_BLUR_X]->GetTexture() );
shader.SetShaderFlag( ShaderManager::BLUR );
shader.SetRadius( lighting.glowRadius );
shader.SetShaderFlag( ShaderManager::BLUR_Y );
Vector3F p0 = { 0, screenport->UIHeight(), 0 };
Vector3F p1 = { screenport->UIWidth(), 0, 0 };
shader.DrawQuad( p0, p1 );
}
renderTarget[RT_BLUR_Y]->SetActive( false, this );
#endif
}
bool Engine::RayFromViewToYPlane( const Vector2F& v, const Matrix4& mvpi, Ray* ray, Vector3F* out )
{
screenport->ViewToWorld( v, &mvpi, ray );
Plane plane;
plane.n.Set( 0.0f, 1.0f, 0.0f );
plane.d = 0.0;
int result = REJECT;
if ( out ) {
float t;
result = IntersectLinePlane( ray->origin, ray->origin+ray->direction, plane, out, &t );
}
return ( result == INTERSECT );
}
void Engine::CalcFrustumPlanes( grinliz::Plane* planes )
{
// --------- Compute the view frustum ----------- //
// A strange and ill-documented algorithm from Real Time Rendering, 2nd ed, pg.613
Matrix4 m;
screenport->ViewProjection3D( &m );
// m is the matrix from multiplying projection and model. The
// subscript is the row.
// Left
// -(m3 + m0) * (x,y,z,0) = 0
planes[0].n.x = ( m.X(3+0) + m.X(0+0) );
planes[0].n.y = ( m.X(3+4) + m.X(0+4) );
planes[0].n.z = ( m.X(3+8) + m.X(0+8) );
planes[0].d = ( m.X(3+12) + m.X(0+12) );
planes[0].Normalize();
// right
planes[1].n.x = ( m.X(3+0) - m.X(0+0) );
planes[1].n.y = ( m.X(3+4) - m.X(0+4) );
planes[1].n.z = ( m.X(3+8) - m.X(0+8) );
planes[1].d = ( m.X(3+12) - m.X(0+12) );
planes[1].Normalize();
// bottom
planes[2].n.x = ( m.X(3+0) + m.X(1+0) );
planes[2].n.y = ( m.X(3+4) + m.X(1+4) );
planes[2].n.z = ( m.X(3+8) + m.X(1+8) );
planes[2].d = ( m.X(3+12) + m.X(1+12) );
planes[2].Normalize();
// top
planes[3].n.x = ( m.X(3+0) - m.X(1+0) );
planes[3].n.y = ( m.X(3+4) - m.X(1+4) );
planes[3].n.z = ( m.X(3+8) - m.X(1+8) );
planes[3].d = ( m.X(3+12) - m.X(1+12) );
planes[3].Normalize();
// near
planes[4].n.x = ( m.X(3+0) + m.X(2+0) );
planes[4].n.y = ( m.X(3+4) + m.X(2+4) );
planes[4].n.z = ( m.X(3+8) + m.X(2+8) );
planes[4].d = ( m.X(3+12) + m.X(2+12) );
planes[4].Normalize();
// far
planes[5].n.x = ( m.X(3+0) - m.X(2+0) );
planes[5].n.y = ( m.X(3+4) - m.X(2+4) );
planes[5].n.z = ( m.X(3+8) - m.X(2+8) );
planes[5].d = ( m.X(3+12) - m.X(2+12) );
planes[5].Normalize();
GLASSERT( DotProduct( planes[PLANE_LEFT].n, planes[PLANE_RIGHT].n ) < 0.0f );
GLASSERT( DotProduct( planes[PLANE_TOP].n, planes[PLANE_BOTTOM].n ) < 0.0f );
GLASSERT( DotProduct( planes[PLANE_NEAR].n, planes[PLANE_FAR].n ) < 0.0f );
}
Model* Engine::IntersectModel( const Vector3F& origin, const Vector3F& dir, float length,
HitTestMethod method,
int required, int exclude, const Model * const * ignore, Vector3F* intersection )
{
//GRINLIZ_PERFTRACK
Model* model = spaceTree->QueryRay( origin, dir, length,
required, exclude, ignore,
method,
intersection );
return model;
}
ModelVoxel Engine::IntersectModelVoxel( const Vector3F& origin,
const Vector3F& _dir,
float length,
HitTestMethod testMethod,
int required, int exclude, const Model* const * ignore )
{
ModelVoxel modelVoxel;
Vector3F dir = _dir;
dir.Normalize();
// Check voxel first; intersections limit the length.
if ( map ) {
modelVoxel.voxel = map->IntersectVoxel( origin, dir, length, &modelVoxel.at );
if ( modelVoxel.voxel.x >= 0 ) {
// We did hit a voxel.
float voxelLength = ( origin - modelVoxel.at ).Length();
length = Min( length, voxelLength ); // don't need to check past the voxel.
}
}
Model* m = IntersectModel( origin, dir, length, testMethod, required, exclude, ignore, &modelVoxel.at );
if ( m ) {
m->AABB(); // does nothing except check for valid memory pointer
// Clear the voxel! can only have one state.
modelVoxel.model = m;
modelVoxel.voxel.Set( -1, -1, -1 );
}
return modelVoxel;
}
void Engine::RestrictCamera( const grinliz::Rectangle2F* bounds )
{
const Vector3F* eyeDir = camera.EyeDir3();
Vector3F intersect;
IntersectRayPlane( camera.PosWC(), eyeDir[0], XZ_PLANE, 0.0f, &intersect );
// GLOUTPUT(( "Intersect %.1f, %.1f, %.1f\n", intersect.x, intersect.y, intersect.z ));
Rectangle2F b;
b.Set( 0, 0, (float)map->Width(), (float)map->Height() );
if ( bounds ) {
b = *bounds;
}
if ( intersect.x < b.min.x ) {
camera.DeltaPosWC( (b.min.x - intersect.x), 0.0f, 0.0f );
}
if ( intersect.x > b.max.x ) {
camera.DeltaPosWC( (b.max.x - intersect.x), 0.0f, 0.0f );
}
if ( intersect.z < b.min.y ) {
camera.DeltaPosWC( 0.0f, 0.0f, ( b.min.y - intersect.z) );
}
if ( intersect.z > b.max.y ) {
camera.DeltaPosWC( 0.0f, 0.0f, ( b.max.y - intersect.z) );
}
}
void Engine::SetZoom( float z )
{
z = Clamp( z, GAME_ZOOM_MIN, GAME_ZOOM_MAX );
float d = Interpolate( GAME_ZOOM_MIN, EL_CAMERA_MIN,
GAME_ZOOM_MAX, EL_CAMERA_MAX,
z );
//GLOUTPUT(("Zoom %.1f\n", z));
const Vector3F* eyeDir = camera.EyeDir3();
Vector3F origin;
int result = IntersectRayPlane( camera.PosWC(), eyeDir[0], 1, 0.0f, &origin );
if ( result != grinliz::INTERSECT ) {
MoveCameraHome();
}
else {
// The 'd' is the actual height. Adjust for the angle of the camera.
float len = ( d ) / eyeDir[0].y;
Vector3F pos = origin + len*eyeDir[0];
camera.SetPosWC( pos );
}
//GLOUTPUT(( "Engine set zoom. y=%f to y=%f", startY, camera.PosWC().y ));
}
float Engine::GetZoom()
{
float z = Interpolate( EL_CAMERA_MIN, GAME_ZOOM_MIN,
EL_CAMERA_MAX, GAME_ZOOM_MAX,
camera.PosWC().y );
return z;
}
Texture* Engine::GetRenderTargetTexture( int i )
{
GLASSERT( i >= 0 && i < RT_COUNT );
return renderTarget[i] ? renderTarget[i]->GetTexture() : 0;
}
void Engine::LoadConfigFiles( const char* particleName, const char* lightName )
{
particleSystem->LoadParticleDefs( particleName );
tinyxml2::XMLDocument doc;
doc.LoadFile( lightName );
GLASSERT( !doc.Error() );
lighting.Load( doc.FirstChildElement( "lighting" ));
spaceTree->SetLightDir( lighting.direction );
}
struct DLine {
Vector3F tail;
Vector3F head;
Vector3F color;
int time;
};
grinliz::CArray<DLine, 100> debugLines;
void DebugLine( const grinliz::Vector3F& tail,
const grinliz::Vector3F& head,
float r, float g, float b,
U32 time )
{
if ( debugLines.HasCap() ) {
DLine* dl = debugLines.PushArr(1);
dl->tail = tail;
dl->head = head;
dl->color.Set( r, g, b );
dl->time = time;
}
}
void DrawDebugLines( U32 delta )
{
FlatShader flatShader;
int i=0;
while( i<debugLines.Size() ) {
DLine* dl = &debugLines[i];
if ( dl->time > 0 ) {
dl->time -= delta;
flatShader.SetColor( dl->color.x, dl->color.y, dl->color.z );
//GPUDevice::Instance()->DrawLine( flatShader, dl->tail, dl->head );
++i;
}
else {
debugLines.SwapRemove(i);
}
}
}