Polygon Filling Algorithm(Boundary Filling) Opengl Program in C

Polygon Filling Algorithm(Boundary Filling) Opengl Program in C:-

#include<GL/glut.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#define SCANMAX 300
#define SIZE 500
#define EDGES 5

const int  A  = 500;
const float B = 500;
const float C = 200;

typedef struct edgenode
{
int x;
int y;
float slope;
struct edgenode *next;
}*nodeptr;

void setPixel(int px, int py)
{
    glColor3f(0.0, 1.0, 0.0);
    glBegin(GL_POINTS);
    glVertex2i(px, py);
    glEnd();
    glFlush();
}

void initialization(void)
{
   glClearColor(0.1f, 0.1f, 0.1f, 0.0);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();          
   glOrtho( -SIZE/2, SIZE/2, -SIZE/2, SIZE/2, -1, 1);
   glMatrixMode(GL_MODELVIEW);
}

void makeSCANtable(nodeptr SCAN[],int polygon[][2][2])
{

int i,j,xmin,xmax,ymin,ymax,k,line;
for(i=0;i<EDGES;i++)
{
for(j=0;j<2;j++)
{
printf("\n");
for(k=0;k<2;k++)
{
printf("%d ",polygon[i][j][k]);
}
setPixel(polygon[i][j][0],polygon[i][j][1]);
}
printf("\n\n");
}

nodeptr node,ptr,n;

float m;

for(i=0;i<SCANMAX;i++)
{
SCAN[i] = (nodeptr)malloc(sizeof(struct edgenode));
SCAN[i] = NULL;
}


for(i=0;i<EDGES;i++)
{
k = polygon[i][0][1]<polygon[i][1][1]?0:1;
line = ymin = polygon[i][k][1];
ymax = polygon[i][(k+1)%2][1];
xmin = polygon[i][k][0];
xmax = polygon[i][(k+1)%2][0];


if(polygon[i][0][1] - polygon[i][1][1])
{
m = (float)(xmax-xmin)/(ymax-ymin);
ptr = SCAN[line];
while(ptr!= NULL && ptr->next!= NULL&&(ptr->next)->x < xmin)
{
ptr = ptr->next;
}

n = (nodeptr)malloc(sizeof(struct edgenode));
n->x = xmin;
n->slope = m;
n->y = ymax;

if(SCAN[line] == NULL)
{
n->next = NULL;
SCAN[line] = n;
}
else
{
n->next = ptr->next;
ptr->next = n ;
}
//printf("\n EDGE%d  xmin = %d xmax = %d ymin = %d ymax = %d slope = %f\n", i,n->x,xmax,ymin,n->y,n->slope);
}
}
//printf("\nedge table complete");
}

void polygonFill(int polygon[][2][2])
{
nodeptr SCAN[SCANMAX],n;
nodeptr activeEdge = NULL,active_ptr,ptr,pt1,pt2;
makeSCANtable(SCAN,polygon);

int fill = 0,i,j,scanline=0;

for(i=-500;i<500;i++)
setPixel(i,0);
for(j=-500;j<500;j++)
setPixel(0,j);

nodeptr start = NULL;

for(scanline = 0;scanline < SCANMAX;scanline++)
{
if(start)
{
while(start && start->y == scanline)
start = start->next;
active_ptr = start;

while(active_ptr != NULL && active_ptr->next!=NULL)
{
if((active_ptr->next)->y == scanline)
{
n = active_ptr->next;
active_ptr->next = n->next;
free(n);
}
active_ptr = active_ptr->next;
}
}

ptr = SCAN[scanline];
while(ptr!= NULL)
{
active_ptr=start;
n = (nodeptr)malloc(sizeof(struct edgenode));
n->x = ptr->x;
n->slope = ptr->slope;
n->y = ptr->y;
if(start==0)
{
n->next = NULL;
start = n;
}
else if(start->x > ptr->x)
{
n->next = start;
start = n;
}
else
{
while(active_ptr->next!= NULL && (active_ptr->next)->x <= ptr->x)
{
active_ptr = active_ptr->next;
}
n->next = active_ptr->next;
active_ptr->next = n;
}
ptr = ptr->next;

}
pt1=start;
if(pt1)
{
while(pt1!=NULL)
{
pt2 = pt1->next;
i = pt1->x;
j=pt2->x;
for(; i <= j;i++)
{
setPixel(i,scanline);
}

pt1->x = pt1->x + pt1->slope ;
pt2->x = pt2->x + pt2->slope ;
pt1 = pt2->next;
}
}
}
}
                 
void display(){
glClear(GL_COLOR_BUFFER_BIT);
int startX=0, endX=100, startY=0, endY=100,i,j;  
int Polygon[EDGES][2][2] = {{{0,20},{0,200}},{{0,20},{200,20}},{{200,20},{200,200}},{{100,100},{0,200}},{{100,100},{200,200}}};
glFlush();
polygonFill(Polygon);
glFlush();
}

int main(int argc, char** argv){
  glutInit(&argc,argv);
  glutInitWindowSize( A, A );
  glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE);
  glutCreateWindow("Polygon Filling");
  glutDisplayFunc(display);
  initialization();              
  glutMainLoop();                
}

Related Posts