package org.destr.jogl.mesh;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.media.opengl.GL2;
public class MeshReader extends DataInputStream {
public static int glTypeToSize(int glType) {
switch(glType) {
case GL2.GL_BYTE: return 1;
case GL2.GL_SHORT: return 2;
case GL2.GL_INT: return 4;
case GL2.GL_DOUBLE: return 8;
case GL2.GL_FLOAT: return 4;
default: return -1;
}
}
public MeshReader(InputStream is) {
super(is);
}
public BufferedMeshData readMesh() throws IOException {
BufferedMeshData mesh = new BufferedMeshData();
String version = readUTF();
if(!version.equals("JMESH1.0")) {
throw new IOException(String.format(
"Invalis header \"%s\"", version
));
}
String format = readUTF().toUpperCase();
if(format.equals("VNC")) {
mesh.count = readInt();
mesh.vGLType = readInt();
mesh.vSize = readInt();
mesh.vStride = readInt();
mesh.vOffset = readLong();
mesh.nUsed = true;
mesh.nGLType = readInt();
mesh.nStride = readInt();
mesh.nOffset = readLong();
mesh.tUsed = true;
mesh.tGLType = readInt();
mesh.tSize = readInt();
mesh.tStride = readInt();
mesh.tOffset = readLong();
int vBytes = mesh.vSize * glTypeToSize(mesh.vGLType);
if(vBytes < 0) {
throw new IOException(String.format(
"Can not read vertex glType %d", mesh.vGLType
));
}
int nBytes = 3 * glTypeToSize(mesh.nGLType);
if(nBytes < 0) {
throw new IOException(String.format(
"Can not read normal glType %d", mesh.nGLType
));
}
int tBytes = mesh.tSize * glTypeToSize(mesh.tGLType);
if(tBytes < 0) {
throw new IOException(String.format(
"Can not read texture coord glType %d", mesh.tGLType
));
}
mesh.data = new byte[mesh.count * (vBytes + nBytes + tBytes)];
read(mesh.data);
} else {
throw new IOException(String.format(
"Invalid format \"%s\"", format
));
}
return mesh;
}
}
Comments