#include <GL/glut.h>
#include <stdio.h>
float boundaryColor[3] = {1.0, 1.0, 1.0}; // White boundary
float fillColor[3] = {1.0, 0.0, 0.0}; // Red fill
void setPixel(int x, int y)
{
glColor3fv(fillColor);
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
glFlush();
}
void getPixel(int x, int y, float color[3])
{
glReadPixels(x, y, 1, 1, GL_RGB, GL_FLOAT, color);
}
void boundaryFill(int x, int y)
{
float currentColor[3];
getPixel(x, y, currentColor);
if ((currentColor[0] != boundaryColor[0] ||
currentColor[1] != boundaryColor[1] ||
currentColor[2] != boundaryColor[2]) &&
(currentColor[0] != fillColor[0] ||
currentColor[1] != fillColor[1] ||
currentColor[2] != fillColor[2]))
{
setPixel(x, y);
// 4-connected fill
boundaryFill(x + 1, y);
boundaryFill(x – 1, y);
boundaryFill(x, y + 1);
boundaryFill(x, y – 1);
}
}
void drawPolygon()
{
glColor3fv(boundaryColor); // White boundary
glBegin(GL_LINE_LOOP);
glVertex2i(150, 150);
glVertex2i(350, 150);
glVertex2i(350, 350);
glVertex2i(150, 350);
glEnd();
glFlush();
}
void mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
boundaryFill(x, 500 – y);
}
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
drawPolygon();
glFlush();
}
void init()
{
glClearColor(0.0, 0.0, 0.0, 1.0); // Black background
gluOrtho2D(0, 500, 0, 500);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow(“Boundary Fill – Red & White”);
init();
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}