Advertisement
Guest User

Untitled

a guest
Oct 6th, 2015
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. #include<GL/glut.h>
  2. #include<stdio.h>
  3. #include<math.h>
  4.  
  5. struct das
  6. {
  7. int x;
  8. int y;
  9. } p[100000];
  10.  
  11. int top;
  12. float oldcolor[3],fillcolor[3];
  13.  
  14. void push(int x,int y)
  15. {
  16. top++;
  17. if(top==100000)
  18. exit(0);
  19. p[top].x=x;
  20. p[top].y=y;
  21. }
  22.  
  23. void pop(int *x,int *y)
  24. {
  25. *x=p[top].x; *y=p[top].y;
  26. top--;
  27. }
  28.  
  29. setPixel(int x,int y)
  30. {
  31. glColor3fv(fillcolor);
  32. glPointSize(10);
  33. glBegin(GL_POINTS);
  34. glVertex2f(x,y);
  35. glEnd();
  36. glFlush();
  37. }
  38.  
  39. void floodfill(int x,int y)
  40. {
  41. int xc,yc;
  42. push(x,y);
  43. float currentcolor[3];
  44. setPixel(x,y);
  45.  
  46. while(top>0)
  47. {
  48. pop(&xc,&yc);
  49. glReadPixels(xc+10,yc,1.0,1.0,GL_RGB,GL_FLOAT,currentcolor);
  50. if(currentcolor[0]==oldcolor[0] && currentcolor[1]==oldcolor[1] && currentcolor[2]==oldcolor[2])
  51. { setPixel(xc+10,yc); push(xc+10,yc); }
  52.  
  53. glReadPixels(xc-10,yc,1.0,1.0,GL_RGB,GL_FLOAT,currentcolor);
  54. if(currentcolor[0]==oldcolor[0] && currentcolor[1]==oldcolor[1] && currentcolor[2]==oldcolor[2])
  55. { setPixel(xc-10,yc); push(xc-10,yc); }
  56.  
  57. glReadPixels(xc,yc+10,1.0,1.0,GL_RGB,GL_FLOAT,currentcolor);
  58. if(currentcolor[0]==oldcolor[0] && currentcolor[1]==oldcolor[1] && currentcolor[2]==oldcolor[2])
  59. { setPixel(xc,yc+10); push(xc,yc+10); }
  60.  
  61. glReadPixels(xc,yc-10,1.0,1.0,GL_RGB,GL_FLOAT,currentcolor);
  62. if(currentcolor[0]==oldcolor[0] && currentcolor[1]==oldcolor[1] && currentcolor[2]==oldcolor[2])
  63. { setPixel(xc,yc-10); push(xc,yc-10); }
  64. }
  65. }
  66.  
  67.  
  68.  
  69. void drawPolygon()
  70. {
  71. glColor3f(1.0,1.0,1.0);
  72. glLineWidth(20);
  73. glBegin(GL_LINES);
  74. glVertex2f(100,100);
  75. glVertex2f(100,400);
  76. glVertex2f(100,400);
  77. glVertex2f(400,400);
  78. glVertex2f(400,400);
  79. glVertex2f(400,100);
  80. glVertex2f(400,100);
  81. glVertex2f(100,100);
  82. glEnd();
  83. glFlush();
  84. }
  85.  
  86. void init()
  87. {
  88. glClearColor(0.0,0.0,0.0,0.0);
  89. glLoadIdentity();
  90. gluOrtho2D(0,500,0,500);
  91. }
  92.  
  93. void display()
  94. {
  95. glClear(GL_COLOR_BUFFER_BIT);
  96. glFlush();
  97. drawPolygon();
  98. fillcolor[0]=0.0; fillcolor[1]=0.0; fillcolor[2]=1.0;
  99. glReadPixels(300,300,1.0,1.0,GL_RGB,GL_FLOAT,oldcolor);
  100. floodfill(300,300);
  101. }
  102.  
  103. int main(int argc, char** argv)
  104. {
  105. glutInit(&argc, argv);
  106. glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
  107. glutInitWindowSize (500, 500);
  108. glutInitWindowPosition (0, 0);
  109. glutCreateWindow (" ");
  110. init ();
  111. glutDisplayFunc(display);
  112. glutMainLoop();
  113. return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement