LWJGL glDrawElements(int, int, int, long) Draws Nothing, While glDrawElements(int, IntBuffer) Draws Properly

StackOverflow https://stackoverflow.com/questions/23486563

  •  16-07-2023
  •  | 
  •  

Question

I started playing around with OpenGL 3.3+ last week, and I ran into this problem when trying to get indexed drawing to work. Right now, I'm just trying to get a triangle to draw using an IBO.

Index buffer, and indexHandle:

int[] tIndices = {
    0, 1, 2
};

IntBuffer indexBuffer = BufferUtils.createIntBuffer(tIndices.length);
indexBuffer.put(tIndices);
indexBuffer.flip();

int indexHandle = glGenBuffers();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexHandle);
glBufferData(indexHandle, indexBuffer, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

This is the main drawing loop in my program, which draws nothing:

while (!Display.isCloseRequested()) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glUseProgram(programHandle);

    glBindVertexArray(vaoHandle);
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexHandle);

    // This call does nothing v
    glDrawElements(GL_TRIANGLE_STRIP, tIndices.length, GL_UNSIGNED_INT, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    glDisableVertexAttribArray(0);

    glBindVertexArray(0);

    glUseProgram(0);
    Display.update();
}

This is the main loop with the glDrawElements(int, IntBuffer) variant (which does draw my triangle):

while (!Display.isCloseRequested()) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glUseProgram(programHandle);

    glBindVertexArray(vaoHandle);

    glEnableVertexAttribArray(0);

    // indexBuffer is an IntBuffer
    glDrawElements(GL_TRIANGLES, indexBuffer);
    glDisableVertexAttribArray(0);

    glBindVertexArray(0);

    glUseProgram(0);
    Display.update();
}

Can someone explain to me why the 1st block is not drawing anything at all? What am I doing wrong?

Also, the 2nd block runs at about 1.3k FPS on a 600x600 window, while the 1st one is about 800 FPS. Why is this?

I will provide more information in the morning if anyone needs them

Was it helpful?

Solution

Your first argument to glBufferData() is wrong for the index buffer. You have this:

glBufferData(indexHandle, indexBuffer, GL_STATIC_DRAW);

The first argument is the target, not the buffer id. It should look like this instead:

glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexBuffer, GL_STATIC_DRAW);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top