0

I am trying to read the color of a rectangle drawn on the screen, but when I try to read from a coord, it appears that it reads it from an offset but this offset is not consistent. (I am using the SDL2 library in case that helps) I have found that it is inverting the coordinates e.g. if y is 0 it actually is y_Max.

I need to know how I can read a pixel from the correct coord, I do not care what method I use but a faster one is optimal.

I do not fully understand all of the code being thrown in here, I'm pretty new to images but I know I cast my project matrix here,

    glEnable(GL_TEXTURE_2D);

    GLuint texture;
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexImage2D(GL_TEXTURE_2D, 0, image->format->BytesPerPixel, image->w, image->h, 0, GL_RGB, GL_UNSIGNED_BYTE, image->pixels);

    glOrtho(0.0f, 640.0f, 480.0f, 0.0f, -1.0f, 1.0f);

and then here I am drawing my rectangle.

glColor3f(Chrome[0], Chrome[1], Chrome[2]);
glTranslatef(0, 0, 0.0f);
glBindTexture(GL_TEXTURE_2D, texture);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(x*128.0f, 0, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(128.0f*x+128, 0.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(128.0f*x+128, 128.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(x*128.0f, 128.0f, 0.0f);
glEnd();
glPopMatrix();
                                    unsigned        char pixel[3];
glReadPixels(5, 5, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixel);
std::cout << "R: " << (int)pixel[0] << std::endl;
std::cout << "G: " << (int)pixel[1] << std::endl;
std::cout << "B: " << (int)pixel[2] << std::endl;
std::cout << "X: "  << std::endl;
std::cout << "Y: "  << std::endl;
std::cout << std::endl;
Matt Hirdler
  • 95
  • 1
  • 10
  • 1
    We need to know your Projection matrix and also if you change the ModelView matrix in any other way? – vallentin Feb 06 '14 at 10:07
  • @Vallentin I have edited my code, I do not change my modelview any other way. – Matt Hirdler Feb 06 '14 at 14:10
  • You need to run `glOrtho` in projection mode, are you doing it?. Try this, `glMatrixMode (GL_PROJECTION); glLoadIdentity();` them the `glOrtho` and then go back to ModelView `glMatrixMode(GL_MODELVIEW);` to start drawing, reading etc. – Dan Feb 06 '14 at 17:30

1 Answers1

2

I accidentally was inverting my Y value, simply fixed by inverting it on the plane. e.g (ScreenWidth-mouse_y)

Matt Hirdler
  • 95
  • 1
  • 10
  • In my case, it was because I was flipping the Y axis in order to get (0,0) in the upper left corner, with `glOrtho(0, WIDTH, HEIGHT, 0, -1, 1);`. – Roger Dahl Feb 25 '19 at 03:56