Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.nio.ByteBuffer;
- import java.nio.ByteOrder;
- import java.nio.FloatBuffer;
- import java.nio.IntBuffer;
- import java.nio.ShortBuffer;
- import java.util.Random;
- import javax.media.opengl.GL;
- import javax.media.opengl.GL2;
- public class Square {
- FloatBuffer mFVertexBuffer;
- ByteBuffer mColorBuffer;
- ShortBuffer mTfan3;
- FloatBuffer mNormalBuffer;
- float verts[] = null;
- float normals[] = null;
- // short normalsi[] = null;
- short faces[] = null;
- byte newcolors[] = null;
- public Square() {
- try {
- ObjParser.parse(new FileInputStream("res/sphere.obj"));
- verts = ObjParser.vertices;
- faces = ObjParser.faces;
- normals = ObjParser.normals;
- } catch (Exception e) {
- e.printStackTrace();
- }
- //ObjParser.toFile();
- Random rand = new Random();
- newcolors = new byte[verts.length * 4];
- for (int i = 0; i < newcolors.length; i += 4) {
- newcolors[i] = (byte) 0;// rand.nextInt(256);
- newcolors[i + 1] = (byte) 255; //rand.nextInt(256);
- newcolors[i + 2] = (byte) 0;// rand.nextInt(256);
- newcolors[i + 3] = (byte) 255;
- }
- mFVertexBuffer = ChibiUtil.makeFloatBuffer(verts);
- mNormalBuffer = ChibiUtil.makeFloatBuffer(normals);
- mColorBuffer = ByteBuffer.allocateDirect(newcolors.length);
- mColorBuffer.put(newcolors);
- mColorBuffer.position(0);
- ByteBuffer ib = ByteBuffer.allocateDirect(faces.length * 2);
- ib.order(ByteOrder.nativeOrder());
- mTfan3 = ib.asShortBuffer();
- mTfan3.put(faces);
- mTfan3.position(0);
- }
- public void draw(GL2 gl) {
- gl.glEnableClientState(GL2.GL_NORMAL_ARRAY);
- gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
- gl.glEnableClientState(GL2.GL_COLOR_ARRAY);
- gl.glNormalPointer(GL.GL_FLOAT, 0, mNormalBuffer);
- gl.glVertexPointer(3, GL.GL_FLOAT, 0, mFVertexBuffer);
- gl.glColorPointer(4, GL.GL_UNSIGNED_BYTE, 0, mColorBuffer);
- gl.glDrawElements(GL2.GL_LINES, faces.length , GL2.GL_UNSIGNED_SHORT,
- mTfan3);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment