-
Notifications
You must be signed in to change notification settings - Fork 0
/
physics-base.cc
127 lines (104 loc) · 4.19 KB
/
physics-base.cc
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
// #include "DirectXMath.h"
/* #include "PhysX/physx/include/geometry/PxMeshQuery.h"
#include "geometry/PxCapsuleGeometry.h"
#include "foundation/PxTransform.h"
*/
#include "physics.h"
#include <string>
#include <iostream>
#include "geometry/PxHeightFieldSample.h"
#include "geometry/PxHeightFieldDesc.h"
#include "geometry/PxHeightFieldGeometry.h"
//
using namespace physx;
//
PBase::PBase() {
allocator = new PxDefaultAllocator();
errorCallback = new PxDefaultErrorCallback();
foundation = PxCreateFoundation(PX_PHYSICS_VERSION, *allocator, *errorCallback);
{
PxCookingParams cookingParams(tolerancesScale);
cookingParams.meshWeldTolerance = 0.15;
// cookingParams.planeTolerance = 0;
cookingParams.meshPreprocessParams |= PxMeshPreprocessingFlag::eDISABLE_CLEAN_MESH;
cookingParams.meshPreprocessParams |= PxMeshPreprocessingFlag::eWELD_VERTICES;
// cookingParams.meshSizePerformanceTradeOff = 0;
cooking = PxCreateCooking(PX_PHYSICS_VERSION, *foundation, cookingParams);
}
}
PBase::~PBase() {}
//
void PBase::cookGeometry(float *positions, unsigned int *indices, unsigned int numPositions, unsigned int numIndices, uint8_t **data, unsigned int *length, PxDefaultMemoryOutputStream **writeStream) {
PxVec3 *verts = (PxVec3 *)positions;
PxU32 nbVerts = numPositions/3;
PxU32 *indices32 = (PxU32 *)indices;
PxU32 triCount = numIndices/3;
PxTriangleMeshDesc meshDesc{};
meshDesc.points.count = nbVerts;
meshDesc.points.stride = sizeof(PxVec3);
meshDesc.points.data = verts;
meshDesc.triangles.count = triCount;
meshDesc.triangles.stride = 3*sizeof(PxU32);
meshDesc.triangles.data = indices32;
/* bool ok = cooking->validateTriangleMesh(meshDesc);
if (!ok) {
std::cerr << "invalid triangle mesh" << std::endl;
} */
*writeStream = new PxDefaultMemoryOutputStream();
bool status = cooking->cookTriangleMesh(meshDesc, **writeStream);
if (!status) {
std::cerr << "geometry triangle mesh bake failed" << std::endl;
}
*data = (*writeStream)->getData();
*length = (*writeStream)->getSize();
}
void PBase::cookConvexGeometry(float *positions, unsigned int *indices, unsigned int numPositions, unsigned int numIndices, uint8_t **data, unsigned int *length, PxDefaultMemoryOutputStream **writeStream) {
PxVec3 *verts = (PxVec3 *)positions;
PxU32 nbVerts = numPositions/3;
PxU32 *indices32 = (PxU32 *)indices;
PxU32 triCount = numIndices/3;
PxConvexMeshDesc meshDesc{};
meshDesc.points.count = nbVerts;
meshDesc.points.stride = sizeof(PxVec3);
meshDesc.points.data = verts;
meshDesc.indices.count = triCount;
meshDesc.indices.stride = 3*sizeof(PxU32);
meshDesc.indices.data = indices32;
meshDesc.flags = PxConvexFlag::eCOMPUTE_CONVEX;
// meshDesc.maxVerts = 10;
*writeStream = new PxDefaultMemoryOutputStream();
bool status = cooking->cookConvexMesh(meshDesc, **writeStream);
if (!status) {
std::cerr << "geometry convex mesh bake failed" << std::endl;
}
*data = (*writeStream)->getData();
*length = (*writeStream)->getSize();
}
void PBase::cookHeightFieldGeometry(unsigned int numRows, unsigned int numColumns, int16_t *scratchStack, uint8_t **data, unsigned int *length, PxDefaultMemoryOutputStream **writeStream) {
PxU32 hfNumVerts = numRows * numColumns;
PxHeightFieldSample* samples = new PxHeightFieldSample[hfNumVerts];
memset(samples,0,hfNumVerts*sizeof(PxHeightFieldSample));
for(PxU32 z = 0; z < numColumns; z++)
{
for(PxU32 x = 0; x < numRows; x++)
{
const PxU32 Index = x + z * numRows;
const PxI16 height = scratchStack[Index];
samples[Index].height = height;
}
}
PxHeightFieldDesc hfDesc{};
hfDesc.nbRows = numRows; // rows: x axis
hfDesc.nbColumns = numColumns; // columns: z axis
hfDesc.samples.data = samples;
hfDesc.samples.stride = sizeof(PxHeightFieldSample);
*writeStream = new PxDefaultMemoryOutputStream();
bool status = cooking->cookHeightField(hfDesc, **writeStream);
if (!status) {
std::cerr << "geometry heightfield bake failed" << std::endl;
}
*data = (*writeStream)->getData();
*length = (*writeStream)->getSize();
}
//
PBase *physicsBase = nullptr;