How do I get the correct values from glReadPixels in OpenGL 3.0?

Posted by NoobScratcher on Game Development See other posts from Game Development or by NoobScratcher
Published on 2012-06-01T16:39:55Z Indexed on 2012/06/01 16:51 UTC
Read the original article Hit count: 314

Filed under:
|

I'm currently trying to Implement mouse selection into my game editor and I ran into a little problem when I look at the values stored in &pixel[0],&pixel[1],&pixel[2],&pixel[3]; I get

r: 0
g: 0
b: 0
a: 0

As you can see I'm not able to get the correct values from glReadPixels();

My 3D models are red colored using glColor3f(255,0,0);

I was hoping someone could help me figure this out.

Here is the source code:

  case WM_LBUTTONDOWN:

  {

    GetCursorPos(&pos);
    ScreenToClient(hwnd, &pos);

     GLenum err = glGetError();

     while (glGetError() != GL_NO_ERROR) {cerr << err << endl;}

     glReadPixels(pos.x, SCREEN_HEIGHT - 1 - pos.y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &pixel[0] );

    cerr << "r: "<< (int)pixel[0] << endl;
    cerr << "g: "<< (int)pixel[1] << endl;
    cerr << "b: "<< (int)pixel[2] << endl;
    cerr << "a: "<< (int)pixel[3] << endl;

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

  }

break;

I use :

WIN32 API
OPENGL 3.0
C++

© Game Development or respective owner

Related posts about c++

Related posts about opengl