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
|
// This code contains NVIDIA Confidential Information and is disclosed to you
// under a form of NVIDIA software license agreement provided separately to you.
//
// Notice
// NVIDIA Corporation and its licensors retain all intellectual property and
// proprietary rights in and to this software and related documentation and
// any modifications thereto. Any use, reproduction, disclosure, or
// distribution of this software and related documentation without an express
// license agreement from NVIDIA Corporation is strictly prohibited.
//
// ALL NVIDIA DESIGN SPECIFICATIONS, CODE ARE PROVIDED "AS IS.". NVIDIA MAKES
// NO WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
// THE MATERIALS, AND EXPRESSLY DISCLAIMS ALL IMPLIED WARRANTIES OF NONINFRINGEMENT,
// MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE.
//
// Information and code furnished is believed to be accurate and reliable.
// However, NVIDIA Corporation assumes no responsibility for the consequences of use of such
// information or for any infringement of patents or other rights of third parties that may
// result from its use. No license is granted by implication or otherwise under any patent
// or patent rights of NVIDIA Corporation. Details are subject to change without notice.
// This code supersedes and replaces all information previously supplied.
// NVIDIA Corporation products are not authorized for use as critical
// components in life support devices or systems without express written approval of
// NVIDIA Corporation.
//
// Copyright (c) 2008-2016 NVIDIA Corporation. All rights reserved.
// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
#include "GuDebug.h"
#include "GuHeightFieldUtil.h"
#include "GuTriangleMesh.h"
#include "GuConvexMesh.h"
#include "PxVisualizationParameter.h"
#include "PxBoxGeometry.h"
#include "PxSphereGeometry.h"
#include "PxPlaneGeometry.h"
#include "PxCapsuleGeometry.h"
#include "PxConvexMeshGeometry.h"
#include "PxGeometryQuery.h"
#include "PxMeshQuery.h"
using namespace physx;
using namespace Gu;
#if PX_ENABLE_DEBUG_VISUALIZATION
static void visualizeSphere(const PxSphereGeometry& geometry, Cm::RenderOutput& out, const PxTransform& absPose)
{
const PxU32 scolor = PxU32(PxDebugColor::eARGB_MAGENTA);
out << scolor << absPose << Cm::DebugCircle(100, geometry.radius);
PxMat44 rotPose(absPose);
Ps::swap(rotPose.column1, rotPose.column2);
rotPose.column1 = -rotPose.column1;
out << scolor << rotPose << Cm::DebugCircle(100, geometry.radius);
Ps::swap(rotPose.column0, rotPose.column2);
rotPose.column0 = -rotPose.column0;
out << scolor << rotPose << Cm::DebugCircle(100, geometry.radius);
}
static void visualizePlane(const PxPlaneGeometry& /*geometry*/, Cm::RenderOutput& out, const PxTransform& absPose)
{
const PxU32 scolor = PxU32(PxDebugColor::eARGB_MAGENTA);
PxMat44 rotPose(absPose);
Ps::swap(rotPose.column1, rotPose.column2);
rotPose.column1 = -rotPose.column1;
Ps::swap(rotPose.column0, rotPose.column2);
rotPose.column0 = -rotPose.column0;
for(PxReal radius = 2.0f; radius < 20.0f ; radius += 2.0f)
out << scolor << rotPose << Cm::DebugCircle(100, radius*radius);
}
static void visualizeCapsule(const PxCapsuleGeometry& geometry, Cm::RenderOutput& out, const PxTransform& absPose)
{
out << PxU32(PxDebugColor::eARGB_MAGENTA);
out.outputCapsule(geometry.radius, geometry.halfHeight, absPose);
}
static void visualizeBox(const PxBoxGeometry& geometry, Cm::RenderOutput& out, const PxTransform& absPose)
{
out << PxU32(PxDebugColor::eARGB_MAGENTA);
out << absPose << Cm::DebugBox(geometry.halfExtents);
}
static void visualizeConvexMesh(const PxConvexMeshGeometry& geometry, Cm::RenderOutput& out, const PxTransform& absPose)
{
(static_cast<const ConvexMesh*>(geometry.convexMesh))->debugVisualize(out, absPose, geometry.scale);
}
static void visualizeTriangleMesh(const PxTriangleMeshGeometry& geometry, Cm::RenderOutput& out, const PxTransform& absPose,
const PxBounds3& cullbox, const PxU64 mask, const PxReal fscale, const PxU32 numMaterials)
{
(static_cast<const TriangleMesh*>(geometry.triangleMesh))->debugVisualize(out, absPose, geometry.scale, cullbox, mask, fscale, numMaterials);
}
static void visualizeHeightField(const PxHeightFieldGeometry& hfGeometry,
Cm::RenderOutput& out, const PxTransform& absPose, const PxBounds3& cullbox,
const PxU64 mask)
{
const HeightField* heightfield = static_cast<const HeightField*>(hfGeometry.heightField);
const bool cscale = !!(mask & (PxU64(1) << PxVisualizationParameter::eCULL_BOX));
const PxDebugColor::Enum colors[] =
{
PxDebugColor::eARGB_BLACK,
PxDebugColor::eARGB_RED,
PxDebugColor::eARGB_GREEN,
PxDebugColor::eARGB_BLUE,
PxDebugColor::eARGB_YELLOW,
PxDebugColor::eARGB_MAGENTA,
PxDebugColor::eARGB_CYAN,
PxDebugColor::eARGB_WHITE,
PxDebugColor::eARGB_GREY,
PxDebugColor::eARGB_DARKRED,
PxDebugColor::eARGB_DARKGREEN,
PxDebugColor::eARGB_DARKBLUE,
};
const PxU32 colorCount = sizeof(colors)/sizeof(PxDebugColor::Enum);
if (mask & (PxU64(1) << PxVisualizationParameter::eCOLLISION_SHAPES))
{
// PT: TODO: the debug viz for HFs is minimal at the moment...
PxU32 scolor = PxU32(PxDebugColor::eARGB_YELLOW);
const PxMat44 midt = PxMat44(PxIdentity);
HeightFieldUtil hfUtil(hfGeometry);
const PxU32 nbRows = heightfield->getNbRowsFast();
const PxU32 nbColumns = heightfield->getNbColumnsFast();
const PxU32 nbVerts = nbRows * nbColumns;
const PxU32 nbTriangles = 2 * nbVerts;
out << midt << scolor; // PT: no need to output the same matrix/color for each triangle
if(cscale)
{
const PxTransform pose0((cullbox.maximum + cullbox.minimum)*0.5f);
const PxBoxGeometry boxGeometry((cullbox.maximum - cullbox.minimum)*0.5f);
const PxTransform pose1(absPose);
PxU32* results = reinterpret_cast<PxU32*>(PX_ALLOC(sizeof(PxU32)*nbTriangles, "tmp triangle indices"));
bool overflow = false;
PxU32 nbTouchedTris = PxMeshQuery::findOverlapHeightField(boxGeometry, pose0, hfGeometry, pose1, results, nbTriangles, 0, overflow);
PxDebugLine* segments = out.reserveSegments(nbTouchedTris*3);
for (PxU32 i=0; i<nbTouchedTris; i++)
{
const PxU32 index= results[i];
PxTriangle currentTriangle;
PxMeshQuery::getTriangle(hfGeometry, pose1, index, currentTriangle);
//The check has been done in the findOverlapHeightField
//if(heightfield->isValidTriangle(index) && heightfield->getTriangleMaterial(index) != PxHeightFieldMaterial::eHOLE)
{
const PxU16 localMaterialIndex = heightfield->getTriangleMaterialIndex(index);
scolor = colors[localMaterialIndex % colorCount];
segments[0] = PxDebugLine(currentTriangle.verts[0], currentTriangle.verts[1], scolor);
segments[1] = PxDebugLine(currentTriangle.verts[1], currentTriangle.verts[2], scolor);
segments[2] = PxDebugLine(currentTriangle.verts[2], currentTriangle.verts[0], scolor);
segments+=3;
}
}
PX_FREE(results);
}
else
{
// PT: transform vertices only once
PxVec3* tmpVerts = reinterpret_cast<PxVec3*>(PX_ALLOC(sizeof(PxVec3)*nbVerts, "PxVec3"));
// PT: TODO: optimize the following line
for(PxU32 i=0;i<nbVerts;i++)
tmpVerts[i] = absPose.transform(hfUtil.hf2shapep(heightfield->getVertex(i)));
for(PxU32 i=0; i<nbTriangles; i++)
{
// PT: TODO: optimize away the useless divisions/modulos in the lines below
if(heightfield->isValidTriangle(i) && heightfield->getTriangleMaterial(i) != PxHeightFieldMaterial::eHOLE)
{
PxU32 vi0, vi1, vi2;
heightfield->getTriangleVertexIndices(i, vi0, vi1, vi2);
const PxU16 localMaterialIndex = heightfield->getTriangleMaterialIndex(i);
out << colors[localMaterialIndex % colorCount];
const PxVec3& vw0 = tmpVerts[vi0];
const PxVec3& vw1 = tmpVerts[vi1];
const PxVec3& vw2 = tmpVerts[vi2];
out.outputSegment(vw0, vw1);
out.outputSegment(vw1, vw2);
out.outputSegment(vw2, vw0);
}
}
PX_FREE(tmpVerts);
}
}
}
namespace physx
{
namespace Gu
{
void Debug::visualize(const PxGeometry& geometry,
Cm::RenderOutput& out,
const PxTransform& absPose,
const PxBounds3& cullbox,
const PxU64 mask,
const PxReal fscale,
const PxU32 numMaterials)
{
const bool cull((mask & (PxU64(1) << PxVisualizationParameter::eCULL_BOX)) != 0);
const bool collisionShapes((mask & (PxU64(1) << PxVisualizationParameter::eCOLLISION_SHAPES)) != 0);
if(cull && !cullbox.intersects(PxGeometryQuery::getWorldBounds(geometry, absPose, 0.0f)))
return;
// triangle meshes can render active edges, but for other types we can just early out if there are no collision shapes
if(!collisionShapes && geometry.getType() != PxGeometryType::eTRIANGLEMESH)
return;
switch(geometry.getType())
{
case PxGeometryType::eSPHERE:
visualizeSphere(static_cast<const PxSphereGeometry&>(geometry), out, absPose);
break;
case PxGeometryType::eBOX:
visualizeBox(static_cast<const PxBoxGeometry&>(geometry), out, absPose);
break;
case PxGeometryType::ePLANE:
visualizePlane(static_cast<const PxPlaneGeometry&>(geometry), out, absPose);
break;
case PxGeometryType::eCAPSULE:
visualizeCapsule(static_cast<const PxCapsuleGeometry&>(geometry), out, absPose);
break;
case PxGeometryType::eCONVEXMESH:
visualizeConvexMesh(static_cast<const PxConvexMeshGeometry&>(geometry), out, absPose);
break;
case PxGeometryType::eTRIANGLEMESH:
visualizeTriangleMesh(static_cast<const PxTriangleMeshGeometry&>(geometry), out, absPose, cullbox, mask, fscale, numMaterials);
break;
case PxGeometryType::eHEIGHTFIELD:
visualizeHeightField(static_cast<const PxHeightFieldGeometry&>(geometry), out, absPose, cullbox, mask);
break;
case PxGeometryType::eINVALID:
break;
case PxGeometryType::eGEOMETRY_COUNT:
break;
}
}
}
}
#endif
|