Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.nio.ByteBuffer;
- import java.nio.FloatBuffer;
- import java.nio.ShortBuffer;
- import javax.media.opengl.GL;
- import javax.media.opengl.GL2;
- import javax.media.opengl.GLAutoDrawable;
- import javax.media.opengl.GLEventListener;
- import javax.media.opengl.glu.GLU;
- public class Drawer implements GLEventListener
- {
- private FloatBuffer vertexPositions;
- private ShortBuffer indices;
- private final int[] vboID = new int[2];
- private final int[] iboID = new int[1];
- private final int componentsPerVertex = 3;
- private final int verticesSize = componentsPerVertex * 4; // 4 vertices
- private final int verticesSizeInBytes = verticesSize * 4; // à 4 bytes
- private final int indicesSize = 6;
- private final int indicesSizeInBytes = indicesSize * 2; // à 2 bytes
- private GLU glu;
- public Drawer()
- {
- glu = new GLU();
- }
- @Override
- public void display(GLAutoDrawable drawable)
- {
- GL2 gl = drawable.getGL().getGL2();
- gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
- gl.glLoadIdentity();
- gl.glTranslatef(-2.0f, 0.0f, -5.0f);
- gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vboID[0]);
- gl.glDrawElements(GL2.GL_TRIANGLES, indicesSize, GL2.GL_UNSIGNED_SHORT, 0);
- gl.glTranslatef(4.0f, 0.0f, 0.0f);
- gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vboID[1]);
- gl.glDrawElements(GL2.GL_TRIANGLES, indicesSize, GL2.GL_UNSIGNED_SHORT, 0);
- printOpenGLErrors(gl, glu);
- gl.glFlush();
- drawable.swapBuffers();
- }
- @Override
- public void dispose(GLAutoDrawable drawable)
- {
- GL2 gl = drawable.getGL().getGL2();
- gl.glDeleteBuffers(2, vboID, 0);
- gl.glDeleteBuffers(1, iboID, 0);
- }
- @Override
- public void init(GLAutoDrawable drawable)
- {
- System.out.println("Drawer.init()");
- GL2 gl = drawable.getGL().getGL2();
- // move this output to debug class or somewhere else, but don't keep it here
- System.out.println("GL_VENDOR: " + gl.glGetString(GL2.GL_VENDOR));
- System.out.println("GL_RENDERER: " + gl.glGetString(GL2.GL_RENDERER));
- System.out.println("GL_VERSION: " + gl.glGetString(GL2.GL_VERSION));
- gl.glDepthFunc(GL2.GL_LEQUAL);
- gl.glEnable(GL2.GL_DEPTH_TEST);
- gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
- setUPVBO(gl);
- setUPIBO(gl);
- gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
- // set data layout in openGL
- gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vboID[0]);
- gl.glVertexPointer(3, GL2.GL_FLOAT, 0, 0);
- gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vboID[1]);
- gl.glVertexPointer(3, GL2.GL_FLOAT, 0, 0);
- }
- private void setUPVBO(GL2 gl)
- {
- // create vbo
- gl.glGenBuffers(2, vboID, 0);
- // create buffer for vbo
- float[] verticesPlain = new float[] { -1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f, -1.0f, 1.0f, 0.0f };
- vertexPositions = ByteBuffer.allocateDirect(verticesSizeInBytes).asFloatBuffer();
- vertexPositions.put(verticesPlain);
- // DEBUG
- System.out.println("dumping vertexPositions");
- vertexPositions.rewind();
- for (int i = 0; vertexPositions.hasRemaining(); ++i)
- {
- if ((i % componentsPerVertex) == 0 && i != 0)
- {
- System.out.println();
- }
- System.out.print(vertexPositions.get() + " ");
- }
- System.out.println();
- // ------
- // upload data
- gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vboID[0]);
- gl.glBufferData(GL.GL_ARRAY_BUFFER, verticesSizeInBytes, null, GL2.GL_STATIC_DRAW);
- gl.glBufferSubData(GL2.GL_ARRAY_BUFFER, 0, verticesSizeInBytes, vertexPositions);
- gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vboID[1]);
- gl.glBufferData(GL.GL_ARRAY_BUFFER, verticesSizeInBytes, null, GL2.GL_STATIC_DRAW);
- gl.glBufferSubData(GL2.GL_ARRAY_BUFFER, 0, verticesSizeInBytes, vertexPositions);
- vertexPositions = null;
- }
- private void setUPIBO(GL2 gl)
- {
- //create ibo
- gl.glGenBuffers(1, iboID, 0);
- // create buffer for ibo
- short[] indicesPlain = new short[] { 0, 1, 2, 2, 3, 0 };
- indices = ByteBuffer.allocateDirect(indicesSizeInBytes).asShortBuffer();
- indices.put(indicesPlain);
- // DEBUG
- System.out.println("dumping indices");
- indices.rewind();
- while (indices.hasRemaining())
- {
- System.out.print(indices.get() + " ");
- }
- System.out.println();
- // ---------
- //upload data
- gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, iboID[0]);
- gl.glBufferData(GL.GL_ELEMENT_ARRAY_BUFFER, indicesSizeInBytes, indices, GL2.GL_STATIC_DRAW);
- indices = null;
- }
- @Override
- public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
- {
- GL2 gl = drawable.getGL().getGL2();
- // build view matrix
- if (height <= 0) // avoid a dividy by zero
- {
- height = 1;
- }
- gl.glViewport(0, 0, width, height);
- gl.glMatrixMode(GL2.GL_PROJECTION);
- gl.glLoadIdentity();
- float aspectRatio = (float) width / (float) height;
- glu.gluPerspective(60.0f, aspectRatio, 2.0, 200.0);
- // change back to world matrix
- gl.glMatrixMode(GL2.GL_MODELVIEW);
- }
- public boolean printOpenGLErrors(GL2 gl, GLU glu)
- {
- int error;
- boolean err = false;
- while ((error = gl.glGetError()) != GL2.GL_NO_ERROR)
- {
- System.out.println("OpenGL Error: " + glu.gluErrorString(error));
- err = true;
- }
- return err;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement