hello guys,I try to load MD2 with my own java code,But entrance some problems,PLZ help me.
I use this code to load MD2 files:
package com.llangry.gamemodel;
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
public class ModelBasicIO {
//basic info
/*
*0 int m_iMagicNum;
1 int m_iVersion;
2 int m_iSkinWidthPx;
3 int m_iSkinHeightPx;
4 int m_iFrameSize;
5 int m_iNumSkins;
6 int m_iNumVertices;
7 int m_iNumTexCoords;
8 int m_iNumTriangles;
9 int m_iNumGLCommands;
10 int m_iNumFrames;
11 int m_iOffsetSkins;
12 int m_iOffsetTexCoords;
13 int m_iOffsetTriangles;
14 int m_iOffsetFrames;
15 int m_iOffsetGlCommands;
16 int m_iFileSize;
* */
public static MD2model getMD2model(String filepath) throws Exception
{
int[] basic_modelinfo = new int[17];
RandomAccessFile files = new RandomAccessFile(new File(filepath),"rw");
byte[] to = new byte[17*4];
files.read(to);
byte[] temm = new byte[4];
for(int a=0;a<17;a++)
{
temm[0]=to[4*a];
temm[1]=to[4*a+1];
temm[2]=to[4*a+2];
temm[3]=to[4*a+3];
basic_modelinfo[a]=byte2int(temm);
}
String[] filehead = {"magicnum","version","skinwidth","skinheight","framesize","numskins"
,"numvertices","numtexcoords","numtriangles","numClcommand","numframes",
"offsetSkies","offsetTexCoords","offsetTriangles","offsetFrames","offsetClcommands"
,"m_ifilesize"};
for(int a=0;a<17;a++)
{
System.out.println(a+" : "+filehead[a]+" : "+basic_modelinfo[a]);
}
float[][] texcord = new float[basic_modelinfo[7]][2];
short[][] index_xyz = new short[basic_modelinfo[8]][3];
short[][] index_tx = new short[basic_modelinfo[8]][3];
String[] framnames = new String[basic_modelinfo[10]];
float[][][] frame_xyz = new float[basic_modelinfo[10]][basic_modelinfo[8]][3];
byte[][] lightnormal=new byte[basic_modelinfo[10]][basic_modelinfo[6]];
//---begin to read texcord
files.seek(basic_modelinfo[11]);
byte[] texcordbytes = new byte[basic_modelinfo[7]*4];
files.read(texcordbytes);
byte[] tempbyte = new byte[2];
for(int a=0;a<texcordbytes.length/4;a++)
{
tempbyte[0]=texcordbytes[a*4];
tempbyte[1]=texcordbytes[a*4+1];
texcord[a][0]=(float)getShort(tempbyte,true)/basic_modelinfo[2];
tempbyte[0]=texcordbytes[a*4+2];
tempbyte[1]=texcordbytes[a*4+3];
texcord[a][1]=(float)getShort(tempbyte,true)/basic_modelinfo[3];
}
//---begin to read triangle_index
files.seek(basic_modelinfo[13]);
byte[] triangle_bytes = new byte[12*basic_modelinfo[8]];
files.read(triangle_bytes);
for(int a=0;a<basic_modelinfo[8];a++)
{
tempbyte[0]=triangle_bytes[a*12];
tempbyte[1]=triangle_bytes[a*12+1];
index_xyz[a][0]=getShort(tempbyte,true);
tempbyte[0]=triangle_bytes[a*12+2];
tempbyte[1]=triangle_bytes[a*12+3];
index_xyz[a][1]=getShort(tempbyte,true);
tempbyte[0]=triangle_bytes[a*12+4];
tempbyte[1]=triangle_bytes[a*12+5];
index_xyz[a][2]=getShort(tempbyte,true);
tempbyte[0]=triangle_bytes[a*12+6];
tempbyte[1]=triangle_bytes[a*12+7];
index_tx[a][0]=getShort(tempbyte,true);
tempbyte[0]=triangle_bytes[a*12+8];
tempbyte[1]=triangle_bytes[a*12+9];
index_tx[a][1]=getShort(tempbyte,true);
tempbyte[0]=triangle_bytes[a*12+10];
tempbyte[1]=triangle_bytes[a*12+11];
index_tx[a][2]=getShort(tempbyte,true);
}
//--begin to read frame_ts
files.seek(basic_modelinfo[14]);
byte[] frame_tbytes = new byte[basic_modelinfo[4]*basic_modelinfo[10]];
files.read(frame_tbytes);
float[][] scale= new float[basic_modelinfo[10]][3];
float[][] translate=new float[basic_modelinfo[10]][3];
byte[] tempfloat = new byte[4];
byte[] name=new byte[16];
for(int a=0;a<basic_modelinfo[10];a++)
{
tempfloat[0]=frame_tbytes[a*basic_modelinfo[4]];
tempfloat[1]=frame_tbytes[a*basic_modelinfo[4]+1];
tempfloat[2]=frame_tbytes[a*basic_modelinfo[4]+2];
tempfloat[3]=frame_tbytes[a*basic_modelinfo[4]+3];
scale[a][0]=getFloat(tempfloat);
tempfloat[0]=frame_tbytes[a*basic_modelinfo[4]+4];
tempfloat[1]=frame_tbytes[a*basic_modelinfo[4]+5];
tempfloat[2]=frame_tbytes[a*basic_modelinfo[4]+6];
tempfloat[3]=frame_tbytes[a*basic_modelinfo[4]+7];
scale[a][1]=getFloat(tempfloat);
//System.out.println(scale[a][1]);
tempfloat[0]=frame_tbytes[a*basic_modelinfo[4]+8];
tempfloat[1]=frame_tbytes[a*basic_modelinfo[4]+9];
tempfloat[2]=frame_tbytes[a*basic_modelinfo[4]+10];
tempfloat[3]=frame_tbytes[a*basic_modelinfo[4]+11];
scale[a][2]=getFloat(tempfloat);
//System.out.println(scale[a][2]);
tempfloat[0]=frame_tbytes[a*basic_modelinfo[4]+12];
tempfloat[1]=frame_tbytes[a*basic_modelinfo[4]+13];
tempfloat[2]=frame_tbytes[a*basic_modelinfo[4]+14];
tempfloat[3]=frame_tbytes[a*basic_modelinfo[4]+15];
translate[a][0]=getFloat(tempfloat);
//System.out.println(translate[a][0]);
tempfloat[0]=frame_tbytes[a*basic_modelinfo[4]+16];
tempfloat[1]=frame_tbytes[a*basic_modelinfo[4]+17];
tempfloat[2]=frame_tbytes[a*basic_modelinfo[4]+18];
tempfloat[3]=frame_tbytes[a*basic_modelinfo[4]+19];
translate[a][1]=getFloat(tempfloat);
//System.out.println(translate[a][1]);
tempfloat[0]=frame_tbytes[a*basic_modelinfo[4]+20];
tempfloat[1]=frame_tbytes[a*basic_modelinfo[4]+21];
tempfloat[2]=frame_tbytes[a*basic_modelinfo[4]+22];
tempfloat[3]=frame_tbytes[a*basic_modelinfo[4]+23];
translate[a][2]=getFloat(tempfloat);
//System.out.println(translate[a][2]);
for(int temp=0;temp<16;temp++)
{
name[temp]=frame_tbytes[a*basic_modelinfo[4]+24+temp];
}
framnames[a]=new String(name);
for(int vn=0;vn<basic_modelinfo[6];vn++)
{
frame_xyz[a][vn][0]=(frame_tbytes[a*basic_modelinfo[4]+vn*4+40])*scale[a][0]+translate[a][0];
frame_xyz[a][vn][1]=(frame_tbytes[a*basic_modelinfo[4]+vn*4+41])*scale[a][1]+translate[a][1];
frame_xyz[a][vn][2]=(frame_tbytes[a*basic_modelinfo[4]+vn*4+42])*scale[a][2]+translate[a][2];
lightnormal[a][vn]= frame_tbytes[a*basic_modelinfo[4]+vn*4+43];
}
}
files.close();
return new MD2model(texcord,index_xyz,
index_tx,framnames,frame_xyz,lightnormal,basic_modelinfo[10],
basic_modelinfo[8],basic_modelinfo[6]);
}
//==================================
// byte - short - int - float
//==================================
public static int byte2int(byte[] res)
{
int targets= (res[0] & 0xff)
| ((res[1]<<8) & 0xff00)
| ((res[2]<<24)>>>8)
| (res[3]<<24);
return targets;
}
public final static short getShort(byte[] b, boolean asc) {
if (b == null) {
throw new IllegalArgumentException("byte array is null!");
}
if (b.length > 2) {
throw new IllegalArgumentException("byte array size > 2 !");
}
short r = 0;
if (asc)
for (int i = b.length - 1; i >= 0; i--) {
r <<= 8;
r |= (b[i] & 0x00ff);
}
else
for (int i = 0; i < b.length; i++) {
r <<= 8;
r |= (b[i] & 0x00ff);
}
return r;
}
public static float getFloat(byte[] b) {
return Float.intBitsToFloat(byte2int(b));
}
}
but I found the only INT type varables are right,others a wrong.
can you tell me why? ???
thank you very much ありがとう ございます