wststreet

Untitled

Apr 19th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.15 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.InputStreamReader;
  8. import java.nio.ByteBuffer;
  9. import java.nio.ByteOrder;
  10. import java.nio.FloatBuffer;
  11. import java.nio.IntBuffer;
  12. import java.nio.ShortBuffer;
  13. import java.util.Random;
  14.  
  15. import javax.media.opengl.GL;
  16. import javax.media.opengl.GL2;
  17.  
  18. public class Square {
  19.  
  20.     FloatBuffer mFVertexBuffer;
  21.     ByteBuffer mColorBuffer;
  22.     ShortBuffer mTfan3;
  23.     FloatBuffer mNormalBuffer;
  24.  
  25.     float verts[] = null;
  26.     float normals[] = null;
  27.     // short normalsi[] = null;
  28.     short faces[] = null;
  29.     byte newcolors[] = null;
  30.  
  31.     public Square() {
  32.  
  33.         try {
  34.             ObjParser.parse(new FileInputStream("res/sphere.obj"));
  35.             verts = ObjParser.vertices;
  36.             faces = ObjParser.faces;
  37.             normals = ObjParser.normals;
  38.         } catch (Exception e) {
  39.             e.printStackTrace();
  40.         }
  41.  
  42.         //ObjParser.toFile();
  43.        
  44.         Random rand = new Random();
  45.  
  46.         newcolors = new byte[verts.length * 4];
  47.  
  48.         for (int i = 0; i < newcolors.length; i += 4) {
  49.             newcolors[i] = (byte) 0;// rand.nextInt(256);
  50.             newcolors[i + 1] = (byte) 255; //rand.nextInt(256);
  51.             newcolors[i + 2] = (byte) 0;// rand.nextInt(256);
  52.             newcolors[i + 3] = (byte) 255;
  53.         }
  54.  
  55.         mFVertexBuffer = ChibiUtil.makeFloatBuffer(verts);
  56.         mNormalBuffer = ChibiUtil.makeFloatBuffer(normals);
  57.  
  58.         mColorBuffer = ByteBuffer.allocateDirect(newcolors.length);
  59.         mColorBuffer.put(newcolors);
  60.         mColorBuffer.position(0);
  61.  
  62.         ByteBuffer ib = ByteBuffer.allocateDirect(faces.length * 2);
  63.         ib.order(ByteOrder.nativeOrder());
  64.         mTfan3 = ib.asShortBuffer();
  65.         mTfan3.put(faces);
  66.         mTfan3.position(0);
  67.  
  68.        
  69.  
  70.     }
  71.  
  72.     public void draw(GL2 gl) {
  73.        
  74.         gl.glEnableClientState(GL2.GL_NORMAL_ARRAY);
  75.         gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
  76.         gl.glEnableClientState(GL2.GL_COLOR_ARRAY);
  77.        
  78.         gl.glNormalPointer(GL.GL_FLOAT, 0, mNormalBuffer);
  79.         gl.glVertexPointer(3, GL.GL_FLOAT, 0, mFVertexBuffer);
  80.         gl.glColorPointer(4, GL.GL_UNSIGNED_BYTE, 0, mColorBuffer);
  81.         gl.glDrawElements(GL2.GL_LINES, faces.length , GL2.GL_UNSIGNED_SHORT,
  82.                 mTfan3);
  83.  
  84.     }
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment