Lab Manual Computer Graphics Laboratory With Mini Project (18CSL67)
Lab Manual Computer Graphics Laboratory With Mini Project (18CSL67)
Prepared By,
Prof. Shryavani K
Assistant Professor
Department of CSE
BIET
Prof. Sumana C
Assistant Professor
Department of CSE
BIET
SYLLABUS
Part A
SL PROGRAMS
NO
01 Implement Bresenham’s line drawing algorithm for all types of slope
02 Create and rotate a triangle about the origin and a fixed point
03 Draw a color cube and spin it using OpenGL transformation matrices
04 Draw a color cube and allow the user to move the camera suitably to experiment
with perspective viewing
05 Clip a lines using Cohen-Sutherland algorithm
06 To draw a simple shaded scene consisting of a tea pot on a table. Define suitably
the position and properties of the light source along with the properties of the
surfaces of the solid object used in the scene
07 Design, develop and implement recursively subdivide a tetrahedron to form 3D
Sierpinski gasket. The number of recursive steps is to be specified by the user
08 Develop a menu driven program to animate a flag using Bezier Curve algorithm
09 Develop a menu driven program to fill the polygon using scan line algorithm
Include Files
For all OpenGL applications, you want to include the gl.h header file in every file.
Almost all OpenGL applications use GLU, the aforementioned OpenGL Utility Library,
which requires inclusion of the glu.h header file. So almost every OpenGL source file begins
with
#include <GL/gl.h>
#include <GL/glu.h>
If you are using GLUT for managing your window manager tasks, you should include
#include <GL/glut.h>
Note that glut.h includes gl.h, glu.h automatically, so including all three files is redundant.
OpenGL Hierarchy
Several levels of abstraction are provided
GL
Lowest level: vertex, matrix manipulation
glVertex3f(point.x, point.y, point.z)
GLU
Helper functions for shapes, transformations
gluPerspective( fovy, aspect, near, far )
gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);
GLUT
Highest level: Window and interface management
glutSwapBuffers()
glutInitWindowSize (500, 500);
OpenGL Implementations
OpenGL IS an API (think of as collection of .h files):
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
Windows, Linux, UNIX, etc. all provide a platform specific implementation.
Windows: opengl32.lib glu32.lib glut32.lib
Linux: -l GL -l GLU –l GLUT
OpenGL API
As a programmer, you need to do the following things:
Specify the location/parameters of camera.
Specify the geometry (and appearance).
Specify the lights (optional).
OpenGL will compute the resulting 2D image
OpenGL: Camera
Two things to specify:
Physical location of camera in the scene (MODELVIEW matrix in OpenGL).
Projection properties of the camera (PROJECTION matrix in OpenGL):
OpenGL Conventions
Many functions have multiple forms:
glVertex2f, glVertex3i, glVertex4dv, etc.
Number indicates number of arguments
Letters indicate type
f: float, d: double, ub: unsigned byte, etc.
“v” (if present) indicates a single pointer argument
Required files for Windows
• In the System Directory
– glu32.dll
– opengl32.dll
– glut32.dll
• In the C++ Include Directory
– gl\gl.h
– l\glu.h
– gl\glaux.h (probably won't need it)
– gl\glut.h (includes both gl.h and glu.h)
• In the C++ Library Directory
– gl\glu32.lib
– l\opengl32.lib
– gl\glaux.lib (probably won't need it)
– gl\glut32.lib
Event Loop
• OpenGL programs often run in an event loop:
– Start the program
– Run some initialization code
– Run an infinite loop and wait for events such as
• Key press
• Mouse move, click
• Reshape window
• Expose event
OpenGL Command Syntax (1)
• OpenGL commands start with “gl”
• OpenGL constants start with “GL_”
• Some commands end in a number and one, two or three letters at the end (indicating
number and type of arguments)
• A Number indicates number of arguments
• Characters indicate type of argument
OpenGL Command Syntax (2)
OpenGL Primitives
glMatrixMode
• glMatrixMode
– - specify which matrix is the current matrix
• C Specification
– void glMatrixMode( GLenum mode )
• Parameters
– mode Specifies which matrix stack is the target for subsequent matrix
operations. Three values are accepted: GL_MODELVIEW,
GL_PROJECTION, and GL_TEXTURE. The default value is
GL_MODELVIEW.
• Description
– glMatrixMode sets the current matrix mode. mode can assume one of three
values: GL_MODELVIEW Applies subsequent matrix operations to the
modelview matrix stack. GL_PROJECTION Applies subsequent matrix
operations to the projection matrix stack.
Step 1: Create a Visual Studio 2017 Project
To create an empty console project in Visual Studio, do the following:
1. Create a new project (File ---> New ---> --->Project)
2. In the Project Types: pane, select Visual C++, Win32. Then select Win 32 Console
Application in the Templates: pane. Name your project, select the location for the project
and click OK.
3. Click the Application Settings tab on the left, check the Empty Project box and
uncheck Security Development Lifecycle (SDL). Then click Finish button.
Experiment 1:
Implement Brenham’s line drawing algorithm for all types of slope
AIM:
To implement Bresenham’s line drawing algorithm with different values of slopes
ALGORITHM:
Step 1 - Input the two end-points of line, storing the left end-point in (x0,y0).
Step 3 - Calculate the constants dx, dy, 2dy, and (2dy – 2dx) and get the first value for the
decision parameter as -
p0=2dy-dx
Step 4 - At each Xk along the line, starting at k = 0, perform the following test -
Otherwise, (xk,yk+1)
pk+1=pk+2dy-2dx
For m > 1, find out whether you need to increment x while incrementing y each time.
PROGRAM:
#include <GL/glut.h>
#include <stdio.h>
int x1, y1, x2, y2;
void myInit()
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0, 500, 0, 500);
}
void draw_pixel(int x, int y)
{
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
void draw_line(int x1, int x2, int y1, int y2)
{
int dx, dy, i, e;
int incx, incy, inc1, inc2;
int x,y;
dx = x2-x1;
dy = y2-y1;
if (dx < 0) dx = -dx;
if (dy < 0) dy = -dy;
incx = 1;
if (x2 < x1) incx = -1;
incy = 1;
if (y2 < y1) incy = -1;
x = x1; y = y1;
if (dx > dy)
{
draw_pixel(x, y);
e = 2 * dy-dx;
inc1 = 2*(dy-dx);
inc2 = 2*dy;
for (i=0; i<dx; i++) {
if (e >= 0) {
y += incy;
e += inc1;
}
else
e += inc2;
x += incx;
draw_pixel(x, y);
}
}
else
{
Dept. of CSE, BIET, Davangere Page 12
18CSL67 Computer Graphics Laboratory
draw_pixel(x, y);
e = 2*dx-dy;
inc1 = 2*(dx-dy);
inc2 = 2*dx;
for (i=0; i<dy; i++) {
if (e >= 0) {
x += incx;
e += inc1;
}
else
e += inc2;
y += incy;
draw_pixel(x, y);
}
}
}
void myDisplay()
{
draw_line(x1, x2, y1, y2);
glFlush();
}
int main(int argc, char **argv) {
printf( "Enter (x1, y1, x2, y2)\n");
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Bresenham's Line Drawing");
myInit();
glutDisplayFunc(myDisplay);
glutMainLoop();
return 0;
}
Sample Output:
Experiment 2: Create and rotate a triangle about the origin and a fixed point
AIM:
To Create and rotate a triangle about the origin and a fixed point.
PROGRAM:
#include<GL/glut.h>
#include<stdio.h>
int x,y;
int where_to_rotate=0; // don't rotate initially
float rotate_angle=0; // initial angle
float translate_x=0,translate_y=0; // initial translation
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
void init()
{
glClearColor(0,0,0,1); //setting to black
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-800, 800, -800, 800);
glMatrixMode(GL_MODELVIEW);
}
if(option==2)
where_to_rotate=2; // rotate around customer's coordinates
if(option==3)
where_to_rotate=3; // stop rotation
}
Sample Output:
Experiment 3:
Draw a color cube and spin it using OpenGL transformation matrices
AIM:
To Draw a color cube and spin it using OpenGL transformation matrices
PROGRAM:
#include<stdlib.h>
#include<GL/glut.h>
GLfloat vertices[]= {-1, -1, -1,
1, -1, -1,
1, 1, -1,
-1, 1, -1,
-1, -1, 1,
1, -1, 1,
1, 1, 1,
-1, 1, 1
};
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(theta[0], 1, 0, 0);
glRotatef(theta[1], 0, 1, 0);
glRotatef(theta[2], 0, 0, 1);
glDrawElements(GL_QUADS,24,GL_UNSIGNED_BYTE,cubeIndices);
glutSwapBuffers();
}
void spinCube()
{
theta[axis] += 2;
glutPostRedisplay();
}
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutIdleFunc(spinCube);
glutMouseFunc(mouse);
glEnable(GL_DEPTH_TEST);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glColor3f(1, 1, 1);
glutMainLoop();
}
Sample Output:
Experiment 4:
Draw a color cube and allow the user to move the camera suitably to experiment
with perspective viewing.
AIM:
To Draw a color cube and allow the user to move the camera suitably to experiment
with perspective viewing.
PROGRAM:
#include <stdlib.h>
#include <GL/glut.h>
void colorcube(void)
Dept. of CSE, BIET, Davangere Page 20
18CSL67 Computer Graphics Laboratory
{
polygon(0,3,2,1);
polygon(0,4,7,3);
polygon(5,4,0,1);
polygon(2,3,7,6);
polygon(1,2,6,5);
polygon(4,5,6,7);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(viewer[0],viewer[1],viewer[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glRotatef(theta[0], 1.0, 0.0, 0.0);
glRotatef(theta[1], 0.0, 1.0, 0.0);
glRotatef(theta[2], 0.0, 0.0, 1.0);
colorcube();
glFlush();
glutSwapBuffers();
}
glMatrixMode(GL_MODELVIEW);
}
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutKeyboardFunc(keys);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}
Sample Output:
Experiment 5:
Clip a lines using Cohen-Sutherland algorithm.
AIM:
To Clip a lines using Cohen-Sutherland algorithm.
ALGORITHM:
Cohen-sutherland line clipping Algorithm
To perform the trivial acceptance and rejection tests, we extend the edges of the window to
divide the plane of the window into the nine regions. Each end point of the line segment is
then assigned the code of the region in which it lies.
If both codes are 0000,(bitwise OR of the codes yields 0000 ) line lies
completely inside the window: pass the endpoints to the draw routine.
If both codes have a 1 in the same bit position (bitwise AND of the codes
is not 0000), the line lies outside the window. It can be trivially rejected.
3. If a line cannot be trivially accepted or rejected, at least one of the two endpoints
must lie outside the window and the line segment crosses a window edge. This line
must be clipped at the window edge before being passed to the drawing routine.
4. Examine one of the endpoints, say . Read 's 4-bit code in
order: Left-to-Right, Bottom-to-Top.
5. When a set bit (1) is found, compute the intersection I of the corresponding window
edge with the line from to . Replace with I and repeat the algorithm.
PROGRAM:
#include <stdio.h>
#include <GL/glut.h>
double xmin = 50, ymin = 50, xmax = 100, ymax = 100; //window coordinates
double xvmin = 200, yvmin = 200, xvmax = 300, yvmax = 300; //viewport coordinates
const int LEFT = 1; // code words for LEFT, RIGHT, BOTTOM &TOP.
const int RIGHT = 2;
const int BOTTOM = 4;
const int TOP = 8;
do
{
if (!(outcode0 | outcode1))
{
accept = true;
done = true;
}
else if (outcode0 & outcode1)
done = true;
else
{
double x, y;
double m = (y1 - y0)/(x1 - x0);
outcodeOut = outcode0? outcode0: outcode1;
y = y0 + m * (xmax - x0);
x = xmax;
}
else
{
y = y0 + m * (xmin - x0);
x = xmin;
}
/* Intersection calculations over */
if (outcodeOut == outcode0)
{
x0 = x;
y0 = y;
outcode0 = ComputeOutCode (x0, y0);
}
else
{
x1 = x;
y1 = y;
outcode1 = ComputeOutCode (x1, y1);
}
}
}
while (!done);
if (accept)
{
double sx = (xvmax - xvmin) / (xmax - xmin);
double sy = (yvmax - yvmin) / (ymax - ymin);
double vx0 = xvmin + (x0 - xmin) * sx;
double vy0 = yvmin + (y0 - ymin) * sy;
double vx1 = xvmin + (x1 - xmin) * sx;
double vy1 = yvmin + (y1 - ymin) * sy;
glBegin(GL_LINE_LOOP);
glVertex2f(xvmin, yvmin);
glVertex2f(xvmax, yvmin);
glVertex2f(xvmax, yvmax);
glVertex2f(xvmin, yvmax);
glEnd();
glBegin(GL_LINES);
glVertex2d (vx0, vy0);
glVertex2d (vx1, vy1);
glEnd();
}
}
void display()
{
double x0 = 60, y0 = 20, x1 = 80, y1 = 120;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 1, 1);//white
glBegin(GL_LINES);
glVertex2d (x0, y0);
glVertex2d (x1, y1);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex2f(xmin, ymin);
glVertex2f(xmax, ymin);
glVertex2f(xmax, ymax);
glVertex2f(xmin, ymax);
glEnd();
glFlush();
}
void myinit()
{
glClearColor(0, 0, 0, 1);//black
gluOrtho2D(0, 500, 0, 500);
}
myinit();
glutDisplayFunc(display);
glutMainLoop();
}
Sample Output:
Experiment 6:
To draw a simple shaded scene consisting of a tea pot on a table. Define suitably the
position and properties of the light source along with the properties of the surfaces
of the solid object used in the scene.
AIM:
To draw a simple shaded scene consisting of a tea pot on a table. Define suitably
the position and properties of the light source along with the properties of the
surfaces of the solid object used in the scene.
PROGRAM
#include<GL/glut.h>
void teapot(GLfloat x,GLfloat y,GLfloat z)
{
glPushMatrix();
glTranslatef(x, y, z);
glutSolidTeapot(0.1);
glPopMatrix();
}
void tableTop(GLfloat x, GLfloat y, GLfloat z)
{
glPushMatrix();
glTranslatef(x, y, z);
glScalef(0.6, 0.02, 0.5);
glutSolidCube(1);
glPopMatrix();
}
void tableLeg(GLfloat x, GLfloat y, GLfloat z)
{
glPushMatrix();
glTranslatef(x, y, z);
glScalef(0.02, 0.3, 0.02);
glutSolidCube(1);
glPopMatrix();
}
void wall(GLfloat x, GLfloat y, GLfloat z)
{
glPushMatrix();
glTranslatef(x, y, z);
glScalef(1, 1, 0.02);
glutSolidCube(1);
glPopMatrix();
}
void light()
{
GLfloat mat_ambient[] = {1, 1, 1, 1};
GLfloat mat_diffuse[] = {0.5, 0.5, 0.5, 1};
GLfloat mat_specular[] = {1, 1, 1, 1};
GLfloat mat_shininess[] = {50.0f};
gluLookAt(-2, 2, 5, 0, 0, 0, 0, 1, 0);
myinit();
glutDisplayFunc(display);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel(GL_SMOOTH);
glEnable(GL_NORMALIZE);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}
OR
#include<gl/glut.h>
void obj(double tx,double ty,double tz,double sx,double sy,double sz)
{
glRotated(50,0,1,0);
glRotated(10,-1,0,0);
glRotated(11.7,0,0,-1);
glTranslated(tx,ty,tz);
glScaled(sx,sy,sz);
glutSolidCube(1);
glLoadIdentity();
}
void display()
{
glViewport(0,0,700,700);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glRotated(50,0,1,0);
glRotated(10,-1,0,0);
glRotated(11.7,0,0,-1);
glTranslated(0.3,-0.1,-0.3);
glutSolidTeapot(0.09);
glFlush();
glLoadIdentity();
}
void main()
{
float ambient[]={1,1,1,1};
float light_pos[]={27,80,2,3};
glutInitWindowSize(700,700);
glutCreateWindow("scene");
glutDisplayFunc(display);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glMaterialfv(GL_FRONT,GL_AMBIENT,ambient);
glLightfv(GL_LIGHT0,GL_POSITION,light_pos);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}
Output:
Experiment 7:
Design, develop and implement recursively subdivide a tetrahedron to form 3D
sierpinski gasket. The number of recursive steps is to be specified by the user.
AIM:
To Design, develop and implement recursively subdivide a tetrahedron to form
3D sierpinski gasket. The number of recursive steps is to be specified by the
user..
PROGRAM
#include<stdlib.h>
#include<stdio.h>
#include<GL/glut.h>
divide_triangle(a,v1,v2,n-1);
glFlush();
divide_triangle(c,v2,v3,n-1);
glFlush();
divide_triangle(b,v3,v1,n-1);
glFlush();
}
else(triangle(a,b,c));
}
Dept. of CSE, BIET, Davangere Page 31
18CSL67 Computer Graphics Laboratory
void tetrahedron(int n)
{
glColor3f(1, 0, 0);
divide_triangle(v[0], v[1], v[2], n);
glColor3f(0, 1, 0);
divide_triangle(v[3], v[2], v[1], n);
glColor3f(0, 0, 1);
divide_triangle(v[0], v[3], v[1], n);
glColor3f(0, 0, 0);
divide_triangle(v[0], v[2], v[3], n);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
tetrahedron(n);
glFlush();
}
void myReshape(int w,int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
glOrtho(-2, 2, -2*(GLfloat)h/(GLfloat)w, 2*(GLfloat)h/(GLfloat)w, -10, 10);
else
glOrtho(-2*(GLfloat)w/(GLfloat)h, 2*(GLfloat)w/(GLfloat)h, -2, 2, -10, 10);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}
int main(int argc,char ** argv)
{
printf("No of Recursive steps/Division: ");
scanf("%d",&n);
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutCreateWindow(" 3D Sierpinski gasket");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glEnable(GL_DEPTH_TEST);
glClearColor(1, 1, 1, 0);
glutMainLoop();
return 0;
}
Sample Output:
Experiment 8:
Develop a menu driven program to animate a flag using Bezier Curve algorithm.
AIM:
To develop a menu driven program to animate a flag using Bezier Curve
algorithm
PROGRAM
#include<GL/glut.h>
#include<stdio.h>
#include<math.h>
#define PI 3.1416
void CreateMenu(void);
void Menu(int value);
struct wcPt3D
{
GLfloat x, y, z;
};
}
glColor3f(0,1.0,0); //Indian flag: green color code
for(int i=0;i<8;i++)
{
glTranslatef(0, -0.8, 0);
bezier(ctrlPts, nCtrlPts, nBezCurvePts);
}
glPopMatrix();
glColor3f(0.7, 0.5,0.3);
glLineWidth(5);
glBegin(GL_LINES);
glVertex2f(20,100);
glVertex2f(20,40);
glEnd();
glFlush();
}
//Karnataka Flag
if(val==2){
glPushMatrix();
glLineWidth(5);
glColor3f(1.0, 1.0, 0.0); //Karnataka flag: Yellow color code
for(int i=0;i<12;i++)
{
glTranslatef(0, -0.8, 0);
bezier(ctrlPts, nCtrlPts, nBezCurvePts);
}
glColor3f(1, 0.0, 0.0); //Karnataka flag: Red color code
for(int i=0;i<12;i++)
{
glTranslatef(0, -0.8, 0);
bezier(ctrlPts, nCtrlPts, nBezCurvePts);
}
glPopMatrix();
glColor3f(0.7, 0.5,0.3);
glLineWidth(5);
glBegin(GL_LINES);
glVertex2f(20,100);
glVertex2f(20,40);
glEnd();
glFlush();
}
glutPostRedisplay();
glutSwapBuffers();
}
void CreateMenu(void)
{
CMenu= glutCreateMenu(Menu);//Creaate Menu Option
glutAddMenuEntry("Indian Flag",1);
glutAddMenuEntry("Karnataka Flag",2);
glutAddMenuEntry("Exit",0);
glutAttachMenu(GLUT_RIGHT_BUTTON);
Sample Output:
Experiment 9:
Develop a menu driven program to fill the polygon using scan line algorithm.
AIM:
To Develop a menu driven program to fill the polygon using scan line algorithm.
ALGORITHM:
Step 1 − Find out the Ymin and Ymax from the given polygon.
Step 2 − ScanLine intersects with each edge of the polygon from Ymin to Ymax. Name
each intersection point of the polygon. As per the figure shown above, they are named as p0,
p1, p2, p3.
Step 3 − Sort the intersection point in the increasing order of X coordinate i.e. (p0, p1), (p1,
p2), and (p2, p3).
Step 4 − Fill all those pair of coordinates that are inside polygons and ignore the alternate
pairs.
PROGRAM
#include <stdlib.h>
#include <stdio.h>
#include <glut.h>
float x1,x2,x3,x4,y1,y2,y3,y4; int fillFlag=0;
void edgedetect(float x1,float y1,float x2,float y2,int *le,int *re)
{
float mx,x,temp; int i;
if((y2-y1)<0)
{
temp=y1;y1=y2;y2=temp;
temp=x1;x1=x2;x2=temp; }
if((y2-y1)!=0)
mx=(x2-x1)/(y2-y1);
else mx=x2-x1;
x=x1;
for(i=y1;i<=y2;i++)
{
if(x<(float)le[i])
le[i]=(int)x;
if(x>(float)re[i])
re[i]=(int)x;
x+=mx;
}
}
void draw_pixel(int x,int y)
{ glColor3f(1.0,1.0,0.0);
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}
void scanfill(float x1,float y1,float x2,float y2,float x3,float y3,float x4,float y4)
{
int le[500],re[500];
int i,y;
for(i=0;i<500;i++)
{
le[i]=500;
re[i]=0;
}
edgedetect(x1,y1,x2,y2,le,re);
edgedetect(x2,y2,x3,y3,le,re);
edgedetect(x3,y3,x4,y4,le,re);
edgedetect(x4,y4,x1,y1,le,re);
for(y=0;y<500;y++)
{
for(i=(int)le[y];i<(int)re[y];i++)
draw_pixel(i,y);
}
}
void display()
{
x1=200.0;y1=200.0;x2=100.0;y2=300.0;x3=200.0;y3=400.0;x4=300.0;y4=300.0;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_LINE_LOOP);
glVertex2f(x1,y1);
glVertex2f(x2,y2);
glVertex2f(x3,y3);
glVertex2f(x4,y4);
glEnd();
if(fillFlag==1)
scanfill(x1,y1,x2,y2,x3,y3,x4,y4);
glFlush();
}
void init()
{
glClearColor(0.0,0.0,0.0,1.0);
glColor3f(1.0,0.0,0.0);
glPointSize(1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,499.0,0.0,499.0);
}
void fillMenu(int option)
{
if(option==1)
fillFlag=1;
if(option==2)
fillFlag=2;
display();
}
void main(int argc, char* argv[])
{ glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("Filling a Polygon using Scan-line Algorithm");
init();
glutDisplayFunc(display);
glutCreateMenu(fillMenu);
glutAddMenuEntry("Fill Polygon",1);
glutAddMenuEntry("Empty Polygon",2);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop();
}
Sample Output: