我正在尝试使用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