Search code examples
openglgpu2djogl

Opengl simple quad rendering


Do you have any idea why this isn't working?

The old immediate mode works but I want to use VAO and VBOs.

(PS: I know the VOA creation should only be created once, but I build it all in this method for the test. I will move thoses lines after testing)

private void allocateIndexBuffer(GL2 graphics, int[] indices) {
    int[] id = new int[1];
    graphics.glGenBuffers(1, id, 0);
    int vboId = id[0];
    graphics.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, vboId);
        
    IntBuffer buffer = IntBuffer.allocate(indices.length);
    buffer.put(0, indices);
    buffer.flip();

    graphics.glBufferData(GL2.GL_ELEMENT_ARRAY_BUFFER, indices.length, buffer, GL2.GL_DYNAMIC_DRAW);
    
    //graphics.glDeleteBuffers(vboId, buffer); TODO: clean up when on closing
}

private void allocateAttributeBuffer(GL2 graphics, int attribute, float[] data) {
    int[] id = new int[1];
    graphics.glGenBuffers(1, id, 0);
    int vboId = id[0];
    graphics.glBindBuffer(GL2.GL_ARRAY_BUFFER, vboId); //juste remplir vboId ou le remplacer à chaque fois ?

    FloatBuffer buffer = FloatBuffer.allocate(data.length);
    buffer.put(0, data);
    buffer.flip();
    
    graphics.glBufferData(GL2.GL_ARRAY_BUFFER, data.length, buffer, GL2.GL_DYNAMIC_DRAW);
    graphics.glVertexAttribPointer(0, 2, GL2.GL_FLOAT, false, 0, 0); //once the buffer is bound
    graphics.glEnableVertexAttribArray(0);
    graphics.glBindBuffer(GL2.GL_ARRAY_BUFFER, 0);
    
    //graphics.glDeleteBuffers(vboId, buffer); TODO: clean up when on closing
    //graphics.glDeleteVertexArrays(vboId, null); TODO: clean up vaos
}

@Override
protected void draw(GL2 graphics) {
    String mode = "new";
    if (mode.equals("new")) {
        float[] vertices = {
            bounds.getX(), bounds.getY(),
            bounds.getX(), bounds.getY() + bounds.getHeight(),
            bounds.getX() + bounds.getWidth(), bounds.getY() + bounds.getHeight(),
            bounds.getX() + bounds.getWidth(), bounds.getY(),
        };

        int[] indices = { 0, 1, 2, 2, 1, 3 };

        //creation vao
        int[] id = new int[1];
        graphics.glGenVertexArrays(1, id, 0);
        int vaoId = id[0];

        graphics.glBindVertexArray(vaoId);
            allocateIndexBuffer(graphics, indices);
            allocateAttributeBuffer(graphics, 0, vertices);
        graphics.glBindVertexArray(0);

        //render
        graphics.glBindVertexArray(vaoId);
            graphics.glEnableVertexAttribArray(0);
                graphics.glDrawElements(GL2.GL_TRIANGLES, indices.length, GL2.GL_UNSIGNED_INT, 0);
            graphics.glDisableVertexAttribArray(0);
        graphics.glBindVertexArray(0);
        graphics.glFlush();

    } else if (mode.equals("old")) {
        graphics.glColor3f(255, 0, 0);
        graphics.glBegin(GL2.GL_QUADS);
            graphics.glVertex2f(bounds.getX(), bounds.getY());
            graphics.glVertex2f(bounds.getX() + bounds.getWidth(), bounds.getY());
            graphics.glVertex2f(bounds.getX() + bounds.getWidth(), bounds.getY() + bounds.getHeight());
            graphics.glVertex2f(bounds.getX(), bounds.getY() + bounds.getHeight());
        graphics.glEnd();
    }
}

Solution

  • The size of the buffer has to be specified in bytes (see glBufferData);

    graphics.glBufferData(GL2.GL_ELEMENT_ARRAY_BUFFER, indices.length, buffer, GL2.GL_DYNAMIC_DRAW);

    graphics.glBufferData(GL2.GL_ELEMENT_ARRAY_BUFFER, indices.capacity() * 4,
        buffer, GL2.GL_DYNAMIC_DRAW);
    

    graphics.glBufferData(GL2.GL_ARRAY_BUFFER, data.length, buffer, GL2.GL_DYNAMIC_DRAW);

    graphics.glBufferData(GL2.GL_ARRAY_BUFFER, data.capacity() * 4,
        buffer, GL2.GL_DYNAMIC_DRAW);