Skip to content

Instantly share code, notes, and snippets.

@shaina7837
Created October 25, 2013 16:01
Show Gist options
  • Save shaina7837/7157069 to your computer and use it in GitHub Desktop.
Save shaina7837/7157069 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <GL/glut.h>
using namespace std;
unsigned char get_col[50][50][3];
void init(void)
{
glClearColor(1.0, 0.0, 0.0, 0.0);
// glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glOrtho(0.0,100.0,0.0,100.0,0.0,100.0);
}
void readbuffervalue(GLint start, GLint end)
{
for(int i=start; i<=end; i++){
for(int j=start; j<=end; j++){
glReadPixels(i, j, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, get_col[i][j]);
}}}
bool checkcolor(GLint x, GLint y)
{
int r = get_col[x][y][0];
int g = get_col[x][y][1];
int b = get_col[x][y][2];
if((r!=255)&&(g==255)&&(b!=255))
return true;
else
return false;
}
void putpoint(int x, int y)
{
get_col[x][y][0] = 0;
get_col[x][y][1] = 0;
get_col[x][y][2] = 255;
glColor3f(0.0,0.0,1.0);
glPointSize(2);
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
glFlush();
}
void filling(int x, int y)
{
bool value = checkcolor(x,y);
if(value == true)
{putpoint(x,y);
filling(x+1,y);
filling(x-1,y);
filling(x,y+1);
filling(x,y-1);
}}
void displayfunc()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,1.0,0.0);
glRecti(0,0,50,50);
glFlush();
readbuffervalue(0,49);
filling(40,40);
glFlush();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(300,300);
glutInitWindowPosition(100,100);
glutCreateWindow("color-filling");
init();
glutDisplayFunc(displayfunc);
glutMainLoop();
return 0;
}
#include <iostream>
#include <GL/glut.h>
using namespace std;
unsigned char pick_col[50][50][3];
void init(void)
{
glClearColor(1.0,0.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
glOrtho(0.0,100.0,0.0,100.0,0.0,100.0);
}
void readAllToBuffer( GLint _start, GLint _end )
{
for (int i=_start;i<_end;i++)
{
for (int j=_start;j<_end;j++)
{
glReadPixels(i , j , 1 , 1 , GL_RGB , GL_UNSIGNED_BYTE , pick_col[i][j]);
}
}
}
bool getpixelcolor(GLint x, GLint y)
{
int r,g,b;
r = pick_col[x][y][0];
g = pick_col[x][y][1];
b = pick_col[x][y][2];
//cout<<"b:"<<b<<"\r\n";
if ((r!=255)&&(b!=255)&&(g==255))//g==1
{
return true;
}
else
{
return false;
}
}
void PutPoint( int x, int y )
{
pick_col[x][y][0]=0;
pick_col[x][y][1]=0;
pick_col[x][y][2]=255;
glPointSize(2);
glBegin (GL_POINTS);
glColor3f (0.0,0.0,1.0);
glVertex2i (x,y);
glEnd();
// glFlush();
}
void Filling(int x,int y)
{
bool Value=getpixelcolor(x,y);
if (Value==true)
{
PutPoint(x, y);
Filling (x+1,y);
Filling (x-1,y);
Filling (x,y+1);
Filling (x,y-1);
}
}
void displayfnc()
{ glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,1.0,0.0);
glRecti(0,0,50,50);
glFlush();
readAllToBuffer(0, 49);
Filling(40,40);
glFlush();
}
/* void Timer(int extra)
{
glutPostRedisplay();
glutTimerFunc(30,Timer,0);
}*/
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(300,300);
glutInitWindowPosition(10, 10);
glutCreateWindow("BoundaryFill");
init();
glutDisplayFunc(displayfnc);
//glutTimerFunc(10,Timer,0);
glutMainLoop();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment