Jump to content
  • Advertisement
Sign in to follow this  
DominicHughes

OpenGL How to use glReadPixels ? OpenGL 3.0

This topic is 2270 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

Hello I was hoping to be able to read the pixels under my mouse cursor but I can't seem to get the function to return the correct values it just crashes.

Has anyone successfully used this function and could tell me what I'm doing wrong and how to use the function?



static unsigned char *buf = new unsigned char[( 3 * (1000) * (1000) )];

GetCursorPos(&pos);

cout << pos.x << endl;
cout << pos.y << endl;

glReadPixels(pos.x, pos.y, 1000, 1000, GL_RGBA ,GL_UNSIGNED_BYTE, buf);

cout << buf << endl;



OpenGL 3.0
WIN32 API
C++ Edited by DarkHorseKnight

Share this post


Link to post
Share on other sites
Advertisement
Two major things:

  1. The corodinate used by the Win32 API has the origin in the upper left corner, while OpenGL has the origin in the lower left corner. You must thus convert the y-coordinate as new_y = height-1-y.
  2. You allocate a 1000x1000 3-channel image and ask for a 1000-1000 4-channel image, so you're overrunning the allocated byffer by one third of its size.

Share this post


Link to post
Share on other sites
Okay its returning blank data in the command prompt window now do you know what could be causing this?

Because I'm confused...


glReadPixels(pos.x, pos.y, 1000, 1000, GL_RGB ,GL_UNSIGNED_BYTE, buf);
Edited by DarkHorseKnight

Share this post


Link to post
Share on other sites
That line by itself is not wrong, it can only be wrong in some context defined by the rest of your program and your expectations. You need to describe the context in which that particular line is not behaving as you expect.

Share this post


Link to post
Share on other sites
I want to be able to read pixels under the mouse cursor smile.png thats what this function does right or kinda does atleast?

This glReadPixels function is inside a case statement WM_LBUTTONDOWN

I use:

GetCursorPos(&pos);

To get the x and y values for the glReadPixels

so :


glReadPixels(pos.x, pos.y, 1000, 1000, GL_RGB ,GL_UNSIGNED_BYTE, buf);



[color=#666600]Should work

[color=#666600]Here is the actual code :

[color=#666600]

case WM_LBUTTONDOWN:



GetCursorPos(&pos);

//for loops are awesome!

cout << pos.x << endl;
cout << pos.y << endl;

glReadPixels(pos.x, pos.y, 1000, 1000, GL_RGB ,GL_UNSIGNED_BYTE, buf);

cout << buf << endl;

break;


Edited by DarkHorseKnight

Share this post


Link to post
Share on other sites
You are not adjusting for my first point, and I don't know what you expect to see when printing the pointer storing the image.

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!