Skip to main content
gamedev.net
🔒 Locked

[java] Reading MD2-files

Started by Terrimus Apr 13, 2009 at 6:17 PM 3 replies 1.9k views
Original Post
Terrimus
Terrimus
Hi I am trying to read a MD2-file into my little engine but are getting some strange values. My function to read a integer from a DataInputStream looks something like this:

byte[] buffer = new byte[4];
inputStream.read(buffer, 0, 4);

int result = (buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24));  // Little Endian
And in my MD2-header loading functions I just read one integer at a time, but I get these results:
Quote:
MD2 Header: Magic Number: IDP2 Version: 8 Skin Width/Height: 256/256 Frame Size: 552 Num Skins: 0 Num Vertices: -128 Num Tex Coords: -91 Num Triangles: -18 Num OpenGL Commands: 2381 Num Frames: -74 Offset Skins: 68 Offset Tex Coords: 68 Offset Triangles: -40 Offset Frames: 3584 Offset OpenGL Commands: -27024 Offset EOF: -92
Seems like the magic number and the version is OK. And the Skin width/height also. But the rest looks kinda screewed. Am I missing out of some unsigned to signed conversion?
Scourage
Scourage
Is your source file valid? Have you tried creating a struct of the header and filling it directly?

struct header{  int magicNumber;  int version;  int width;  int height;  ....  int offsetEof;};header theHeader;inputStream.read(&theHeader, 0, sizeof(header));


Cheers,

Bob

[size="3"]Halfway down the trail to Hell...
Terrimus
Terrimus
Well, no. Since this is Java, I haven't tried to read it directly in.

I know the source file is working (it is from the data with the book "Focus on 3D Models" by Pipho.

Edit: I've also tried loading other md2-files, but got the same results: the magic number and versions is OK, but the data is not.
Trond A
Trond A
You are probably onto something regarding unsigned/signed conversion. Try this example:

byte[] buffer = new byte[4];				buffer[0] = (byte)0xFF;		buffer[1] = 0;		buffer[2] = 0;		buffer[3] = 0;		int result = (int)((long)(buffer[0] & 0xFF) | ((long)(buffer[1] & 0xFF) << 8) | ((long)(buffer[2] & 0xFF) << 16) | ((long)(buffer[3] & 0xFF) << 24));				System.out.println(result);				result = ((buffer[0]) | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24));				System.out.println(result);


The expected result should be 255, yet it returns -1 if you do not bitwise copy the byte into a larger primitive before the shifting.

Edit: source-tags.
-Trond
Terrimus
Terrimus
Takk Trond ;)

Works like a charm now.

The md2-file I am loading is 110 KB (113 572 bytes), and here is the output after loading the header:

Quote:

MD2 Header:
Magic Number: IDP2
Version: 8
Skin Width/Height: 256/256
Frame Size: 552
Num Skins: 0
Num Vertices: 128
Num Tex Coords: 165
Num Triangles: 238
Num OpenGL Commands: 2381
Num Frames: 182
Offset Skins: 68
Offset Tex Coords: 68
Offset Triangles: 728
Offset Frames: 3584
Offset OpenGL Commands: 104048
Offset EOF: 113572


Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.