sergio
sergio

Reputation: 183

glReadPixels with screen pixels

I am trying to read the RGB value of a screen pixel doing:

#include "stdafx.h"
#include<windows.h>
#include<stdio.h>
#include <gl\GL.h>

int main(int argc, char** argv)
{
    GLubyte color[3];
    glReadPixels(800, 800, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &color);

    printf("R:%d G:%d B:%d", color[0], color[1], color[2]);

    while (1);

}

But it doesn't matter which coordinates I ask for. It always returns me R:204 G:204 B:204

What am I doing wrong?

Upvotes: 0

Views: 3550

Answers (2)

Spektre
Spektre

Reputation: 51835

As mentioned in the other answer and comments You can use glReadPixels only for pixels inside your OpenGL context window that are actually rendered at that time ... As you mentioned you have used GetPixel it hints you are using GDI. In that case you are trying to read GDI Canvas pixel from OpenGL which is not possible (the other way around is possible however but slow). So I advice to read this:

Where you can find example of both methods. If you are trying to obtain pixel from your own app then you can use different api then GetPixel.

If you're in VCL environment use Graphics::TBitmap::ScanLine property which can be used for direct pixel access without restrictions or performance hits if used properly.

On MSVC++ of GCC environment use WinAPI BitBlt but that is a bit slower and not as elegant (at least from my point of view).

Upvotes: 1

genpfault
genpfault

Reputation: 52084

glReadPixels() won't work without a current GL context. Though even if you did have a current context you can't read the system framebuffer that way.

Upvotes: 1

Related Questions