opengl如何读取一点像素的颜色?
#include <GL/gl.h>
#include <GL/glut.h>
#include <GL/glu.h>
#include <iostream>
#include "stack.h"
#define FILL_COLOR 100
#define BORDER_COLOR 0
using namespace std;
void DrawRegion();
void myInit(void);
void myDisplay(void);
void RegionFill(void);
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(100, 150);
glutCreateWindow("my first attempt");
myInit();
glutDisplayFunc(myDisplay);
//glutReshapeFunc(myReshape);
//glutMouseFunc(myMouse);
//glutKeyboardFunc(myKeyboard);
glutMainLoop();
return 0;
}
void myInit(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glPointSize(1.0);//设置点的大小为一个像素
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
DrawRegion();
RegionFill();
glFlush();
}
void RegionFill(void)
{
Point seed = {300, 180};
Stack stack;
stack.Push(seed);
glColor3ub(100, 0, 0);
glBegin(GL_POINTS);
while(!stack.IsEmpty())
{
Point point;
stack.Pop(point);
glVertex2i(point.x, point.y);
GLint xi = point.x + 1;
GLubyte iPixel = 88;
glReadPixels(xi, point.y, 1, 1, GL_RED, GL_UNSIGNED_BYTE, &iPixel);//读取一点的红色通道值
cerr << int(iPixel) <<" "<<xi << point.y;
while(iPixel != BORDER_COLOR)//向右填充
{
glVertex2i(xi, point.y);
xi++;
glReadPixels(xi, point.y, 1, 1, GL_RED, GL_UNSIGNED_INT, &iPixel);
}
GLint xRight = xi--;//记录最右点
xi = point.x - 1;
glReadPixels(xi, point.y, 1, 1, GL_RED, GL_INT, &iPixel);
while(&iPixel != BORDER_COLOR)
{
glVertex2i(xi, point.y);
xi--;
glReadPixels(xi, point.y, 1, 1, GL_RED, GL_INT, &iPixel);
}
//GLint xLeft = xi++;//记录最左点
for(int i = -1; i <= 1; i += 2)
{
while(xi <= xRight)
{
glReadPixels(xi, point.y + i, 1, 1, GL_RED, GL_INT, &iPixel);
// while(iPixel != BORDER_COLOR && iPixel != FILL_COLOR && xi <= xRight)
{
xi++;
glReadPixels(xi, point.y + i, 1, 1, GL_RED, GL_INT, &iPixel);
}
Point tmpPoint = {xi - 1, point.y + i};
stack.Push(tmpPoint);
// while(iPixel == BORDER_COLOR || iPixel == FILL_COLOR && xi <= xRight)
{
xi++;
glReadPixels(xi, point.y + i, 1, 1, GL_RED, GL_INT, &iPixel);
}
}
}
}
glEnd();
}
void DrawRegion(void)
{
glColor3ub(0, 0, 0);
glBegin(GL_LINES);
glVertex2i(50, 50);
glVertex2i(180, 300);
glVertex2i(180, 300);
glVertex2i(300, 200);
glVertex2i(300, 200);
glVertex2i(450, 320);
glVertex2i(450, 320);
glVertex2i(350, 80);
glVertex2i(350, 80);
glVertex2i(250, 150);
glVertex2i(250, 150);
glVertex2i(150, 150);
glVertex2i(150, 150);
glVertex2i(50, 50);
glEnd();
glFlush();
}
输出的值总是88,感觉glReadPixels没执行过,也没错误信息