Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

glReadPixels doesn't work

Tags:

c++

opengl

I'm trying to get data from pixel using glReadPixels. It was working for some time. But now it stopped working and I don't know why.

I need to make Flood fill algorithm.

glBegin(GL_POINTS);

    float target[3] = { 1.0, 1.0, 0.0 }; // target color
    float border[3] = { 1.0, 1.0, 1.0 }; // border color
    float clearp[3] = { 0.0, 0.0, 0.0 }; // clear color
    std::stack<pixel*> colored; // stack with pixels
    if (!stack.empty()) // stack contains first pixel
        colored.push(stack.top());

    while(!colored.empty()) {

        pixel *p = colored.top();
        glRasterPos2i(p->x, p->y); 
        glDrawPixels(1, 1, GL_RGB, GL_FLOAT, target);
        colored.pop();

        //up
        float pix[3];
        glReadPixels(p->x, p->y + 1, 1, 1, GL_RGB, GL_FLOAT, pix);
        if (!compare(pix,border) && compare(pix,clearp)) {
            pixel *pn = new pixel();
            pn->x = p->x;
            pn->y = p->y + 1;
            colored.push(pn);
        }
        //down
        glReadPixels(p->x, p->y - 1, 1, 1, GL_RGB, GL_FLOAT, pix);
        if (!compare(pix,border) && compare(pix,clearp)) {
            pixel *pn = new pixel();
            pn->x = p->x;
            pn->y = p->y - 1;
            colored.push(pn);
        }

        //left
        glReadPixels(p->x - 1, p->y, 1, 1, GL_RGB, GL_FLOAT, pix);
        if (!compare(pix,border) && compare(pix,clearp)) {
            pixel *pn = new pixel();
            pn->x = p->x - 1;
            pn->y = p->y;
            colored.push(pn);
        }

        //right
        glReadPixels(p->x + 1, p->y, 1, 1, GL_RGB, GL_FLOAT, pix);
        if (!compare(pix,border) && compare(pix,clearp)) {
            pixel *pn = new pixel();
            pn->x = p->x + 1;
            pn->y = p->y;
            colored.push(pn);
        }

    }
glEnd();

But array pix does not contain RGB color data. It usually something like this -1.0737418e+008

Where is the problem? It should work fine...

like image 250
lapots Avatar asked Dec 15 '25 02:12

lapots


1 Answers

It's an error to call anything but a very limited set of functions between glBegin and glEnd. That list is mostly composed of functions related to specifying attributes of a vertex (e.g., glVertex, glNormal, glColor, glTexCoord, etc.).

So, if your OpenGL implementation is following the OpenGL specification, glReadPixels should return immediately without executing because of being called within a glBegin/glEnd group. Remove those from around your calls, and glReadPixels should work as expected.

like image 98
radical7 Avatar answered Dec 16 '25 15:12

radical7



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!