Destrovi4

MeshReader

Feb 25th, 2015
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.09 KB | None | 0 0
  1. package org.destr.jogl.mesh;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6.  
  7. import javax.media.opengl.GL2;
  8.  
  9. public class MeshReader extends DataInputStream {
  10.  
  11.     public static int glTypeToSize(int glType) {
  12.         switch(glType) {
  13.        
  14.             case GL2.GL_BYTE:   return  1;
  15.             case GL2.GL_SHORT:  return  2;
  16.             case GL2.GL_INT:    return  4;
  17.             case GL2.GL_DOUBLE: return  8;
  18.            
  19.             case GL2.GL_FLOAT:  return  4;
  20.            
  21.             default: return -1;
  22.         }
  23.     }
  24.    
  25.     public MeshReader(InputStream is) {
  26.         super(is);
  27.     }
  28.    
  29.     public BufferedMeshData readMesh() throws IOException {
  30.         BufferedMeshData mesh = new BufferedMeshData();
  31.        
  32.         String version = readUTF();
  33.        
  34.         if(!version.equals("JMESH1.0")) {
  35.             throw new IOException(String.format(
  36.                 "Invalis header \"%s\"", version
  37.             ));
  38.         }
  39.        
  40.         String format = readUTF().toUpperCase();
  41.        
  42.         if(format.equals("VNC")) {
  43.            
  44.             mesh.count  = readInt();
  45.            
  46.             mesh.vGLType    = readInt();
  47.             mesh.vSize  = readInt();
  48.             mesh.vStride    = readInt();
  49.             mesh.vOffset    = readLong();
  50.            
  51.             mesh.nUsed  = true;
  52.             mesh.nGLType    = readInt();
  53.             mesh.nStride    = readInt();
  54.             mesh.nOffset    = readLong();
  55.            
  56.             mesh.tUsed  = true;
  57.             mesh.tGLType    = readInt();
  58.             mesh.tSize  = readInt();
  59.             mesh.tStride    = readInt();
  60.             mesh.tOffset    = readLong();
  61.                        
  62.             int vBytes = mesh.vSize * glTypeToSize(mesh.vGLType);
  63.             if(vBytes < 0) {
  64.                 throw new IOException(String.format(
  65.                     "Can not read vertex glType %d", mesh.vGLType
  66.                 ));
  67.             }
  68.            
  69.             int nBytes = 3 * glTypeToSize(mesh.nGLType);
  70.             if(nBytes < 0) {
  71.                 throw new IOException(String.format(
  72.                     "Can not read normal glType %d", mesh.nGLType
  73.                 ));
  74.             }
  75.            
  76.             int tBytes = mesh.tSize * glTypeToSize(mesh.tGLType);
  77.             if(tBytes < 0) {
  78.                 throw new IOException(String.format(
  79.                     "Can not read texture coord glType %d", mesh.tGLType
  80.                 ));
  81.             }
  82.            
  83.             mesh.data = new byte[mesh.count * (vBytes + nBytes + tBytes)];
  84.             read(mesh.data);
  85.            
  86.         } else {
  87.             throw new IOException(String.format(
  88.                 "Invalid format \"%s\"", format
  89.             ));
  90.         }
  91.        
  92.         return mesh;
  93.     }
  94.    
  95. }
Advertisement
Add Comment
Please, Sign In to add comment