glReadPixels不起作用。

3

我正在尝试使用glReadPixels从像素获得数据。

它曾经一段时间有效。但现在停止工作了,我不知道为什么。

我需要实现泛洪填充算法

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();

但是数组pix并不包含RGB颜色数据,通常是这样的-1.0737418e+008

问题出在哪里?它应该正常工作...


glGetError() 除了返回 GL_NO_ERROR(0)之外还会返回其他值吗?另外:在这里使用 glBegin(GL_POINTS) 和相应的 glEnd() 是无用的,因为你并没有绘制任何点 - 你正在绘制像素,并且使用 glReadPixels/glDrawPixels 会非常慢(根据加速图形标准)。 - user2802841
1
能否请大家不要再使用OpenGL的glReadPixels/glDrawPixels来实现洪水填充算法了?这是最低效的操作帧缓冲像素的方式。请不要这样做。 - datenwolf
2
另一种获取和设置像素颜色的方法是什么? - lapots
1个回答

8
glBeginglEnd之间调用除少量与顶点属性相关的函数(例如glVertexglNormalglColorglTexCoord等)以外的任何函数都是错误的。因此,如果你的OpenGL实现遵循OpenGL规范,那么在glBegin/glEnd组内调用glReadPixels应该立即返回而不执行。去掉这些函数的调用,并且glReadPixels应该按预期工作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接