Computer Graphics LAB Programs For 6TH SEM BE
Computer Graphics LAB Programs For 6TH SEM BE
CSE
1JS10CS402
LAKSHMIKANTH.D
JSSATE
CSE
glutInitWindowSize(700,700);
glutInitWindowPosition(50,50);
glutCreateWindow("Rectangular Mesh");
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}
OUTPUT:
[kanth@localhost ~]$ cc d1.c -lglut -lGL
[kanth@localhost ~]$ ./a.out
1JS10CS402
LAKSHMIKANTH.D
JSSATE
CSE
1JS10CS402
LAKSHMIKANTH.D
JSSATE
CSE
for(i=0;i<n;i+=3)
{
circle_draw(h,k+i,r);
}
}
void rectangle_draw(GLint x1,GLint x2,GLint y1,GLint y2)
{
GLint i,j;
GLint x[i],y[i];
glColor3f(0.0,0.0,1.0);
glBegin(GL_LINE_LOOP);
glVertex2i(x1,y1);
glVertex2i(x2,y1);
glVertex2i(x2,y2);
glVertex2i(x1,y2);
glEnd();
}
void parallelopiped_draw()
{
GLint i ;
GLint x1=300,x2=400,y1=100,y2=175;
GLint n=50;
for(i=0;i<n;i+=2)
{
rectangle_draw(x1+i,x2+i,y1+i,y2+i);
}
}
void init()
{
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(0.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,499.0,0.0,499.0);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
cylinder_draw();
parallelopiped_draw();
glFlush();
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(50,60);
glutCreateWindow("Circle and Rectangle");
1JS10CS402
LAKSHMIKANTH.D
JSSATE
CSE
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
OUTPUT:
[kanth@localhost ~]$ cc d2.c -lglut -lGL
[kanth@localhost ~]$ ./a.out
1JS10CS402
LAKSHMIKANTH.D
JSSATE
CSE
1JS10CS402
LAKSHMIKANTH.D
JSSATE
CSE
glColor3f(0.3,0.3,0.3);
divide_triangle(v[0],v[2],v[3],m);
}
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.0,2.0,-2.0*(GLfloat)h/(GLfloat)w,2.0*(GLfloat)h/(GLfloat)w,
-10.0,10.0);
else
glOrtho(-2.0*(GLfloat)w/(GLfloat)h,2.0*(GLfloat)w/(GLfloat)h,-2.0,2.0,
-10.0,10.0);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}
int main(int argc,char **argv)
{
printf("Enter no. of division\n");
scanf("%d",&n);
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("3D GASKET");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glEnable(GL_DEPTH_TEST);
glClearColor(1.0,1.0,1.0,1.0);
glutMainLoop();
return 0;
}
OUTPUT:
[kanth@localhost ~]$ cc d3.c -lglut -lGL
[kanth@localhost ~]$ ./a.out
Enter no. of division
0
1JS10CS402
LAKSHMIKANTH.D
JSSATE
CSE
1JS10CS402
LAKSHMIKANTH.D
JSSATE
CSE
1JS10CS402
LAKSHMIKANTH.D
JSSATE
CSE
3. Program to draw a color cube and spin it using OpenGL transformation matrix.
#include<stdlib.h>
#include<GL/glut.h>
GLfloat vertices[ ][3]={{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0},{1.0,1.0,-1.0},{-1.0,1.0,-1.0},
{-1.0,-1.0,1.0},{1.0,-1.0,1.0},{1.0,1.0,1.0},{-1.0,1.0,1.0}};
GLfloat normals[ ][3]={{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0},{1.0,1.0,-1.0},{-1.0,1.0,-1.0},
{-1.0,-1.0,1.0},{1.0,-1.0,1.0},{1.0,1.0,1.0},{-1.0,1.0,1.0}};
GLfloat color[ ][3]={{0.0,0.0,0.0},{1.0,0.0,0.0},{1.0,1.0,0.0},{0.0,1.0,0.0},
{0.0,0.0,1.0},{1.0,0.0,1.0},{1.0,1.0,1.0},{0.0,1.0,1.0}};
void polygon(int a,int b,int c,int d)
{
glBegin(GL_POLYGON);
glColor3fv(color[a]);
glNormal3fv(normals[a]);
glVertex3fv(vertices[a]);
glColor3fv(color[b]);
glNormal3fv(normals[b]);
glVertex3fv(vertices[b]);
glColor3fv(color[c]);
glNormal3fv(normals[c]);
glVertex3fv(vertices[c]);
glColor3fv(color[d]);
glNormal3fv(normals[d]);
glVertex3fv(vertices[d]);
glEnd();
}
void colorcube(void)
{
polygon(0,3,2,1);
polygon(2,3,7,6);
polygon(0,4,7,3);
polygon(1,2,6,5);
polygon(4,5,6,7);
polygon(0,1,5,4);
}
static GLfloat theta[]={0.0,0.0,0.0,0.0};
static GLint axis=2;
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
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();
1JS10CS402
LAKSHMIKANTH.D
JSSATE
CSE
glFlush();
glutSwapBuffers();
}
void spincube()
{
theta[axis]+=1.0;
if(theta[axis]>360.0)
theta[axis]-=360.0;
glutPostRedisplay();
}
void mouse(int btn,int state,int x,int y)
{
if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
axis=0;
if(btn==GLUT_MIDDLE_BUTTON && state==GLUT_DOWN)
axis=1;
if(btn==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
axis=2;
}
void myreshape(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
glOrtho(-2.0,2.0,-2.0*(GLfloat)h/(GLfloat)w,2.0*(GLfloat)h/(GLfloat)w,
-10.0,10.0);
else
glOrtho(-2.0*(GLfloat)w/(GLfloat)h,2.0*(GLfloat)w/(GLfloat)h,-2.0,2.0,
-10.0,10.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowPosition(0,0);
glutInitWindowSize(500,500);
glutCreateWindow("SPIN AND ROTATE COLOR CUBE");
glutReshapeFunc(myreshape);
glutDisplayFunc(display);
glutIdleFunc(spincube);
glutMouseFunc(mouse);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return 0;
}
OUTPUT:
1JS10CS402
LAKSHMIKANTH.D
JSSATE
CSE
1JS10CS402
LAKSHMIKANTH.D
JSSATE
1JS10CS402
CSE
LAKSHMIKANTH.D
JSSATE
CSE
8. Program to draw a color cube and allow the user to move the camera suitably to
experiment with perspective viewing. Use OpenGL functions.
#include<stdlib.h>
#include<GL/glut.h>
GLfloat vertices[ ][3]={{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0},{1.0,1.0,-1.0},{-1.0,1.0,-1.0},
{-1.0,-1.0,1.0},{1.0,-1.0,1.0},{1.0,1.0,1.0},{-1.0,1.0,1.0}};
GLfloat normals[ ][3]={{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0},{1.0,1.0,-1.0},{-1.0,1.0,-1.0},
{-1.0,-1.0,1.0},{1.0,-1.0,1.0},{1.0,1.0,1.0},{-1.0,1.0,1.0}};
GLfloat colors[ ][3]={{0.0,0.0,0.0},{1.0,0.0,0.0},{1.0,1.0,0.0},{0.0,1.0,0.0},
{0.0,0.0,1.0},{1.0,0.0,1.0},{1.0,1.0,1.0},{0.0,1.0,1.0}};
void polygon(int a,int b,int c,int d)
{
glBegin(GL_POLYGON);
glColor3fv(colors[a]);
glNormal3fv(normals[a]);
glVertex3fv(vertices[a]);
glColor3fv(colors[b]);
glNormal3fv(normals[b]);
glVertex3fv(vertices[b]);
glColor3fv(colors[c]);
glNormal3fv(normals[c]);
glVertex3fv(vertices[c]);
glColor3fv(colors[d]);
glNormal3fv(normals[d]);
glVertex3fv(vertices[d]);
glEnd();
}
void color_cube(void)
{
polygon(0,3,2,1);
polygon(2,3,7,6);
polygon(0,4,7,3);
polygon(1,2,6,5);
polygon(4,5,6,7);
polygon(0,1,5,4);
}
static GLfloat theta[ ]={0.0,0.0,0.0};
static GLint axis=2;
static GLdouble viewer[ ]={0.0,0.0,5.0};
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);
1JS10CS402
LAKSHMIKANTH.D
JSSATE
CSE
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);
color_cube();
glFlush();
glutSwapBuffers();
}
void spinCube()
{
theta[axis]+=1.0;
if(theta[axis]>360.0)
theta[axis]-=360.0;
glutPostRedisplay();
}
void mouse(int btn,int state,int x,int y)
{
if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
axis=0;
if(btn==GLUT_MIDDLE_BUTTON && state==GLUT_DOWN)
axis=1;
if(btn==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
axis=2;
theta[axis]+=2.0;
if(theta[axis]>360.0)
theta[axis]-=360.0;
display();
}
void keys(unsigned char key,int x,int y)
{
if(key=='x') viewer[0]-=1.0;
if(key=='X') viewer[0]+=1.0;
if(key=='y') viewer[1]-=1.0;
if(key=='Y') viewer[1]+=1.0;
if(key=='z') viewer[2]-=1.0;
if(key=='Z') viewer[2]+=1.0;
display();
}
void myreshape(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
glOrtho(-2.0,2.0,-2.0*(GLfloat)h/(GLfloat)w,2.0*(GLfloat)h/(GLfloat)w,
-10.0,10.0);
else
1JS10CS402
LAKSHMIKANTH.D
JSSATE
CSE
glOrtho(-2.0*(GLfloat)w/(GLfloat)h,2.0*(GLfloat)w/(GLfloat)h,-2.0,2.0,
-10.0,10.0);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
1JS10CS402
LAKSHMIKANTH.D
JSSATE
1JS10CS402
CSE
LAKSHMIKANTH.D
JSSATE
CSE
4. Program to create a house like figure and rotate it about a given fixed point using OpenGL
functions.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<GL/glut.h>
GLfloat house[3][9]={{250.0,250.0,325.0,400.0,400.0,300.0,300.0,350.0,350.0},
{250.0,400.0,475.0,400.0,250.0,250.0,300.0,300.0,250.0},
{1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0}};
GLfloat result[3][9]={{0},{0},{0}};
GLfloat rot_mat[3][3]={{0},{0},{0}};
GLfloat h=250.0;
GLfloat k=250.0;
GLfloat theta;
void drawhouse()
{
glColor3f(0.25,0.25,0.25);
glBegin(GL_LINE_LOOP);
glVertex2f(house[0][0],house[1][0]);
glVertex2f(house[0][1],house[1][1]);
glVertex2f(house[0][3],house[1][3]);
glVertex2f(house[0][4],house[1][4]);
glEnd();
glColor3f(0.9,0.45,0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(house[0][5],house[1][5]);
glVertex2f(house[0][6],house[1][6]);
glVertex2f(house[0][7],house[1][7]);
glVertex2f(house[0][8],house[1][8]);
glEnd();
glColor3f(0.25,0.25,0.25);
glBegin(GL_LINE_LOOP);
glVertex2f(house[0][1],house[1][1]);
glVertex2f(house[0][2],house[1][2]);
glVertex2f(house[0][3],house[1][3]);
glEnd();
}
void myinit()
{
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(0.0,0.0,1.0);
glPointSize(3.0);
glLoadIdentity();
1JS10CS402
LAKSHMIKANTH.D
JSSATE
CSE
gluOrtho2D(0.0,499.0,0.0,499.0);
}
void multiply()
{
int i,j,l;
for(i=0;i<3;i++)
for(j=0;j<9;j++)
{
result[i][j]=0;
for(l=0;l<3;l++)
result[i][j]=result[i][j]+rot_mat[i][l]*house[l][j];
}
}
void rotate()
{
GLfloat m,n;
m=-h*(cos(theta)-1)+k*(sin(theta));
n=-k*(cos(theta)-1)-h*(sin(theta));
rot_mat[0][0]=cos(theta);
rot_mat[0][1]=-sin(theta);
rot_mat[0][2]=m;
rot_mat[1][0]=sin(theta);
rot_mat[1][1]=cos(theta);
rot_mat[1][2]=n;
rot_mat[2][0]=0;
rot_mat[2][1]=0;
rot_mat[2][2]=1;
multiply();
}
void drawrotated_house()
{
glColor3f(0.9,0.45,0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(result[0][0],result[1][0]);
glVertex2f(result[0][1],result[1][1]);
glVertex2f(result[0][3],result[1][3]);
glVertex2f(result[0][4],result[1][4]);
glEnd();
glColor3f(0.25,0.25,0.25);
glBegin(GL_LINE_LOOP);
glVertex2f(result[0][5],result[1][5]);
glVertex2f(result[0][6],result[1][6]);
glVertex2f(result[0][7],result[1][7]);
glVertex2f(result[0][8],result[1][8]);
glEnd();
1JS10CS402
LAKSHMIKANTH.D
JSSATE
CSE
glColor3f(0.9,0.45,0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(result[0][1],result[1][1]);
glVertex2f(result[0][2],result[1][2]);
glVertex2f(result[0][3],result[1][3]);
glEnd();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
drawhouse();
rotate();
drawrotated_house();
glFlush();
}
1JS10CS402
LAKSHMIKANTH.D
JSSATE
CSE
1JS10CS402
LAKSHMIKANTH.D
JSSATE
CSE
1JS10CS402
LAKSHMIKANTH.D
JSSATE
CSE
if(p<0.0)
{
if(t>*t1)
*t1=t;
if(t>*t2)
return(false);
}
else if(p>0.0)
{
if(t<*t2)
*t2=t;
if(t<*t1)
return(false);
}
else if(p==0.0)
{
if(q<0.0)
return(false);
}
return(true);
1JS10CS402
LAKSHMIKANTH.D
JSSATE
CSE
}
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=xvmin+(y1-ymin)*sy;
glColor3f(1,0,0);
glBegin(GL_LINES);
glVertex2f(vx0,vy0);
glVertex2f(vx1,vy1);
glEnd();
glColor3f(1,0,0);
glBegin(GL_LINE_LOOP);
glVertex2f(xvmin,yvmin);
glVertex2f(xvmax,yvmin);
glVertex2f(xvmax,yvmax);
glVertex2f(xvmin,yvmax);
glEnd();
}
void display(void)
{
double x0=60,y0=20,x1=80,y1=120;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,0,0);
glBegin(GL_LINES);
glVertex2f(x0,y0);
glVertex2f(x1,y1);
glEnd();
glColor3f(1,0,0);
glBegin(GL_LINE_LOOP);
glVertex2f(xmin,ymin);
glVertex2f(xmax,ymin);
glVertex2f(xmax,ymax);
glVertex2f(xmin,ymax);
glEnd();
}
void init()
{
1JS10CS402
lblcad(x0,y0,x1,y1);
glFlush();
glClearColor(1,1,1,1);
LAKSHMIKANTH.D
JSSATE
CSE
glColor3f(1,0,0);
glPointSize(50.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,499.0,0.0,499.0);
1JS10CS402
LAKSHMIKANTH.D
JSSATE
CSE
LAKSHMIKANTH.D
JSSATE
CSE
}
else
{
y=y0+(y1-y0)*(xmin-x0)/(x1-x0);
x=xmin;
}
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;
glColor3f(1,0,0);
glBegin(GL_LINES);
glVertex2f(vx0,vy0);
glVertex2f(vx1,vy1);
glEnd();
glColor3f(1,0,0);
glBegin(GL_LINE_LOOP);
glVertex2f(xvmin,yvmin);
glVertex2f(xvmax,yvmin);
glVertex2f(xvmax,yvmax);
glVertex2f(xvmin,yvmax);
glEnd();
}
1JS10CS402
LAKSHMIKANTH.D
JSSATE
CSE
code=TOP;
else if(y<ymin)
code=BOTTOM;
else if(x>xmax)
code=RIGHT;
else if(x<xmin)
code=LEFT;
return code;
void display(void)
{
double x0=60,y0=20,x1=80,y1=120;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,0,0);
glBegin(GL_LINES);
glVertex2f(x0,y0);
glVertex2f(x1,y1);
glEnd();
glColor3f(1,0,0);
glBegin(GL_LINE_LOOP);
glVertex2f(xmin,ymin);
glVertex2f(xmax,ymin);
glVertex2f(xmax,ymax);
glVertex2f(xmin,ymax);
glEnd();
}
void init()
{
cohensu(x0,y0,x1,y1);
glFlush();
glClearColor(1,1,1,1);
glColor3f(1,0,0);
glPointSize(1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,499.0,0.0,499.0);
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(0,0);
1JS10CS402
LAKSHMIKANTH.D
JSSATE
CSE
glutInitWindowSize(500,500);
glutCreateWindow("cohen");
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
OUTPUT:
[kanth@localhost ~]$ cc d8.c -lglut -lGL
[kanth@localhost ~]$ ./a.out
1JS10CS402
LAKSHMIKANTH.D
JSSATE
CSE
1JS10CS402
LAKSHMIKANTH.D
JSSATE
CSE
void dispsolid()
{
GLfloat mat_ambient[]={0.7f,0.7f,0.7f,1.0f};
GLfloat mat_diffuse[]={0.5f,0.5f,0.5f,1.0f};
GLfloat mat_specular[]={1.0f,1.0f,1.0f,1.0f};
GLfloat mat_shininess[]={50.0f};
glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);
glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
GLfloat lightint[] = {0.7f,0.7f,0.7f,1.0f};
GLfloat lightpos[] = {2.0f,6.0f,3.0f,0.0f};
glLightfv(GL_LIGHT0,GL_POSITION,lightpos);
glLightfv(GL_LIGHT0,GL_DIFFUSE,lightint);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double winht=1.0;
glOrtho(-winht*64/48.0,winht*64/48.0,winht,winht,0.1,100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(2.3,1.3,2.0,0.0,0.25,0.0,0.0,1.0,0.0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslated(0.6,0.38,0.5);
glRotated(30,0,1,0);
glutSolidTeapot(0.08);
glPopMatrix();
glPushMatrix();
glTranslated(0.4,0.0,0.4);
tbl(0.6,0.02,0.02,0.3);
glPopMatrix();
wall(0.02);
glPushMatrix();
glRotated(90.0,0,0,1.0);
wall(0.02);
glPopMatrix();
glPushMatrix();
glRotated(-90.0,1.0,0.0,0.0);
wall(0.02);
glPopMatrix();
glFlush();
}
int main(int argc,char ** argv)
1JS10CS402
LAKSHMIKANTH.D
JSSATE
{
CSE
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,100);
glutCreateWindow("TEAPOT");
glutDisplayFunc(dispsolid);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glClearColor(0.1,0.1,0.1,0.0);
glViewport(0.0,0.0,640,480);
glutMainLoop();
return 0;
OUTPUT:
[kanth@localhost ~]$ cc d9.c -lglut -lGL
[kanth@localhost ~]$ ./a.out
1JS10CS402
LAKSHMIKANTH.D
JSSATE
CSE
9. Program to fill any given polygon using scan-line area filling algorithm.
#include<stdio.h>
#include<stdlib.h>
#include<GL/glut.h>
float x1,y11,x2,y2,x3,y3,x4,y4;
void ed(float x1,float y11,float x2,float y2,int *le,int *re)
{
float mx,x,temp;
int i;
if((y2-y11)<0)
{
temp=y11;y11=y2;y2=temp;
temp=x1;x1=x2;x2=temp;
}
if((y2-y11)!=0)
mx=(x2-x1)/(y2-y11);
else
mx=(x2-x1);
x=x1;
for(i=y11;i<=y2;i++)
{
if(x<(float)le[i])
le[i]=(int)x;
if(x>(float)re[i])
re[i]=(int)x;
x+=mx;
}
}
void dp(int x,int y,int val)
{
glColor3f(1,1,0);
glBegin(GL_POINTS);
glVertex2f(x,y);
glEnd();
}
void scan(float x1,float y11,float x2,float y2,float x3,float y3,float x4,float y4)
{
int le[500],re[500];
int i,j;
for(i=0;i<500;i++)
{
le[i]=500;
1JS10CS402
LAKSHMIKANTH.D
JSSATE
CSE
re[i]=0;
}
ed(x1,y11,x2,y2,le,re);
ed(x2,y2,x3,y3,le,re);
ed(x3,y3,x4,y4,le,re);
ed(x4,y4,x1,y11,le,re);
for(j=0;j<500;j++)
{
if(le[j]<=re[j])
for(i=(int)le[j];i<(int)re[j];i++)
dp(i,j,0);
}
void display(void)
{
x1=200,y11=200;
x2=100,y2=300;
x3=200,y3=400;
x4=300,y4=300;
glClear(GL_COLOR_BUFFER_BIT);
}
void init()
{
glColor3f(1,0,0);
glBegin(GL_LINE_LOOP);
glVertex2f(x1,y11);
glVertex2f(x2,y2);
glVertex2f(x3,y3);
glVertex2f(x4,y4);
glEnd();
scan(x1,y11,x2,y2,x3,y3,x4,y4);
glFlush();
glClearColor(0,0,0,0);
glColor3f(1,0,0);
glPointSize(1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,499.0,0.0,499.0);
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(0,0);
1JS10CS402
LAKSHMIKANTH.D
JSSATE
CSE
glutInitWindowSize(500,500);
glutCreateWindow(" scan line");
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
OUTPUT:
[kanth@localhost ~]$ cc d10.c -lglut -lGL
[kanth@localhost ~]$ ./a.out
1JS10CS402
LAKSHMIKANTH.D
JSSATE
CSE
$ ./a.out
1JS10CS402
LAKSHMIKANTH.D