-
Notifications
You must be signed in to change notification settings - Fork 142
/
gkPhysXStream.cpp
198 lines (172 loc) · 4.29 KB
/
gkPhysXStream.cpp
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
#include "StableHeader.h"
#include "gkPhysXStream.h"
///////////////////////////////////////////////////////////////////////////////
MemoryOutputStream::MemoryOutputStream() :
mData (NULL),
mSize (0),
mCapacity (0)
{
}
MemoryOutputStream::~MemoryOutputStream()
{
if(mData)
delete[] mData;
}
PxU32 MemoryOutputStream::write(const void* src, PxU32 size)
{
PxU32 expectedSize = mSize + size;
if(expectedSize > mCapacity)
{
mCapacity = expectedSize + 4096;
PxU8* newData = new PxU8[mCapacity];
PX_ASSERT(newData!=NULL);
if(newData)
{
memcpy(newData, mData, mSize);
delete[] mData;
}
mData = newData;
}
memcpy(mData+mSize, src, size);
mSize += size;
return size;
}
///////////////////////////////////////////////////////////////////////////////
MemoryInputData::MemoryInputData(PxU8* data, PxU32 length) :
mSize (length),
mData (data),
mPos (0)
{
}
PxU32 MemoryInputData::read(void* dest, PxU32 count)
{
PxU32 length = PxMin<PxU32>(count, mSize-mPos);
memcpy(dest, mData+mPos, length);
mPos += length;
return length;
}
PxU32 MemoryInputData::getLength() const
{
return mSize;
}
void MemoryInputData::seek(PxU32 offset)
{
mPos = PxMin<PxU32>(mSize, offset);
}
PxU32 MemoryInputData::tell() const
{
return mPos;
}
///////////////////////////////////////////////////////////////////////////////
FileOutputStream::FileOutputStream(const char* filename)
{
mFile = NULL;
fopen_s(&mFile, filename, "wb");
}
FileOutputStream::~FileOutputStream()
{
if(mFile)
fclose(mFile);
}
PxU32 FileOutputStream::write(const void* src, PxU32 count)
{
return mFile ? (PxU32)fwrite(src, 1, count, mFile) : 0;
}
bool FileOutputStream::isValid()
{
return mFile != NULL;
}
///////////////////////////////////////////////////////////////////////////////
FileInputData::FileInputData(const char* filename)
{
mFile = NULL;
fopen_s(&mFile, filename, "rb");
if(mFile)
{
fseek(mFile, 0, SEEK_END);
mLength = ftell(mFile);
fseek(mFile, 0, SEEK_SET);
}
else
{
mLength = 0;
}
}
FileInputData::~FileInputData()
{
if(mFile)
fclose(mFile);
}
PxU32 FileInputData::read(void* dest, PxU32 count)
{
PX_ASSERT(mFile);
const size_t size = fread(dest, 1, count, mFile);
PX_ASSERT(PxU32(size)==count);
return PxU32(size);
}
PxU32 FileInputData::getLength() const
{
return mLength;
}
void FileInputData::seek(PxU32 pos)
{
fseek(mFile, pos, SEEK_SET);
}
PxU32 FileInputData::tell() const
{
return ftell(mFile);
}
bool FileInputData::isValid() const
{
return mFile != NULL;
}
//
// ///////////////////////////////////////////////////////////////////////////////
//
// PxTriangleMesh* PxToolkit::createTriangleMesh32(PxPhysics& physics, PxCooking& cooking, const PxVec3* verts, PxU32 vertCount, const PxU32* indices32, PxU32 triCount)
// {
// PxTriangleMeshDesc meshDesc;
// meshDesc.points.count = vertCount;
// meshDesc.points.stride = sizeof(PxVec3);
// meshDesc.points.data = verts;
//
// meshDesc.triangles.count = triCount;
// meshDesc.triangles.stride = 3*sizeof(PxU32);
// meshDesc.triangles.data = indices32;
//
// PxToolkit::MemoryOutputStream writeBuffer;
// bool status = cooking.cookTriangleMesh(meshDesc, writeBuffer);
// if(!status)
// return NULL;
//
// PxToolkit::MemoryInputData readBuffer(writeBuffer.getData(), writeBuffer.getSize());
// return physics.createTriangleMesh(readBuffer);
// }
//
// PxTriangleMesh* PxToolkit::createTriangleMesh32(PxPhysics& physics, PxCooking& cooking, PxTriangleMeshDesc* meshDesc)
// {
// PxToolkit::MemoryOutputStream writeBuffer;
// bool status = cooking.cookTriangleMesh(*meshDesc, writeBuffer);
// if(!status)
// return NULL;
//
// PxToolkit::MemoryInputData readBuffer(writeBuffer.getData(), writeBuffer.getSize());
// return physics.createTriangleMesh(readBuffer);
// }
//
// PxConvexMesh* PxToolkit::createConvexMesh(PxPhysics& physics, PxCooking& cooking, const PxVec3* verts, PxU32 vertCount, PxConvexFlags flags)
// {
// PxConvexMeshDesc convexDesc;
// convexDesc.points.count = vertCount;
// convexDesc.points.stride = sizeof(PxVec3);
// convexDesc.points.data = verts;
// convexDesc.flags = flags;
//
// MemoryOutputStream buf;
// if(!cooking.cookConvexMesh(convexDesc, buf))
// return NULL;
//
// PxToolkit::MemoryInputData input(buf.getData(), buf.getSize());
// return physics.createConvexMesh(input);
// }
//