Jump to content
  • Advertisement
Sign in to follow this  
Scorcher24

Again glReadPixels Problem

This topic is 4863 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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


Link to post
Share on other sites
Advertisement
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
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
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
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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!