-
Notifications
You must be signed in to change notification settings - Fork 100
/
geoplg.cpp
339 lines (316 loc) · 9 KB
/
geoplg.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "rwbase.h"
#include "rwerror.h"
#include "rwplg.h"
#include "rwpipeline.h"
#include "rwobjects.h"
#include "rwengine.h"
#include "rwanim.h"
#include "rwplugins.h"
#include "ps2/rwps2.h"
#include "ps2/rwps2plg.h"
#include "d3d/rwxbox.h"
#include "d3d/rwd3d8.h"
#include "d3d/rwd3d9.h"
#include "gl/rwwdgl.h"
#include "gl/rwgl3.h"
#define PLUGIN_ID 2
namespace rw {
static uint16 nextSerialNum = 1;
// Mesh
// Allocate a mesh header, meshes and optionally indices.
// If existing meshes already exist, retain their information.
MeshHeader*
Geometry::allocateMeshes(int32 numMeshes, uint32 numIndices, bool32 noIndices)
{
uint32 sz;
MeshHeader *mh;
Mesh *m;
uint16 *indices;
int32 oldNumMeshes;
int32 i;
sz = sizeof(MeshHeader) + numMeshes*sizeof(Mesh);
if(!noIndices)
sz += numIndices*sizeof(uint16);
if(this->meshHeader){
oldNumMeshes = this->meshHeader->numMeshes;
mh = (MeshHeader*)rwResize(this->meshHeader, sz, MEMDUR_EVENT | ID_GEOMETRY);
this->meshHeader = mh;
}else{
oldNumMeshes = 0;
mh = (MeshHeader*)rwNew(sz, MEMDUR_EVENT | ID_GEOMETRY);
mh->flags = 0;
this->meshHeader = mh;
}
mh->numMeshes = numMeshes;
mh->serialNum = nextSerialNum++;
mh->totalIndices = numIndices;
m = mh->getMeshes();
indices = (uint16*)&m[numMeshes];
for(i = 0; i < mh->numMeshes; i++){
// keep these
if(i >= oldNumMeshes){
m->material = nil;
m->numIndices = 0;
}
// always init indices
if(noIndices)
m->indices = nil;
else{
m->indices = indices;
indices += m->numIndices;
}
m++;
}
return mh;
}
void
MeshHeader::setupIndices(void)
{
int32 i;
uint16 *indices;
Mesh *m;
m = this->getMeshes();
indices = m->indices;
// return if native
if(indices == nil)
return;
for(i = 0; i < this->numMeshes; i++){
m->indices = indices;
indices += m->numIndices;
m++;
}
}
struct MeshHeaderStream
{
uint32 flags;
uint32 numMeshes;
uint32 totalIndices;
};
struct MeshStream
{
uint32 numIndices;
int32 matIndex;
};
static Stream*
readMesh(Stream *stream, int32 len, void *object, int32, int32)
{
MeshHeaderStream mhs;
MeshStream ms;
MeshHeader *mh;
Mesh *mesh;
int32 indbuf[256];
uint16 *indices;
Geometry *geo = (Geometry*)object;
stream->read32(&mhs, sizeof(MeshHeaderStream));
// Have to do this dance for War Drum's meshes
bool32 hasData = len > int32(sizeof(MeshHeaderStream)+mhs.numMeshes*sizeof(MeshStream));
assert(geo->meshHeader == nil);
geo->meshHeader = nil;
mh = geo->allocateMeshes(mhs.numMeshes, mhs.totalIndices,
geo->flags & Geometry::NATIVE && !hasData);
mh->flags = mhs.flags;
mesh = mh->getMeshes();
indices = mesh->indices;
for(uint32 i = 0; i < mh->numMeshes; i++){
stream->read32(&ms, sizeof(MeshStream));
mesh->numIndices = ms.numIndices;
mesh->material = geo->matList.materials[ms.matIndex];
if(geo->flags & Geometry::NATIVE){
// War Drum OpenGL stores uint16 indices here
if(hasData){
mesh->indices = indices;
indices += mesh->numIndices;
stream->read16(mesh->indices,
mesh->numIndices*2);
}
}else{
mesh->indices = indices;
indices += mesh->numIndices;
uint16 *ind = mesh->indices;
int32 numIndices = mesh->numIndices;
for(; numIndices > 0; numIndices -= 256){
int32 n = numIndices < 256 ? numIndices : 256;
stream->read32(indbuf, n*4);
for(int32 j = 0; j < n; j++)
ind[j] = indbuf[j];
ind += n;
}
}
mesh++;
}
return stream;
}
static Stream*
writeMesh(Stream *stream, int32, void *object, int32, int32)
{
MeshHeaderStream mhs;
MeshStream ms;
int32 indbuf[256];
Geometry *geo = (Geometry*)object;
mhs.flags = geo->meshHeader->flags;
mhs.numMeshes = geo->meshHeader->numMeshes;
mhs.totalIndices = geo->meshHeader->totalIndices;
stream->write32(&mhs, sizeof(MeshHeaderStream));
Mesh *mesh = geo->meshHeader->getMeshes();
for(uint32 i = 0; i < geo->meshHeader->numMeshes; i++){
ms.numIndices = mesh->numIndices;
ms.matIndex = geo->matList.findIndex(mesh->material);
stream->write32(&ms, sizeof(MeshStream));
if(geo->flags & Geometry::NATIVE){
assert(geo->instData != nil);
if(geo->instData->platform == PLATFORM_WDGL)
stream->write16(mesh->indices,
mesh->numIndices*2);
}else{
uint16 *ind = mesh->indices;
int32 numIndices = mesh->numIndices;
for(; numIndices > 0; numIndices -= 256){
int32 n = numIndices < 256 ? numIndices : 256;
for(int32 j = 0; j < n; j++)
indbuf[j] = ind[j];
stream->write32(indbuf, n*4);
ind += n;
}
}
mesh++;
}
return stream;
}
static int32
getSizeMesh(void *object, int32, int32)
{
Geometry *geo = (Geometry*)object;
if(geo->meshHeader == nil)
return -1;
int32 size = 12 + geo->meshHeader->numMeshes*8;
if(geo->flags & Geometry::NATIVE){
assert(geo->instData != nil);
if(geo->instData->platform == PLATFORM_WDGL)
size += geo->meshHeader->totalIndices*2;
}else{
size += geo->meshHeader->totalIndices*4;
}
return size;
}
void
registerMeshPlugin(void)
{
Geometry::registerPlugin(0, ID_MESH, nil, nil, nil);
Geometry::registerPluginStream(ID_MESH, readMesh, writeMesh, getSizeMesh);
}
// Returns the maximum number of triangles. Just so
// we can allocate enough before instancing. This does not
// take into account degerate triangles or ADC bits as
// we don't look at the data.
uint32
MeshHeader::guessNumTriangles(void)
{
if(this->flags == MeshHeader::TRISTRIP)
return this->totalIndices - 2*this->numMeshes;
else
return this->totalIndices/3;
}
// Native Data
static void*
destroyNativeData(void *object, int32 offset, int32 size)
{
Geometry *geometry = (Geometry*)object;
if(geometry->instData == nil)
return object;
if(geometry->instData->platform == PLATFORM_PS2)
return ps2::destroyNativeData(object, offset, size);
if(geometry->instData->platform == PLATFORM_WDGL)
return wdgl::destroyNativeData(object, offset, size);
if(geometry->instData->platform == PLATFORM_XBOX)
return xbox::destroyNativeData(object, offset, size);
if(geometry->instData->platform == PLATFORM_D3D8)
return d3d8::destroyNativeData(object, offset, size);
if(geometry->instData->platform == PLATFORM_D3D9)
return d3d9::destroyNativeData(object, offset, size);
if(geometry->instData->platform == PLATFORM_GL3)
return gl3::destroyNativeData(object, offset, size);
return object;
}
static Stream*
readNativeData(Stream *stream, int32 len, void *object, int32 o, int32 s)
{
ChunkHeaderInfo header;
uint32 libid;
uint32 platform;
// ugly hack to find out platform
stream->seek(-4);
libid = stream->readU32();
readChunkHeaderInfo(stream, &header);
if(header.type == ID_STRUCT &&
libraryIDPack(header.version, header.build) == libid){
platform = stream->readU32();
stream->seek(-16);
if(platform == PLATFORM_PS2)
return ps2::readNativeData(stream, len, object, o, s);
else if(platform == PLATFORM_XBOX)
return xbox::readNativeData(stream, len, object, o, s);
else if(platform == PLATFORM_D3D8)
return d3d8::readNativeData(stream, len, object, o, s);
else if(platform == PLATFORM_D3D9)
return d3d9::readNativeData(stream, len, object, o, s);
else{
fprintf(stderr, "unknown platform %d\n", platform);
stream->seek(len);
}
}else{
stream->seek(-12);
wdgl::readNativeData(stream, len, object, o, s);
}
return stream;
}
static Stream*
writeNativeData(Stream *stream, int32 len, void *object, int32 o, int32 s)
{
Geometry *geometry = (Geometry*)object;
if(geometry->instData == nil)
return stream;
if(geometry->instData->platform == PLATFORM_PS2)
return ps2::writeNativeData(stream, len, object, o, s);
else if(geometry->instData->platform == PLATFORM_WDGL)
return wdgl::writeNativeData(stream, len, object, o, s);
else if(geometry->instData->platform == PLATFORM_XBOX)
return xbox::writeNativeData(stream, len, object, o, s);
else if(geometry->instData->platform == PLATFORM_D3D8)
return d3d8::writeNativeData(stream, len, object, o, s);
else if(geometry->instData->platform == PLATFORM_D3D9)
return d3d9::writeNativeData(stream, len, object, o, s);
return stream;
}
static int32
getSizeNativeData(void *object, int32 offset, int32 size)
{
Geometry *geometry = (Geometry*)object;
if(geometry->instData == nil)
return 0;
if(geometry->instData->platform == PLATFORM_PS2)
return ps2::getSizeNativeData(object, offset, size);
else if(geometry->instData->platform == PLATFORM_WDGL)
return wdgl::getSizeNativeData(object, offset, size);
else if(geometry->instData->platform == PLATFORM_XBOX)
return xbox::getSizeNativeData(object, offset, size);
else if(geometry->instData->platform == PLATFORM_D3D8)
return d3d8::getSizeNativeData(object, offset, size);
else if(geometry->instData->platform == PLATFORM_D3D9)
return d3d9::getSizeNativeData(object, offset, size);
return 0;
}
void
registerNativeDataPlugin(void)
{
Geometry::registerPlugin(0, ID_NATIVEDATA,
nil, destroyNativeData, nil);
Geometry::registerPluginStream(ID_NATIVEDATA,
readNativeData,
writeNativeData,
getSizeNativeData);
}
}