File: MeshEntity.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 (1560 lines) | stat: -rw-r--r-- 47,329 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
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
/* 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/MeshEntity.h"

#include "hpl1/engine/graphics/Material.h"
#include "hpl1/engine/graphics/Mesh.h"
#include "hpl1/engine/graphics/RenderList.h"
#include "hpl1/engine/graphics/SubMesh.h"
#include "hpl1/engine/graphics/VertexBuffer.h"
#include "hpl1/engine/resources/AnimationManager.h"
#include "hpl1/engine/resources/MaterialManager.h"
#include "hpl1/engine/resources/MeshManager.h"
#include "hpl1/engine/resources/Resources.h"

#include "hpl1/engine/resources/FileSearcher.h"
#include "hpl1/engine/resources/MeshLoaderHandler.h"

#include "hpl1/engine/graphics/Animation.h"
#include "hpl1/engine/graphics/AnimationTrack.h"
#include "hpl1/engine/graphics/Bone.h"
#include "hpl1/engine/graphics/BoneState.h"
#include "hpl1/engine/graphics/Skeleton.h"

#include "hpl1/engine/scene/AnimationState.h"
#include "hpl1/engine/scene/NodeState.h"
#include "hpl1/engine/scene/Scene.h"
#include "hpl1/engine/scene/SoundEntity.h"
#include "hpl1/engine/scene/World3D.h"

#include "hpl1/engine/physics/PhysicsBody.h"
#include "hpl1/engine/physics/PhysicsWorld.h"

#include "hpl1/engine/math/Math.h"

#include "hpl1/engine/game/Game.h"

namespace hpl {

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

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

cMeshEntity::cMeshEntity(const tString asName, cMesh *apMesh, cMaterialManager *apMaterialManager,
						 cMeshManager *apMeshManager, cAnimationManager *apAnimationManager) : iRenderable(asName) {
	mpMaterialManager = apMaterialManager;
	mpMeshManager = apMeshManager;
	mpAnimationManager = apAnimationManager;

	mpWorld = NULL;

	mpCallback = NULL;

	mbCastShadows = false;

	mpMesh = apMesh;

	mpRootNode = NULL;
	mpRootCallback = NULL;

	mpBody = NULL;

	mbSkeletonPhysics = false;
	mfSkeletonPhysicsWeight = 1.0f;
	mbSkeletonPhysicsFading = false;
	mfSkeletonPhysicsFadeSpeed = 1.0f;

	mbSkeletonPhysicsSleeping = false;
	mbSkeletonPhysicsCanSleep = true;

	mbSkeletonColliders = false;

	mbUpdatedBones = false;

	mlStartSleepCount = 0;
	mlUpdateCount = 0;
	mfTimeStepAccum = 0;

	////////////////////////////////////////////////
	// Create sub entities
	for (int i = 0; i < mpMesh->GetSubMeshNum(); i++) {
		cSubMesh *pSubMesh = mpMesh->GetSubMesh(i);
		cSubMeshEntity *pSub = hplNew(cSubMeshEntity, (pSubMesh->GetName(), this, pSubMesh, mpMaterialManager));

		// Log("Creating sub entity %s\n",pSub->GetName().c_str());

		mvSubMeshes.push_back(pSub);
		m_mapSubMeshes.insert(tSubMeshEntityMap::value_type(mpMesh->GetSubMesh(i)->GetName(), pSub));

		iVertexBuffer *pVtxBuffer = mpMesh->GetSubMesh(i)->GetVertexBuffer();

		if (mpMesh->GetNodeNum() <= 0) {
			mBoundingVolume.AddArrayPoints(pVtxBuffer->GetArray(eVertexFlag_Position),
										   pVtxBuffer->GetVertexNum());
		} else {
			pSub->mBoundingVolume.AddArrayPoints(pVtxBuffer->GetArray(eVertexFlag_Position),
												 pVtxBuffer->GetVertexNum());
			pSub->mBoundingVolume.CreateFromPoints(kvVertexElements[cMath::Log2ToInt(eVertexFlag_Position)]);
		}
	}

	if (mpMesh->GetNodeNum() <= 0) {
		mBoundingVolume.CreateFromPoints(kvVertexElements[cMath::Log2ToInt(eVertexFlag_Position)]);

		// Log("CREATED BV Min: %s Max: %s\n",	mBoundingVolume.GetMin().ToString().c_str(),
		//									mBoundingVolume.GetMax().ToString().c_str());
	}

	////////////////////////////////////////////////
	// Create animation states
	mvAnimationStates.reserve(mpMesh->GetAnimationNum());
	for (int i = 0; i < mpMesh->GetAnimationNum(); i++) {
		cAnimation *pAnimation = mpMesh->GetAnimation(i);

		cAnimationState *pAnimState = hplNew(cAnimationState, (pAnimation, pAnimation->GetName(), NULL));

		mvAnimationStates.push_back(pAnimState);

		tAnimationStateIndexMap::value_type value(pAnimState->GetName(), (int)mvAnimationStates.size() - 1);
		m_mapAnimationStateIndices.insert(value);
	}

	////////////////////////////////////////////////
	// Create Nodes
	mbHasNodes = false;
	if (mpMesh->GetNodeNum() > 0 &&
		(mpMesh->GetPhysicsJointNum() <= 0 || mpMesh->GetSkeleton() == NULL) &&
		mpMesh->GetAnimationNum() > 0) {
		mbHasNodes = true;
		mvNodeStates.reserve(mpMesh->GetNodeNum());

		// Create the root node and attach all node without parents to this.
		mpRootNode = hplNew(cNode3D, ("NodeRoot", false));

		// Create the root callback
		mpRootCallback = hplNew(cMeshEntityRootNodeUpdate, ());
		this->AddCallback(mpRootCallback);

		// Fill the node array.
		for (int i = 0; i < mpMesh->GetNodeNum(); i++) {
			cNode3D *pMeshNode = mpMesh->GetNode(i);

			cBoneState *pNode = hplNew(cBoneState, (pMeshNode->GetName(), false));
			pNode->SetMatrix(pMeshNode->GetLocalMatrix());

			/*Log("Node: %s has local translate: %s world translate: %s\n",
					pNode->GetName(),
					pMeshNode->GetLocalMatrix().GetTranslation().ToString().c_str(),
					pMeshNode->GetWorldMatrix().GetTranslation().ToString().c_str());*/

			// Add node to array and add it's index to the map.
			mvNodeStates.push_back(pNode);
			m_mapNodeStateIndices.insert(tNodeStateIndexMap::value_type(pNode->GetName(), i));

			// Connect with sub mesh entity
			cSubMeshEntity *pSubEntity = GetSubMeshEntityName(pMeshNode->GetSource());
			if (pSubEntity) {
				pSubEntity->SetLocalNode(pNode);
			}
		}

		// Set parents and children of the nodes in the array
		for (int i = 0; i < (int)mvNodeStates.size(); i++) {
			cNode3D *pStateNode = mvNodeStates[i];
			cNode3D *pMeshNode = mpMesh->GetNode(i);

			// Set the parent if there is one
			if (pMeshNode->GetParent()) {
				cNode3D *pParentNode = GetNodeStateFromName(pMeshNode->GetParent()->GetName());
				if (pParentNode)
					pStateNode->SetParent(pParentNode);
				else
					pStateNode->SetParent(mpRootNode);
			}
			// If not set root node as parent.
			else {
				pStateNode->SetParent(mpRootNode);
			}

			// Add children if there are any.
			cNodeIterator it = pMeshNode->GetChildIterator();
			while (it.HasNext()) {
				cNode3D *pChildNode = static_cast<cNode3D *>(it.Next());
				pStateNode->AddChild(GetNodeStateFromName(pChildNode->GetName()));
			}
		}

		// make sure all nodes are updated.
		mpRootNode->SetMatrix(cMatrixf::Identity);

		UpdateBVFromSubs();
	}

	////////////////////////////////////////////////
	// Create Joint Nodes
	// These are created to support joints.
	else if (mpMesh->GetPhysicsJointNum() > 0 || mpMesh->HasSeveralBodies()) {
		mbHasNodes = true;

		// Fill the node array.
		for (int i = 0; i < mpMesh->GetNodeNum(); i++) {
			cNode3D *pMeshNode = mpMesh->GetNode(i);

			// Log("MeshNode %s\n local: (%s)\n world: (%s)\n",pMeshNode->GetName(),
			//									pMeshNode->GetLocalPosition().ToString().c_str(),
			//									pMeshNode->GetWorldPosition().ToString().c_str());

			// Set the sub entity with the mesh node world matrix
			cSubMeshEntity *pSubEntity = GetSubMeshEntityName(pMeshNode->GetSource());
			if (pSubEntity) {
				/*cSubMesh *pSubMesh = */ pSubEntity->GetSubMesh();
				cMatrixf mtxSub = pMeshNode->GetWorldMatrix();

				pSubEntity->SetMatrix(mtxSub);

				// Log(" SubEnity: %s\n",pSubEntity->GetWorldPosition().ToString().c_str());
			}
		}

		UpdateBVFromSubs();
	}

	////////////////////////////////////////////////
	// Create Skeleton

	// Create the node states from skeleton
	cSkeleton *pSkeleton = mpMesh->GetSkeleton();

	// Create bones states if there is a skeleton.
	if (pSkeleton) {
		mbApplyTransformToBV = false;
		mbHasNodes = false;

		if (mpRootNode == NULL) {
			// Create the root node and attach all node without parents to this.
			mpRootNode = hplNew(cNode3D, ("NodeRoot", false));

			// Create the root callback
			mpRootCallback = hplNew(cMeshEntityRootNodeUpdate, ());
			this->AddCallback(mpRootCallback);
		}

		mvBoneStates.reserve(pSkeleton->GetBoneNum());

		// Fill the state array with the bones so
		// that each state has the same index as the bones.
		for (int i = 0; i < pSkeleton->GetBoneNum(); i++) {
			cBone *pBone = pSkeleton->GetBoneByIndex(i);

			cBoneState *pState = hplNew(cBoneState, (pBone->GetName(), false));
			pState->SetMatrix(pBone->GetLocalTransform());

			// Log("Created bone state: '%s'\n",pState->GetName());

			// Add bone to array and add it's index to the map.
			mvBoneStates.push_back(pState);
			m_mapBoneStateIndices.insert(tBoneIdxNameMap::value_type(pState->GetName(), i));
		}

		// Set parents and children of the nodes in the array
		for (int i = 0; i < (int)mvBoneStates.size(); i++) {
			cNode3D *pState = mvBoneStates[i];
			cBone *pBone = pSkeleton->GetBoneByIndex(i);

			// Log("State: %s\n",pState->GetName());

			// Set the parent if there is one
			// TODO: Perhaps this should be removed.
			if (pBone->GetParent()) {
				cNode3D *pParentState = GetBoneStateFromName(pBone->GetParent()->GetName());
				if (pParentState)
					pState->SetParent(pParentState);
				else
					pState->SetParent(mpRootNode);
				// else
				//	Error("Couldn't find parent bone state '%s' in bone\n",pBone->GetParent()->GetName().c_str(),
				//														pBone->GetName().c_str());
				// if(pParentState)Log("  Parent: %s\n",pParentState->GetName());
			} else {
				pState->SetParent(mpRootNode);
			}

			// Add children if there are any.
			/*cBoneIterator it = pBone->GetChildIterator();
			while(it.HasNext())
			{
				cBone* pChildBone = it.Next();
				cNode3D *pChildState = GetBoneStateFromName(pChildBone->GetName());
				if(pChildState)
					pState->AddChild(pChildState);
				//else
				//	Error("Couldn't find child bone state '%s'",pChildBone->GetName());
				//if(pChildState)Log("  Child: %s\n",pChildState->GetName());
			}*/
		}

		// Create an array to fill with bone matrices
		mvBoneMatrices.resize(pSkeleton->GetBoneNum());

		// Reset all bones states
		for (size_t i = 0; i < mvBoneStates.size(); i++) {
			cNode3D *pState = mvBoneStates[i];
			cBone *pBone = mpMesh->GetSkeleton()->GetBoneByIndex((int)i);

			pState->SetMatrix(pBone->GetLocalTransform());
		}

		// Create temp bone nodes
		mvTempBoneStates.resize(mvBoneStates.size());
		for (size_t i = 0; i < mvTempBoneStates.size(); i++) {
			mvTempBoneStates[i] = hplNew(cBoneState, (mvBoneStates[i]->GetName(), false));
		}
	}

	////////////////////////////////////////////////////
	// If it has no nodes, attach the sub entities to the
	// main mesh
	if (mbHasNodes == false) {
		for (size_t i = 0; i < mvSubMeshes.size(); ++i) {
			AddChild(mvSubMeshes[i]);
		}
	}
}

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

cMeshEntity::~cMeshEntity() {
	for (tEntity3DListIt it = mlstAttachedEntities.begin(); it != mlstAttachedEntities.end(); ++it) {
		// iEntity3D *pEntity = *it;
		//  TODO: if(mpWorld) mpWorld->DestroyUnknownEntity(pEntity);
	}

	for (int i = 0; i < (int)mvSubMeshes.size(); i++) {
		hplDelete(mvSubMeshes[i]);
	}

	if (mpRootNode)
		hplDelete(mpRootNode);
	if (mpRootCallback)
		hplDelete(mpRootCallback);

	mpMeshManager->Destroy(mpMesh);

	STLDeleteAll(mvNodeStates);
	STLDeleteAll(mvBoneStates);

	STLDeleteAll(mvTempBoneStates);

	STLDeleteAll(mvAnimationStates);
}

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

//////////////////////////////////////////////////////////////////////////
// ROOT NODE UDPATE CALLBACK
//////////////////////////////////////////////////////////////////////////

void cMeshEntityRootNodeUpdate::OnTransformUpdate(iEntity3D *apEntity) {
	cMeshEntity *pMeshEntity = static_cast<cMeshEntity *>(apEntity);

	pMeshEntity->mpRootNode->SetMatrix(apEntity->GetWorldMatrix());
}

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

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

// Ths functions sets the matrices of the bones according the matrices of
// the body that is attached to each bone.
// To get max speed all this is doen recursivly from the root and then down.
// To usre function iterate notes of root bone and call this function for each.
void cMeshEntity::SetBoneMatrixFromBodyRec(const cMatrixf &a_mtxParentWorld, cBoneState *apBoneState) {
	iPhysicsBody *pBody = apBoneState->GetBody();

	if (pBody) {
		cMatrixf mtxBoneWorld = cMath::MatrixMul(pBody->GetWorldMatrix(), apBoneState->GetInvBodyMatrix());
		cMatrixf mtxParentInv = cMath::MatrixInverse(a_mtxParentWorld);

		apBoneState->SetMatrix(cMath::MatrixMul(mtxParentInv, mtxBoneWorld), false);

		cNodeIterator BoneIt = apBoneState->GetChildIterator();
		while (BoneIt.HasNext()) {
			cBoneState *pBoneState = static_cast<cBoneState *>(BoneIt.Next());
			SetBoneMatrixFromBodyRec(mtxBoneWorld, pBoneState);
		}
	} else {
		apBoneState->UpdateWorldTransform();
		const cMatrixf &mtxBoneWorld = apBoneState->GetWorldMatrix();

		cNodeIterator BoneIt = apBoneState->GetChildIterator();
		while (BoneIt.HasNext()) {
			cBoneState *pBoneState = static_cast<cBoneState *>(BoneIt.Next());

			SetBoneMatrixFromBodyRec(mtxBoneWorld, pBoneState);
		}
	}
}

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

// int glDebugTabs=0;
void cMeshEntity::UpdateNodeMatrixRec(const cMatrixf &a_mtxParentWorld, cNode3D *apNode) {
	if (apNode->IsActive()) {
		// tString sTemp="";for(int i=0; i<glDebugTabs;++i)sTemp+=" ";
		// LogUpdate("   %sUpdate %s\n",sTemp.c_str(),apNode->GetName());
		apNode->UpdateMatrix(false);
	}

	apNode->UpdateWorldTransform();
	const cMatrixf &mtxWorld = apNode->GetWorldMatrix();

	// glDebugTabs++;

	cNodeIterator NodeIt = apNode->GetChildIterator();
	while (NodeIt.HasNext()) {
		cNode3D *pChildNode = static_cast<cNode3D *>(NodeIt.Next());

		UpdateNodeMatrixRec(mtxWorld, pChildNode);
	}

	// glDebugTabs--;
}

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

void cMeshEntity::UpdateLogic(float afTimeStep) {
	// LogUpdate("---- Start mesh update ----------------------\n");

	// if(!IsStatic()){START_TIMING_TAB(Skeleton);}

	/////////////////////////////////////////////////////
	// If not rendered the previous frame then we do not update as often.
	bool bRegularUpdate = GetGlobalRenderCount() == cRenderList::GetGlobalRenderCount();

	// Check if any skeleton  child has been rendered.
	if (mpMesh->GetSkeleton() && bRegularUpdate == false) {
		for (size_t i = 0; i < mvBoneStates.size(); ++i) {
			cBoneState *pState = mvBoneStates[i];

			cEntityIterator it = pState->GetEntityIterator();
			while (it.HasNext()) {
				iEntity3D *pEntity = static_cast<iEntity3D *>(it.Next());
				if (pEntity->GetGlobalRenderCount() == cRenderList::GetGlobalRenderCount()) {
					bRegularUpdate = true;
					break;
				}
			}
			if (bRegularUpdate)
				break;
		}
	}

	const int lMaxSleepCount = 30;
	if (bRegularUpdate == false) {
		if (mlStartSleepCount < lMaxSleepCount)
			++mlStartSleepCount;
	} else {
		mlStartSleepCount = 0;
	}

	if (mlStartSleepCount >= lMaxSleepCount) {
		++mlUpdateCount;
		mfTimeStepAccum += afTimeStep;

		if (mlUpdateCount < 20) {
			// if(cString::GetLastStringPos(msName, "infected")>=0) LogUpdate("   return\n");
			return;
		}

		mlUpdateCount = 0;
		afTimeStep = mfTimeStepAccum;
		mfTimeStepAccum = 0;
	} else {
		mlUpdateCount = 0;
		mfTimeStepAccum = 0;
	}
	// if(cString::GetLastStringPos(msName, "infected")>=0) LogUpdate("   do stuff\n");

	/////////////////////////////////////////////
	// Update the skeleton physics fade
	if (mbSkeletonPhysicsFading && mbSkeletonPhysics) {
		mfSkeletonPhysicsWeight -= afTimeStep * mfSkeletonPhysicsFadeSpeed;
		if (mfSkeletonPhysicsWeight <= 0) {
			mbSkeletonPhysicsFading = false;
			mfSkeletonPhysicsWeight = 1.0f;
			SetSkeletonPhysicsActive(false);
		}
	}

	// LogUpdate(" -skeleton physics\n");
	/////////////////////////////////////////////
	// Check if all bodies connected to the skeleton is at rest,
	// If so we can skip skinning the body and simply just use the mesh as is.
	//(has some problems so turned off at the moment)
	mbSkeletonPhysicsSleeping = false;
	if (mbSkeletonPhysics && mfSkeletonPhysicsWeight == 1.0f && mbSkeletonPhysicsCanSleep) {
		bool bEnabled = false;
		for (int bone = 0; bone < GetBoneStateNum(); ++bone) {
			cBoneState *pState = GetBoneState(bone);
			iPhysicsBody *pBody = pState->GetBody();

			if (pBody && pBody->GetEnabled()) {
				bEnabled = true;
				break;
			}
		}
		if (bEnabled == false) {
			// mbSkeletonPhysicsSleeping = true;
		}
	}

	// if(!IsStatic()){STOP_TIMING_TAB(Skeleton);}

	/////////////////////////////////////////////
	// Update animations

	//////////////////////////////////////
	// Check if it has nodes, then use special animation update,
	// else it as skepetn which needs another update.
	if (mbHasNodes) {
		// Reset all node states to prepare for animations.
		if (mvAnimationStates.size() > 0) {
			for (size_t i = 0; i < mvNodeStates.size(); i++) {
				cNode3D *pState = mvNodeStates[i];
				if (pState->IsActive())
					pState->SetMatrix(cMatrixf::Identity);
			}
		}

		/////////////////////////
		// Go through all animations states and set the node's
		// temporary states
		bool bAnimated = false;
		for (size_t i = 0; i < mvAnimationStates.size(); i++) {
			cAnimationState *pAnimState = mvAnimationStates[i];
			if (pAnimState->IsActive()) {
				bAnimated = true;

				cAnimation *pAnim = pAnimState->GetAnimation();

				for (int j = 0; j < pAnim->GetTrackNum(); j++) {
					cAnimationTrack *pTrack = pAnim->GetTrack(j);

					if (pTrack->GetNodeIndex() < 0) {
						pTrack->SetNodeIndex(GetNodeStateIndex(pTrack->GetName()));
					}
					cNode3D *pNodeState = GetNodeState(pTrack->GetNodeIndex());

					if (pNodeState->IsActive())
						pTrack->ApplyToNode(pNodeState, pAnimState->GetTimePosition(), pAnimState->GetWeight());
				}

				pAnimState->Update(afTimeStep);
			}
		}

		//////////////////////
		// Go through all states and update the matrices (and thereby adding the animations together).
		if (mvAnimationStates.size() > 0 && bAnimated) {
			cNodeIterator NodeIt = mpRootNode->GetChildIterator();
			while (NodeIt.HasNext()) {
				cNode3D *pBoneState = static_cast<cNode3D *>(NodeIt.Next());

				UpdateNodeMatrixRec(mpRootNode->GetWorldMatrix(), pBoneState);
			}
		}

		//////////////////////
		// Call callback to be run after animation
		if (mpCallback)
			mpCallback->AfterAnimationUpdate(this, afTimeStep);

		UpdateBVFromSubs();

		// Update the entity transform, this so that the portal info, attached entities, callbacks and stuff is updated.
		SetTransformUpdated(true);
	}
	//////////////////////////////////////
	// If the entity has a skeleton:
	else if (mpMesh->GetSkeleton()) {
		// if(!IsStatic()){START_TIMING_TAB(Animation);}

		////////////////////////
		// Check if it is animated
		bool bAnimationActive = false;
		for (size_t i = 0; i < mvAnimationStates.size(); i++) {
			if (mvAnimationStates[i]->IsActive()) {
				bAnimationActive = true;
				break;
			}
		}

		//////////
		// Reset all bones states
		// LogUpdate(" -reset bones\n");
		if (bAnimationActive || mbUpdatedBones == false || (mbSkeletonPhysics && !mbSkeletonPhysicsSleeping)) {
			for (size_t i = 0; i < mvBoneStates.size(); i++) {
				cNode3D *pState = mvBoneStates[i];
				cBone *pBone = mpMesh->GetSkeleton()->GetBoneByIndex((int)i);

				if (pState->IsActive()) {
					pState->SetMatrix(pBone->GetLocalTransform(), false);
				}

				// can optimize this by doing it in the order of the tree
				// and using recursive. (should be enough as is...)
				if (mbSkeletonPhysics && mfSkeletonPhysicsWeight != 1.0f) {
					mvTempBoneStates[i]->SetMatrix(pBone->GetLocalTransform(), false);
				}
			}
		}

		///////////////////////////
		// Update skeleton physics
		// LogUpdate(" -reset physics again\n");
		if (mbSkeletonPhysics && (!mbSkeletonPhysicsSleeping || mbUpdatedBones == false)) {
			mbUpdatedBones = true;
			cNodeIterator BoneIt = mpRootNode->GetChildIterator();
			while (BoneIt.HasNext()) {
				cBoneState *pBoneState = static_cast<cBoneState *>(BoneIt.Next());

				SetBoneMatrixFromBodyRec(mpRootNode->GetWorldMatrix(), pBoneState);
			}

			// Interpolate matrices
			if (mfSkeletonPhysicsWeight != 1.0f) {
				for (size_t i = 0; i < mvBoneStates.size(); i++) {
					cMatrixf mtxMixLocal = cMath::MatrixSlerp(mfSkeletonPhysicsWeight,
															  mvTempBoneStates[i]->GetLocalMatrix(),
															  mvBoneStates[i]->GetLocalMatrix(),
															  true);

					mvBoneStates[i]->SetMatrix(mtxMixLocal, false);
				}
			}
		}

		// if(!IsStatic()){STOP_TIMING_TAB(Animation);}

		// if(!IsStatic()){START_TIMING_TAB(Bones);}
		//////////////////////////////////
		// Go through all animations states and update the bones
		// LogUpdate(" -animation states\n");
		for (size_t i = 0; i < mvAnimationStates.size(); i++) {
			cAnimationState *pAnimState = mvAnimationStates[i];

			// Log("Testing state: '%s'\n",pAnimState->GetName());

			if (pAnimState->IsActive()) {
				cAnimation *pAnim = pAnimState->GetAnimation();

				for (int i2 = 0; i2 < pAnim->GetTrackNum(); i2++) {
					cAnimationTrack *pTrack = pAnim->GetTrack(i2);

					cNode3D *pState = GetBoneState(pTrack->GetNodeIndex());

					// Log("Animating bone %s\n",pState->GetName());

					if (pState->IsActive())
						pTrack->ApplyToNode(pState, pAnimState->GetTimePosition(), pAnimState->GetWeight());
				}

				// Log("Time: %f\n",pAnimState->GetTimePosition());

				pAnimState->Update(afTimeStep);
			}
		}
		// if(!IsStatic()){STOP_TIMING_TAB(Bones);}

		// if(!IsStatic()){START_TIMING_TAB(States);}
		//////////////////////////////////
		// Go through all states and update the matrices (and thereby adding the animations together).
		// LogUpdate(" -Bone update\n");
		if (bAnimationActive) {
			cNodeIterator NodeIt = mpRootNode->GetChildIterator();
			while (NodeIt.HasNext()) {
				cNode3D *pBoneState = static_cast<cNode3D *>(NodeIt.Next());

				UpdateNodeMatrixRec(mpRootNode->GetWorldMatrix(), pBoneState);
			}

			// Entities are updated after BV is calculated, as the entity has the rootnode attached to it.
		}
		// if(!IsStatic()){STOP_TIMING_TAB(States);}

		// if(!IsStatic()){START_TIMING_TAB(Colliders);}
		//////////////////////////////////
		// Update the colliders if they are active
		// Note this must be done after all bone states are updated.
		// LogUpdate(" -Colliders\n");
		if (mbSkeletonColliders && mbSkeletonPhysics == false) {
			for (size_t i = 0; i < mvBoneStates.size(); i++) {
				cBoneState *pState = mvBoneStates[i];
				iPhysicsBody *pColliderBody = pState->GetColliderBody();

				if (pColliderBody) {
					cMatrixf mtxBody = cMath::MatrixMul(pState->GetWorldMatrix(), pState->GetBodyMatrix());
					pColliderBody->SetMatrix(mtxBody);
				}
			}
		}
		// if(!IsStatic()){STOP_TIMING_TAB(Colliders);}

		// Call callback
		if (mpCallback)
			mpCallback->AfterAnimationUpdate(this, afTimeStep);

		///////////////////////////
		// Update bounding volume
		// LogUpdate(" -BV update\n");
		UpdateBVFromSubs();

		// Update the entity transform, this so that the portal info, attached entities, callbacks and stuff is updated.
		SetTransformUpdated(true);
	}

	// if(!IsStatic()){START_TIMING_TAB(SubEntities);}
	/////////////////////////////////////////
	/// Update sub entities
	// LogUpdate(" -Subs\n");
	for (size_t i = 0; i < mvSubMeshes.size(); ++i) {
		cSubMeshEntity *pSub = mvSubMeshes[i];

		pSub->UpdateLogic(afTimeStep);
	}
	// if(!IsStatic()){STOP_TIMING_TAB(SubEntities);}

	/////////////////////////////////////////
	/// Update animation events
	for (size_t i = 0; i < mvAnimationStates.size(); ++i) {
		cAnimationState *pState = mvAnimationStates[i];

		if (pState->IsActive() == false || pState->IsPaused())
			continue;

		for (int j = 0; j < pState->GetEventNum(); ++j) {
			cAnimationEvent *pEvent = pState->GetEvent(j);

			if (pEvent->mfTime >= pState->GetPreviousTimePosition() &&
				pEvent->mfTime < pState->GetTimePosition()) {
				HandleAnimationEvent(pEvent);
			}
		}
	}

	// LogUpdate("---- End mesh update ----------------------\n");
}

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

cAnimationState *cMeshEntity::AddAnimation(cAnimation *apAnimation, const tString &asName, float afBaseSpeed) {
	cAnimationState *pAnimState = hplNew(cAnimationState, (apAnimation, asName, mpAnimationManager));

	pAnimState->SetBaseSpeed(afBaseSpeed);

	mvAnimationStates.push_back(pAnimState);

	tAnimationStateIndexMap::value_type value(pAnimState->GetName(), (int)mvAnimationStates.size() - 1);
	m_mapAnimationStateIndices.insert(value);

	return pAnimState;
}

void cMeshEntity::ClearAnimations() {
	STLDeleteAll(mvAnimationStates);
}

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

cAnimationState *cMeshEntity::GetAnimationState(int alIndex) {
	return mvAnimationStates[alIndex];
}
int cMeshEntity::GetAnimationStateIndex(const tString &asName) {
	tAnimationStateIndexMapIt it = m_mapAnimationStateIndices.find(asName);
	if (it != m_mapAnimationStateIndices.end()) {
		return it->second;
	} else {
		return -1;
	}
}
cAnimationState *cMeshEntity::GetAnimationStateFromName(const tString &asName) {
	int lIdx = GetAnimationStateIndex(asName);
	if (lIdx >= 0) {
		return mvAnimationStates[lIdx];
	} else {
		return NULL;
	}
}
int cMeshEntity::GetAnimationStateNum() {
	return (int)mvAnimationStates.size();
}

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

void cMeshEntity::Play(int alIndex, bool abLoop, bool bStopPrev) {
	if (bStopPrev)
		Stop();

	mvAnimationStates[alIndex]->SetActive(true);
	mvAnimationStates[alIndex]->SetTimePosition(0);
	mvAnimationStates[alIndex]->SetLoop(abLoop);
	mvAnimationStates[alIndex]->SetWeight(1);
}

void cMeshEntity::PlayName(const tString &asName, bool abLoop, bool bStopPrev) {
	int lIdx = GetAnimationStateIndex(asName);
	if (lIdx >= 0) {
		Play(lIdx, abLoop, bStopPrev);
	} else {
		Warning("Can not find animation '%s' in meshentity '%s'\n", asName.c_str(),
				msName.c_str());
	}
}

void cMeshEntity::Stop() {
	for (size_t i = 0; i < mvAnimationStates.size(); i++) {
		mvAnimationStates[i]->SetActive(false);
		mvAnimationStates[i]->SetTimePosition(0);
	}
}

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

cBoneState *cMeshEntity::GetBoneState(int alIndex) {
	return mvBoneStates[alIndex];
}

int cMeshEntity::GetBoneStateIndex(const tString &asName) {
	tNodeStateIndexMapIt it = m_mapBoneStateIndices.find(asName);
	if (it != m_mapBoneStateIndices.end()) {
		return it->second;
	} else {
		return -1;
	}
}

cBoneState *cMeshEntity::GetBoneStateFromName(const tString &asName) {
	int lIdx = GetBoneStateIndex(asName);
	if (lIdx >= 0) {
		return mvBoneStates[lIdx];
	} else {
		return NULL;
	}
}
int cMeshEntity::GetBoneStateNum() {
	return (int)mvBoneStates.size();
}

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

void cMeshEntity::SetSkeletonPhysicsActive(bool abX) {
	mbSkeletonPhysics = abX;

	mbUpdatedBones = false;
	ResetGraphicsUpdated();

	mbSkeletonPhysicsFading = false;
	mfSkeletonPhysicsWeight = 1.0f;

	for (int bone = 0; bone < GetBoneStateNum(); ++bone) {
		cBoneState *pState = GetBoneState(bone);
		iPhysicsBody *pBody = pState->GetBody();
		iPhysicsBody *pColliderBody = pState->GetColliderBody();

		if (pBody) {
			pBody->SetActive(abX);
			pBody->SetEnabled(abX);

			if (abX == false) {
				pBody->SetLinearVelocity(0);
				pBody->SetAngularVelocity(0);
			}

			if (mbSkeletonColliders) {
				pColliderBody->SetActive(!abX);
			}
		}
	}
}

bool cMeshEntity::GetSkeletonPhysicsActive() {
	return mbSkeletonPhysics;
}

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

void cMeshEntity::FadeSkeletonPhysicsWeight(float afTime) {
	if (mbSkeletonPhysics) {
		mbSkeletonPhysicsFading = true;
		mfSkeletonPhysicsFadeSpeed = 1.0f / afTime;

		for (int bone = 0; bone < GetBoneStateNum(); ++bone) {
			cBoneState *pState = GetBoneState(bone);
			iPhysicsBody *pBody = pState->GetBody();
			/*iPhysicsBody *pColliderBody = */ pState->GetColliderBody();

			if (pBody)
				pBody->SetActive(false);
		}
	}
}

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

float cMeshEntity::GetSkeletonPhysicsWeight() {
	return mfSkeletonPhysicsWeight;
}
void cMeshEntity::SetSkeletonPhysicsWeight(float afX) {
	mfSkeletonPhysicsWeight = afX;
}

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

void cMeshEntity::SetSkeletonCollidersActive(bool abX) {
	mbSkeletonColliders = abX;

	// Set active to the correct state.
	for (int bone = 0; bone < GetBoneStateNum(); ++bone) {
		cBoneState *pState = GetBoneState(bone);
		iPhysicsBody *pColliderBody = pState->GetColliderBody();

		if (pColliderBody) {
			if (abX && !mbSkeletonPhysics)
				pColliderBody->SetActive(true);
			else if (!abX)
				pColliderBody->SetActive(false);
		}
	}
}
bool cMeshEntity::GetSkeletonCollidersActive() {
	return mbSkeletonColliders;
}

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

void cMeshEntity::AlignBodiesToSkeleton(bool abCalculateSpeed) {
	for (int bone = 0; bone < GetBoneStateNum(); ++bone) {
		cBoneState *pState = GetBoneState(bone);
		iPhysicsBody *pBody = pState->GetBody();

		if (pBody) {
			cMatrixf mtxBody = cMath::MatrixMul(pState->GetWorldMatrix(), pState->GetBodyMatrix());
			pBody->SetMatrix(mtxBody);

			if (abCalculateSpeed) {
				// TODO: calculate speed based on the previous frame of the animation.
			}
		}
	}
}

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

cMatrixf cMeshEntity::CalculateTransformFromSkeleton(cVector3f *apPostion, cVector3f *apAngles) {
	// Root bone
	cNodeIterator StateIt = GetRootNode()->GetChildIterator();
	cBoneState *pBoneState = static_cast<cBoneState *>(StateIt.Next());

	// Get the root bone (should only be one)
	cBoneIterator BoneIt = GetMesh()->GetSkeleton()->GetRootBone()->GetChildIterator();
	cBone *pBone = BoneIt.Next();

	// Rotation and position
	cMatrixf mtxInvBind = pBone->GetInvWorldTransform();
	cMatrixf mtxInvBone = cMath::MatrixInverse(pBoneState->GetWorldMatrix());
	cVector3f vStateForward = mtxInvBone.GetForward();
	cVector3f vBindForward = mtxInvBind.GetForward();

	float fBindYAngle = -cMath::GetAngleFromPoints2D(0, cVector2f(-vBindForward.x, -vBindForward.z));
	float fStateYAngle = -cMath::GetAngleFromPoints2D(0, cVector2f(-vStateForward.x, -vStateForward.z));
	float fYAngle = fStateYAngle - fBindYAngle;

	cMatrixf mtxTransform = cMath::MatrixRotateY(fYAngle);

	cVector3f vRootBoneOffset = pBone->GetLocalTransform().GetTranslation();
	vRootBoneOffset.y = 0;
	vRootBoneOffset = cMath::MatrixMul(mtxTransform, vRootBoneOffset);

	mtxTransform.SetTranslation(pBoneState->GetWorldPosition()); // - vRootBoneOffset);

	if (apPostion)
		*apPostion = pBoneState->GetWorldPosition(); // - vRootBoneOffset;
	if (apAngles)
		*apAngles = cVector3f(0, fYAngle, 0);

	return mtxTransform;
}

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

bool cMeshEntity::CheckColliderShapeCollision(iPhysicsWorld *apWorld,
											  iCollideShape *apShape, const cMatrixf &a_mtxShape,
											  tVector3fList *apPosList, tIntList *apNumList) {
	bool bCollision = false;
	cCollideData collideData;
	collideData.SetMaxSize(1);

	for (size_t i = 0; i < mvBoneStates.size(); ++i) {
		cBoneState *pState = mvBoneStates[i];
		iPhysicsBody *pBody = pState->GetColliderBody();
		if (pBody == NULL)
			continue;

		cMatrixf mtxBody = cMath::MatrixMul(pState->GetWorldMatrix(), pState->GetBodyMatrix());
		pBody->SetMatrix(mtxBody);

		bool bRet = apWorld->CheckShapeCollision(pBody->GetShape(),
												 pBody->GetLocalMatrix(),
												 apShape, a_mtxShape,
												 collideData, 1);

		if (bRet) {
			bCollision = true;
			if (!apPosList && !apNumList)
				break;

			if (apPosList)
				apPosList->push_back(collideData.mvContactPoints[0].mvPoint);
			if (apNumList)
				apNumList->push_back((int)i);
		}
	}

	return bCollision;
}

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

void cMeshEntity::ResetGraphicsUpdated() {
	for (size_t i = 0; i < mvSubMeshes.size(); ++i) {
		mvSubMeshes[i]->mbGraphicsUpdated = false;
	}
	mbUpdatedBones = false;
}

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

cNode3D *cMeshEntity::GetNodeState(int alIndex) {
	return mvNodeStates[alIndex];
}

int cMeshEntity::GetNodeStateIndex(const tString &asName) {
	tNodeStateIndexMapIt it = m_mapNodeStateIndices.find(asName);
	if (it != m_mapNodeStateIndices.end()) {
		return it->second;
	} else {
		return -1;
	}
}

cNode3D *cMeshEntity::GetNodeStateFromName(const tString &asName) {
	int lIdx = GetNodeStateIndex(asName);
	if (lIdx >= 0) {
		return mvNodeStates[lIdx];
	} else {
		return NULL;
	}
}
int cMeshEntity::GetNodeStateNum() {
	return (int)mvNodeStates.size();
}

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

void cMeshEntity::SetCastsShadows(bool abX) {
	if (abX == mbCastShadows)
		return;

	mbCastShadows = abX;
	for (int i = 0; i < (int)mvSubMeshes.size(); i++) {
		mvSubMeshes[i]->SetCastsShadows(abX);
	}
}

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

cSubMeshEntity *cMeshEntity::GetSubMeshEntity(unsigned int alIdx) {
	if (alIdx >= mvSubMeshes.size())
		return NULL;

	return mvSubMeshes[alIdx];
}

cSubMeshEntity *cMeshEntity::GetSubMeshEntityName(const tString &asName) {
	tSubMeshEntityMapIt it = m_mapSubMeshes.find(asName);
	if (it == m_mapSubMeshes.end())
		return NULL;

	return it->second;
}

int cMeshEntity::GetSubMeshEntityNum() {
	return (int)mvSubMeshes.size();
}

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

void cMeshEntity::UpdateGraphics(cCamera3D *apCamera, float afFrameTime, cRenderList *apRenderList) {
	// Update the bone matrices
	cSkeleton *pSkeleton = mpMesh->GetSkeleton();
	if (pSkeleton) {
		// Save the root node to a temp matrix
		/*cMatrixf mtxTemp;
		if(mpRootNode)
		{
			mtxTemp = mpRootNode->GetWorldMatrix();
			mpRootNode->SetMatrix(cMatrixf::Identity);
		}*/
		cMatrixf *pInvWorldMtx = GetInvModelMatrix();

		for (int i = 0; i < pSkeleton->GetBoneNum(); i++) {
			cBone *pBone = pSkeleton->GetBoneByIndex(i);
			cNode3D *pState = mvBoneStates[i];

			// Transform the movement of the bone into the
			// Bind pose's local space.
			cMatrixf mtxLocal = cMath::MatrixMul(*pInvWorldMtx, pState->GetWorldMatrix());

			mvBoneMatrices[i] = cMath::MatrixMul(mtxLocal, pBone->GetInvWorldTransform());
		}

		// Set back the matrix.
		/*if(mpRootNode){
			mpRootNode->SetMatrix(mtxTemp);
			SetTransformUpdated(true);
		}
		else {
			SetTransformUpdated(true);
		}*/
	}
}

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

bool cMeshEntity::AttachEntityToParent(iEntity3D *apEntity, const tString &asParent) {
	mlstAttachedEntities.push_back(apEntity);

	// If no parent is specified add to first sub entity.
	if (asParent == "") {
		// Log("Added %s to mesh\n",apEntity->GetName().c_str());
		AddChild(apEntity);
		return true;
	}

	// Check submeshes
	cSubMeshEntity *pSubEntity = GetSubMeshEntityName(asParent);
	if (pSubEntity) {
		// Log("Added %s to subentity %s\n",apEntity->GetName().c_str(),pSubEntity->GetName().c_str());
		pSubEntity->AddChild(apEntity);
		return true;
	}

	// Check nodes
	cNode3D *pNode = GetNodeStateFromName(asParent);
	if (pNode) {
		// Log("Added %s to node %s\n",apEntity->GetName().c_str(),pNode->GetName());
		pNode->AddEntity(apEntity);
		return true;
	}

	// Check bones
	cNode3D *pBone = GetBoneStateFromName(asParent);
	if (pBone) {
		// Log("Added %s to bone\n",apEntity->GetName().c_str());
		pBone->AddEntity(apEntity);
		return true;
	}

	Warning("Parent '%s' couldn't be found! Failed to attach '%s' to '%s'.Attaching directly to mesh.\n", asParent.c_str(),
			apEntity->GetName().c_str(), GetName().c_str());
	AddChild(apEntity);

	return false;
}

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

void cMeshEntity::SetRendered(bool abX) {
	if (abX == mbRendered)
		return;

	mbRendered = abX;
	for (int i = 0; i < (int)mvSubMeshes.size(); i++) {
		mvSubMeshes[i]->SetRendered(abX);
		mvSubMeshes[i]->SetGlobalRenderCount(cRenderList::GetGlobalRenderCount());
	}
	SetGlobalRenderCount(cRenderList::GetGlobalRenderCount());
	mlStartSleepCount = 0;
}

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

iMaterial *cMeshEntity::GetMaterial() {
	return NULL;
}

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

iVertexBuffer *cMeshEntity::GetVertexBuffer() {
	return NULL;
}

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

bool cMeshEntity::IsShadowCaster() {
	return mbCastShadows;
}

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

cBoundingVolume *cMeshEntity::GetBoundingVolume() {
	return &mBoundingVolume;
}

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

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

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

int cMeshEntity::GetMatrixUpdateCount() {
	return GetTransformUpdateCount();
}

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

eRenderableType cMeshEntity::GetRenderType() {
	return eRenderableType_Mesh;
}

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

//////////////////////////////////////////////////////////////////////////
// PRIAVTE METHODS
//////////////////////////////////////////////////////////////////////////

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

void cMeshEntity::HandleAnimationEvent(cAnimationEvent *apEvent) {
	if (apEvent->msValue == "")
		return;

	switch (apEvent->mType) {
	case eAnimationEventType_PlaySound: {
		cSoundEntity *pSound = mpWorld->CreateSoundEntity("AnimEvent", apEvent->msValue, true);
		if (pSound) {
			cNodeIterator nodeIt = mpRootNode->GetChildIterator();
			if (nodeIt.HasNext()) {
				iNode *pNode = nodeIt.Next();
				pNode->AddEntity(pSound);
			} else {
				pSound->SetPosition(mBoundingVolume.GetWorldCenter());
			}
		}
		break;
	}
	default:
		break;
	}
}

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

void cMeshEntity::UpdateBVFromSubs() {
	/////////////////////////////////////
	// Skeleton
	if (mpMesh->GetSkeleton()) {
		if (mvBoneStates.empty()) {
			////////////////////////////////
			// Using vertices

			// Go through all the sub meshes and build BV from vertices.
			for (int i = 0; i < GetSubMeshEntityNum(); i++) {
				cSubMeshEntity *pSub = GetSubMeshEntity(i);

				iVertexBuffer *pVtxBuffer = pSub->GetVertexBuffer();

				mBoundingVolume.AddArrayPoints(pVtxBuffer->GetArray(eVertexFlag_Position),
											   pVtxBuffer->GetVertexNum());
			}
			mBoundingVolume.CreateFromPoints(kvVertexElements[cMath::Log2ToInt(eVertexFlag_Position)]);
		} else {
			////////////////////////////////
			// Using bones
			cVector3f vMin = mvBoneStates[0]->GetWorldPosition();
			cVector3f vMax = vMin;

			for (size_t i = 1; i < mvBoneStates.size(); ++i) {
				cBoneState *pState = mvBoneStates[i];
				cVector3f vPos = pState->GetWorldPosition();

				if (vMax.x < vPos.x)
					vMax.x = vPos.x;
				if (vMax.y < vPos.y)
					vMax.y = vPos.y;
				if (vMax.z < vPos.z)
					vMax.z = vPos.z;

				if (vMin.x > vPos.x)
					vMin.x = vPos.x;
				if (vMin.y > vPos.y)
					vMin.y = vPos.y;
				if (vMin.z > vPos.z)
					vMin.z = vPos.z;
			}

			// Add 10 percent to the box borders.
			vMin -= (vMax - vMin) * 0.1f + 0.1f;
			vMax += (vMax - vMin) * 0.1f + 0.1f;

			mBoundingVolume.SetTransform(cMatrixf::Identity);
			mBoundingVolume.SetLocalMinMax(vMin, vMax);
		}

	}
	////////////////////////////////////
	// Nodes
	else {
		// Use this to make sure the the nodes are in the same positions as when
		// exported. This is to give a working Bounding Volume.
		// This feels kind of slow...but might be the only way.

		cMatrixf mtxTemp2;
		if (mpRootNode) {
			mtxTemp2 = mpRootNode->GetWorldMatrix();
			mpRootNode->SetMatrix(cMatrixf::Identity);
		}

		cVector3f vFinalMin = mvSubMeshes[0]->mBoundingVolume.GetMin();
		cVector3f vFinalMax = mvSubMeshes[0]->mBoundingVolume.GetMax();

		for (int i = 1; i < (int)mvSubMeshes.size(); i++) {
			cVector3f vMin = mvSubMeshes[i]->mBoundingVolume.GetMin();
			cVector3f vMax = mvSubMeshes[i]->mBoundingVolume.GetMax();

			if (vFinalMin.x > vMin.x)
				vFinalMin.x = vMin.x;
			if (vFinalMax.x < vMax.x)
				vFinalMax.x = vMax.x;

			if (vFinalMin.y > vMin.y)
				vFinalMin.y = vMin.y;
			if (vFinalMax.y < vMax.y)
				vFinalMax.y = vMax.y;

			if (vFinalMin.z > vMin.z)
				vFinalMin.z = vMin.z;
			if (vFinalMax.z < vMax.z)
				vFinalMax.z = vMax.z;
		}

		mBoundingVolume.SetLocalMinMax(vFinalMin, vFinalMax);

		if (mpRootNode) {
			mpRootNode->SetMatrix(mtxTemp2);
		} else {
		}
	}
}
//-----------------------------------------------------------------------

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

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

kBeginSerialize(cSaveData_cMeshEntity, cSaveData_iRenderable)
	kSerializeVar(msMeshName, eSerializeType_String)

		kSerializeVar(mbCastShadows, eSerializeType_Bool)

			kSerializeVar(mlBodyId, eSerializeType_Int32)

				kSerializeClassContainer(mvSubEntities, cSaveData_cSubMeshEntity, eSerializeType_Class)
					kSerializeClassContainer(mvAnimStates, cSaveData_cAnimationState, eSerializeType_Class)
						kEndSerialize()

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

	iSaveObject *cSaveData_cMeshEntity::CreateSaveObject(cSaveObjectHandler *apSaveObjectHandler, cGame *apGame) {
	cResources *pResources = apGame->GetResources();
	cWorld3D *pWorld = apGame->GetScene()->GetWorld3D();

	/*cMeshLoaderHandler *pMeshLoadHandler = */ pResources->GetMeshLoaderHandler();
	/*cFileSearcher *pFileSearcher = */ pResources->GetFileSearcher();

	///////////////////////////////////
	// Load mesh
	cMesh *pMesh = pResources->GetMeshManager()->CreateMesh(msMeshName);
	if (pMesh == NULL)
		return NULL;

	///////////////////////
	// Load Animations
	// TODO: bad, bad bad...redo animation storing.
	/*if(pMesh->GetAnimationNum()<=1 && mvAnimStates.Size()>1)
	{
		pMesh->ClearAnimations(true);

		for(size_t i=0; i< mvAnimStates.Size(); ++i)
		{
			cAnimation *pAnimation = pResources->GetAnimationManager()->CreateAnimation(mvAnimStates[i].msAnimationName);

			pAnimation->SetAnimationName(mvAnimStates[i].msName);
			pAnimation->SetDefaultSpeed(mvAnimStates[i].mfDefaultSpeed);

			pMesh->AddAnimation(pAnimation);
		}
	}*/

	///////////////////////
	/// Create entity
	cMeshEntity *pEntity = pWorld->CreateMeshEntity(msName, pMesh);

	///////////////////////
	/// Add sub meshes to the save object handler.
	for (int sub = 0; sub < pEntity->GetSubMeshEntityNum(); ++sub) {
		cSubMeshEntity *pSub = pEntity->GetSubMeshEntity(sub);

		pSub->LoadFromSaveData(&mvSubEntities[sub]);
		apSaveObjectHandler->Add(pSub);
	}

	return pEntity;
}

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

int cSaveData_cMeshEntity::GetSaveCreatePrio() {
	return 2;
}

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

iSaveData *cMeshEntity::CreateSaveData() {
	return hplNew(cSaveData_cMeshEntity, ());
}

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

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

	////////////////////////////
	// Set the name of the data
	pData->msMeshName = mpMesh->GetName();

	////////////////////////////
	// Sub Entities
	pData->mvSubEntities.Resize(GetSubMeshEntityNum());
	for (int i = 0; i < GetSubMeshEntityNum(); i++) {
		cSubMeshEntity *pSubEntity = GetSubMeshEntity(i);

		pSubEntity->SaveToSaveData(&pData->mvSubEntities[i]);
	}

	////////////////////////////
	// Animation states
	pData->mvAnimStates.Resize(GetAnimationStateNum());
	for (int i = 0; i < GetAnimationStateNum(); i++) {
		cAnimationState *pAnimState = GetAnimationState(i);

		pAnimState->SaveToSaveData(&pData->mvAnimStates[i]);
	}
	// Log("MeshEntity %s has %d animation states saved\n",GetName().c_str(),pData->mvAnimStates.Size());

	////////////////////////////
	// Variables
	kSaveData_SaveTo(mbCastShadows);

	////////////////////////////
	// Pointers
	kSaveData_SaveObject(mpBody, mlBodyId);
}

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

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

	///////////////////////
	// Sub entities
	// This is done on creation instead.
	/*for(size_t i=0; i<pData->mvSubEntities.Size(); i++)
	{
		cSubMeshEntity *pSubEntity = GetSubMeshEntity((int)i);

		pSubEntity->LoadFromSaveData(&pData->mvSubEntities[i]);
	}*/

	///////////////////////
	// Set Animation states data
	for (size_t i = 0; i < pData->mvAnimStates.Size(); ++i) {
		cAnimationState *pAnimationState = GetAnimationState((int)i);

		pAnimationState->LoadFromSaveData(&pData->mvAnimStates[i]);
	}
	// Log("LOADED: MeshEntity %s has %d animation states, in entity: %d\n",GetName().c_str(),
	//			pData->mvAnimStates.Size(),GetAnimationStateNum());

	//////////////////////
	// Variables
	kSaveData_LoadFrom(mbCastShadows);
}

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

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

	///////////////////////
	// Sub entities
	// Should not be needed since they are added to the save object handler.
	for (int i = 0; i < GetSubMeshEntityNum(); i++) {
		cSubMeshEntity *pSubEntity = GetSubMeshEntity(i);
		pSubEntity->SaveDataSetup(apSaveObjectHandler, apGame);
	}

	///////////////////////
	// Body
	kSaveData_LoadObject(mpBody, mlBodyId, iPhysicsBody *);
	if (mpBody) {
		mpBody->CreateNode()->AddEntity(this);
	}
}

//-----------------------------------------------------------------------
} // namespace hpl