Recommended Posts

Hi Folks ! I have similar to other peoples a Problem with glReadPixels. I have tuhe following code:
byte* buffer = NULL;
buffer = new byte[width*height*4];
memset(buffer, 0, sizeof(byte)*width*height*4);
if (!buffer)return;
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNISGNED_BYTE, buffer);
SaveTGA(width, height, 4, &buffer);

The Problem is, that the Array of bytes remains unchanged and I get a black Image (black is normal because I zero the Array ;)). I have tried to use glReadBuffer(GL_FRONT) before caling glReadPixels, but this doesn't help. Does anyone know how to fix this ?? rya. Scorcher24

Share on other sites
A few things.
Make sure that you have a valid OpenGL context when you attempt the read.
Ensure that you're reading from the correct buffer. Are you drawing to the back buffer and doing a swap? If so, you'll need a glReadBuffer(GL_BACK).

If you want to ensure that you're getting everything you rendered you might want a glFinish() before the read pixels to make sure that everything you sent to the board is placed into the frame buffer.

Share on other sites
Quote:
 Original post by Scorcher24byte* buffer = NULL;buffer = new byte[width*height*4];memset(buffer, 0, sizeof(byte)*width*height*4);if (!buffer)return;glReadPixels(0, 0, width, height, GL_RGBA, GL_UNISGNED_BYTE, buffer);SaveTGA(width, height, 4, &buffer);
I recommend moving your NULL-pointer check to before the call to memset.

Do you get a white image when you memset the values to 255?

Share on other sites
you should always call glFlush before reading any buffers...

Share on other sites
Actually, a flush() doesn't guarantee that the rendering commands you have sent have finished. It forces the pipeline to begin execution of all those commands.

a finish() will block until the pipeline is emptied so you should have everything you sent to the pipeline in the framebuffer after it returns.

Share on other sites
Yes, I do get a white Image when setting the memset values to 255.
To my mind this means that glReadPixels() doesn't touch the buffer @ all.
Whether glReadBuffer with either GL_FRONT or GL_BACK (GL_BACK is standard, so it doesn't need to be set, when I don't change it, or ??) nor glFinish() or glFlush()
help here. I've tried them all.
What I forgot to say:
The Commands are called within a keypress, as a screenshot function. But this shouldn't make any problems, or ??
Well, the HDC is valid and supports fully opengl. I've checked that, too.

rya.
Scorcher24

Share on other sites
Try putting the block in main memory

static char image[size];

Share on other sites
I couldn't get saving screenshots to work until I did this:

glPixelStorei(GL_PACK_ALIGNMENT, 1);glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

Share on other sites

I have figured the Problem out:

I have done Screenshots per pressing F9 and executed the Code there. This was really dumb. I have tested the result with glGetError() and received always an GL_INVALID_OPERATION. So I made an extra Function and a boolean flag. When pressing the key now, the flag is set to TRUE and the screenshot function is then executed AT THE END OF THE RENDERLOOP. This was a really bad mistake by me, but we all learn... ;)

Thanks for your postings and rya.
Scorcher24

Create an account

Register a new account

• Forum Statistics

• Total Topics
627718
• Total Posts
2978790

• 9
• 21
• 14
• 12
• 42