Why is glDrawElements() not drawing anything?

Vinegare01 :

I've seen a couple of questions about the same topic but still couldn't figure out what was wrong with my code.

It worked to get a window running, where a keypress would change the background color with glClearColor().

But now, when trying to draw a quad onto the screen, the screen stays black. A little summary of how I think it should work:

How mesh is created:

public Mesh(Vertex[] verts, int[] indices) {
    this.verts = verts;
    this.indices = indices;
}

//Differend class
public Mesh mesh = new Mesh(new Vertex[] {
        new Vertex(new Vector3f(-0.5f,  0.5f, 0.0f)),
        new Vertex(new Vector3f(-0.5f, -0.5f, 0.0f)),
        new Vertex(new Vector3f( 0.5f, -0.5f, 0.0f)),
        new Vertex(new Vector3f( 0.5f,  0.5f, 0.0f))
    }, new int[] {
        0, 1, 2,
        0, 3, 2
    });

For initializing the game, this is called:

public void create() {
    vao = GL30.glGenVertexArrays();
    GL30.glBindVertexArray(vao);

    FloatBuffer positionBuffer = MemoryUtil.memAllocFloat(verts.length * 3);
    float[] positionData = new float[verts.length * 3];
    for (int i = 0; i < verts.length; i++) {
        positionData[i * 3] = verts[i].getPosition().getX();
        positionData[i * 3 + 1] = verts[i].getPosition().getY();
        positionData[i * 3 + 2] = verts[i].getPosition().getZ();
    }

    positionBuffer.put(positionData).flip();

    pbo = GL15.glGenBuffers();
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, pbo);
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, positionBuffer, GL15.GL_STATIC_DRAW);
    GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

    IntBuffer indexBuffer = MemoryUtil.memAllocInt(indices.length * 3);
    indexBuffer.put(indices).flip();

    ibo = GL15.glGenBuffers();
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, ibo);
    GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indexBuffer, GL15.GL_STATIC_DRAW);
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
}

And lastly, the mesh should be rendered with this:

public void renderMesh(Mesh mesh) {
    clear();

    GL30.glBindVertexArray(mesh.getVAO());
    GL30.glEnableVertexAttribArray(0);
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, mesh.getIBO());
    GL11.glDrawElements(GL11.GL_TRIANGLES, mesh.getIndices().length, GL11.GL_FLOAT, 0);
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
    GL30.glDisableVertexAttribArray(0);
    GL30.glBindVertexArray(0);
}

I've checked that the methods are actually invoked by using System.out.println. The game's input still works. It's just that the screen is black and not showing any quad. Why is it not drawing anything onto the screen?

Rabbid76 :

The type argument which is set to glDrawElements hs to correspond to the index buffer, rather than the vertex buffer.
In your case it has to be GL_UNSIGNED_INT rather than GL_FLOAT. See glDrawElements.

GL11.glDrawElements(GL11.GL_TRIANGLES, mesh.getIndices().length, GL11.GL_FLOAT, 0);

GL11.glDrawElements(GL11.GL_TRIANGLES, mesh.getIndices().length, GL11.GL_UNSIGNED_INT, 0);

Note, GL_FLOAT is not an accepted value for glDrawElements and will cause an INVALID_ENUM error.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=422779&siteId=1