0% found this document useful (0 votes)
37 views

9

This code defines functions to detect edges and fill polygons using the scanline algorithm in OpenGL. It initializes variables to define the vertices of a polygon and arrays to store the left and right edge coordinates for each scanline. The edgedetect function calculates the slope between two vertices and uses it to populate the left and right edge arrays. The scanfill function loops through these arrays to draw pixels between the detected edges. A menu is also created to toggle the filled state of the polygon rendered in the display function.

Uploaded by

Nithin SS
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

9

This code defines functions to detect edges and fill polygons using the scanline algorithm in OpenGL. It initializes variables to define the vertices of a polygon and arrays to store the left and right edge coordinates for each scanline. The edgedetect function calculates the slope between two vertices and uses it to populate the left and right edge arrays. The scanfill function loops through these arrays to draw pixels between the detected edges. A menu is also created to toggle the filled state of the polygon rendered in the display function.

Uploaded by

Nithin SS
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include<glut.

h>

float x1=200,y1=200,x2=100,y2=300,x3=200,y3=400,x4=300,y4=300;
float le[500],re[500];
int filled = 0;

void edgedetect(float x1,float y1,float x2,float y2) ///should be float only


{
float mx,x,temp;
int y;
if(y2<y1)
{
temp=y1; y1=y2; y2=temp; //scan line will start from lower y to
higher y value
temp=x1; x1=x2; x2=temp; //y1 should have lower Y value so swap if
required
}
if(y2>y1)
{
mx=(x2-x1)/(y2-y1); //inverse of slope xk+1 = xk+1/m
}
else
{
mx=x2-x1; //line is parallel to Y-axis
}

x=x1;
for(y=y1;y<=y2;y++)
{
if(x<le[y])
{
le[y]=x;
}

if(x>re[y])
{
re[y]=x;
}
x = x + mx;
}
}

void scanfill()
{
int x,y;

for(y=0;y<500;y++) //initialize Left and Rightmost Co-ordinate values


{
le[y]=500;
re[y]=0;
}

//For each edge call the edgedetect function


edgedetect(x1,y1,x2,y2);
edgedetect(x2,y2,x3,y3);
edgedetect(x3,y3,x4,y4);
edgedetect(x4,y4,x1,y1);

glColor3f(1,0,0);
glPointSize(1); //Vary Point Size
glBegin(GL_POINTS);

for(y=0;y<500;y++)
{
for(x=le[y];x<=re[y];x++)
{
glVertex2f(x,y);
}
}
glEnd();
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(1,1,1,0);

if(filled==0)
{
glColor3f(0,0,1); //include each vertex of polygon to be filled
glBegin(GL_LINE_LOOP);
glVertex2f(x1,y1);
glVertex2f(x2,y2);
glVertex2f(x3,y3);
glVertex2f(x4,y4);
glEnd();
}
else
{
scanfill();
}
glFlush();
}

void init()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,500,0,500);
}

void menu(int num)


{
if(num == 0)
{
filled = 1;
}
else
{
filled = 0;
}
glutPostRedisplay();
}
void createMenu()
{
glutCreateMenu(menu); //Menu Callback Function
glutAddMenuEntry("Filled Polygon", 0);
glutAddMenuEntry("Un-Filled Polygon", 1);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}

void main(int argc,char **argv)


{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB |GLUT_SINGLE);
glutInitWindowPosition(0,0);
glutInitWindowSize(500,500);
glutCreateWindow("Scan Line");
glutDisplayFunc(display);

createMenu();
init();

glutMainLoop();
}

You might also like