Thursday, May 15, 2014

Opengl,C++ : Boundary-Fill Algorithm Using Recursion

1:  #include <GL/glut.h>  
2:  int ww = 600, wh = 500;  
3:  float fillCol[3] = {0.4,0.0,0.0};  
4:  float borderCol[3] = {0.0,0.0,0.0};  
5:  void setPixel(int pointx, int pointy, float f[3])  
6:  {  
7:       glBegin(GL_POINTS);  
8:            glColor3fv(f);  
9:            glVertex2i(pointx,pointy);  
10:       glEnd();  
11:       glFlush();  
12:  }  
13:  void getPixel(int x, int y, float pixels[3])  
14:  {  
15:       glReadPixels(x,y,1.0,1.0,GL_RGB,GL_FLOAT,pixels);  
16:  }  
17:  void drawPolygon(int x1, int y1, int x2, int y2)  
18:  {       
19:       glColor3f(0.0,0.0,0.0);  
20:       glBegin(GL_LINES);  
21:            glVertex2i(x1, y1);  
22:            glVertex2i(x1, y2);   
23:       glEnd();  
24:       glBegin(GL_LINES);  
25:            glVertex2i(x2, y1);  
26:            glVertex2i(x2, y2);  
27:       glEnd();  
28:       glBegin(GL_LINES);  
29:            glVertex2i(x1, y1);  
30:            glVertex2i(x2, y1);  
31:       glEnd();  
32:       glBegin(GL_LINES);  
33:            glVertex2i(x1, y2);  
34:            glVertex2i(x2, y2);  
35:       glEnd();  
36:       glFlush();  
37:  }  
38:  void display()  
39:  {  
40:       glClearColor(0.6,0.8,0.1, 1.0);  
41:       glClear(GL_COLOR_BUFFER_BIT);  
42:       drawPolygon(150,250,200,300);  
43:       glFlush();  
44:  }  
45:  void boundaryFill4(int x,int y,float fillColor[3],float borderColor[3])  
46:  {  
47:       float interiorColor[3];  
48:       getPixel(x,y,interiorColor);  
49:       if((interiorColor[0]!=borderColor[0] && (interiorColor[1])!=borderColor[1] && (interiorColor[2])!=borderColor[2]) && (interiorColor[0]!=fillColor[0] && (interiorColor[1])!=fillColor[1] && (interiorColor[2])!=fillColor[2]))  
50:       {  
51:            setPixel(x,y,fillColor);  
52:            boundaryFill4(x+1,y,fillColor,borderColor);  
53:            boundaryFill4(x-1,y,fillColor,borderColor);  
54:            boundaryFill4(x,y+1,fillColor,borderColor);  
55:            boundaryFill4(x,y-1,fillColor,borderColor);  
56:       }  
57:  }  
58:  void mouse(int btn, int state, int x, int y)  
59:  {  
60:       if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN)   
61:       {  
62:            int xi = x;  
63:            int yi = (wh-y);  
64:            boundaryFill4(xi,yi,fillCol,borderCol);  
65:       }  
66:  }  
67:  void myinit()  
68:  {        
69:       glViewport(0,0,ww,wh);  
70:       glMatrixMode(GL_PROJECTION);  
71:       glLoadIdentity();  
72:       gluOrtho2D(0.0,(GLdouble)ww,0.0,(GLdouble)wh);  
73:       glMatrixMode(GL_MODELVIEW);  
74:  }  
75:  int main(int argc, char** argv)  
76:  {  
77:       glutInit(&argc,argv);  
78:       glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);  
79:       glutInitWindowSize(ww,wh);  
80:       glutCreateWindow("Bountry-Fill-Recursive");  
81:       glutDisplayFunc(display);  
82:       myinit();  
83:       glutMouseFunc(mouse);  
84:       glutMainLoop();  
85:       return 0;  
86:  }  

4 comments: