#include
#include
#include
#define width 450
#define height 450
class scrPt
{
public: GLint x, y;
};
scrPt vert1, vert2, vert3,vert4,vert5,vert6,vert7;
scrPt seed;
float currentBuffer[width][height][3];//3 color
void init (void)
{
glClearColor (1.0, 1.0, 1.0, 0.0); // Set display-window color to white. backgroud color
glMatrixMode (GL_PROJECTION); // Set projection parameters.
gluOrtho2D (0.0, 200.0, 0.0, 150.0);
}
void getColor(int x, int y, float drawColor[3])
{
int m = (x-(x/90)*90)/30;// m can be 0, 1 ,2
int n = (y-(y/90)*90)/30;// n can be 0, 1 ,2
//int m = (x-(x/30)*30)/10;// m can be 0, 1 ,2
//int n = (y-(y/30)*30)/10;// n can be 0, 1 ,2
if (n==0)
{
if (m==0)//red
{
drawColor[0] = 1.0;
drawColor[1] = 0.0;
drawColor[2] = 0.0;
}
if (m==1) //green
{
drawColor[0] = 0.0;
drawColor[1] = 1.0;
drawColor[2] = 0.0;
}
if (m==2) //white
{
drawColor[0] = 1.0;
drawColor[1] = 1.0;
drawColor[2] = 1.0;
}
}
if (n==1)
{
if (m==0) //yellow
{
drawColor[0] = 1.0;
drawColor[1] = 1.0;
drawColor[2] = 0.0;
}
if (m==1) //blue
{
drawColor[0] = 0.0;
drawColor[1] = 0.0;
drawColor[2] = 1.0;
}
if (m==2) //white
{
drawColor[0] = 1.0;
drawColor[1] = 1.0;
drawColor[2] = 1.0;
}
}
if (n==2)//white
{
drawColor[0] = 1.0;
drawColor[1] = 1.0;
drawColor[2] = 1.0;
}
}
bool insidePolygon(int x,int y)
{
int polySides = 7;// 7 corners
int i;
int j=polySides-1;
bool flag = false;
int polyX[]={vert1.x,vert2.x,vert3.x,vert4.x,vert5.x,vert6.x,vert7.x};
int polyY[]={vert1.y,vert2.y,vert3.y,vert4.y,vert5.y,vert6.y,vert7.y};
//origial idea from this excellent page
//http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
for (i = 0; i <>y) != (polyY[j]>y)) && (x < (polyX[j]-polyX[i]) * (y-polyY[i]) / (polyY[j]-polyY[i]) + polyX[i]) ) { flag=!flag; } j=i; } return flag; } void floodFill4 (int x, int y , float interiorColor[3]) { float drawColor[3]; //if (interiorColor[0] == currentBuffer[y][x][0] && interiorColor[1] == currentBuffer[y][x][1] && interiorColor[2] == currentBuffer[y][x][2]) if (x >= 0 && x <>= 0 && y < 450 && interiorColor[0] == currentBuffer[y][x][0] && interiorColor[1] == currentBuffer[y][x][1] && interiorColor[2] == currentBuffer[y][x][2])
{
getColor(x,y, drawColor);
currentBuffer[y][x][0] = drawColor[0];
currentBuffer[y][x][1] = drawColor[1];
currentBuffer[y][x][2] = drawColor[2];
floodFill4(x+1,y,interiorColor);
floodFill4(x-1,y,interiorColor);
floodFill4(x,y+1,interiorColor);
floodFill4(x,y-1,interiorColor);
}
}
void PolygonFill (void)
{
float interiorColor[3];
glClear (GL_COLOR_BUFFER_BIT); // Clear display window.
glColor3f (1.0, 0.0, 1.0);
interiorColor[0] = 1.0;
interiorColor[1] = 0.0; //0.0
interiorColor[2] = 1.0;
glBegin (GL_POLYGON);
glVertex2i (vert1.x, vert1.y);
glVertex2i (vert2.x, vert2.y);
glVertex2i (vert3.x, vert3.y);
glVertex2i (vert4.x, vert4.y);
glVertex2i (vert5.x, vert5.y);
glVertex2i (vert6.x, vert6.y);
glVertex2i (vert7.x, vert7.y);
glEnd ( );
// You will need this function to get the color information of a block of pixels
// the block is a rectangle and defined by the first four parameters
// the color will be stored in an array "currentColor"
glReadPixels(0,0, width, height, GL_RGB, GL_FLOAT, currentBuffer);
// call the recursive function floodFill4 to fill the polygon
floodFill4 (seed.x,seed.y, interiorColor);
// draw the results stored in "currentBuffer"
glDrawPixels(width, height, GL_RGB, GL_FLOAT, currentBuffer);
glFlush ( ); // Process all OpenGL routines as quickly as possible.
}
void winReshapeFcn (int newWidth, int newHeight)
{
glMatrixMode (GL_PROJECTION);
glLoadIdentity ( );
gluOrtho2D (0.0, (GLdouble) newWidth, 0.0, (GLdouble) newHeight);
glClear (GL_COLOR_BUFFER_BIT);
}
void spec ( int key, int x, int y )
{
switch ( key )
{
case GLUT_KEY_UP :
printf ("UP arrow pressed\n");
vert1.y += 10;
vert2.y += 10;
vert3.y += 10;
vert4.y += 10;
vert5.y += 10;
vert6.y += 10;
vert7.y += 10;
seed.y += 10;
PolygonFill();
break;
case GLUT_KEY_DOWN :
printf ("DOWN arrow pressed\n");
vert1.y -= 10;
vert2.y -= 10;
vert3.y -= 10;
vert4.y -= 10;
vert5.y -= 10;
vert6.y -= 10;
vert7.y -= 10;
seed.y -= 10;
PolygonFill();
break;
case GLUT_KEY_RIGHT :
printf ("RIGHT arrow pressed\n");
vert1.x += 10;
vert2.x += 10;
vert3.x += 10;
vert4.x += 10;
vert5.x += 10;
vert6.x += 10;
vert7.x += 10;
seed.x += 10;
PolygonFill();
break;
case GLUT_KEY_LEFT :
printf ("LEFT arrow pressed\n");
vert1.x -= 10;
vert2.x -= 10;
vert3.x -= 10;
vert4.x -= 10;
vert5.x -= 10;
vert6.x -= 10;
vert7.x -= 10;
seed.x -= 10;
PolygonFill();
break;
}
}
void main (int argc, char** argv)
{
glutInit (&argc, argv); // Initialize GLUT.
glutInitDisplayMode (GLUT_SINGLE GLUT_RGB); // Set display mode.
glutInitWindowPosition (100, 100); // Set top-left display-window position.
glutInitWindowSize (width, height);
glutCreateWindow ("OpenGL Program 01"); // Create display window.
init ( ); // Execute initialization procedure.
vert1.x = 250;
vert1.y = 250;
vert2.x = 225;
vert2.y = 200;
vert3.x = 200;
vert3.y = 250;
vert4.x = 175;
vert4.y = 200;
vert5.x = 175;
vert5.y = 250;
vert6.x = 150;
vert6.y = 250;
vert7.x = 200;
vert7.y = 320;
seed.x = 200;
seed.y = 250;
/*vert1.x = 250;
vert1.y = 250;
vert2.x = 225;
vert2.y = 200;
vert3.x = 200;
vert3.y = 250;
vert4.x = 175;
vert4.y = 200;
vert5.x = 175;
vert5.y = 250;
vert6.x = 150;
vert6.y = 250;
vert7.x = 200;
vert7.y = 300;
seed.x = 200;
seed.y = 250;*/
glutDisplayFunc (PolygonFill); // Send graphics to display window.
glutReshapeFunc (winReshapeFcn);
glutSpecialFunc( spec ) ;//keyboard moving
//glutSpecialFunc( movemouse ) ;//mouse moving
glutMainLoop ( ); // Display everything and wait.
}
No comments:
Post a Comment