aboutsummaryrefslogtreecommitdiff
path: root/APEX_1.4/include/destructible/ExplicitHierarchicalMesh.h
blob: d088d867cafbf580d76bba1c0a2c4a163cdbae46 (plain) (blame)
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
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//  * Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//  * Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//  * Neither the name of NVIDIA CORPORATION nor the names of its
//    contributors may be used to endorse or promote products derived
//    from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Copyright (c) 2018 NVIDIA Corporation. All rights reserved.



#ifndef EXPLICIT_HIERARCHICAL_MESH_H
#define EXPLICIT_HIERARCHICAL_MESH_H

#include "foundation/Px.h"
#include "IProgressListener.h"
#include "RenderMeshAsset.h"
#include "ConvexHullMethod.h"
#include "foundation/PxPlane.h"

namespace nvidia
{
namespace apex
{

PX_PUSH_PACK_DEFAULT

/**
	ExplicitVertexFormat

	This is used when authoring an VertexBuffer, to define which data channels exist.
 */
struct ExplicitVertexFormat
{
	/** This value defines which vertex winding orders will be rendered.  See RenderCullMode. */
	uint32_t			mWinding;

	/** Whether or not the accompanying vertex data has defined static vertex positions. */
	bool				mHasStaticPositions;

	/** Whether or not the accompanying vertex data has defined static vertex normals. */
	bool				mHasStaticNormals;

	/** Whether or not the accompanying vertex data has defined static vertex tangents. */
	bool				mHasStaticTangents;

	/** Whether or not the accompanying vertex data has defined static vertex binormals. */
	bool				mHasStaticBinormals;

	/** Whether or not the accompanying vertex data has defined static vertex colors. */
	bool				mHasStaticColors;

	/** Whether or not to create separate render resource for a static bone index buffer. */
	bool				mHasStaticSeparateBoneBuffer;

	/** Whether or not the accompanying vertex data has defined dynamic displacement coordinates */
	bool				mHasStaticDisplacements;

	/** Whether or not the accompanying vertex data has defined dynamic vertex positions. */
	bool				mHasDynamicPositions;

	/** Whether or not the accompanying vertex data has defined dynamic vertex normals. */
	bool				mHasDynamicNormals;

	/** Whether or not the accompanying vertex data has defined dynamic vertex tangents. */
	bool				mHasDynamicTangents;

	/** Whether or not the accompanying vertex data has defined dynamic vertex binormals. */
	bool				mHasDynamicBinormals;

	/** Whether or not the accompanying vertex data has defined dynamic vertex colors. */
	bool				mHasDynamicColors;

	/** Whether or not to create separate render resource for a dynamic bone index buffer. */
	bool				mHasDynamicSeparateBoneBuffer;

	/** Whether or not the accompanying vertex data has defined dynamic displacement coordinates */
	bool				mHasDynamicDisplacements;

	/** How many UV coordinate channels there are (per vertex) */
	uint32_t			mUVCount;

	/** How many bones may influence a vertex */
	uint32_t			mBonesPerVertex;

	/** Constructor, calls clear() to set formats to default settings */
	ExplicitVertexFormat()
	{
		clear();
	}

	/**
		Set formats to default settings:

		mWinding = RenderCullMode::CLOCKWISE;
		mHasStaticPositions = false;
		mHasStaticNormals = false;
		mHasStaticTangents = false;
		mHasStaticBinormals = false;
		mHasStaticColors = false;
		mHasStaticSeparateBoneBuffer = false;
		mHasStaticDisplacements = false;
		mHasDynamicPositions = false;
		mHasDynamicNormals = false;
		mHasDynamicTangents = false;
		mHasDynamicBinormals = false;
		mHasDynamicColors = false;
		mHasDynamicSeparateBoneBuffer = false;
		mHasDynamicDisplacements = false;
		mUVCount = 0;
		mBonesPerVertex = 0;
	*/
	void	clear()
	{
		mWinding = RenderCullMode::CLOCKWISE;
		mHasStaticPositions = false;
		mHasStaticNormals = false;
		mHasStaticTangents = false;
		mHasStaticBinormals = false;
		mHasStaticColors = false;
		mHasStaticSeparateBoneBuffer = false;
		mHasStaticDisplacements = false;
		mHasDynamicPositions = false;
		mHasDynamicNormals = false;
		mHasDynamicTangents = false;
		mHasDynamicBinormals = false;
		mHasDynamicColors = false;
		mHasDynamicSeparateBoneBuffer = false;
		mHasDynamicDisplacements = false;
		mUVCount = 0;
		mBonesPerVertex = 0;
	}

	/**
		Equality operator.  All values are tested for equality except mBonesPerVertex.
	*/
	bool	operator == (const ExplicitVertexFormat& data) const
	{
		if (mWinding != data.mWinding)
		{
			return false;
		}
		if (mHasStaticPositions != data.mHasStaticPositions ||
		        mHasStaticNormals != data.mHasStaticNormals ||
		        mHasStaticTangents != data.mHasStaticTangents ||
		        mHasStaticBinormals != data.mHasStaticBinormals ||
		        mHasStaticColors != data.mHasStaticColors ||
		        mHasStaticSeparateBoneBuffer != data.mHasStaticSeparateBoneBuffer ||
		        mHasStaticDisplacements != data.mHasStaticDisplacements)
		{
			return false;
		}
		if (mHasDynamicPositions != data.mHasDynamicPositions ||
		        mHasDynamicNormals != data.mHasDynamicNormals ||
		        mHasDynamicTangents != data.mHasDynamicTangents ||
		        mHasDynamicBinormals != data.mHasDynamicBinormals ||
		        mHasDynamicColors != data.mHasDynamicColors ||
		        mHasDynamicSeparateBoneBuffer != data.mHasDynamicSeparateBoneBuffer ||
		        mHasDynamicDisplacements != data.mHasDynamicDisplacements)
		{
			return false;
		}
		if (mUVCount != data.mUVCount)
		{
			return false;
		}
		return true;
	}

	/**
		Returns the logical complement of the == operator.
	*/
	bool	operator != (const ExplicitVertexFormat& data) const
	{
		return !(*this == data);
	}

	/**
		Creates a render-ready VertexFormat corresponding to this structure's member values.
	*/
	void	copyToVertexFormat(VertexFormat* format) const
	{
		format->reset();
		uint32_t bi;
		if (mHasStaticPositions)
		{
			bi = (uint32_t)format->addBuffer(format->getSemanticName(RenderVertexSemantic::POSITION));
			format->setBufferFormat(bi, RenderDataFormat::FLOAT3);
			format->setBufferAccess(bi, mHasDynamicPositions ? RenderDataAccess::DYNAMIC :  RenderDataAccess::STATIC);
		}
		if (mHasStaticNormals)
		{
			bi = (uint32_t)format->addBuffer(format->getSemanticName(RenderVertexSemantic::NORMAL));
			format->setBufferFormat(bi, RenderDataFormat::FLOAT3);
			format->setBufferAccess(bi, mHasDynamicNormals ? RenderDataAccess::DYNAMIC :  RenderDataAccess::STATIC);
		}
		if (mHasStaticTangents)
		{
			bi = (uint32_t)format->addBuffer(format->getSemanticName(RenderVertexSemantic::TANGENT));
			format->setBufferFormat(bi, RenderDataFormat::FLOAT3);
			format->setBufferAccess(bi, mHasDynamicTangents ? RenderDataAccess::DYNAMIC :  RenderDataAccess::STATIC);
		}
		if (mHasStaticBinormals)
		{
			bi = (uint32_t)format->addBuffer(format->getSemanticName(RenderVertexSemantic::BINORMAL));
			format->setBufferFormat(bi, RenderDataFormat::FLOAT3);
			format->setBufferAccess(bi, mHasDynamicBinormals ? RenderDataAccess::DYNAMIC :  RenderDataAccess::STATIC);
		}
		if (mHasStaticDisplacements)
		{
			bi = (uint32_t)format->addBuffer(format->getSemanticName(RenderVertexSemantic::DISPLACEMENT_TEXCOORD));
			format->setBufferFormat(bi, RenderDataFormat::FLOAT3);
			format->setBufferAccess(bi, mHasDynamicDisplacements ? RenderDataAccess::DYNAMIC : RenderDataAccess::STATIC);
			bi = (uint32_t)format->addBuffer(format->getSemanticName(RenderVertexSemantic::DISPLACEMENT_FLAGS));
			format->setBufferFormat(bi, RenderDataFormat::UINT1);
			format->setBufferAccess(bi, mHasDynamicDisplacements ? RenderDataAccess::DYNAMIC : RenderDataAccess::STATIC);
		}
		if (mUVCount > 0)
		{
			bi = (uint32_t)format->addBuffer(format->getSemanticName(RenderVertexSemantic::TEXCOORD0));
			format->setBufferFormat(bi, RenderDataFormat::FLOAT2);
		}
		if (mUVCount > 1)
		{
			bi = (uint32_t)format->addBuffer(format->getSemanticName(RenderVertexSemantic::TEXCOORD1));
			format->setBufferFormat(bi, RenderDataFormat::FLOAT2);
		}
		if (mUVCount > 2)
		{
			bi = (uint32_t)format->addBuffer(format->getSemanticName(RenderVertexSemantic::TEXCOORD2));
			format->setBufferFormat(bi, RenderDataFormat::FLOAT2);
		}
		if (mUVCount > 3)
		{
			bi = (uint32_t)format->addBuffer(format->getSemanticName(RenderVertexSemantic::TEXCOORD3));
			format->setBufferFormat(bi, RenderDataFormat::FLOAT2);
		}
		switch (mBonesPerVertex)
		{
		case 1:
			bi = (uint32_t)format->addBuffer(format->getSemanticName(RenderVertexSemantic::BONE_INDEX));
			format->setBufferFormat(bi, RenderDataFormat::USHORT1);
			break;
		case 2:
			bi = (uint32_t)format->addBuffer(format->getSemanticName(RenderVertexSemantic::BONE_INDEX));
			format->setBufferFormat(bi, RenderDataFormat::USHORT2);
			bi = (uint32_t)format->addBuffer(format->getSemanticName(RenderVertexSemantic::BONE_WEIGHT));
			format->setBufferFormat(bi, RenderDataFormat::FLOAT2);
			break;
		case 3:
			bi = (uint32_t)format->addBuffer(format->getSemanticName(RenderVertexSemantic::BONE_INDEX));
			format->setBufferFormat(bi, RenderDataFormat::USHORT3);
			bi = (uint32_t)format->addBuffer(format->getSemanticName(RenderVertexSemantic::BONE_WEIGHT));
			format->setBufferFormat(bi, RenderDataFormat::FLOAT3);
			break;
		case 4:
			bi = (uint32_t)format->addBuffer(format->getSemanticName(RenderVertexSemantic::BONE_INDEX));
			format->setBufferFormat(bi, RenderDataFormat::USHORT4);
			bi = (uint32_t)format->addBuffer(format->getSemanticName(RenderVertexSemantic::BONE_WEIGHT));
			format->setBufferFormat(bi, RenderDataFormat::FLOAT4);
			break;
		}

		format->setHasSeparateBoneBuffer(mHasStaticSeparateBoneBuffer);
		format->setWinding((RenderCullMode::Enum)mWinding);
	}
};


/**
	ExplicitSubmeshData

	This is used when authoring an RenderMeshAsset.

	This is the per-submesh data: the material name, and vertex format.
 */
struct ExplicitSubmeshData
{
	/**
		Enum for storing material name buffer size
	*/
	enum
	{
		MaterialNameBufferSize = 1024
	};

	/**
		Material name char buffer
	*/
	char					mMaterialName[MaterialNameBufferSize];
	
	/**
		Explicit vertex format
		\see ExplicitVertexFormat
	*/
	ExplicitVertexFormat	mVertexFormat; 

	/**
		Equal operator for ExplicitSubmeshData
	*/
	bool	operator == (const ExplicitSubmeshData& data) const
	{
		return !::strcmp(mMaterialName, data.mMaterialName) && mVertexFormat == data.mVertexFormat;
	}

	/**
		Not equal operator for ExplicitSubmeshData
	*/
	bool	operator != (const ExplicitSubmeshData& data) const
	{
		return !(*this == data);
	}
};


/**
	Collision volume descriptor for a chunk
*/
struct CollisionVolumeDesc
{
	CollisionVolumeDesc()
	{
		setToDefault();
	}

	/**
		Set CollisionVolumeDesc to default values
	*/
	void setToDefault()
	{
		mHullMethod = ConvexHullMethod::CONVEX_DECOMPOSITION;
		mConcavityPercent = 4.0f;
		mMergeThreshold = 4.0f;
		mRecursionDepth = 0;
		mMaxVertexCount = 0;
		mMaxEdgeCount = 0;
		mMaxFaceCount = 0;
	}

	/**
		How to generate convex hulls for a chunk.  See ConvexHullMethod::Enum.
		Default = CONVEX_DECOMPOSITION.
	*/
	ConvexHullMethod::Enum	mHullMethod;

	/**
		Allowed concavity if mHullMethod = ConvexHullMethod::CONVEX_DECOMPOSITION.
		Default = 4.0.
	*/
	float					mConcavityPercent;

	/**
		Merge threshold if mHullMethod = ConvexHullMethod::CONVEX_DECOMPOSITION.
		Default = 4.0.
	*/
	float					mMergeThreshold;
	
	/**
		Recursion depth if mHullMethod = ConvexHullMethod::CONVEX_DECOMPOSITION.
		Depth = 0 generates a single convex hull.  Higher recursion depths may generate
		more convex hulls to fit the mesh.
		Default = 0.
	*/
	uint32_t				mRecursionDepth;

	/**
		The maximum number of vertices each hull may have.  If 0, there is no limit.
		Default = 0.
	*/
	uint32_t				mMaxVertexCount;

	/**
		The maximum number of edges each hull may have.  If 0, there is no limit.
		Default = 0.
	*/
	uint32_t				mMaxEdgeCount;

	/**
		The maximum number of faces each hull may have.  If 0, there is no limit.
		Default = 0.
	*/
	uint32_t				mMaxFaceCount;
};


/**
	Collision descriptor
*/
struct CollisionDesc
{
	/**
		How many collision volume descriptors are in the mVolumeDescs array.

		This count need not match the depth count of the destructible to be created.  If it is greater than the depth count of the destructible,
		the extra volume descriptors will be ignored.  If it is less than the depth count of the destructible, then mVolumeDescs[depthCount-1] will
		be used unless depthCount is zero.  In that case, the defeult CollisionVolumeDesc() will be used.

		This may be zero, in which case all volume descriptors will be the default values.  If it is not zero, mVolumeDescs must be a valid pointer.
	*/
	unsigned				mDepthCount;

	/**
		Array of volume descriptors of length depthCount (may be NULL if depthCount is zero).
	*/
	CollisionVolumeDesc*	mVolumeDescs;

	/**
		The maximum amount to trim overlapping collision hulls (as a percentage of the hulls' widths)

		Default = 0.2f
	*/
	float					mMaximumTrimming;

	/** Constructor sets default values. */
	CollisionDesc()
	{
		setToDefault();
	}

	/**
		Set CollisionDesc to default values
	*/
	void setToDefault()
	{
		mDepthCount = 0;
		mVolumeDescs = NULL;
		mMaximumTrimming = 0.2f;
	}
};


/**
	Enumeration of current fracture methods.  Used when an authored mesh needs to know how it was created, for
	example if we need to re-apply UV mapping information.
*/
struct FractureMethod
{
	/**
		Enum of fracture methods
	*/
	enum Enum
	{
		Unknown,
		Slice,
		Cutout,
		Voronoi,

		FractureMethodCount
	};
};


/**
	FractureMaterialDesc

	Descriptor for materials applied to interior faces.
*/
struct FractureMaterialDesc
{
	/** The UV scale (geometric distance/unit texture distance) for interior materials.
	Default = (1.0f,1.0f).
	*/
	PxVec2	uvScale;

	/** A UV origin offset for interior materials.
	Default = (0.0f,0.0f).
	*/
	PxVec2	uvOffset;

	/** World space vector specifying surface tangent direction.  If this vector
	is (0.0f,0.0f,0.0f), then an arbitrary direction will be chosen.
	Default = (0.0f,0.0f,0.0f).
	*/
	PxVec3	tangent;

	/** Angle from tangent direction for the u coordinate axis.
	Default = 0.0f.
	*/
	float	uAngle;

	/**
		The submesh index to use for the newly-created triangles.
		If an invalid index is given, 0 will be used.
	*/
	uint32_t interiorSubmeshIndex;

	/** Constructor sets defaults */
	FractureMaterialDesc()
	{
		setToDefault();
	}

	/**
		Set default values:
		uvScale = PxVec2(1.0f);
		uvOffset = PxVec2(0.0f);
		tangent = PxVec3(0.0f);
		uAngle = 0.0f;
		interiorSubmeshIndex = 0;
	*/
	void	setToDefault()
	{
		uvScale = PxVec2(1.0f);
		uvOffset = PxVec2(0.0f);
		tangent = PxVec3(0.0f);
		uAngle = 0.0f;
		interiorSubmeshIndex = 0;
	}
};


/**
	A reference frame for applying UV mapping to triangles.  Also includes the fracturing method and an index
	which is used internally for such operations as re-applying UV mapping information.
*/
struct MaterialFrame
{
	MaterialFrame() :
		mCoordinateSystem(PxVec4(1.0f)),
		mUVPlane(PxVec3(0.0f, 0.0f, 1.0f), 0.0f),
		mUVScale(1.0f),
		mUVOffset(0.0f),
		mFractureMethod(FractureMethod::Unknown),
		mFractureIndex(-1),
		mSliceDepth(0)
	{		
	}

	/**
		Builds coordinate system from material desc
	*/
	void	buildCoordinateSystemFromMaterialDesc(const nvidia::FractureMaterialDesc& materialDesc, const PxPlane& plane)
			{
				PxVec3 zAxis = plane.n;
				zAxis.normalize();
				PxVec3 xAxis = materialDesc.tangent;
				PxVec3 yAxis = zAxis.cross(xAxis);
				const float l2 = yAxis.magnitudeSquared();
				if (l2 > PX_EPS_F32*PX_EPS_F32)
				{
					yAxis *= PxRecipSqrt(l2);
				}
				else
				{
					uint32_t maxDir =  PxAbs(plane.n.x) > PxAbs(plane.n.y) ?
						(PxAbs(plane.n.x) > PxAbs(plane.n.z) ? 0u : 2u) :
						(PxAbs(plane.n.y) > PxAbs(plane.n.z) ? 1u : 2u);
					xAxis = PxMat33(PxIdentity)[(maxDir + 1) % 3];
					yAxis = zAxis.cross(xAxis);
					yAxis.normalize();
				}
				xAxis = yAxis.cross(zAxis);

				const float c = PxCos(materialDesc.uAngle);
				const float s = PxSin(materialDesc.uAngle);

				mCoordinateSystem.column0 = PxVec4(c*xAxis + s*yAxis, 0.0f);
				mCoordinateSystem.column1 = PxVec4(c*yAxis - s*xAxis, 0.0f);
				mCoordinateSystem.column2 = PxVec4(zAxis, 0.0f);
				mCoordinateSystem.setPosition(plane.project(PxVec3(0.0f)));

				mUVPlane = plane;
				mUVScale = materialDesc.uvScale;
				mUVOffset = materialDesc.uvOffset;
			}

	PxMat44	mCoordinateSystem; ///< Coordinate system
	PxPlane	mUVPlane; ///< UV plane
	PxVec2	mUVScale; ///< UV scale
	PxVec2	mUVOffset; ///< UV offset
	uint32_t	mFractureMethod; ///< Fracture method
	int32_t	mFractureIndex; ///< Fracture index
	uint32_t	mSliceDepth;	///< The depth being created when this split is done.  mSliceDepth = 0 means "unknown"
};


/**
	Interface to a "displacement map volume," used with tessellated fracturing.
	A displacement map volume captures how to displace a particular point in 3D space
	along the x, y and z axes.  The data is stored as a 3D texture volume, with
	corresponding displacement coordinates acting as a look-up into this volume.
	X, Y and Z offsets correspond to R, G, and B color channels

	Various approaches can be used to generate the 3D noise field, in this case
	Perlin noise is used, with appropriate settings specified by the FractureSliceDesc.
*/
class DisplacementMapVolume
{
public:
	/** 
		Fills the specified array and parameters with texture-compatible information. 

		The corresponding texture aligns with the displacement UVs generated as fracturing occurs
		when displacement maps are enabled, with RGB data corresponding to XYZ offsets, respectively.
	*/
	virtual void getData(uint32_t& width, uint32_t& height, uint32_t& depth, uint32_t& size, unsigned char const** ppData) const = 0;

	virtual ~DisplacementMapVolume() { }
};


/**
	Handling of open meshes.

	BSPMode::Automatic causes the mesh to be analyzed to determine if it's open or closed
	BSPOpenMode::Closed means the mesh should be closed and interior faces are created when the mesh is split
	BSPOpenMode::Open means the mesh should be open and no interior faces are created when it is split
*/
struct BSPOpenMode
{
	/**
		Enum of BSP open modes
	*/
	enum Enum
	{
		Automatic,
		Closed,
		Open,

		BSPOpenModeCount
	};
};


/**
	ExplicitHierarchicalMesh

	An interface to a representation of a render mesh that is used for authoring.

	The "hierarchical" nature of this mesh is represented by extra parent/child relations
	among the parts that will become the parts of an RenderMeshAsset.
 */
class ExplicitHierarchicalMesh
{
public:
	/** Used in the visualize method to determine what to draw. */
	enum Enum
	{
		/**
			Draws the convex hulls associated with the BSP regions marked "outside," if a BSP has
			been created for this object (see calculateMeshBSP()).
		*/
		VisualizeMeshBSPOutsideRegions	= (1 << 0),

		/**
			Draws the convex hulls associated with the BSP regions marked "inside," if a BSP has
			been created for this object (see calculateMeshBSP()).
		*/
		VisualizeMeshBSPInsideRegions	= (1 << 1),

		/**
			Draws the convex hull associated with a single BSP regions, if a BSP has
			been created for this object (see calculateMeshBSP()).  The region index must
			be passed into the visualize function if this flag is set.
		*/
		VisualizeMeshBSPSingleRegion	= (1 << 8),

		/** Currently unused */
		VisualizeSliceBSPOutsideRegions	= (1 << 16),

		/** Currently unused */
		VisualizeSliceBSPInsideRegions	= (1 << 17),

		/** Currently unused */
		VisualizeSliceBSPSingleRegion	= (1 << 24),

		VisualizeMeshBSPAllRegions		= VisualizeMeshBSPOutsideRegions | VisualizeMeshBSPInsideRegions
	};

	/**
		Used in the serialize and deserialize methods, to embed extra data.
		The user must implement this class to serialize and deserialize
		the enumerated data type given.
	*/
	class Embedding
	{
	public:
		/**
			Enum of data types
		*/
		enum DataType
		{
			MaterialLibrary
		};
		
		/**
			Serialize the enumerated data type
		*/
		virtual void	serialize(PxFileBuf& stream, Embedding::DataType type) const = 0;
		
		/**
			Deserialize the enumerated data type
		*/
		virtual void	deserialize(PxFileBuf& stream, Embedding::DataType type, uint32_t version) = 0;
	};

	/**
		Used to access the collision data for each mesh part
	*/
	class ConvexHull
	{
	protected:
		ConvexHull()
		{
		}

		virtual ~ConvexHull()
		{
		}

	public:
		/**
			Builds the convex hull of the points given.
		*/
		virtual void					buildFromPoints(const void* points, uint32_t numPoints, uint32_t pointStrideBytes) = 0;

		/**
			The hull's axis aligned bounding box.
		*/
		virtual const PxBounds3&		getBounds() const = 0;

		/**
			The hull's volume.
		*/
		virtual float					getVolume() const = 0;

		/**
			This is the number of vertices in the convex hull.
		*/
		virtual uint32_t				getVertexCount() const = 0;

		/**
			This is the vertex indexed by vertexIndex, which must in
			the range [0, getVertexCount()-1].
		*/
		virtual PxVec3					getVertex(uint32_t vertexIndex) const = 0;

		/**
			This is the number of edges in the convex hull.
		*/
		virtual uint32_t				getEdgeCount() const = 0;

		/**
			This is an edge endpoint indexed by edgeIndex, which must in
			the range [0, getEdgeCount()-1], and
			whichEndpoint, which must be 0 or 1.
		*/
		virtual PxVec3					getEdgeEndpoint(uint32_t edgeIndex, uint32_t whichEndpoint) const = 0;

		/**
			This is the number of planes which bound the convex hull.
		*/
		virtual uint32_t				getPlaneCount() const = 0;

		/**
			This is the plane indexed by planeIndex, which must in
			the range [0, getPlaneCount()-1].
		*/
		virtual PxPlane					getPlane(uint32_t planeIndex) const = 0;

		/**
			Perform a ray cast against the convex hull.

			\param in				this MUST be set to the minimum 'time' that you wish to have reported for intersection.
										you may consider this an origin offset for the ray.
										On exit, if the hull is intersected, this value will contain the time of intersection,
										or its original value, which ever is larger.

			\param out				this MUST be set to the maximum 'time' that you wish to have reported for intersection.
										you may consider this the endpoint of a line segment intersection.
										On exit, if the hull is intersected, this value will contain the time that the ray
										exits the hull, or its original value, which ever is smaller.

			\param orig				describe the ray to intersect with the convex hull.
			\param dir				describe the ray to intersect with the convex hull.

			\param localToWorldRT	the rotation applied to the convex hull.

			\param scale			the scale applied to the convex hull.

			\param normal			if not NULL, *normal will contain the surface normal of the convex hull at the
										point of intersection (at the 'in' time).  If the point on the ray at the 'in' time lies
										within the volume of the convex hull, then *normal will be set to (0,0,0).

			\return					returns true if the line segment described by the user's supplied 'in' and 'out'
										parameters along the ray intersects the convex hull, false otherwise.
		*/
		virtual bool					rayCast(float& in, float& out, const PxVec3& orig, const PxVec3& dir,
		                                        const PxTransform& localToWorldRT, const PxVec3& scale, PxVec3* normal = NULL) const = 0;
		/**
			Removes vertices from the hull until the bounds given in the function's parameters are met.
			If inflated = true, then the maximum counts given are compared with the cooked hull, which may have higher counts due to beveling.
			
			\note		a value of zero indicates no limit, effectively infinite.
			
			\return		true if successful, i.e. the limits were met.  False otherwise.
		*/
		virtual bool					reduceHull(uint32_t maxVertexCount, uint32_t maxEdgeCount, uint32_t maxFaceCount, bool inflated) = 0;

		/**
			Releases all memory associated with this object and deletes itself.
		*/
		virtual void					release() = 0;
	};

	/**
		"Resets" this object to its initial state, freeing all internal data.
		If keepRoot is true, then parts up to the root depth will not be cleared.
		(In this case, not all of the submesh data, etc. will be deleted.)
		The root depth is set when the ExplicitHierarchicalMesh is first created.
		Fracturing methods create pieces beyond the root depth.
	*/
	virtual void						clear(bool keepRoot = false) = 0;

	/**
		The maximum child depth in the hierarchy.  Depth 0 is the base, depth 1 parts are children of depth 0, etc.
		If there are no parts, this function returns -1.
	*/
	virtual int32_t						maxDepth() const = 0;

	/**
		The number of parts in this mesh.
	*/
	virtual uint32_t					partCount() const = 0;

	/**
		The number of chunks in this mesh.
	*/
	virtual uint32_t					chunkCount() const = 0;

	/**
		The parent index of the chunk indexed by chunkIndex.
		Depth 0 parts have no parents, and for those parts this function returns -1.
	*/
	virtual int32_t*					parentIndex(uint32_t chunkIndex) = 0;

	/**
		A runtime unique identifier for a chunk.  During one execution of an application which
		contains the fracture tools, this chunk ID will be unique for the chunk.
	*/
	virtual uint64_t					chunkUniqueID(uint32_t chunkIndex) = 0;

	/**
		The geometric part index this chunk references
	*/
	virtual int32_t*					partIndex(uint32_t chunkIndex) = 0;

	/**
		If instanced, the part instance offset (translation).
	*/
	virtual PxVec3*						instancedPositionOffset(uint32_t chunkIndex) = 0;

	/**
		If instanced, the part instance offset (UV).
	*/
	virtual PxVec2*						instancedUVOffset(uint32_t chunkIndex) = 0;

	/**
		The number of triangles in the part indexed by partIndex.
		This includes all submeshes.
	*/
	virtual uint32_t					meshTriangleCount(uint32_t partIndex) const = 0;

	/**
		A pointer into the array of ExplicitRenderTriangles which form the mesh
		of the part indexed by partIndex.
	*/
	virtual ExplicitRenderTriangle*		meshTriangles(uint32_t partIndex) = 0;

	/**
		The axis aligned bounding box of the triangles for the part index by partIndex.
	*/
	virtual PxBounds3					meshBounds(uint32_t partIndex) const = 0;

	/**
		The axis aligned bounding box of the triangles for the chunk index by chunkIndex.
	*/
	virtual PxBounds3					chunkBounds(uint32_t chunkIndex) const = 0;

	/**
		Flags describing attributes of the part indexed by partIndex.
		See DestructibleAsset::ChunkFlags
	*/
	virtual uint32_t*					chunkFlags(uint32_t chunkIndex) const = 0;

	/**
		Build collision volumes for the part indexed by partIndex, using (See CollisionVolumeDesc.)
	*/
	virtual void						buildCollisionGeometryForPart(uint32_t partIndex, const CollisionVolumeDesc& desc) = 0;

	/**
		Build collision volumes for all parts referenced by chunks at the root depth.

		If aggregateRootChunkParentCollision, then every chunk which is the parent of root chunks
		gets all of the collision hulls of its children.  Otherwise, all root chunks have their
		collision volumes separately calculated.
	*/
	virtual void						buildCollisionGeometryForRootChunkParts(const CollisionDesc& desc, bool aggregateRootChunkParentCollision = true) = 0;

	/**
		Calls IConvexMesh::reduceHull on all part convex hulls.  See IConvexMesh::reduceHull.
	*/
	virtual void						reduceHulls(const CollisionDesc& desc, bool inflated) = 0;

	/**
		The number of convex hulls for the given part.
	*/
	virtual uint32_t					convexHullCount(uint32_t partIndex) const = 0;

	/**
		The convex hulls for the given part.
	*/
	virtual const ConvexHull**			convexHulls(uint32_t partIndex) const = 0;

	/**
		The outward surface normal associated with the chunk mesh geometry.
	*/
	virtual PxVec3*						surfaceNormal(uint32_t partIndex) = 0;

	/**
		The displacement map volume for the mesh.
	*/
	virtual const DisplacementMapVolume&	displacementMapVolume() const = 0;

	/**
		The number of submeshes.  The explicit mesh representation is just a list
		of ExplicitRenderTriangles for each part, and each ExplicitRenderTriangle has
		a submesh index.  These indices will lie in a contiguous range from 0 to submeshCount()-1.
	*/
	virtual uint32_t					submeshCount() const = 0;

	/**
		The submeshData indexed by submeshIndex.  See ExplicitSubmeshData.
	*/
	virtual ExplicitSubmeshData*		submeshData(uint32_t submeshIndex) = 0;

	/**
		Creates a submesh and adds it to the list of submeshes, and returns the index of
		the newly created submesh.
	*/
	virtual uint32_t					addSubmesh(const ExplicitSubmeshData& submeshData) = 0;

	/**
		If there are interior submeshes, then triangles belonging
		to those submesh will have materials applied to them with a using a coordinate frame.
		In the event that materials need to be re-applied (possibly at a different scale), it
		is convenient to store the material frames used.  This function returns the array of
		material frames.  The index extraDataIndex stored in each ExplicitRenderTriangle
		references this array.
	*/
	virtual uint32_t					getMaterialFrameCount() const = 0;
	virtual nvidia::MaterialFrame		getMaterialFrame(uint32_t index) const = 0; ///< \see ConvexHull::getMaterialFrameCount
	virtual void						setMaterialFrame(uint32_t index, const nvidia::MaterialFrame& materialFrame) = 0; ///< \see ConvexHull::getMaterialFrameCount
	virtual uint32_t					addMaterialFrame() = 0; ///< \see ConvexHull::getMaterialFrameCount

	/**
		Serialization.  The user must instantiate Embedding in order to successfully
		serialize any embedded data.
	*/
	virtual void						serialize(PxFileBuf& stream, Embedding& embedding) const = 0;
	
	/**
		Serialization.  The user must instantiate Embedding in order to successfully
		serialize any embedded data.
	*/
	virtual void						deserialize(PxFileBuf& stream, Embedding& embedding) = 0;

	/**
		Copies the input mesh in to this object.
	*/
	virtual void						set(const ExplicitHierarchicalMesh& mesh) = 0;

	/**
		Creates an internal BSP representation of the mesh parts up to the root depth.
		This is used by authoring tools to perform CSG operations.  If the user instantiates
		IProgressListener, they may pass it in to report progress of this operation.
		If microgridSize is not NULL, *microgridSize is used in the BSP calculation.  Otherwise the
		default parameters are used.
		meshMode is used to determine if the mesh is open or closed.  See NxMeshProcessingParameters::MeshMode
	*/
	virtual void						calculateMeshBSP(uint32_t randomSeed, IProgressListener* progressListener = NULL, const uint32_t* microgridSize = NULL, BSPOpenMode::Enum meshMode = BSPOpenMode::Automatic) = 0;

	/**
		Utility to replace the submesh on a set of interior triangles.
	*/
	virtual void						replaceInteriorSubmeshes(uint32_t partIndex, uint32_t frameCount, uint32_t* frameIndices, uint32_t submeshIndex) = 0;

	/**
		Draws various components of this object to the debugRenderer, as
		defined by the flags (see the visualization Enum above).  Some
		of the flags require an index be passed in as well.
	*/
	virtual void						visualize(RenderDebugInterface& debugRender, uint32_t flags, uint32_t index = 0) const = 0;

	/**
		Releases all memory associated with this object and deletes itself.
	*/
	virtual void						release() = 0;

protected:
	/**
		Constructor and destructor are not public
		Use createExplicitHierarchicalMesh() to instantiate an ExplicitHierarchicalMesh and
		ExplicitHierarchicalMesh::release() to destroy it.
	*/
	ExplicitHierarchicalMesh() {}
	virtual								~ExplicitHierarchicalMesh() {}

private:
	/** The assignment operator is disabled, use set() instead. */
	ExplicitHierarchicalMesh&			operator = (const ExplicitHierarchicalMesh&)
	{
		return *this;
	}
};


PX_POP_PACK

}
} // end namespace nvidia


#endif // EXPLICIT_HIERARCHICAL_MESH_H