vbo - glDrawElements problem

I want to draw something using vbo and indices but my code doesn’t work - I get a black screen. What’s wrong ?

Creating vbo’s:


  vboVertices = new float[]{3.0f, 3.0f, 3.0f, 1.0f, 
                            -3.0f, 3.0f, 3.0f, 1.0f,
                            -3.0f, -3.0f, 3.0f, 1.0f,
                            3.0f, -3.0f, 3.0f, 1.0f,
                            -3.0f, 3.0f, -3.0f, 1.0f,
                            3.0f, 3.0f, -3.0f, 1.0f,
                            3.0f, -3.0f, -3.0f, 1.0f,
                            -3.0f, -3.0f, -3.0f, 1.0f};
        
  vboIndices = new byte[]{0,1, 0,3, 0,5,
                          1,4, 1,2, 2,7,
                          2,3, 3,6, 4,7,
                          4,5, 5,6, 6,7};

  int buffer[] = new int[2];
  gl.glGenBuffers(2, IntBuffer.wrap(buffer));
  vbo[0] = buffer[0];
  vbo[1] = buffer[1];

  gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, vbo[0]);
  gl.glBufferData(GL3.GL_ARRAY_BUFFER, vboVertices.length * Sizeof.FLOAT, FloatBuffer.wrap(vboVertices), GL3.GL_DYNAMIC_DRAW);
  gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, 0);
        
  gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, vbo[1]);
  gl.glBufferData(GL3.GL_ELEMENT_ARRAY_BUFFER, vboIndices.length * Sizeof.BYTE, ByteBuffer.wrap(vboIndices), GL3.GL_DYNAMIC_DRAW);
  gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, 0);

Rendering


gl.glUseProgram(shaderProgramID);
projectionMatrixUniform = gl.glGetUniformLocation(shaderProgramID, "pMatrix");
gl.glUniformMatrix4fv(projectionMatrixUniform, 1, false, projectionMatrix, 0);
modelViewMatrixUniform = gl.glGetUniformLocation(shaderProgramID, "mMatrix");
gl.glUniformMatrix4fv(modelViewMatrixUniform, 1, false, modelViewMatrix, 0);
colorUniform = gl.glGetUniformLocation(shaderProgramID, "color");
gl.glUniform4f(colorUniform, 1.0f, 1.0f, 1.0f, 1.0f);

gl.glEnableVertexAttribArray(0);
gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, vbo[0]);
gl.glVertexAttribPointer(0, 4, GL3.GL_FLOAT, false, 0, 0);
gl.glBindBuffer(GL3.GL_ELEMENT_ARRAY_BUFFER, vbo[1]);
gl.glDrawElements(GL3.GL_LINES, 12, GL3.GL_BYTE, 0);
gl.glDisableVertexAttribArray(0);
gl.glUseProgram(0);


Are you sure that the location of whatever attribute you are trying to set with

gl.glVertexAttribPointer(0, 4, GL3.GL_FLOAT, false, 0, 0);

is 0? What does your shader look like?

Vertex shader:


private static String vertexShader =
    "#version 330" + "\n" +
    "layout (location = 0) in vec4 position;" + "\n" +
    "uniform mat4 pMatrix;" + "\n" +
    "uniform mat4 mMatrix;" + "\n" +
    "void main()" + "\n" +
    "{" + "\n" +
    "gl_Position = pMatrix * mMatrix * position;" + "\n" +
    "}";

Fragment shader:


private static String fragmentShader =
    "#version 330" + "\n" + 
    "out vec4 outputColor;" + "\n" +
    "uniform vec4 color;" + "\n" +   
    "void main()" + "\n" +
    "{" + "\n" +
    "outputColor = color;" + "\n" +
    "}";      

Try using a DebugGL to see if you’ve made an OpenGL error, which prevent anything from rendering.

Exception in thread “AWT-EventQueue-0-AWTAnimator-1” javax.media.opengl.GLException: javax.media.opengl.GLException: Thread[AWT-EventQueue-0,6,main] glGetError() returned the following error codes after a call to glBufferData( 0x8893, 24, <java.nio.Buffer> java.nio.HeapByteBuffer[pos=0 lim=24 cap=24], 0x88E8): GL_INVALID_OPERATION ( 1282 0x502),

Caused by: javax.media.opengl.GLException: Thread[AWT-EventQueue-0,6,main] glGetError() returned the following error codes after a call to glBufferData( 0x8893, 24, <java.nio.Buffer> java.nio.HeapByteBuffer[pos=0 lim=24 cap=24], 0x88E8): GL_INVALID_OPERATION ( 1282 0x502)

refer to:


gl.glBufferData(GL3.GL_ELEMENT_ARRAY_BUFFER, vboFrameIndices.length * Sizeof.BYTE, ByteBuffer.wrap(vboFrameIndices), GL3.GL_DYNAMIC_DRAW);

Exception in thread “AWT-EventQueue-0” javax.media.opengl.GLException: Thread[AWT-EventQueue-0,6,main] glGetError() returned the following error codes after a call to glDrawElements( 0x1, 0xC, 0x1400, 0): GL_INVALID_ENUM ( 1280 0x500)

refer to:


gl.glDrawElements(GL3.GL_LINES, 12, GL3.GL_BYTE, 0);

Regarding the 1st exception, you don’t have an element array vbo bound when you call glBufferData. In your code, change your call GL_ARRAY_BUFFER to GL_ELEMENT_ARRAY_BUFFER in your calls to glBindBuffer() before and after the call glBufferData(GL_ELEMENT_ARRAY_BUFFER, …).

Regarding the 2nd exception, the docs say that the INVALID_ENUM is generated when the mode argument (1st) is not an accepted value. However, GL_LINES is valid. I did notice that GL_BYTE is not an accepted type value, you can only use UNSIGNED_x types so you’d need to switch it to GL_UNSIGNED_BYTE. I don’t know why their documentation doesn’t say that an unsupported type will cause an error, but it’s likely the cause.

You don’t need to worry about changing the byte values since BYTE and UNSIGNED_BYTE have the same bit patterns when they are from 0 to 127 (if you need values higher than that but still fit into 0-255, just cast the higher values to byte and it will work out for you).

GL_BYTE being unsupported is completely logical. You can’t have negative indices, can you? :wink: Good job spotting it though!

I think its logical from a theoretical point of view, the docs just don’t say an error is raised if you pass it in, which is what I thought was strange (expected it to be supported but not make sense, or have a documented error).