#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
#include <windows.h>
#include <iostream>
using namespace std;
typedef float Color[3];
bool rgbColorEqual(Color c1, Color c2)
{
if ( abs(c1[1] - c2[1]) < 0.001 && abs(c1[2] - c2[2]) < 0.001 && abs(c1[0] - c2[0]) < 0.001)
return true;
else
return false;
}
void setPixel(GLint x, GLint y)
{
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
cout << x << "" << y << endl;
}
void getPixel(GLint x, GLint y, Color c)
{
glReadPixels(x, y, 1, 1, GL_RGB, GL_FLOAT, c);
}
void boundaryFill4(int x, int y, Color fillColor, Color borderColor)
{
Color interiorColor;
getPixel(x, y, interiorColor);
if (!(rgbColorEqual(interiorColor, borderColor)) && !(rgbColorEqual(interiorColor, fillColor)))
{
setPixel(x, y);
Sleep(2);
boundaryFill4(x + 1, y, fillColor, borderColor); //右
boundaryFill4(x - 1, y, fillColor, borderColor); //左
boundaryFill4(x , y+1 , fillColor, borderColor); //上
boundaryFill4(x , y-1 , fillColor, borderColor); //下
}
}
void init(void)
{
glClearColor(1.0, 10, 1.0, 0.0);
glMatrixMode(GL_PROJECTION); //指定投影矩阵
gluOrtho2D(0.0, 400.0, 0.0, 400.0); //指定显示的区域
}
void myDisplay(void)
{
Color a = { 1.0 ,0.0 ,0.0 }, b = { 0.0,0.0,1.0 };
glColor3fv(b);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINE_LOOP);
glVertex2i(100, 100);
glVertex2i(200, 100);
glVertex2i(150, 150);
glEnd();
glColor3fv(a);
boundaryFill4(150, 120, a, b);
glFlush();
}
void main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("窗口名称");
init();
glutDisplayFunc(&myDisplay);
glutMainLoop();
}
结果图