Sign in to follow this  
Scorcher24

Again glReadPixels Problem

Recommended Posts

Scorcher24    122
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 this post


Link to post
Share on other sites
Aeluned    196
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 this post


Link to post
Share on other sites
Kalidor    1087
Quote:
Original post by Scorcher24
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);
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 this post


Link to post
Share on other sites
Aeluned    196
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 this post


Link to post
Share on other sites
Scorcher24    122
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.

Thanks for your Posts !!
rya.
Scorcher24

Share this post


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


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

Share this post


Link to post
Share on other sites
Scorcher24    122
Thanks for all your Help.

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this