#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”);
glutDisplayFunc(myDisplay);
//glutReshapeFunc(myReshape);
//glutMouseFunc(myMouse);
//glutKeyboardFunc(myKeyboard);
myInit();
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)
{
GLubyte Pixel;
glReadPixels(50, 50, 1, 1, GL_RED, GL_UNSIGNED_BYTE, &Pixel);//
int ret = glGetError();
cerr << ret;
Point seed = {300, 180};
Stack stack;
stack.Push(seed);
glColor3ub(100, 0, 0);
while(!stack.IsEmpty())
{
Point point;
stack.Pop(point);
glBegin(GL_POINTS);
glVertex2i(point.x, point.y);
glEnd();
GLint xi = point.x + 1;
GLubyte iPixel = 0;
glReadPixels(50, 50, 1, 1, GL_RED, GL_UNSIGNED_BYTE, &iPixel);//
while(iPixel != BORDER_COLOR)//向右填充
{
glBegin(GL_POINTS);
glVertex2i(xi, point.y);
glEnd();
xi++;
glReadPixels(xi, point.y, 1, 1, GL_RED, GL_UNSIGNED_BYTE, &iPixel);
}
GLint xRight = xi--;//记录最右点
xi = point.x - 1;
glReadPixels(xi, point.y, 1, 1, GL_RED, GL_UNSIGNED_BYTE, &iPixel);
while(int(iPixel) != BORDER_COLOR)
{
glBegin(GL_POINTS);
glVertex2i(xi, point.y);
glEnd();
xi--;
glReadPixels(xi, point.y, 1, 1, GL_RED, GL_UNSIGNED_BYTE, &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_UNSIGNED_BYTE, &iPixel);
while(iPixel != BORDER_COLOR && iPixel != FILL_COLOR && xi <= xRight)
{
xi++;
glReadPixels(xi, point.y + i, 1, 1, GL_RED, GL_UNSIGNED_BYTE, &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_UNSIGNED_BYTE, &iPixel);
}
}
}
}
}
void DrawRegion(void)
{
glColor3ub(0, 0, 0);
glBegin(GL_LINE_LOOP);
glVertex2i(50, 50);
glVertex2i(180, 300);
glVertex2i(300, 200);
glVertex2i(450, 320);
glVertex2i(350, 80);
glVertex2i(250, 150);
glVertex2i(150, 150);
glVertex2i(50, 50);
glEnd();
GLubyte Pixel;
glReadPixels(50 , 50, 1, 1, GL_RED, GL_UNSIGNED_BYTE, &Pixel);
glFlush();
cout << int(Pixel) << " ";
}
This is my program. I run it step by step with gdb. The first glReadPixels in DrawRegion returns a right result of Pixel(0).
But when it returns from the DrawRegion() and get into the RegionFill(), the glReadPixels at the first line of RegionFill returns a wrong result(118).Why the glReadPixels() in two functions executes one after another returns the different results.
I run the program on T23 with ubuntu. Thanks.