Roy
Roy

Reputation: 316

glReadPixels() fails to fill ByteBuffer

Well, I'm trying to take a screenshot for a window in OpenGL using LWJGL. Here's the code:

ByteBuffer pixels = ByteBuffer.allocateDirect(800*600*4);
pixels.order(ByteOrder.nativeOrder());

while(!Display.isCloseRequested()) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
    render();
    Display.update();

    // "Screenshot" block
    if(Keyboard.isKeyDown(Keyboard.KEY_Q)) {
        pixels.clear();
        glReadPixels(0, 0, 800, 600, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
        pixels.flip();

        // pixels.position() and pixels.limit() tells us that there is nothing in the buffer
        // ...
    }
}

And...

So, what's the problem? Any ideas? Thanks in advance.

Upvotes: 0

Views: 2136

Answers (2)

Roy
Roy

Reputation: 316

I further researched Nico Bolas's answer. And realized glReadPixels indeed returned information despite swapping or not swapping the frame buffers first.

So, this is how I copied the byte buffer out.

glReadPixels(0, 0, 800, 600, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

byte[] data = new byte[800*600*4];
while(pixels.hasRemaining()) {
    int curr = pixels.position() / 4;
    int offset = (curr%800+(curr))*4;
    data[offset] = pixels.get();
    data[offset+1] = pixels.get();
    data[offset+2] = pixels.get();
    data[offset+3] = pixels.get();
}

I jumped to conclusion too quickly, that glReadPixels() didn't return anything based on its position and limit alone. Thanks, Nicol Bolas for the helpful input. =D

Upvotes: 0

Nicol Bolas
Nicol Bolas

Reputation: 473272

pixels.position() and pixels.limit() tells us that there is nothing in the buffer

Do they? I think a more foolproof method would be to look at the contents of the buffer.

Also, Display.update swaps buffers. The contents of the back buffer are undefined after a swap. So you should either read from the front buffer (not a good idea) or read the back buffer before swapping.

Upvotes: 1

Related Questions