LWJGL - Getting error : "Cannot use offsets when Element Array Buffer Object is disabled"

724 views Asked by At

I am trying to render a basic cube in LWJGL (Java). But the program keeps crashing, telling me that it "Cannot use offsets when Element Array Buffer Object is disabled". I am guessing that this is not the exact error right here and there might be something less obvious than that.

(I actually did the code that I'm about to show you in C++ and it was working very fine)

Init function (called once)

FloatBuffer vertices = BufferUtils.createFloatBuffer(4 * 5);
vertices.put(new float[]{
    // pos                  // Color
    0.5f, 0.5f,             1.0f, 0.0f, 0.5f,
    0.5f, -0.5f,            0.5f, 0.0f, 0.75f,
    -0.5f, -0.5f,           0.0f, 1.0f, 0.0f,
    -0.5f, 0.5f,            0.5f, 0.5f, 1.0f
});
vertices.flip();

indices = BufferUtils.createByteBuffer(2 * 3);
indices.put(new byte[]{
    0, 1, 3,
    1, 2, 3
});
indices.flip();
    
// VAO
VAO = GL30.glGenVertexArrays();
GL30.glBindVertexArray(VAO);

// VBO
VBO = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW);

// IBO
IBO = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, IBO);
glBufferData(GL_ARRAY_BUFFER, indices, GL_STATIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER, VBO);

glEnableVertexAttribArray(0);
//                    ,-- position in layout (see shader)
//                    |  ,-- Nb of component per vertex (2 for 2D (x, y))
//                    |  |                 ,-- Normalized ? (between 0 - 1)
//                    |  |                 |      ,-- Offset between things (size of a line)
//                    |  |                 |      |                ,- Where to start ?
glVertexAttribPointer(0, 2, GL11.GL_FLOAT, false, 5 * Float.SIZE , 0);
glDisableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL11.GL_FLOAT, false, 5 * Float.SIZE , 2 * Float.SIZE);
glDisableVertexAttribArray(1);

glBindBuffer(GL_ARRAY_BUFFER, IBO);

// Unbinds the VAO
GL30.glBindVertexArray(0);

And here is the render function (static for now, until I figure this out)

glUseProgram(shaderProgram.getID());

GL30.glBindVertexArray(VAO);

// Error in the line bellow
GL11.glDrawElements(GL11.GL_TRIANGLES, 4, GL11.GL_UNSIGNED_BYTE, 0);

GL30.glBindVertexArray(0);

A little help would be very appreciated. Also, I might be something very wrong that I'm not aware of, so if you spot something that would be awesome.

Thank you


Vertex Shader:

#version 330 core

layout(location = 0) in vec2 position;
layout(location = 1) in vec3 color;

out vec4 Color;

void main()
{
    gl_Position = vec4(position, 0.0, 1.0);
    Color = vec4(color, 1.0);
}

Framgent Shader:

#version 330 core

in vec4 Color;

out vec4 color;

void main()
{
    color = Color;
}
1

There are 1 answers

6
BDL On BEST ANSWER

The following lines

IBO = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, IBO);
glBufferData(GL_ARRAY_BUFFER, indices, GL_STATIC_DRAW);

are creating a new array buffer. from the name you choose and the data you copy to, I guess this is your index buffer and thus should be of type GL_ELEMENT_ARRAY_BUFFER.

What the error message is telling you is, that you draw command cannot use 0 as offset into your index buffer if there is non.

Why is there nothing drawn after fixing the index buffer?

Problem 1: You are only drawing 4 indices. If you want to draw 2 triangles, you will need 6 indices.

Problem 2: OpenGL is a statemachine and VAOs only store the last state that was set when they are unbound. This means your calls to glDisableVertexAttribArray will disable the attributes for your VAO. You should move this calls after GL30.glBindVertexArray(0);