-
Notifications
You must be signed in to change notification settings - Fork 7
/
ioMD3.cpp
406 lines (334 loc) · 9.88 KB
/
ioMD3.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
/* OpenBRF -- by marco tarini. Provided under GNU General Public License */
#include <vector>
#include <set>
#include <stdio.h>
#include <vcg/space/box3.h>
#include <vcg/math/matrix44.h>
#include <vcg/space/point3.h>
#include <vcg/space/point2.h>
#include <vector>
using namespace vcg;
using namespace std;
#include "brfMesh.h"
#include "ioMD3.h"
#include "saveLoad.h"
#include "platform.h"
#define ERRSIZE 512
static wchar_t errorStr[ERRSIZE];
static const unsigned int MAGIC_MD3 = 0x33504449;
static const unsigned int MAGIC_MD2 = 0x32504449; // "IDP2"
typedef unsigned int uint;
typedef unsigned char Byte;
bool LoadStringFix(FILE* f,char *res, int max){
if (fread(res,max,1,f) != 1) throw std::runtime_error("Read in LoadStringFix() failed!");
return true;
}
void SaveStringFix(FILE* f,const char *res, int max){
bool inside = true;
for (int i=0; i<max-1; i++){
if (inside) SaveByte(f,res[i]); else
SaveByte(f,0);
if (res[i]==0) inside = false;
}
SaveByte(f,0);
}
static const float RATIO = 100.0;
void SavePoint16(FILE *f, vcg::Point3f p) {
SaveShort(f, int(p.X()*RATIO*64) );
SaveShort(f, int(p.Z()*RATIO*64) );
SaveShort(f, int(p.Y()*RATIO*64) );
}
void LoadPoint16(FILE *f, vcg::Point3f &p) {
short int x,y,z;
LoadShort(f,x);
LoadShort(f,z);
LoadShort(f,y);
p = vcg::Point3f(
x/(RATIO*64),
y/(RATIO*64),
z/(RATIO*64)
);
}
static void tryExtractNumber(char* st, int &res){
while (!(*st>='0' && *st<='9')) {
if (!st) return;
st++;
}
sscanf(st,"%d",&res);
}
wchar_t* IoMD::LastErrorString(){
return errorStr;
}
static vcg::Point3f int2norm(Byte zen,Byte azi){
double zend = zen * (2 * M_PI ) / 255.0;
double azid = azi * (2 * M_PI ) / 255.0;
vcg::Point3f res;
res.X() = (float)(cos ( zend ) * sin ( azid ));
res.Z() = (float)(sin ( zend ) * sin ( azid ));
res.Y() = (float)(cos ( azid ));
return res;
}
static void norm2int(vcg::Point3f res, Byte &zenb,Byte &azib){
double zen = atan2(res.Z(), res.X() );
double azi = acos(res.Y() );
int zeni = int( zen / (2 * M_PI ) * 255.0);
int azii = int( azi / (2 * M_PI ) * 255.0);
if (zeni>255) zeni -= 255;
if (zeni<0) zeni += 255;
if (azii>255) azii -= 255;
if (azii<0) azii += 255;
zenb = zeni;
azib = azii;
}
bool IoMD::ExportMD2(const wchar_t *filename, const BrfMesh &m){
FILE *f = wfopen(filename,"wb");
if (!f){
swprintf(errorStr,ERRSIZE,L"Cannot write on file:\n %ls",filename);
return false;
}
SaveUint(f,MAGIC_MD2);
SaveInt(f,8); // version
SaveInt(f,1024); // text width
SaveInt(f,1024); // text width
SaveInt(f, m.frame.size() );
int nframes = m.frame.size();
SaveInt(f, 1 ); // number of textures
SaveInt(f, m.frame[0].pos.size() * nframes ); // total n of xyz
SaveInt(f, m.vert.size() ); //
SaveInt(f, m.face.size() );
SaveInt(f, 0 ); // openGL commands?
SaveInt(f, nframes );
/*
int ofs_skins; // offset to skin names (64 bytes each)
int ofs_st; // offset to s-t texture coordinates
int ofs_tris; // offset to triangles
int ofs_frames; // offset to frame data
int ofs_glcmds; // offset to opengl commands
int ofs_end; // offset to end of file
*/
unsigned int ofs_pos = ftell( f ); //
SaveUint( f, ofs_pos );
//swprintf(errorStr,ERRSIZE,"TEST1 %d == %d\n",ftell(f),64+11*4);
// frames
for (uint i=0; i<m.frame.size(); i++){
SavePoint(f,m.bbox.min*RATIO); // 4*3
SavePoint(f,m.bbox.max*RATIO); // 4*3
SavePoint(f,vcg::Point3f(0,0,0)); // 4*3
SaveFloat(f,m.bbox.Diag()*RATIO); // 4
char tmp[255];
sprintf(tmp, "T%d", m.frame[i].time);
SaveStringFix(f,tmp,16); // 16
}
fclose(f);
return true;
}
bool IoMD::Export(const wchar_t *filename, const BrfMesh &m){
if (m.frame.size()>1024){
swprintf(errorStr,ERRSIZE,L"Too many frames %d. Max = 1024",m.frame.size());
return false;
}
if (m.vert.size()>4096){
swprintf(errorStr,ERRSIZE,L"Too many vertices %d. Max = 4096",m.vert.size());
return false;
}
if (m.face.size()>8192){
swprintf(errorStr,ERRSIZE,L"Too many faces: %d. Max = 8192",m.face.size());
return false;
}
FILE *f = wfopen(filename,"wb");
if (!f){
swprintf(errorStr,ERRSIZE,L"Cannot write on file:\n %ls",filename);
return false;
}
SaveUint(f,MAGIC_MD3);
SaveInt(f,15); // version
SaveStringFix(f,m.name,64);
SaveInt(f,0); // flags
SaveUint(f,m.frame.size());
SaveInt(f,0); // tags
SaveUint(f,1); // surfaces
SaveUint(f,0); // skins
long int off = 64+11*4;
SaveUint(f,off); // framwa
off+= m.frame.size()*(4*3 + 4*3 + 4*3 + 4 + 16);
SaveUint(f,off); // tags
SaveUint(f,off); // surfaces
off += 10000 ;
SaveUint(f,off); // oef
//swprintf(errorStr,ERRSIZE,"TEST1 %d == %d\n",ftell(f),64+11*4);
// frames
for (uint i=0; i<m.frame.size(); i++){
SavePoint(f,m.bbox.min*RATIO); // 4*3
SavePoint(f,m.bbox.max*RATIO); // 4*3
SavePoint(f,vcg::Point3f(0,0,0)); // 4*3
SaveFloat(f,m.bbox.Diag()*RATIO); // 4
char tmp[255];
sprintf(tmp, "T%d", m.frame[i].time);
SaveStringFix(f,tmp,16); // 16
}
// surface
//sprintf(errorStr,"%sTEST %d == %d",errorStr,ftell(f),off-10000);
//return false;
SaveUint(f,MAGIC_MD3); // 4
SaveStringFix(f,m.name,64); //64
SaveInt(f,0); // flags // 4
SaveUint(f,m.frame.size()); // 4
SaveUint(f,1); // shaders 4
SaveUint(f,m.vert.size()); // 4
SaveUint(f,m.face.size()); // 4
off = 64 + 11*4 + (64 + 4);
SaveUint(f,off ); // offset tri 4
SaveUint(f,64 + 11*4 ); // offset shader 4
off += m.face.size()*3*4; // three int per face
SaveUint(f,off ); // offset st 4
off += m.vert.size()*2*4;
SaveUint(f,off ); // offset xyznorm 4
off += m.vert.size()*4*2*m.frame.size();
SaveUint(f,off ); // offset endf 4
// surface shader
SaveStringFix(f,m.material,64);
SaveInt(f,0); // index
// surface triangles
for (uint i=0; i<m.face.size(); i++){
for (int j=0; j<3; j++){
SaveInt(f,m.face[i].index[2-j]);
}
}
// surface st
for (uint i=0; i<m.vert.size(); i++){
SavePoint(f,m.vert[i].ta);
}
// surface xyzn
for (uint j=0; j<m.frame.size(); j++)
for (uint i=0; i<m.vert.size(); i++){
SavePoint16(f,m.frame[j].pos[ m.vert[i].index ]);
Byte n0,n1;
norm2int(m.frame[j].norm[ i ],n0,n1);
SaveByte(f,n0);
SaveByte(f,n1);
}
fclose(f);
return true;
}
bool IoMD::Import(FILE *f, BrfMesh &m){
long pos = ftell(f);
unsigned int magic=0;
LoadUint(f,magic);
if (magic != MAGIC_MD3) {
swprintf(errorStr,ERRSIZE,L"Invalid magic number in surface: %X",magic);
return false;
}
char str[65];
LoadStringFix(f,str,64);
sprintf(m.name,"%s",str);
sprintf(m.material,"%s",str);
unsigned int flags;
LoadUint(f,flags);
unsigned int nframes,nshaders,nverts,ntriangles;
unsigned int otriangles,oshaders,ost,oxyz,oend;
LoadUint(f,nframes);
LoadUint(f,nshaders);
LoadUint(f,nverts);
LoadUint(f,ntriangles);
LoadUint(f,otriangles);
LoadUint(f,oshaders);
LoadUint(f,ost);
LoadUint(f,oxyz);
LoadUint(f,oend);
swprintf(errorStr,ERRSIZE,L"Loaded: %dv %dt %df\n",nverts,ntriangles,nframes);
m.face.resize(ntriangles);
m.vert.resize(nverts);
m.frame.resize(nframes);
for (unsigned int i=0; i<nframes; i++) {
m.frame[i].pos.resize(nverts);
m.frame[i].norm.resize(nverts);
m.frame[i].time = (i<2)?i:99+(i-2)*5; // default timings for icon animations
}
// load triangles
fseek(f,pos+otriangles,SEEK_SET);
for (unsigned int i=0; i<ntriangles; i++)
for (int j=0; j<3; j++) LoadInt(f,m.face[i].index[2-j]);
// load shader into material
fseek(f,pos+oshaders,SEEK_SET);
LoadStringFix(f,m.material,32);
// load text coords
fseek(f,pos+ost,SEEK_SET);
for (unsigned int j=0; j<nverts; j++) {
LoadPoint(f,m.vert[j].ta);
m.vert[j].tb = m.vert[j].ta;
m.vert[j].tangi = 0;
m.vert[j].index = j;
}
// load XYZ + norms
fseek(f,pos+oxyz,SEEK_SET);
for (unsigned int i=0; i<nframes; i++)
for (unsigned int j=0; j<nverts; j++) {
LoadPoint16(f,m.frame[i].pos[j]);
Byte n0,n1;
LoadByte(f,n0); // norm
LoadByte(f,n1); // norm
m.frame[i].norm[j] = int2norm(n0,n1);
}
m.ColorAll(0xFFFFFFFF);
//m.ComputeNormals();
m.AdjustNormDuplicates();
m.UpdateBBox();
m.skinning.clear();
m.flags = 0;
fseek(f,pos+oend,SEEK_SET);
return true;
}
bool IoMD::Import(const wchar_t *filename, std::vector<BrfMesh> &mv){
FILE *f = wfopen(filename,"rb");
if (!f) {
swprintf(errorStr,ERRSIZE,L"File not found");
return false;
}
unsigned int magic=0,ver=0;
LoadUint(f,magic);
if (magic != MAGIC_MD3) {
swprintf(errorStr,ERRSIZE,L"Invalid magic number: %X",magic);
return false;
}
LoadUint(f,ver);
char name[64];
LoadStringFix(f,name,64);
unsigned int nframes,ntags,nsurfaces,nskins;
unsigned int oframes,otags,osurfaces,flags;
LoadUint(f,flags);
LoadUint(f,nframes);
LoadUint(f,ntags);
LoadUint(f,nsurfaces);
LoadUint(f,nskins);
LoadUint(f,oframes);
LoadUint(f,otags);
LoadUint(f,osurfaces);
//sprintf(errorStr,"Found: %s (%d surface %d frames, off: %0X)",name,nsurfaces,nframes,osurfaces);
if (nsurfaces>255 ) {
swprintf(errorStr,ERRSIZE,L"File format error (%d surfaces?)",nsurfaces);
return false;
}
if (nsurfaces==0 ) {
swprintf(errorStr,ERRSIZE,L"no \"surface\" found");
return false;
}
fseek(f,osurfaces,SEEK_SET);
for (unsigned int i=0; i<nsurfaces; i++) {
BrfMesh m;
if (!Import(f,m)) return false;
mv.push_back(m);
}
fseek(f,oframes,SEEK_SET);
for (unsigned int i=0; i<nframes; i++) {
Skip(f,10*4); // skip bounding box and stuff
char tmp[17];
LoadStringFix(f,tmp,16);
for (unsigned int j=0; j<nsurfaces; j++){
tryExtractNumber(tmp,mv[j].frame[i].time);
//sprintf(mv[j].name,tmp);
}
}
fclose(f);
return true;
//fseek("")
}