Destrovi4

MeshRAM

Dec 30th, 2012
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.83 KB | None | 0 0
  1. package destr.jogl.pointer;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.io.UnsupportedEncodingException;
  8. import java.nio.ByteBuffer;
  9. import java.nio.ByteOrder;
  10. import java.util.ArrayList;
  11. import java.util.StringTokenizer;
  12.  
  13. import javax.media.opengl.GL;
  14. import javax.media.opengl.GL2;
  15.  
  16. public class MeshRAM {
  17.    
  18.     private static ByteBuffer makeBuff( float[] data ){
  19.         ByteBuffer byteBuffer = ByteBuffer.allocateDirect(data.length * Float.SIZE);
  20.         byteBuffer.order(ByteOrder.nativeOrder());
  21.         byteBuffer.asFloatBuffer().put(data).position(0);
  22.         return byteBuffer;
  23.     }
  24.    
  25.     private static int[] extractObjFace(String line) {//TODO сделать более жевучий эекстрактер
  26.         StringTokenizer fTokens = new StringTokenizer(line.replaceFirst("//", "/0/"), "/");
  27.         return new int[]{
  28.             Integer.parseInt(fTokens.nextToken())-1,
  29.             Integer.parseInt(fTokens.nextToken())-1,
  30.             Integer.parseInt(fTokens.nextToken())-1,
  31.         };
  32.     }
  33.    
  34.     private static float[] arrByIndexArray( Float[] array, int size, Integer[] index) {
  35.         float[] result = new float[index.length*size];
  36.         int ii = 0;
  37.         for(int i = 0; i < index.length; i++){
  38.             int start   =   index[i] * size;
  39.             for(int j = 0; j < size; j++ ) {
  40.                 float value = 0;
  41.                 try {
  42.                     value = array[start+j].floatValue();
  43.                 } catch (ArrayIndexOutOfBoundsException e) {
  44.                     System.err.println("Bad index:"+e.getMessage());
  45.                     //TODO вывод ошибки с укаzанием наzвания файла и индекса
  46.                 } finally {
  47.                     result[ii++] = value;
  48.                 }
  49.             }
  50.         }
  51.         return result;
  52.     }
  53.    
  54.     public MeshRAM( GL gl, InputStream is ){
  55.         try {
  56.             InputStreamReader isr = new InputStreamReader(is, "UTF-8");
  57.             BufferedReader br = new BufferedReader(isr);
  58.             objReader(gl, br);
  59.         } catch (UnsupportedEncodingException e) {
  60.             // TODO Auto-generated catch block
  61.             e.printStackTrace();
  62.         } catch (NumberFormatException e) {
  63.             // TODO Auto-generated catch block
  64.             e.printStackTrace();
  65.         } catch (IOException e) {
  66.             // TODO Auto-generated catch block
  67.             e.printStackTrace();
  68.         }
  69.     }
  70.    
  71.     private void objReader(GL gl, BufferedReader reader)
  72.         throws NumberFormatException, IOException{
  73.        
  74.         ArrayList<Float> vertixList = new ArrayList<Float>(),
  75.                  texCoordsList = new ArrayList<Float>(),
  76.                  normalsList = new ArrayList<Float>();
  77.         ArrayList<Integer> vertixIndexList = new ArrayList<Integer>(),
  78.                    texCoordsIndexList = new ArrayList<Integer>(),
  79.                    normalIndexList = new ArrayList<Integer>();
  80.        
  81.         String line;
  82.         while (((line = reader.readLine()) != null) ) {
  83.             if(line.length() <= 0) continue;
  84.            
  85.             StringTokenizer tokens = new StringTokenizer(line, " ");
  86.             if (line.startsWith("v ")){
  87.                 tokens.nextToken(); // skip the OBJ word
  88.                 vertixList.add(Float.parseFloat(tokens.nextToken()));
  89.                 vertixList.add(Float.parseFloat(tokens.nextToken()));
  90.                 vertixList.add(Float.parseFloat(tokens.nextToken()));
  91.             } else if (line.startsWith("vt")) { // tex-coord
  92.                 tokens.nextToken();
  93.                 texCoordsList.add(Float.parseFloat(tokens.nextToken()));
  94.                 texCoordsList.add(Float.parseFloat(tokens.nextToken()));
  95.                 //texCoordsList.add(Float.parseFloat(tokens.nextToken()));
  96.             } else if (line.startsWith("vn")) {// normal
  97.                 tokens.nextToken();
  98.                 normalsList.add(Float.parseFloat(tokens.nextToken()));
  99.                 normalsList.add(Float.parseFloat(tokens.nextToken()));
  100.                 normalsList.add(Float.parseFloat(tokens.nextToken()));
  101.             } else if (line.startsWith("f ")) {
  102.                 tokens.nextToken();
  103.                 int []face;
  104.                 face = extractObjFace(tokens.nextToken());
  105.                 vertixIndexList.add(face[0]);
  106.                 texCoordsIndexList.add(face[1]);
  107.                 normalIndexList.add(face[2]);
  108.                
  109.                 face = extractObjFace(tokens.nextToken());
  110.                 vertixIndexList.add(face[0]);
  111.                 texCoordsIndexList.add(face[1]);
  112.                 normalIndexList.add(face[2]);
  113.                
  114.                 face = extractObjFace(tokens.nextToken());
  115.                 vertixIndexList.add(face[0]);
  116.                 texCoordsIndexList.add(face[1]);
  117.                 normalIndexList.add(face[2]);
  118.                
  119.             } else if (line.startsWith("s ")) {
  120.             } else if (line.startsWith("#")) {
  121.                    
  122.             }
  123.         }
  124.        
  125.         Float[] nullFloatArr = new Float[0]; // Требуется для формирование Float[] иz ArrayList<Float>
  126.         Integer[] nullIntArr = new Integer[0]; // Тожесамое для Integer
  127.         onBufferLoad( gl,
  128.             makeBuff(arrByIndexArray(vertixList.toArray(nullFloatArr),
  129.             3, vertixIndexList.toArray(nullIntArr))),
  130.             makeBuff(arrByIndexArray(normalsList.toArray(nullFloatArr),
  131.             3, normalIndexList.toArray(nullIntArr))),
  132.             makeBuff(arrByIndexArray(texCoordsList.toArray(nullFloatArr),
  133.             2, texCoordsIndexList.toArray(nullIntArr)))
  134.         );
  135.     }
  136.    
  137.     private int vertixLength;
  138.    
  139.     private ByteBuffer vertixBB, normalesBB, texCoordsBB;
  140.    
  141.     private void onBufferLoad( GL gl,
  142.             ByteBuffer vertixBB,
  143.             ByteBuffer normalesBB,
  144.             ByteBuffer texCoordsBB){
  145.        
  146.         vertixLength        = vertixBB.capacity()/Float.SIZE/3;
  147.         this.vertixBB       = vertixBB;
  148.         this.normalesBB     = normalesBB;
  149.             this.texCoordsBB    = texCoordsBB;
  150.     }
  151.    
  152.     public void enable(GL2 gl){
  153.         gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
  154.             gl.glEnableClientState(GL2.GL_NORMAL_ARRAY);
  155.             gl.glEnableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
  156.     }
  157.    
  158.     public void bind(GL2 gl){
  159.         gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);        
  160.         gl.glVertexPointer(3, GL2.GL_FLOAT, 0, vertixBB);
  161.         gl.glNormalPointer(GL2.GL_FLOAT, 0, normalesBB);
  162.         gl.glTexCoordPointer(2, GL2.GL_FLOAT, 0, texCoordsBB);
  163.     }
  164.    
  165.     public void draw(GL2 gl){
  166.         gl.glDrawArrays(GL.GL_TRIANGLES, 0, vertixLength);
  167.     }
  168.    
  169.     public void unBind(GL2 gl){
  170.     }
  171.    
  172.     public void disable(GL2 gl){
  173.         gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
  174.             gl.glDisableClientState(GL2.GL_NORMAL_ARRAY);
  175.             gl.glDisableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment