Search code examples
javaopengljogl

JOGL draw VBO black screen


I began to try to draw VBO using JOGL. Prior to that, I drew with the help of glBegin and glEnd, and everything worked. And then I see only a black screen. What could be the problem? I read somewhere that using VBO for drawing requires shaders. Is it so? Code:

public class Main implements GLEventListener {

       public static DisplayMode dm, dm_old;
       private GLU glu = new GLU();
       private float xrot,yrot,zrot;
       private int texture;
       Texture t;
       @Override
       public void display(GLAutoDrawable drawable) {
          final GL2 gl = drawable.getGL().getGL2();
          gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
          gl.glLoadIdentity(); // Reset The View
          gl.glTranslatef(0f, 0f, -5.0f);

          gl.glBindTexture(GL2.GL_TEXTURE_2D, texture);

          final float[] coordData = {
                  0, 0, //
                  1, 0, //
                  0, 1, //
          };
          final float[] vertices = {
                  -1.0f, -1.0f, 1.0f,
                  1.0f, -1.0f, 1.0f,
                  -1.0f, 1.0f, 1.0f,
          };
          // Setup the vertices into the buffer
          FloatBuffer verts = Buffers.newDirectFloatBuffer(vertices.length);
          verts.put(vertices).position(0);

          // Setup the texture coordinates
          FloatBuffer coords = Buffers.newDirectFloatBuffer(coordData.length);
          coords.put(coordData).position(0);

          gl.glVertexPointer(3, GL2.GL_FLOAT, 0, verts);
          gl.glTexCoordPointer(2, GL2.GL_FLOAT, 0, coords);
          gl.glDrawArrays(GL2.GL_TRIANGLES, 0, 3);

          //change the speeds here
          xrot += 5f;
       }

       @Override
       public void init(GLAutoDrawable drawable) {
          final GL2 gl = drawable.getGL().getGL2();
          gl.glShadeModel(GL2.GL_SMOOTH);
          gl.glClearColor(0f, 0f, 0f, 0f);
          gl.glClearDepth(1.0f);
          gl.glEnable(GL2.GL_DEPTH_TEST);
          gl.glDepthFunc(GL2.GL_LEQUAL);
          gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);

          gl.glEnable(GL2.GL_TEXTURE_2D);
          try {
             File im = new File("/home/congard/pic/t.jpeg");
             t = TextureIO.newTexture(im, true);
             texture= t.getTextureObject(gl);

          }catch(IOException e){
             e.printStackTrace();
          }
       }

       @Override
       public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
          // TODO Auto-generated method stub
       }

       public static void main(String[] args) {
            // some code
       }
}

Solution

  • You didn't enable the client-side capabilities for vertex and texture coordinates. See Client-Side Vertex Arrays and glEnableClientState.

    Add the following to your code:

    gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
    
    gl.glVertexPointer(3, GL2.GL_FLOAT, 0, verts);
    gl.glTexCoordPointer(2, GL2.GL_FLOAT, 0, coords);