• Advertisement
Sign in to follow this  

Java read md2_triangle_t

This topic is 1772 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to load an .md2 model in Java.

 

There's a structure like this:

struct md2_triangle_t 
{ 
    unsigned short vertex[3]; 
    unsigned short st[3]; 
};

 

Will there be byte packing? Where and how many bytes do I skip? Or can I read the 2-byte unsigned shorts one after the other?

 

Share this post


Link to post
Share on other sites
Advertisement

Take a look at the DataInputStream, there are methods to read ushorts etc. Padding etc. are often memory/compiler options, I would think, that the above structure is written 1:1, therefor try something like this:

// fast pseudo code, needs some checking and cleaning up 
class md2_triangle_t 
{
  public int vertex[3];
  public int st[3];


public void readFromStream(DataInputStream _inputStream)
 {
vertex[0] = _inputStream.readUnsignedShort();
vertex[1] = _inputStream.readUnsignedShort();
vertex[2] = _inputStream.readUnsignedShort();

st[0] = _inputStream.readUnsignedShort();
st[1] = _inputStream.readUnsignedShort();
st[2] = _inputStream.readUnsignedShort();
}

}

Share this post


Link to post
Share on other sites

They're little-endian ushorts so

 

        // http://darksleep.com/player/JavaAndUnsignedTypes.html
	static char ReadUShort(InputStream iS, int offset)
	{
		byte[] bucket = ReadBytes(iS, offset, 2);
                // switched bytes for little-endian:
                int firstByte = (0x000000FF & ((int)bucket[1]));
                int secondByte = (0x000000FF & ((int)bucket[0]));
                return (char) (firstByte << 8 | secondByte);
	}

           

 

        static byte[] ReadBytes(InputStream iS, int offset, int readlen)
        {
                byte[] bucket = new byte[offset+readlen];
        
                try 
                {
                        iS.read(bucket, offset, readlen);
                } 
                catch (IOException e) 
                {
                        e.printStackTrace();
                }

                byte subbucket[] = new byte[readlen];

                for(int i=0; i<readlen; i++)
                        subbucket = bucket[offset+i];
        
                return subbucket;
        }
Edited by polyfrag

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement