文档库 最新最全的文档下载
当前位置:文档库 › opengl边界填充算法

opengl边界填充算法

#include // (or others, depending on the system in use)
#include
#include
#include

typedef float Color[3];


void init (void)
{
glClearColor (1.0, 1.0, 1.0, 0.0); // Set display-window color to white.
glMatrixMode (GL_PROJECTION); // Set projection parameters.
gluOrtho2D (0.0, 400.0, 0.0, 400.0);
}

void setPixel(GLint x,GLint y)
{
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();

glFlush();
}


void getPixel(GLint x, GLint y, Color c)
{
glReadPixels(x,y,1,1,GL_RGB,GL_FLOAT,c);
}

bool rgbColorEqual(Color c1,Color c2)
{
if((abs(c1[0]-c2[0])>0.001)||(abs(c1[1]-c2[1])>0.001)||(abs(c1[2]-c2[2])>0.001))
return 0;
else return 1;
}


void boundaryFill4 (int x, int y, Color fillColor, Color borderColor)
{
Color interiorColor;
/* Set current color to fillColor, then perform following oprations. */
getPixel (x, y, interiorColor);
glColor3f (fillColor[0], fillColor[1], fillColor[2]);
if (!(rgbColorEqual(interiorColor,borderColor)) && !(rgbColorEqual(interiorColor,fillColor))) {
setPixel (x, y); // Set color of pixel to fillColor.
boundaryFill4 (x + 1, y , fillColor, borderColor);
boundaryFill4 (x - 1, y , fillColor, borderColor);
boundaryFill4 (x , y + 1, fillColor, borderColor);
boundaryFill4 (x , y - 1, fillColor, borderColor);
}
return;
}



void myDraw (void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (0.0, 0.0, 0.0);


glBegin(GL_LINE_LOOP);
glVertex2f(20.0f, 80.0f);
glVertex2f(30.0f, 95.0f);
glVertex2f(50.0f, 95.0f);
glVertex2f(60.0f, 80.0f);
glVertex2f(70.0f, 95.0f);
glVertex2f(90.0f, 95.0f);
glVertex2f(100.0f, 80.0f);
glVertex2f(60.0f, 30.0f);

glEnd();

int x=60,y=60;
Color fillColor,borderColor;
fillColor[0]=1.0;fillColor[1]=0.0;fillColor[2]=0.0;
borderColor[0]=0.0;borderColor[1]=0.0;borderColor[2]=0.0;
boundaryFill4 (x,y,fillColor, borderColor);
glFlush ( );
}


void main (int argc, char** argv)
{
glutInit (&argc, argv); // Initialize GLUT.
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // Set display mode.
glutInitWindowPosition (50, 100); // Set top-left display-window position.
glutInitWindowSize (400, 400); // Set display-window width and height.
glutCreateWindow ("bianjie"); // Create display window.

init ( ); // Execute initialization procedure.
glutDisplayFunc (myDraw); // Send graphics to display window.
glutMainLoop ( ); // Display everything and wait.
}

相关文档
相关文档 最新文档