Advertisement
Guest User

joglTestApplication

a guest
Aug 19th, 2011
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.21 KB | None | 0 0
  1. import java.nio.ByteBuffer;
  2. import java.nio.FloatBuffer;
  3. import java.nio.ShortBuffer;
  4.  
  5. import javax.media.opengl.GL;
  6. import javax.media.opengl.GL2;
  7. import javax.media.opengl.GLAutoDrawable;
  8. import javax.media.opengl.GLEventListener;
  9. import javax.media.opengl.glu.GLU;
  10.  
  11. public class Drawer implements GLEventListener
  12. {
  13.     private FloatBuffer vertexPositions;
  14.     private ShortBuffer indices;
  15.     private final int[] vboID                       = new int[2];
  16.     private final int[] iboID                       = new int[1];
  17.     private final int       componentsPerVertex = 3;
  18.     private final int       verticesSize            = componentsPerVertex * 4; // 4 vertices
  19.     private final int       verticesSizeInBytes = verticesSize * 4;         // à 4 bytes
  20.     private final int       indicesSize             = 6;
  21.     private final int       indicesSizeInBytes  = indicesSize * 2;          // à 2 bytes
  22.     private GLU             glu;
  23.    
  24.    
  25.     public Drawer()
  26.     {
  27.         glu = new GLU();
  28.     }
  29.    
  30.     @Override
  31.     public void display(GLAutoDrawable drawable)
  32.     {
  33.         GL2 gl = drawable.getGL().getGL2();
  34.        
  35.         gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
  36.        
  37.         gl.glLoadIdentity();
  38.        
  39.         gl.glTranslatef(-2.0f, 0.0f, -5.0f);
  40.         gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vboID[0]);
  41.         gl.glDrawElements(GL2.GL_TRIANGLES, indicesSize, GL2.GL_UNSIGNED_SHORT, 0);
  42.        
  43.         gl.glTranslatef(4.0f, 0.0f, 0.0f);
  44.         gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vboID[1]);
  45.         gl.glDrawElements(GL2.GL_TRIANGLES, indicesSize, GL2.GL_UNSIGNED_SHORT, 0);
  46.        
  47.         printOpenGLErrors(gl, glu);
  48.        
  49.         gl.glFlush();
  50.        
  51.         drawable.swapBuffers();
  52.     }
  53.    
  54.     @Override
  55.     public void dispose(GLAutoDrawable drawable)
  56.     {
  57.         GL2 gl = drawable.getGL().getGL2();
  58.        
  59.         gl.glDeleteBuffers(2, vboID, 0);
  60.         gl.glDeleteBuffers(1, iboID, 0);
  61.     }
  62.    
  63.     @Override
  64.     public void init(GLAutoDrawable drawable)
  65.     {
  66.         System.out.println("Drawer.init()");
  67.        
  68.         GL2 gl = drawable.getGL().getGL2();
  69.        
  70.         // move this output to debug class or somewhere else, but don't keep it here
  71.         System.out.println("GL_VENDOR: " + gl.glGetString(GL2.GL_VENDOR));
  72.         System.out.println("GL_RENDERER: " + gl.glGetString(GL2.GL_RENDERER));
  73.         System.out.println("GL_VERSION: " + gl.glGetString(GL2.GL_VERSION));
  74.        
  75.         gl.glDepthFunc(GL2.GL_LEQUAL);
  76.         gl.glEnable(GL2.GL_DEPTH_TEST);
  77.        
  78.         gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  79.        
  80.         setUPVBO(gl);
  81.         setUPIBO(gl);
  82.        
  83.         gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
  84.        
  85.         // set data layout in openGL
  86.         gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vboID[0]);
  87.         gl.glVertexPointer(3, GL2.GL_FLOAT, 0, 0);
  88.        
  89.         gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vboID[1]);
  90.         gl.glVertexPointer(3, GL2.GL_FLOAT, 0, 0);
  91.     }
  92.    
  93.     private void setUPVBO(GL2 gl)
  94.     {
  95.         // create vbo
  96.         gl.glGenBuffers(2, vboID, 0);
  97.        
  98.         // create buffer for vbo
  99.         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 };
  100.        
  101.         vertexPositions = ByteBuffer.allocateDirect(verticesSizeInBytes).asFloatBuffer();
  102.         vertexPositions.put(verticesPlain);
  103.        
  104.         // DEBUG
  105.         System.out.println("dumping vertexPositions");
  106.         vertexPositions.rewind();
  107.         for (int i = 0; vertexPositions.hasRemaining(); ++i)
  108.         {
  109.             if ((i % componentsPerVertex) == 0 && i != 0)
  110.             {
  111.                 System.out.println();
  112.             }
  113.             System.out.print(vertexPositions.get() + " ");
  114.         }
  115.         System.out.println();
  116.         // ------
  117.        
  118.         // upload data
  119.         gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vboID[0]);
  120.         gl.glBufferData(GL.GL_ARRAY_BUFFER, verticesSizeInBytes, null, GL2.GL_STATIC_DRAW);
  121.         gl.glBufferSubData(GL2.GL_ARRAY_BUFFER, 0, verticesSizeInBytes, vertexPositions);
  122.        
  123.         gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vboID[1]);
  124.         gl.glBufferData(GL.GL_ARRAY_BUFFER, verticesSizeInBytes, null, GL2.GL_STATIC_DRAW);
  125.         gl.glBufferSubData(GL2.GL_ARRAY_BUFFER, 0, verticesSizeInBytes, vertexPositions);
  126.        
  127.         vertexPositions = null;
  128.     }
  129.    
  130.     private void setUPIBO(GL2 gl)
  131.     {
  132.         //create ibo
  133.         gl.glGenBuffers(1, iboID, 0);
  134.        
  135.         // create buffer for ibo
  136.         short[] indicesPlain = new short[] { 0, 1, 2, 2, 3, 0 };
  137.        
  138.         indices = ByteBuffer.allocateDirect(indicesSizeInBytes).asShortBuffer();
  139.         indices.put(indicesPlain);
  140.        
  141.         // DEBUG
  142.         System.out.println("dumping indices");
  143.         indices.rewind();
  144.         while (indices.hasRemaining())
  145.         {
  146.             System.out.print(indices.get() + " ");
  147.         }
  148.         System.out.println();
  149.         // ---------
  150.        
  151.         //upload data
  152.         gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, iboID[0]);
  153.         gl.glBufferData(GL.GL_ELEMENT_ARRAY_BUFFER, indicesSizeInBytes, indices, GL2.GL_STATIC_DRAW);
  154.        
  155.         indices = null;
  156.     }
  157.    
  158.     @Override
  159.     public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
  160.     {
  161.         GL2 gl = drawable.getGL().getGL2();
  162.        
  163.         // build view matrix
  164.         if (height <= 0) // avoid a dividy by zero
  165.         {
  166.             height = 1;
  167.         }
  168.        
  169.         gl.glViewport(0, 0, width, height);
  170.        
  171.         gl.glMatrixMode(GL2.GL_PROJECTION);
  172.         gl.glLoadIdentity();
  173.        
  174.         float aspectRatio = (float) width / (float) height;
  175.         glu.gluPerspective(60.0f, aspectRatio, 2.0, 200.0);
  176.        
  177.         // change back to world matrix
  178.         gl.glMatrixMode(GL2.GL_MODELVIEW);
  179.     }
  180.    
  181.     public boolean printOpenGLErrors(GL2 gl, GLU glu)
  182.     {
  183.         int error;
  184.         boolean err = false;
  185.         while ((error = gl.glGetError()) != GL2.GL_NO_ERROR)
  186.         {
  187.             System.out.println("OpenGL Error: " + glu.gluErrorString(error));
  188.             err = true;
  189.         }
  190.        
  191.         return err;
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement