Hey, I'm loading md2 files from binary on my Mac, based on this tutorial:
Clicky.
I am, of course, not dumb enough to load entire structures with fread, so I read the individual integers into the header with a little endian-independence wrapper and SDL's little endian flipper. The problem is that when I jump to the triangle offset and start reading, the first 256 or so triangles are (0,0,0) and then they go to insane exponent problems (including NaN for some vertices). I've tried it with some md2s from PolyCount as well.
AFAIK this swapping should be fine! I think there's something screwing up somewhere else (link to the rest of my source at the bottom).
inline Uint32 AEndian(const Uint32 x)
{return (x>>24)|(x>>8)&0x0000ff00|(x<<8)&0x00ff0000|(x<<24);}
// SDL_SwapLE32 does the same thing as AEndian, this is just here to test
inline float floatSwap(const float x)
{
Uint32 retval = AEndian(*((Uint32*)&x));
return *((float*)&retval);
}
inline float loadFloat(float src) {
return floatSwap(src);
}
inline int getInteger(FILE* fp, int &result, bool swap = true) {
int dummy = 0;
fread(&dummy, 1, sizeof(int), fp);
//printf("Got %i\n", dummy);
if(swap) {
result = loadInteger(dummy);
}
else {
result = dummy;
}
return 1;
}
inline void getFloat(FILE* fp, float &result) {
float dummy = 0;
fread(&dummy, 1, sizeof(float), fp);
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
result = loadFloat(dummy);
}
else {
result = dummy;
}
}
I call getFloat on the members of the 'triangle' array, like this:
for(int i = 0; i < header.numTriangles; i++) {
for(int j = 0; j < 3; j++) {
getFloat(fp, tris.vertex[j]);
}
It
ONLY seems to do this for floats -- the magic number and version integers are all OK, and the other integer values in the header seem to be within the realm of sanity:
Header
Skins: 256 x 256
Frame Size: 936
Num Skins: 0
Num Vertices: 224
Num Tex Coords 374
Num Triangles 416
Num GL Commands: 2089
Num Frames 198
Offset Skins: 68
Offset Tex Coords 68
Offset Triangles 1564
Offset Frames 6556
Offset GL commands 191884
Offset End 200240
I'm kind of curious about the two 68's but it makes sense if there's no skin. My wager is that I've got the wrong offset for triangles in the provided file somehow. You can get the whole (temporary) file at
this directory for what I've written. I was missing some reads into the headers before but I think I have all of them that were outlined in the tutorial now.
I know this is a lot of crap to read, so thanks if you can help [grin]
I also have a log of my loading attempt
here, which raises further questions: are texture coordinates supposed to be negative? And huge?
[Edited by - Ravuya on November 23, 2005 10:11:01 AM]