md2 loader problem

Started by
3 comments, last by ulrik04 15 years, 3 months ago
Hi all, I'm making a 3D game, where I want animations with MD2 files. I've found a loader I'm using, but I have a problem, because it's not loading values correctly. This is the code I'm using for loading the header values from the file:

void MD2Model::ReadHeader(FILE *filePtr, MD2ModelHeader & header){
	// Go to Beginning of File
	fseek(filePtr, 0, SEEK_SET);
	
	// Read Header
	header.ident = ReadInt(filePtr);
	header.version = ReadInt(filePtr);
	header.skinwidth = ReadInt(filePtr);
	header.skinheight = ReadInt(filePtr);
	header.framesize = ReadInt(filePtr);
	header.NumSkins = ReadInt(filePtr);
	header.NumVertices = ReadInt(filePtr);
	header.NumTexCoords = ReadInt(filePtr);
	header.NumTriangles = ReadInt(filePtr);
	header.NumGLCmds = ReadInt(filePtr);
	header.NumFrames = ReadInt(filePtr);
	header.offsetSkins = ReadInt(filePtr);
	header.offsetST = ReadInt(filePtr);
	header.offsetTriangles = ReadInt(filePtr);
	header.offsetFrames = ReadInt(filePtr);
	header.offsetGLCmds = ReadInt(filePtr);
	header.offsetEndOfFile = ReadInt(filePtr);
}

and here's the output (for the first 10 values): 1229213746 134217728 65536 65536 -1677328384 0 -1660878848 755105792 402849792 470745088 I really don't know what's wrong, does anybody now what I possibly could be doing wrong? I know the MD2 file is valid, because it loads into Blender and it came with the MD2 example. [Edited by - ulrik04 on January 13, 2009 11:14:01 AM]
Advertisement
Could you please post some more code? Especially the ReadInt function?

And please use the source-tags ( [ source ] and [ /source ], without the spaces).
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
Here's the ReadInt function:
int MD2Model::ReadInt(FILE *filePtr){	int value = 0;	fread(&value, sizeof(int), 1, filePtr);		#if MD2_BYTEORDER == MD2_BIG_ENDIAN		value = MD2SwapInt(value);	#endif	return value;}

And the MD2SwapInt function:
static __inline__ Uint32 MD2SwapInt(Uint32 D){	return((D<<24)|((D<<8)&0x00FF0000)|((D>>8)&0x0000FF00)|(D>>24));}

And the code to open the file
filePtr = fopen(modelFile.c_str(), "rb");if (filePtr == NULL){	return false;}fseek(filePtr, 0, SEEK_END);fileLength = ftell(filePtr);// Extract model file header from bufferReadHeader(filePtr, header);

Just tell me if more code is needed, can't post it all though because it's a quite long code... The problem in the code should be somewhere in the posted code.
Why not open the file in a hex editor (Visual Studio can open a file as hex too), and see what the bytes are? The first two values look correct ("IDP0" and 0x00000008), the next two look like they're correct (Your texture is 256x256?), and then the rest seems to be gibberish.

Does MD2_BYTEORDER == MD2_BIG_ENDIAN? I.e. is your MD2SwapInt() function being called? What if you disable that call, does it look right then?

Overall, it looks like you're swapping endian when you shouldn't. On a PC, you shouldn't need to swap the endian at all.
That did it :D Actually I made it swap, and it worked. This is the code for swapping/not swapping:
#if  defined(__i386__) || defined(__ia64__) || defined(WIN32) ||     (defined(__alpha__) || defined(__alpha)) ||      defined(__arm__) ||     (defined(__mips__) && defined(__MIPSEL__)) ||      defined(__SYMBIAN32__) ||      defined(__x86_64__) ||      defined(__LITTLE_ENDIAN__)		#define MD2_BYTEORDER	MD2_LIL_ENDIAN#else	#define MD2_BYTEORDER	MD2_BIG_ENDIAN#endif

When I removed the #if MD2_BYTEORDER == MD2_BIG_ENDIAN code, it loaded the correct values. Is LIL and BIG endian just getting swapped or what? :P

This topic is closed to new replies.

Advertisement