Opengl 简单四边形渲染

Opengl simple quad rendering

你知道为什么这不起作用吗?

旧的 立即模式有效,但我想使用 VAO 和 VBO。

(PS:我知道VOA的创建应该只创建一次,但我都是用这个方法构建的,用于测试。测试后我会移动这些行)

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();
    }
}

缓冲区的大小必须以字节为单位指定(参见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);