#include <GL/gl.h>
#include <GL/glut.h>
#include <GL/glu.h>
#include <iostream>
#include "stack.h"#define FILL_COLOR 100
#define BORDER_COLOR 255using 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);
    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(xi, point.y, 1, 1, GL_RED, GL_UNSIGNED_BYTE, &iPixel);//读取一点的红色通道值
        int ret = glGetError();
        cerr << int(iPixel) <<" " << ret;
        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);
        }
        cerr << xi;
        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_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();
    GLubyte Pixel;
    glReadPixels(50, 50, 1, 1, GL_RED, GL_UNSIGNED_BYTE, &Pixel);
    cerr << int(Pixel)<< " ";
}
输出的结果终端的时候是0 118 0,gdb的时候255 0 0
还都是感觉不正确的