File: Light3D.cpp

package info (click to toggle)
scummvm 2.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 450,268 kB
  • sloc: cpp: 4,297,604; asm: 28,322; python: 12,901; sh: 11,219; java: 8,477; xml: 7,843; perl: 2,633; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (1036 lines) | stat: -rw-r--r-- 35,195 bytes parent folder | download | duplicates (2)
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
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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/>.
 *
 */

/*
 * Copyright (C) 2006-2010 - Frictional Games
 *
 * This file is part of HPL1 Engine.
 */

#include "hpl1/engine/scene/Light3D.h"

#include "hpl1/engine/graphics/BillBoard.h"
#include "hpl1/engine/graphics/LowLevelGraphics.h"
#include "hpl1/engine/graphics/Mesh.h"
#include "hpl1/engine/graphics/ParticleSystem3D.h"
#include "hpl1/engine/graphics/RenderList.h"
#include "hpl1/engine/graphics/Renderer3D.h"
#include "hpl1/engine/graphics/SubMesh.h"
#include "hpl1/engine/impl/tinyXML/tinyxml.h"
#include "hpl1/engine/math/Math.h"
#include "hpl1/engine/resources/FileSearcher.h"
#include "hpl1/engine/resources/Resources.h"
#include "hpl1/engine/resources/TextureManager.h"
#include "hpl1/engine/scene/Camera3D.h"
#include "hpl1/engine/scene/MeshEntity.h"
#include "hpl1/engine/scene/PortalContainer.h"
#include "hpl1/engine/scene/SectorVisibility.h"
#include "hpl1/engine/scene/World3D.h"
#include "hpl1/engine/system/low_level_system.h"

namespace hpl {

//////////////////////////////////////////////////////////////////////////
// CONSTRUCTORS
//////////////////////////////////////////////////////////////////////////

//-----------------------------------------------------------------------

iLight3D::iLight3D(tString asName, cResources *apResources) : iLight(), iRenderable(asName) {
	mbStaticCasterAdded = false;

	mbOnlyAffectInInSector = false;

	mbApplyTransformToBV = false;

	mpTextureManager = apResources->GetTextureManager();
	mpFileSearcher = apResources->GetFileSearcher();

	mpFalloffMap = mpTextureManager->Create1D("core_falloff_linear", false);
	if (mpFalloffMap) {
		mpFalloffMap->SetWrapS(eTextureWrap_ClampToEdge);
		mpFalloffMap->SetWrapT(eTextureWrap_ClampToEdge);
	}

	mpVisSectorCont = NULL;
	mlSectorVisibilityCount = -1;

	for (int i = 0; i < 3; ++i)
		mvTempTextures[i] = NULL;
}

//-----------------------------------------------------------------------

iLight3D::~iLight3D() {
	if (mpFalloffMap)
		mpTextureManager->Destroy(mpFalloffMap);

	if (mpVisSectorCont)
		hplDelete(mpVisSectorCont);
}

//-----------------------------------------------------------------------

//////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
//////////////////////////////////////////////////////////////////////////

//-----------------------------------------------------------------------

void iLight3D::SetVisible(bool abVisible) {
	SetRendered(abVisible);

	for (size_t i = 0; i < mvBillboards.size(); ++i) {
		mvBillboards[i]->SetVisible(abVisible);
	}
}

//-----------------------------------------------------------------------

void iLight3D::AddShadowCaster(iRenderable *apObject, cFrustum *apFrustum, bool abStatic, cRenderList *apRenderList) {
	// Log("Testing: %s\n",apObject->GetName().c_str());
	/////////////////////////////////////////////////////
	// Check if the object should be added
	eRenderableType renderType = apObject->GetRenderType();

	// Is it affected by the light.
	if (GetOnlyAffectInSector() && apObject->IsInSector(GetCurrentSector()) == false)
		return;

	// Is it visible
	if (apObject->GetForceShadow() == false) {
		if (apObject->IsVisible() == false)
			return;
		// Does the object cast shadows?
		if (apObject->IsShadowCaster() == false)
			return;

		// Can be material cast shadows?
		iMaterial *pMaterial = apObject->GetMaterial();
		if (pMaterial) {
			if (pMaterial->IsTransperant() || pMaterial->HasAlpha())
				return;
		}

		// Check so that the object is the right type
		if (renderType != eRenderableType_Mesh && renderType != eRenderableType_Normal) {
			return;
		}
	}

	// Log("Right type!\n");

	// Check if the object has all ready been added
	if (abStatic) {
		if (m_setStaticCasters.find(apObject) != m_setStaticCasters.end())
			return;
	} else {
		if (m_setDynamicCasters.find(apObject) != m_setDynamicCasters.end())
			return;
	}

	// Log("Not in list!\n");

	// Check if the object touches the light.
	// if(CollidesWithBV(apObject->GetBoundingVolume())==false){
	//	return;
	// }

	if (CheckObjectIntersection(apObject) == false)
		return;

	// Log("Collides!\n");

	// Log("Shadow is in frustum!\n");

	////////////////////////////////////////////////////
	// All checks passed, add the object!
	if (renderType == eRenderableType_Mesh) {
		cMeshEntity *pMesh = static_cast<cMeshEntity *>(apObject);

		/// Need to add shadow casted objects else shadows might get choppy.
		if (abStatic == false) {
			pMesh->SetGlobalRenderCount(cRenderList::GetGlobalRenderCount());
		}

		for (int i = 0; i < pMesh->GetSubMeshEntityNum(); i++) {
			cSubMeshEntity *pSub = pMesh->GetSubMeshEntity(i);

			if (pSub->IsVisible() == false)
				continue;
			if (apObject->GetForceShadow() == false) {
				iMaterial *pMaterial = pSub->GetMaterial();
				if (pMaterial) {
					if (pMaterial->IsTransperant() || pMaterial->HasAlpha())
						continue;
				}
			}

			if (abStatic) {
				m_setStaticCasters.insert(pSub);
			} else {
				m_setDynamicCasters.insert(pSub);
				/// Need to add shadow casted objects else shadows might get choppy.
				pSub->SetGlobalRenderCount(cRenderList::GetGlobalRenderCount());
			}
		}
	} else {
		if (abStatic) {
			m_setStaticCasters.insert(apObject);
		} else {
			m_setDynamicCasters.insert(apObject);
			apObject->SetGlobalRenderCount(cRenderList::GetGlobalRenderCount());
		}
	}
}

bool iLight3D::HasStaticCasters() {
	return m_setStaticCasters.empty() ? false : true;
}

void iLight3D::ClearCasters(bool abClearStatic) {
	if (abClearStatic)
		m_setStaticCasters.clear();
	m_setDynamicCasters.clear();
}

//-----------------------------------------------------------------------

bool iLight3D::CheckObjectIntersection(iRenderable *apObject) {
	// Log("------ Checking %s with light %s -----\n",apObject->GetName().c_str(), GetName().c_str());
	// Log(" BV: min: %s max: %s\n",	apObject->GetBoundingVolume()->GetMin().ToString().c_str(),
	//								apObject->GetBoundingVolume()->GetMax().ToString().c_str());

	//////////////////////////////////////////////////////////////
	// If the lights cast shadows, cull objects that are in shadow
	if (mbCastShadows) {
		/////////////////////////////////////
		// Check if the visibility needs update
		if (mlSectorVisibilityCount != GetMatrixUpdateCount()) {
			mlSectorVisibilityCount = GetMatrixUpdateCount();
			if (mpVisSectorCont)
				hplDelete(mpVisSectorCont);

			mpVisSectorCont = CreateSectorVisibility();
			// Log("Creating Visibility container!\n");
		}

		// Get the data list containing the sectors the object is connected to
		tRenderContainerDataList *pDataList = apObject->GetRenderContainerDataList();

		// It is not attached to any room. Just use BV test.
		if (pDataList->empty()) {
			// Log("Empty data list using BV\n");
			return CollidesWithBV(apObject->GetBoundingVolume());
		}
		// The object is in one or more sectors
		else {
			// Iterate the sectors and remove the object from them.
			tRenderContainerDataListIt it = pDataList->begin();
			for (; it != pDataList->end(); ++it) {
				cSector *pSector = static_cast<cSector *>(*it);

				// Log("Checking intersection in sector %s\n",pSector->GetId().c_str());

				cSectorVisibility *pVisSector = mpVisSectorCont->GetSectorVisibilty(pSector);
				if (pVisSector) {
					if (pVisSector->IntersectionBV(apObject->GetBoundingVolume())) {
						// Log("Intersected!\n");
						// Log("-----------------------");
						return true;
					}
				}
			}

			// Log("-----------------------");
			return false;
		}

	}
	/////////////////////////////////////////////////
	// Light is not in shadow, do not do any culling
	else {
		// Log("No shadow, using BV\n");
		return CollidesWithBV(apObject->GetBoundingVolume());
	}
}

//-----------------------------------------------------------------------

bool iLight3D::BeginDraw(cRenderSettings *apRenderSettings, iLowLevelGraphics *apLowLevelGraphics) {
	// Clear Stencil Buffer
	/*		apLowLevelGraphics->SetClearStencilActive(true);
			apLowLevelGraphics->SetClearDepthActive(false);
			apLowLevelGraphics->SetClearColorActive(false);

			apLowLevelGraphics->SetClearStencil(0);

			apLowLevelGraphics->ClearScreen();

			apLowLevelGraphics->SetClearStencilActive(false);
			apLowLevelGraphics->SetClearDepthActive(true);
			apLowLevelGraphics->SetClearColorActive(true);*/

	cRect2l ClipRect;
	bool bVisible = CreateClipRect(ClipRect, apRenderSettings, apLowLevelGraphics);

	if (bVisible) {
		apLowLevelGraphics->SetScissorActive(true);
		apLowLevelGraphics->SetScissorRect(ClipRect);

		if (apRenderSettings->mbLog)
			Log("Cliprect pos: (%d, %d) size: (%d, %d)\n", ClipRect.x, ClipRect.y, ClipRect.w, ClipRect.h);
	} else {
		if (apRenderSettings->mbLog)
			Log("Cliprect entire screen\n");
	}

	//////////////////////////////////////////////////////////
	// Cast shadows
	if (mbCastShadows && apRenderSettings->mShowShadows != eRendererShowShadows_None && apRenderSettings->extrudeProgram) {
		// Get temp index array. (Remove this when the index pool
		//  is implemented.).
		mpIndexArray = apRenderSettings->mpTempIndexArray;

		// Setup for shadow drawing
		apLowLevelGraphics->SetStencilActive(true);
		// Do no set this when debugging.
		apLowLevelGraphics->SetColorWriteActive(false, false, false, false);

		///////////////////////////////////////////////////////////////////////////
		// Clear stencil, since scissor is set this should be fast.
		apLowLevelGraphics->SetClearStencilActive(true);
		apLowLevelGraphics->SetClearDepthActive(false);
		apLowLevelGraphics->SetClearColorActive(false);

		apLowLevelGraphics->SetClearStencil(0);

		apLowLevelGraphics->ClearScreen();

		apLowLevelGraphics->SetClearStencilActive(false);
		apLowLevelGraphics->SetClearDepthActive(true);
		apLowLevelGraphics->SetClearColorActive(true);

		if (apLowLevelGraphics->GetCaps(eGraphicCaps_TwoSideStencil)) {
			apLowLevelGraphics->SetCullActive(false);
		}

		apLowLevelGraphics->SetDepthWriteActive(false);

		// Setup the depth test so that shadow volume is not rendered in front
		// off the normal graphics.
		apLowLevelGraphics->SetDepthTestFunc(eDepthTestFunc_Less);

		// Resert the algo (zfail or zpass) used.
		apRenderSettings->mlLastShadowAlgo = 0;

		// Reset this variable so it can be used when rendering shadows.
		apRenderSettings->mbMatrixWasNULL = false;

		// Set the fragment program.
		if (apRenderSettings->extrudeProgram) {
			if (apRenderSettings->mbLog)
				Log("Setting fragment program: '%s'\n",
					apRenderSettings->extrudeProgram->GetName().c_str());
			apRenderSettings->extrudeProgram->Bind();
			apRenderSettings->gpuProgram = apRenderSettings->extrudeProgram;
		}

		// Render shadows
		tCasterCacheSetIt it = m_setDynamicCasters.begin();

		if (apRenderSettings->mShowShadows == eRendererShowShadows_All) {
			it = m_setDynamicCasters.begin();
			for (; it != m_setDynamicCasters.end(); ++it) {
				RenderShadow(*it, apRenderSettings, apLowLevelGraphics);
			}
		}

		it = m_setStaticCasters.begin();
		for (; it != m_setStaticCasters.end(); ++it) {
			RenderShadow(*it, apRenderSettings, apLowLevelGraphics);
		}

		// Make rendering ready for the objects.
		// apLowLevelGraphics->SetStencilTwoSideActive(false);

		apLowLevelGraphics->SetDepthTestFunc(eDepthTestFunc_Equal);

		apLowLevelGraphics->SetColorWriteActive(true, true, true, true);
		apLowLevelGraphics->SetCullActive(true);

		apLowLevelGraphics->SetStencil(eStencilFunc_Equal, 0, 0xFF,
									   eStencilOp_Keep, eStencilOp_Keep, eStencilOp_Keep);
	}

	// Reset this var so that the new light properties are set.
	apRenderSettings->mbMatrixWasNULL = false;

	return true;
}

//-----------------------------------------------------------------------

void iLight3D::EndDraw(cRenderSettings *apRenderSettings, iLowLevelGraphics *apLowLevelGraphics) {
	apLowLevelGraphics->SetScissorActive(false);
	apLowLevelGraphics->SetStencilActive(false);
}

//-----------------------------------------------------------------------

void iLight3D::SetFarAttenuation(float afX) {
	mfFarAttenuation = afX;

	mbUpdateBoundingVolume = true;

	// This is so that the render container is updated.
	SetTransformUpdated();
}
//-----------------------------------------------------------------------

void iLight3D::SetNearAttenuation(float afX) {
	mfNearAttenuation = afX;
	if (mfNearAttenuation > mfFarAttenuation)
		SetFarAttenuation(mfNearAttenuation);
}
//-----------------------------------------------------------------------

cVector3f iLight3D::GetLightPosition() {
	return GetWorldPosition();
}

//-----------------------------------------------------------------------

void iLight3D::UpdateLogic(float afTimeStep) {
	UpdateLight(afTimeStep);
	if (mfFadeTime > 0 || mbFlickering) {
		mbUpdateBoundingVolume = true;

		// This is so that the render container is updated.
		SetTransformUpdated();
	}
}

//-----------------------------------------------------------------------

cBoundingVolume *iLight3D::GetBoundingVolume() {
	if (mbUpdateBoundingVolume) {
		UpdateBoundingVolume();
		mbUpdateBoundingVolume = false;
	}

	return &mBoundingVolume;
}

//-----------------------------------------------------------------------

cMatrixf *iLight3D::GetModelMatrix(cCamera3D *apCamera) {
	mtxTemp = GetWorldMatrix();
	return &mtxTemp;
}

//-----------------------------------------------------------------------

bool iLight3D::IsVisible() {
	if (mDiffuseColor.r <= 0 && mDiffuseColor.g <= 0 && mDiffuseColor.b <= 0 && mDiffuseColor.a <= 0)
		return false;
	if (mfFarAttenuation <= 0)
		return false;

	return IsRendered();
}

//-----------------------------------------------------------------------

iTexture *iLight3D::GetFalloffMap() {
	return mpFalloffMap;
}

void iLight3D::SetFalloffMap(iTexture *apTexture) {
	if (mpFalloffMap)
		mpTextureManager->Destroy(mpFalloffMap);

	if (apTexture) {
		mpFalloffMap = apTexture;
		mpFalloffMap->SetWrapS(eTextureWrap_ClampToEdge);
		mpFalloffMap->SetWrapT(eTextureWrap_ClampToEdge);
	} else {
		mpFalloffMap = nullptr;
	}

	Common::fill(mvTempTextures, mvTempTextures + 3, nullptr);
}

//-----------------------------------------------------------------------

void iLight3D::LoadXMLProperties(const tString asFile) {
	tString sPath = mpFileSearcher->GetFilePath(asFile);
	if (sPath != "") {
		TiXmlDocument *pDoc = hplNew(TiXmlDocument, (sPath.c_str()));
		if (pDoc->LoadFile()) {
			TiXmlElement *pRootElem = pDoc->RootElement();

			TiXmlElement *pMainElem = pRootElem->FirstChildElement("MAIN");
			if (pMainElem != NULL) {
				mbCastShadows = cString::ToBool(pMainElem->Attribute("CastsShadows"), mbCastShadows);

				mDiffuseColor.a = cString::ToFloat(pMainElem->Attribute("Specular"), mDiffuseColor.a);

				tString sFalloffImage = cString::ToString(pMainElem->Attribute("FalloffImage"), "");
				iTexture *pTexture = mpTextureManager->Create1D(sFalloffImage, false);
				if (pTexture)
					SetFalloffMap(pTexture);

				ExtraXMLProperties(pMainElem);
			} else {
				Error("Cannot find main element in %s\n", asFile.c_str());
			}
		} else {
			Error("Couldn't load file '%s'\n", asFile.c_str());
		}
		hplDelete(pDoc);
	} else {
		Error("Couldn't find file '%s'\n", asFile.c_str());
	}
}

//-----------------------------------------------------------------------

void iLight3D::AttachBillboard(cBillboard *apBillboard) {
	mvBillboards.push_back(apBillboard);
	apBillboard->SetColor(cColor(mDiffuseColor.r, mDiffuseColor.g, mDiffuseColor.b, 1));
	apBillboard->SetVisible(IsVisible());
}

void iLight3D::RemoveBillboard(cBillboard *apBillboard) {
	Common::Array<cBillboard *>::iterator it = mvBillboards.begin();
	for (; it != mvBillboards.end(); ++it) {
		if (*it == apBillboard) {
			mvBillboards.erase(it);
		}
	}
}

//-----------------------------------------------------------------------

//-----------------------------------------------------------------------

//////////////////////////////////////////////////////////////////////////
// PROTECTED METHODS
//////////////////////////////////////////////////////////////////////////

//-----------------------------------------------------------------------

void iLight3D::RenderShadow(iRenderable *apObject, cRenderSettings *apRenderSettings,
							iLowLevelGraphics *apLowLevelGraphics) {
	int lIndexCount = 0;

	////////////////////////////////////////////////////////////////////////
	// Check if the shadow volume collides with the frustum
	cShadowVolumeBV *pVolume = apObject->GetBoundingVolume()->GetShadowVolume(
		GetWorldPosition(), mfFarAttenuation, true);
	cFrustum *pFrustum = apRenderSettings->mpFrustum;
	if (pVolume && pFrustum) {
		if (pFrustum->CheckVolumeIntersection(pVolume) == false) {
			// This can be skipped if the AABB shadow volume is better.
			/// that is includes the AABB.
			if (pFrustum->CollideBoundingVolume(apObject->GetBoundingVolume()) == eFrustumCollision_Outside) {
				return;
			}
		}
	}

	if (apRenderSettings->mbLog)
		Log("Rendering shadow for '%s'\n", apObject->GetName().c_str());

	cSubMeshEntity *pSubEntity = static_cast<cSubMeshEntity *>(apObject);
	cSubMesh *pSubMesh = pSubEntity->GetSubMesh();

	//////////////////////////////////////////
	// Check what method to use.
	bool bZFail = false;

	if (pVolume && pFrustum) {
		cBoundingVolume *pFrustumBV = pFrustum->GetOriginBV();
		if (cMath::CheckSphereInPlanes(pFrustumBV->GetWorldCenter(), pFrustumBV->GetRadius(),
									   pVolume->mvPlanes, pVolume->mlPlaneCount)) {
			bZFail = true;
		}
		// This is because the AABB-shadow volume is not perfect
		else if (cMath::CheckCollisionBV(*pFrustumBV, *apObject->GetBoundingVolume())) {
			bZFail = true;
		}
	} else {
		bZFail = true;
	}
	if (apRenderSettings->mbLog)
		Log("Rendering shadow with '%s'\n", bZFail ? "ZFail" : "ZPass");

	//////////////////////////////////////////
	// Setup the stencil buffer.
	// If two sided stencil is not supported, do the set up later on.
	if (apLowLevelGraphics->GetCaps(eGraphicCaps_TwoSideStencil)) {
		if (bZFail) {
			if (apRenderSettings->mlLastShadowAlgo != 1) {
				apLowLevelGraphics->SetStencilTwoSide(eStencilFunc_Always, eStencilFunc_Always, 0, 0x00,
													  eStencilOp_Keep, eStencilOp_DecrementWrap, eStencilOp_Keep,
													  eStencilOp_Keep, eStencilOp_IncrementWrap, eStencilOp_Keep);

				apRenderSettings->mlLastShadowAlgo = 1;
			}
		} else {
			if (apRenderSettings->mlLastShadowAlgo != 2) {
				apLowLevelGraphics->SetStencilTwoSide(eStencilFunc_Always, eStencilFunc_Always, 0, 0x00,
													  eStencilOp_Keep, eStencilOp_Keep, eStencilOp_IncrementWrap,
													  eStencilOp_Keep, eStencilOp_Keep, eStencilOp_DecrementWrap);

				apRenderSettings->mlLastShadowAlgo = 2;
			}
		}
	}

	//////////////////////////////////////////
	// Check if the cache as data.
	/* TO BE IMPLEMENTED*/

	///////////////////////////////////////////
	// Get local light position
	cVector3f vLocalLight = GetWorldPosition();
	cMatrixf *pInvModelMtx = apObject->GetInvModelMatrix();
	if (pInvModelMtx) {
		vLocalLight = cMath::MatrixMul(*pInvModelMtx, vLocalLight);
	}

	/////////////////////////////////////////////
	// Set the model matrix
	cMatrixf *pModelMtx = apObject->GetModelMatrix(NULL);
	if (pModelMtx) {
		apLowLevelGraphics->SetMatrix(eMatrix_ModelView, cMath::MatrixMul(
															 apRenderSettings->mpCamera->GetViewMatrix(),
															 *pModelMtx));
	} else if (apRenderSettings->mbMatrixWasNULL == false) {
		apLowLevelGraphics->SetMatrix(eMatrix_ModelView, apRenderSettings->mpCamera->GetViewMatrix());
	}

	/////////////////////////////////////////////////////////
	// Get the data arrays
	const float *pPosArray = pSubEntity->GetVertexBuffer()->GetArray(eVertexFlag_Position);
	unsigned int *pIdxArray = pSubEntity->GetVertexBuffer()->GetIndices();

	int lVtxStride = kvVertexElements[cMath::Log2ToInt(eVertexFlag_Position)];

	const bool bDoubleSided = pSubMesh->GetDoubleSided();

	/////////////////////////////////////////////////////////
	// Iterate faces and check which are facing the light.
	cTriangleData *pTriangles = pSubEntity->GetTriangleVecPtr()->data();
	const int lTriNum = pSubEntity->GetTriangleNum();
	for (int tri = 0, idx = 0; tri < lTriNum; tri++, idx += 3) {
		cTriangleData &Tri = pTriangles[tri];

		const float *pPoint = &pPosArray[pIdxArray[idx] * lVtxStride];

		const cVector3f &vNormal = Tri.normal;

		// Use Dot product to check
		Tri.facingLight = ((pPoint[0] - vLocalLight.x) * vNormal.x +
						   (pPoint[1] - vLocalLight.y) * vNormal.y +
						   (pPoint[2] - vLocalLight.z) * vNormal.z) < 0;
	}

	/////////////////////////////////////////////////////////
	// Iterate edges and find possible silhouette
	// Get edge pointer, index pointer and offset
	unsigned int *pCurrentIndexPos = &mpIndexArray[0];
	const cTriEdge *pEdges = pSubMesh->GetEdgeVecPtr()->data();
	int lOffset = pSubEntity->GetVertexBuffer()->GetVertexNum();
	// Iterate
	const int lEdgeNum = pSubMesh->GetEdgeNum();
	for (int edge = 0; edge < lEdgeNum; edge++) {
		const cTriEdge &Edge = pEdges[edge];

		const cTriangleData *pTri1 = &pTriangles[Edge.tri1];
		const cTriangleData *pTri2 = nullptr;
		if (Edge.invert_tri2 == false)
			pTri2 = &pTriangles[Edge.tri2];

		// Check if this edge has one triangle facing and one not facing the light.
		// If the triangel is onesided (invert_tri2) then it is always a silhouette
		if ((Edge.invert_tri2) ||
			(pTri1->facingLight && !pTri2->facingLight) ||
			(pTri2->facingLight && !pTri1->facingLight)) {
			if (pTri1->facingLight) {
				*(pCurrentIndexPos++) = Edge.point1;
				*(pCurrentIndexPos++) = Edge.point2;
				*(pCurrentIndexPos++) = Edge.point2 + lOffset;
				*(pCurrentIndexPos++) = Edge.point1;
				*(pCurrentIndexPos++) = Edge.point2 + lOffset;
				*(pCurrentIndexPos++) = Edge.point1 + lOffset;
				lIndexCount += 6;
			}
			// Do not draw if the edge only has one face and it is not facing the light.
			else if (!Edge.invert_tri2) {
				*(pCurrentIndexPos++) = Edge.point2;
				*(pCurrentIndexPos++) = Edge.point1;
				*(pCurrentIndexPos++) = Edge.point1 + lOffset;
				*(pCurrentIndexPos++) = Edge.point2;
				*(pCurrentIndexPos++) = Edge.point1 + lOffset;
				*(pCurrentIndexPos++) = Edge.point2 + lOffset;
				lIndexCount += 6;
			}

			// DEBUG:
			/*if(!(bDoubleSided && Edge.invert_tri2==false))
			{
				//apRenderSettings->mpVtxExtrudeProgram->UnBind();
				apLowLevelGraphics->SetDepthTestActive(false);
				apLowLevelGraphics->SetStencilActive(false);
				apLowLevelGraphics->DrawLine(
					pSubMesh->GetVertexBuffer()->GetVector3(eVertexFlag_Position,Edge.point1),
					pSubMesh->GetVertexBuffer()->GetVector3(eVertexFlag_Position,Edge.point2),
					cColor(1,0.9f,0,1));
				apLowLevelGraphics->SetStencilActive(true);
				apLowLevelGraphics->SetDepthTestActive(true);
			}*/
		}
	}

	///////////////////////////////////////////////////////
	// If Z fail is used, generate front and back cap
	if (bZFail) {
		for (int tri = 0, idx = 0; tri < lTriNum; tri++, idx += 3) {
			cTriangleData &Data = pSubEntity->GetTriangle(tri);

			// Front cap
			if (Data.facingLight) {
				memcpy(pCurrentIndexPos, &pIdxArray[idx], 3 * sizeof(unsigned int));
				pCurrentIndexPos += 3;
				lIndexCount += 3;
				/*mpIndexArray[lIndexCount+0] = pIdxArray[idx+0];
				mpIndexArray[lIndexCount+1] = pIdxArray[idx+1];
				mpIndexArray[lIndexCount+2] = pIdxArray[idx+2];*/

				if (bDoubleSided) {
					mpIndexArray[lIndexCount + 0] = pIdxArray[idx + 2] + lOffset;
					mpIndexArray[lIndexCount + 1] = pIdxArray[idx + 1] + lOffset;
					mpIndexArray[lIndexCount + 2] = pIdxArray[idx + 0] + lOffset;
					pCurrentIndexPos += 3;
					lIndexCount += 3;
				}
			}
			// Back Cap
			// If double sided, the sides facing the light supply their own
			else if (!bDoubleSided) {
				mpIndexArray[lIndexCount + 0] = pIdxArray[idx + 0] + lOffset;
				mpIndexArray[lIndexCount + 1] = pIdxArray[idx + 1] + lOffset;
				mpIndexArray[lIndexCount + 2] = pIdxArray[idx + 2] + lOffset;
				pCurrentIndexPos += 3;
				lIndexCount += 3;
			}
		}
	}

	///////////////////////////////////////////////////////
	// Draw the volume:

	// Set light position and model view matrix, this does not have to be set if last
	// object was static.
	if (pModelMtx || apRenderSettings->mbMatrixWasNULL == false) {
		apRenderSettings->extrudeProgram->SetVec3f("lightPosition", vLocalLight);
		apRenderSettings->extrudeProgram->SetMatrixf("worldViewProj",
													 eGpuProgramMatrix_ViewProjection,
													 eGpuProgramMatrixOp_Identity);

		// If a null matrix has been set, let other passes know.
		if (pModelMtx)
			apRenderSettings->mbMatrixWasNULL = false;
		else
			apRenderSettings->mbMatrixWasNULL = true;
	}

	// Set vertex buffer
	if (apRenderSettings->mpVtxBuffer != pSubEntity->GetVertexBuffer()) {
		if (apRenderSettings->mbLog)
			Log(" Setting vertex buffer %d\n", (size_t)pSubEntity->GetVertexBuffer());

		pSubEntity->GetVertexBuffer()->Bind();
		apRenderSettings->mpVtxBuffer = pSubEntity->GetVertexBuffer();
	}

	// Draw vertex buffer
	if (apLowLevelGraphics->GetCaps(eGraphicCaps_TwoSideStencil)) {
		pSubEntity->GetVertexBuffer()->DrawIndices(mpIndexArray, lIndexCount);
		if (apRenderSettings->mbLog)
			Log(" Drawing front and back simultaneously.\n");
	} else {
		if (apRenderSettings->mbLog)
			Log(" Drawing front and back separately.\n");

		if (bZFail) {
			// Front
			apLowLevelGraphics->SetStencil(eStencilFunc_Always, 0, 0x0,
										   eStencilOp_Keep, eStencilOp_DecrementWrap, eStencilOp_Keep);
			pSubEntity->GetVertexBuffer()->DrawIndices(mpIndexArray, lIndexCount);

			// Back
			apLowLevelGraphics->SetCullMode(eCullMode_Clockwise);
			apLowLevelGraphics->SetStencil(eStencilFunc_Always, 0, 0x0,
										   eStencilOp_Keep, eStencilOp_IncrementWrap, eStencilOp_Keep);
			pSubEntity->GetVertexBuffer()->DrawIndices(mpIndexArray, lIndexCount);
		} else {
			// Front
			apLowLevelGraphics->SetStencil(eStencilFunc_Always, 0, 0x0,
										   eStencilOp_Keep, eStencilOp_Keep, eStencilOp_IncrementWrap);
			pSubEntity->GetVertexBuffer()->DrawIndices(mpIndexArray, lIndexCount);

			// Back
			apLowLevelGraphics->SetCullMode(eCullMode_Clockwise);
			apLowLevelGraphics->SetStencil(eStencilFunc_Always, 0, 0x0,
										   eStencilOp_Keep, eStencilOp_Keep, eStencilOp_DecrementWrap);
			pSubEntity->GetVertexBuffer()->DrawIndices(mpIndexArray, lIndexCount);
		}

		apLowLevelGraphics->SetCullMode(eCullMode_CounterClockwise);
	}

	if (apLowLevelGraphics->GetCaps(eGraphicCaps_TwoSideStencil)) {
		apLowLevelGraphics->SetStencilTwoSide(false);
		apRenderSettings->mlLastShadowAlgo = 0;
	}
}

//-----------------------------------------------------------------------

void iLight3D::OnFlickerOff() {
	// Particle system
	if (msFlickerOffPS != "") {
		/*cParticleSystem3D *pPS = */ mpWorld3D->CreateParticleSystem(GetName() + "_PS",
																	  msFlickerOffPS, cVector3f(1, 1, 1), GetWorldMatrix());
	}
}

//-----------------------------------------------------------------------

void iLight3D::OnFlickerOn() {
	// Particle system
	if (msFlickerOnPS != "") {
		/*cParticleSystem3D *pPS = */ mpWorld3D->CreateParticleSystem(GetName() + "_PS",
																	  msFlickerOnPS, cVector3f(1, 1, 1), GetWorldMatrix());
	}
}

//-----------------------------------------------------------------------

void iLight3D::OnSetDiffuse() {
	for (size_t i = 0; i < mvBillboards.size(); ++i) {
		cBillboard *pBill = mvBillboards[i];
		pBill->SetColor(cColor(mDiffuseColor.r, mDiffuseColor.g, mDiffuseColor.b, 1));
	}
}

//////////////////////////////////////////////////////////////////////////
// SAVE OBJECT STUFF
//////////////////////////////////////////////////////////////////////////

//-----------------------------------------------------------------------

kBeginSerializeVirtual(cSaveData_iLight3D, cSaveData_iRenderable)
	kSerializeVar(msFalloffMap, eSerializeType_String)
		kSerializeVarContainer(mlstBillboardIds, eSerializeType_Int32)

			kSerializeVar(mDiffuseColor, eSerializeType_Color)
				kSerializeVar(mSpecularColor, eSerializeType_Color)
					kSerializeVar(mfIntensity, eSerializeType_Float32)
						kSerializeVar(mfFarAttenuation, eSerializeType_Float32)
							kSerializeVar(mfNearAttenuation, eSerializeType_Float32)
								kSerializeVar(mfSourceRadius, eSerializeType_Float32)

									kSerializeVar(mbCastShadows, eSerializeType_Bool)
										kSerializeVar(mbAffectMaterial, eSerializeType_Bool)

											kSerializeVar(mColAdd, eSerializeType_Color)
												kSerializeVar(mfRadiusAdd, eSerializeType_Float32)
													kSerializeVar(mDestCol, eSerializeType_Color)
														kSerializeVar(mfDestRadius, eSerializeType_Float32)
															kSerializeVar(mfFadeTime, eSerializeType_Float32)

																kSerializeVar(mbFlickering, eSerializeType_Bool)
																	kSerializeVar(msFlickerOffSound, eSerializeType_String)
																		kSerializeVar(msFlickerOnSound, eSerializeType_String)
																			kSerializeVar(msFlickerOffPS, eSerializeType_String)
																				kSerializeVar(msFlickerOnPS, eSerializeType_String)
																					kSerializeVar(mfFlickerOnMinLength, eSerializeType_Float32)
																						kSerializeVar(mfFlickerOffMinLength, eSerializeType_Float32)
																							kSerializeVar(mfFlickerOnMaxLength, eSerializeType_Float32)
																								kSerializeVar(mfFlickerOffMaxLength, eSerializeType_Float32)
																									kSerializeVar(mFlickerOffColor, eSerializeType_Color)
																										kSerializeVar(mfFlickerOffRadius, eSerializeType_Float32)
																											kSerializeVar(mbFlickerFade, eSerializeType_Bool)
																												kSerializeVar(mfFlickerOnFadeLength, eSerializeType_Float32)
																													kSerializeVar(mfFlickerOffFadeLength, eSerializeType_Float32)

																														kSerializeVar(mFlickerOnColor, eSerializeType_Color)
																															kSerializeVar(mfFlickerOnRadius, eSerializeType_Float32)

																																kSerializeVar(mbFlickerOn, eSerializeType_Bool)
																																	kSerializeVar(mfFlickerTime, eSerializeType_Float32)
																																		kSerializeVar(mfFlickerStateLength, eSerializeType_Float32)
																																			kEndSerialize()

	//-----------------------------------------------------------------------

	iSaveData *iLight3D::CreateSaveData() {
	return NULL;
}

//-----------------------------------------------------------------------

void iLight3D::SaveToSaveData(iSaveData *apSaveData) {
	kSaveData_SaveToBegin(iLight3D);

	//////////////////////////
	// Data
	pData->msFalloffMap = mpFalloffMap == NULL ? "" : mpFalloffMap->GetName();

	pData->mlstBillboardIds.Clear();
	for (size_t i = 0; i < mvBillboards.size(); ++i) {
		pData->mlstBillboardIds.Add(mvBillboards[i]->GetSaveObjectId());
	}

	//////////////////////////
	// Variables
	kSaveData_SaveTo(mDiffuseColor);
	kSaveData_SaveTo(mSpecularColor);
	kSaveData_SaveTo(mfIntensity);
	kSaveData_SaveTo(mfFarAttenuation);
	kSaveData_SaveTo(mfNearAttenuation);
	kSaveData_SaveTo(mfSourceRadius);

	kSaveData_SaveTo(mbCastShadows);
	kSaveData_SaveTo(mbAffectMaterial);

	kSaveData_SaveTo(mColAdd);
	kSaveData_SaveTo(mfRadiusAdd);
	kSaveData_SaveTo(mDestCol);
	kSaveData_SaveTo(mfDestRadius);
	kSaveData_SaveTo(mfFadeTime);

	kSaveData_SaveTo(mbFlickering);
	kSaveData_SaveTo(msFlickerOffSound);
	kSaveData_SaveTo(msFlickerOnSound);
	kSaveData_SaveTo(msFlickerOffPS);
	kSaveData_SaveTo(msFlickerOnPS);
	kSaveData_SaveTo(mfFlickerOnMinLength);
	kSaveData_SaveTo(mfFlickerOffMinLength);
	kSaveData_SaveTo(mfFlickerOnMaxLength);
	kSaveData_SaveTo(mfFlickerOffMaxLength);
	kSaveData_SaveTo(mFlickerOffColor);
	kSaveData_SaveTo(mfFlickerOffRadius);
	kSaveData_SaveTo(mbFlickerFade);
	kSaveData_SaveTo(mfFlickerOnFadeLength);
	kSaveData_SaveTo(mfFlickerOffFadeLength);

	kSaveData_SaveTo(mFlickerOnColor);
	kSaveData_SaveTo(mfFlickerOnRadius);

	kSaveData_SaveTo(mbFlickerOn);
	kSaveData_SaveTo(mfFlickerTime);
	kSaveData_SaveTo(mfFlickerStateLength);
}

//-----------------------------------------------------------------------

void iLight3D::LoadFromSaveData(iSaveData *apSaveData) {
	kSaveData_LoadFromBegin(iLight3D);

	//////////////////////////
	// Data
	if (pData->msFalloffMap != "") {
		iTexture *pTex = mpTextureManager->Create1D(pData->msFalloffMap, false);
		if (pTex)
			SetFalloffMap(pTex);
	}

	//////////////////////////
	// Variables
	kSaveData_LoadFrom(mDiffuseColor);
	kSaveData_LoadFrom(mSpecularColor);
	kSaveData_LoadFrom(mfIntensity);
	kSaveData_LoadFrom(mfFarAttenuation);
	kSaveData_LoadFrom(mfNearAttenuation);
	kSaveData_LoadFrom(mfSourceRadius);

	kSaveData_LoadFrom(mbCastShadows);
	kSaveData_LoadFrom(mbAffectMaterial);

	kSaveData_LoadFrom(mColAdd);
	kSaveData_LoadFrom(mfRadiusAdd);
	kSaveData_LoadFrom(mDestCol);
	kSaveData_LoadFrom(mfDestRadius);
	kSaveData_LoadFrom(mfFadeTime);

	kSaveData_LoadFrom(mbFlickering);
	kSaveData_LoadFrom(msFlickerOffSound);
	kSaveData_LoadFrom(msFlickerOnSound);
	kSaveData_LoadFrom(msFlickerOffPS);
	kSaveData_LoadFrom(msFlickerOnPS);
	kSaveData_LoadFrom(mfFlickerOnMinLength);
	kSaveData_LoadFrom(mfFlickerOffMinLength);
	kSaveData_LoadFrom(mfFlickerOnMaxLength);
	kSaveData_LoadFrom(mfFlickerOffMaxLength);
	kSaveData_LoadFrom(mFlickerOffColor);
	kSaveData_LoadFrom(mfFlickerOffRadius);
	kSaveData_LoadFrom(mbFlickerFade);
	kSaveData_LoadFrom(mfFlickerOnFadeLength);
	kSaveData_LoadFrom(mfFlickerOffFadeLength);

	kSaveData_LoadFrom(mFlickerOnColor);
	kSaveData_LoadFrom(mfFlickerOnRadius);

	kSaveData_LoadFrom(mbFlickerOn);
	kSaveData_LoadFrom(mfFlickerTime);
	kSaveData_LoadFrom(mfFlickerStateLength);
}

//-----------------------------------------------------------------------

void iLight3D::SaveDataSetup(cSaveObjectHandler *apSaveObjectHandler, cGame *apGame) {
	kSaveData_SetupBegin(iLight3D);

	// Get attached billboards.
	cContainerListIterator<int> it = pData->mlstBillboardIds.GetIterator();
	while (it.HasNext()) {
		int lId = it.Next();
		iSaveObject *pObject = apSaveObjectHandler->Get(lId);
		cBillboard *pBill = static_cast<cBillboard *>(pObject);

		if (pBill == NULL) {
			Error("Couldn't find billboard id %s\n", lId);
			continue;
		}

		AttachBillboard(pBill);
	}
}

//-----------------------------------------------------------------------

} // namespace hpl