-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpop3dobj.cpp
547 lines (418 loc) · 11.5 KB
/
pop3dobj.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
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
/*
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
made by ALACN
http://www.strategyplanet.com/populous
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
*/
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include "pop.h"
#include "3ds.h"
#ifdef _DEBUG
//#include <assert.h>
#define assert(exp) {if(!(exp)){__asm{ int 3 }}}
#endif
#define STR_SIZE 256
struct PART_UV
{
float u,
v;
};
struct PART_FACE
{
PART_FACE *Next,
*Prev;
BYTE PointsCount;
_3DS_POINT *Points;
PART_UV *UV;
};
struct PART
{
PART *Next,
*Prev;
PART_FACE *Child;
signed short TextureIdx;
};
void ErrorMsg(char *msg)
{
MessageBox(0, msg, "by ALACN", MB_ICONHAND);
}
bool ExtractTo3ds(char *objects, char *faces, char *points, char *destpath, char *prefix)
{
bool rs;
char str[STR_SIZE];
ObjEditObjectsInfo *Objs = 0;
ObjEditFacesInfo *Facs = 0;
Points3DWord *Pnts = 0;
DWORD ObjsCount,
FacsCount,
PntsCount;
HANDLE h;
DWORD dwA, dwRW;
// objs
h = CreateFile(objects, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if(h == INVALID_HANDLE_VALUE)
{
sprintf(str, "cannot open objs: %s", objects);
ErrorMsg(str);
goto failed;
}
dwA = SetFilePointer(h, 0, 0, 2);
Objs = (ObjEditObjectsInfo*)malloc(dwA);
ObjsCount = dwA / sizeof(ObjEditObjectsInfo);
SetFilePointer(h, 0, 0, 0);
dwRW = 0;
ReadFile(h, Objs, dwA, &dwRW, 0);
if(dwRW != dwA)
{
CloseHandle(h);
sprintf(str, "objs read error: %s" , objects);
ErrorMsg(str);
goto failed;
}
CloseHandle(h);
// facs
h = CreateFile(faces, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if(h == INVALID_HANDLE_VALUE)
{
sprintf(str, "cannot open facs: %s", faces);
ErrorMsg(str);
goto failed;
}
dwA = SetFilePointer(h, 0, 0, 2);
Facs = (ObjEditFacesInfo*)malloc(dwA);
FacsCount = dwA / sizeof(ObjEditFacesInfo);
SetFilePointer(h, 0, 0, 0);
dwRW = 0;
ReadFile(h, Facs, dwA, &dwRW, 0);
if(dwRW != dwA)
{
CloseHandle(h);
sprintf(str, "facs read error", faces);
ErrorMsg(str);
goto failed;
}
CloseHandle(h);
// pnts
h = CreateFile(points, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if(h == INVALID_HANDLE_VALUE)
{
sprintf(str, "cannot open pnts: %s", points);
ErrorMsg(str);
goto failed;
}
dwA = SetFilePointer(h, 0, 0, 2);
Pnts = (Points3DWord*)malloc(dwA);
PntsCount = dwA / sizeof(Points3DWord);
SetFilePointer(h, 0, 0, 0);
dwRW = 0;
ReadFile(h, Pnts, dwA, &dwRW, 0);
if(dwRW != dwA)
{
CloseHandle(h);
sprintf(str, "pnts read error: %s", points);
ErrorMsg(str);
goto failed;
}
CloseHandle(h);
for(dwA = 0; dwA < ObjsCount; dwA++)
{
if(!Objs[dwA].NumFaces) continue;
if(!Objs[dwA].NumPoints) continue;
CHUNK root;
memset(&root, 0, sizeof(root));
root.hdr.id = M3DMAGIC;
CHUNK *mdata;
mdata = new CHUNK;
memset(mdata, 0, sizeof(CHUNK));
mdata->hdr.id = MDATA;
LINK(root.Child, mdata);
PART *IdxList = 0,
*p;
PART_FACE *pp;
#ifdef _DEBUG
assert(Objs[dwA].StartFace < FacsCount);
assert(Objs[dwA].StartPoint < PntsCount);
assert(Objs[dwA].StartFace > 0);
assert(Objs[dwA].StartPoint > 0);
#endif
DWORD dwEF = Objs[dwA].StartFace + Objs[dwA].NumFaces - 1;
for(DWORD dwF = Objs[dwA].StartFace - 1; dwF < dwEF; dwF++)
{
#ifdef _DEBUG
assert(dwF < FacsCount);
#endif
if(Facs[dwF].NumPoints != 3 && Facs[dwF].NumPoints != 4) continue;
PART *partidx = 0;
p = IdxList;
if(p) do
{
if(p->TextureIdx == Facs[dwF].TextureIdx)
{
partidx = p;
break;
}
p = p->Next;
}
while(p != IdxList);
if(!partidx)
{
partidx = new PART;
memset(partidx, 0, sizeof(PART));
partidx->TextureIdx = Facs[dwF].TextureIdx;
LINK(IdxList, partidx);
}
pp = new PART_FACE;
memset(pp, 0, sizeof(PART_FACE));
LINK(partidx->Child, pp);
pp->PointsCount = Facs[dwF].NumPoints;
DWORD a;
a = sizeof(_3DS_POINT) * pp->PointsCount;
pp->Points = (_3DS_POINT*)malloc(a);
memset(pp->Points, 0, a);
a = sizeof(PART_UV) * pp->PointsCount;
pp->UV = (PART_UV*)malloc(a);
memset(pp->UV, 0, a);
for(a = 0; a < pp->PointsCount; a++)
{
#ifdef _DEBUG
assert((Objs[dwA].StartPoint + Facs[dwF].PointOffsets[a] - 1) < PntsCount);
#endif
pp->Points[a].x = (float)Pnts[Objs[dwA].StartPoint + Facs[dwF].PointOffsets[a] - 1].WX / ((float)Objs[dwA].Scale * 3.0f);
pp->Points[a].y = (float)Pnts[Objs[dwA].StartPoint + Facs[dwF].PointOffsets[a] - 1].WY / ((float)Objs[dwA].Scale * 3.0f);
pp->Points[a].z = (float)Pnts[Objs[dwA].StartPoint + Facs[dwF].PointOffsets[a] - 1].WZ / ((float)Objs[dwA].Scale * 3.0f);
pp->UV[a].u = (float)Facs[dwF].TmapSrcCoords[a].X / (float)0x200000;
pp->UV[a].v = 1.0f - ((float)Facs[dwF].TmapSrcCoords[a].Y / (float)0x200000);
}
}
DWORD n = 0;
p = IdxList;
if(p) do
{
CHUNK *c, *c2, *c3, *c4, *matname;
char str[STR_SIZE];
// mat entry
c = new CHUNK;
memset(c, 0, sizeof(CHUNK));
c->hdr.id = MAT_ENTRY;
LINK(mdata->Child, c);
// mat entry - mat name
c2 = new CHUNK;
memset(c2, 0, sizeof(CHUNK));
c2->hdr.id = MAT_NAME;
LINK(c->Child, c2);
matname = c2;
sprintf(str, "t%d", p->TextureIdx);
c2->data_size = strlen(str) + 1;
c2->data = malloc(c2->data_size);
memcpy(c2->data, str, c2->data_size);
// mat entry - mat diffuse
c2 = new CHUNK;
memset(c2, 0, sizeof(CHUNK));
c2->hdr.id = MAT_DIFFUSE;
LINK(c->Child, c2);
// mat entry - mat diffuse - color 24
c3 = new CHUNK;
memset(c3, 0, sizeof(CHUNK));
c3->hdr.id = COLOR_24;
LINK(c2->Child, c3);
c3->data_size = sizeof(_3DS_MATERIAL_BYTE);
c3->data = malloc(c3->data_size);
((_3DS_MATERIAL_BYTE*)c3->data)->r =
((_3DS_MATERIAL_BYTE*)c3->data)->g =
((_3DS_MATERIAL_BYTE*)c3->data)->b = 0xFF;
if(p->TextureIdx != -1 && p->TextureIdx < 256)
{
// mat entry - mat texmap
c2 = new CHUNK;
memset(c2, 0, sizeof(CHUNK));
c2->hdr.id = MAT_TEXMAP;
LINK(c->Child, c2);
// mat entry - mat texmap - mat mapname
c3 = new CHUNK;
memset(c3, 0, sizeof(CHUNK));
c3->hdr.id = MAT_MAPNAME;
LINK(c2->Child, c3);
sprintf(str, "tx%d.bmp", p->TextureIdx);
c3->data_size = strlen(str) + 1;
c3->data = malloc(c3->data_size);
memcpy(c3->data, str, c3->data_size);
}
// named objects
c = new CHUNK;
memset(c, 0, sizeof(CHUNK));
c->hdr.id = NAMED_OBJECT;
LINK(mdata->Child, c);
sprintf(str, "obj%d", n++);
c->data_size = strlen(str) + 1;
c->data = malloc(c->data_size);
memcpy(c->data, str, c->data_size);
// n tri object
c2 = new CHUNK;
memset(c2, 0, sizeof(CHUNK));
c2->hdr.id = N_TRI_OBJECT;
LINK(c->Child, c2);
c = c2;
// count
WORD VCount = 0,
FCount = 0;
pp = p->Child;
if(pp) do
{
VCount += pp->PointsCount;
if(pp->PointsCount == 3)
FCount += 1;
else //if(pp->PointsCount == 4)
FCount += 2;
pp = pp->Next;
}
while(pp != p->Child);
// point array
c2 = new CHUNK;
memset(c2, 0, sizeof(CHUNK));
c2->hdr.id = POINT_ARRAY;
LINK(c->Child, c2);
c2->data_size = sizeof(WORD) + (sizeof(_3DS_POINT) * VCount);
c2->data = malloc(c2->data_size);
memset(c2->data, 0, c2->data_size);
*(WORD*)c2->data = VCount;
_3DS_POINT *_3dspoint = (_3DS_POINT*)((BYTE*)c2->data + sizeof(WORD));
// tex verts
c3 = new CHUNK;
memset(c3, 0, sizeof(CHUNK));
c3->hdr.id = TEX_VERTS;
LINK(c->Child, c3);
c3->data_size = sizeof(WORD) + (sizeof(_3DS_UV) * VCount);
c3->data = malloc(c3->data_size);
memset(c3->data, 0, c3->data_size);
*(WORD*)c3->data = VCount;
_3DS_UV *_3dsuv = (_3DS_UV*)((BYTE*)c3->data + sizeof(WORD));
// face array
c4 = new CHUNK;
memset(c4, 0, sizeof(CHUNK));
c4->hdr.id = FACE_ARRAY;
LINK(c->Child, c4);
c4->data_size = sizeof(WORD) + (sizeof(_3DS_FACE) * FCount);
c4->data = malloc(c4->data_size);
memset(c4->data, 0, c4->data_size);
*(WORD*)c4->data = FCount;
_3DS_FACE *_3dsface = (_3DS_FACE*)((BYTE*)c4->data + sizeof(WORD));
c2 = new CHUNK;
memset(c2, 0, sizeof(CHUNK));
c2->hdr.id = MSH_MAT_GROUP;
LINK(c4->Child, c2);
c2->data_size = matname->data_size + (sizeof(WORD) * (FCount + 1));
c2->data = malloc(c2->data_size);
memcpy(c2->data, matname->data, matname->data_size);
WORD *w = (WORD*)((BYTE*)c2->data + matname->data_size);
*w = FCount;
w++;
WORD wf;
for(wf = 0; wf < FCount; wf++)
{
*w = wf;
w++;
}
//
wf = 0;
WORD wp = 0;
pp = p->Child;
if(pp) do
{
//
_3dspoint[wp].x = pp->Points[0].x;
_3dspoint[wp].y = pp->Points[0].y;
_3dspoint[wp].z = pp->Points[0].z;
_3dsuv[wp].u = pp->UV[0].u;
_3dsuv[wp].v = pp->UV[0].v;
_3dsface[wf].a = wp;
wp++;
//
_3dspoint[wp].x = pp->Points[1].x;
_3dspoint[wp].y = pp->Points[1].y;
_3dspoint[wp].z = pp->Points[1].z;
_3dsuv[wp].u = pp->UV[1].u;
_3dsuv[wp].v = pp->UV[1].v;
_3dsface[wf].b = wp;
wp++;
//
_3dspoint[wp].x = pp->Points[2].x;
_3dspoint[wp].y = pp->Points[2].y;
_3dspoint[wp].z = pp->Points[2].z;
_3dsuv[wp].u = pp->UV[2].u;
_3dsuv[wp].v = pp->UV[2].v;
_3dsface[wf].c = wp;
wf++;
wp++;
if(pp->PointsCount == 4)
{
_3dspoint[wp].x = pp->Points[3].x;
_3dspoint[wp].y = pp->Points[3].y;
_3dspoint[wp].z = pp->Points[3].z;
_3dsuv[wp].u = pp->UV[3].u;
_3dsuv[wp].v = pp->UV[3].v;
_3dsface[wf].a = wp - 1;
_3dsface[wf].b = wp;
_3dsface[wf].c = wp - 3;
wf++;
wp++;
}
pp = pp->Next;
}
while(pp != p->Child);
//
p = p->Next;
}
while(p != IdxList);
while(IdxList)
{
p = IdxList;
UNLINK(IdxList, p);
while(p->Child)
{
pp = p->Child;
UNLINK(p->Child, pp);
if(pp->Points) free(pp->Points);
if(pp->UV) free(pp->UV);
delete pp;
}
delete p;
}
char filename[STR_SIZE];
sprintf(filename, "%s\\%s%d.3ds", destpath, prefix, dwA);
if(!_3dsWriteFile(filename, &root))
{
sprintf(str, "_3ds Write Failed: %s", filename);
ErrorMsg(str);
_3dsDestroyChunk(&root);
goto failed;
}
_3dsDestroyChunk(&root);
}
rs = true;
goto skip;
failed:
rs = false;
skip:
if(Objs) free(Objs);
if(Facs) free(Facs);
if(Pnts) free(Pnts);
return rs;
}
int __stdcall WinMain(HINSTANCE, HINSTANCE, char*, int)
{
ExtractTo3ds("objs0-0.dat", "facs0-0.dat", "pnts0-0.dat", "3ds", "obj0_");
ExtractTo3ds("objs0-1.dat", "facs0-1.dat", "pnts0-1.dat", "3ds", "obj1_");
ExtractTo3ds("objs0-2.dat", "facs0-2.dat", "pnts0-2.dat", "3ds", "obj2_");
ExtractTo3ds("objs0-3.dat", "facs0-3.dat", "pnts0-3.dat", "3ds", "obj3_");
ExtractTo3ds("objs0-4.dat", "facs0-4.dat", "pnts0-4.dat", "3ds", "obj4_");
ExtractTo3ds("objs0-5.dat", "facs0-5.dat", "pnts0-5.dat", "3ds", "obj5_");
ExtractTo3ds("objs0-6.dat", "facs0-6.dat", "pnts0-6.dat", "3ds", "obj6_");
ExtractTo3ds("objs0-7.dat", "facs0-7.dat", "pnts0-7.dat", "3ds", "obj7_");
ExtractTo3ds("objs0-8.dat", "facs0-8.dat", "pnts0-8.dat", "3ds", "obj8_");
MessageBox(0, "done", "by ALACN", 0);
return 0;
}