Sie sind auf Seite 1von 35

Mcsl 054 (53)

----Session 2----
Exercise 4:
#include<windows.h>
#include<gl/gl.h>
#include<gl/glu.h>
#include<gl/glut.h>

void init()
{
glClearColor(0.0,0.0,0.0,0.0); // background Color
glColor3f(1.0,1.0,0.0);//Drawing Color
glPointSize(4);
o m
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
o t.c
gluOrtho2D(0.0,800,0.0,600.0);

s p
}
void display()
lo g
{

e . b
it
glClear(GL_COLOR_BUFFER_BIT);

s
u
glBegin(GL_POLYGON);

n oglVertex2i(100,100);

i g glVertex2i(100,500);
glVertex2i(700,500);
glVertex2i(700,100);
glEnd();
glFlush();
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(800,600);
glutCreateWindow("Session2, Exer4(Niraj)");
glutDisplayFunc(display);
init();

www.Ignousite.blogspot.com (ignou study helper)


glutMainLoop();
}
Output:

Exercise 5:
o m
/*
o t .c
sp
* Problem definition: Yello rectange on black background
* By: Prasun Pal(prsn)

lo g
* Date: 21/04/2009
*/
e . b
s
#include<windows.h>
it
u
#include<gl/gl.h>

o
i g n
#include<gl/glu.h>
#include<gl/glut.h>

void init()
{
glClearColor(0.0,0.0,0.0,0.0); // background Color
glColor3f(1.0,0.0,0.0);//Drawing Color
glPointSize(4);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,800,0.0,600.0);
}
void display()
{

www.Ignousite.blogspot.com (ignou study helper)


glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glVertex2i(100,100);
glVertex2i(100,500);
glVertex2i(700,500);
glVertex2i(700,100);
glEnd();
glFlush();
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(800,600);
glutCreateWindow("Session2, Exer4(Niraj)");
o m
glutDisplayFunc(display);
init();
o t.c
glutMainLoop();
sp
}
Output:
lo g
e . b
s it
o u
i g n

Exercise 6:
/*
* Problem definition: just to draw basic primitives of OpenGl

www.Ignousite.blogspot.com (ignou study helper)


* such as GL_INES, GL_QUAD, GL_TRIANGLES etc.
* By: Prasun Pal(prsn)
* Date: 21/04/2009
*/

#include<windows.h>
#include<gl/gl.h>
#include<gl/glu.h>
#include<gl/glut.h>

void init()
{
glClearColor(1.0,1.0,1.0,0.0); //background Color
glColor3f(0.0,0.0,0.0);//Drawing Color
glPointSize(4);
o m
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
o t .c
gluOrtho2D(0.0,800,0.0,600.0);
sp
}
void display()
lo g
{
e . b
it
glClear(GL_COLOR_BUFFER_BIT);

s
o u
glBegin(GL_LINES);
glVertex2i(500,500);

i g n glVertex2i(550,400);
glEnd();

glBegin(GL_QUADS);
glVertex2i(125,350);
glVertex2i(362,254);
glVertex2i(154,352);
glVertex2i(346,254);
glVertex2i(258,45);
glVertex2i(454,253);
glVertex2i(259,475);
glVertex2i(12,389);
glEnd();

www.Ignousite.blogspot.com (ignou study helper)


glBegin(GL_TRIANGLES);
glVertex2i(600,200);
glVertex2i(650,300);
glVertex2i(700,200);
glEnd();

glFlush();
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(800,600);
glutCreateWindow("Session2, Exer4(prsn)");
glutDisplayFunc(display);
o m
init();
glutMainLoop();
o t.c
}
sp
Output:

lo g
e . b
s it
o u
i g n

----Session 3----

www.Ignousite.blogspot.com (ignou study helper)


Exercise 7:

/*
* Problem definition:To impement DDA Line-generation algorithm
* By: Prasun Pal(prsn)
* Date: 21/04/2009
*/

#include<windows.h>
#include<gl/gl.h>
#include<gl/glu.h>
#include<gl/glut.h>
#include<math.h>
void nkjSwap(float *,float *);

o m
void nkjInit()
{
o t .c
sp
glClearColor(1.0,1.0,1.0,0.0); //Black background Color

glPointSize(2);
lo g
glColor3f(0.0,0.0,0.0);//Drawing Color yellow

e . b
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

s it
}
o u
gluOrtho2D(0.0,800,0.0,600.0);

i g n
void nkjDDA4f(float x1, float y1, float x2, float y2)
{
if(x1>x2)
{
nkjSwap(&x1,&x2);
nkjSwap(&y1,&y2);
}

float slope=(y2-y1)/(float)(x2-x1);
if(slope>0 && slope<1)
{
while(x1<=x2)
{

www.Ignousite.blogspot.com (ignou study helper)


glVertex2i((int)x1,int(y1));
x1++;
y1+=slope;
}
}
else if(slope>=1)
{
float slope1=1/slope;
while(y1<=y2)
{
glVertex2i(int(x1),int(y1));
y1++;
x1+=slope1;
}

o m
}
}

o t.c
sp
void nkjSwap(float *x, float *y)
{
lo g
float temp=*x;
e . b
*x=*y;

s it
}
*y=temp;

o u
i g n
void nkjDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
nkjDDA4f(100.0,100.0,350.0,350.0);//two points
p1(100,100), p2(350,350)
glEnd();
glFlush();
}

void main(int argc, char **argv)


{

www.Ignousite.blogspot.com (ignou study helper)


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(800,600);
glutCreateWindow("Session2, Exer4(Niraj)");
glutDisplayFunc(nkjDisplay);
nkjInit();
glutMainLoop();

Output:

o m
o t.c
sp
lo g
e . b
s it
o u
i g n

Exercise 8:

/*

www.Ignousite.blogspot.com (ignou study helper)


* Problem definition:To impement Bresenham Circle-generation
algorithm
* By: Prasun Pal(prsn)
* Date: 21/04/2009
*/

#include<windows.h>
#include<gl/gl.h>
#include<gl/glu.h>
#include<gl/glut.h>
#include<math.h>
#include<stdio.h>

void nkjSwap(float *,float *);

o m
.c
void nkjInit()
{

o t
p
glClearColor(1.0,1.0,1.0,0.0); //Black background Color

s
glPointSize(2);
lo g
glColor3f(0.0,0.0,0.0);//Drawing Color yellow

. b
glMatrixMode(GL_PROJECTION);

e
glLoadIdentity();

s it
gluOrtho2D(0.0,800,0.0,600.0);
}
o u
i g n
void nkjBresenhamCircleGeneration3f(float x1, float y1, float
radious)
{
float dcsnPrmtr=5/4-radious;//decision Parameter
int k=0;
float x=x1, y=y1;
x1=0;
y1=radious;
while(x1<y1)
{
if(dcsnPrmtr<0)
{
dcsnPrmtr += 2 * x1 + 2 + 1;

www.Ignousite.blogspot.com (ignou study helper)


x1++;
}
else //if(dcsnPrmtr
{
dcsnPrmtr += 2 * x1 + 2 - 2 * y1 - 2 + 1;
x1++;
y1--;
}
//generate symmetry points

glVertex2i((int)(x+x1),(int)(y+y1));
glVertex2i((int)(x-x1),(int)(y+y1));
glVertex2i((int)(x+x1),(int)(y-y1));
glVertex2i((int)(x-x1),(int)(y-y1));

o m
glVertex2i((int)(x+y1),(int)(y+x1));
glVertex2i((int)(x-y1),(int)(y+x1));
o t .c
sp
glVertex2i((int)(x+y1),(int)(y-x1));

}
lo g
glVertex2i((int)(x-y1),(int)(y-x1));

}
e . b
void nkjDisplay()

s it
{

o u
glClear(GL_COLOR_BUFFER_BIT);

i g n
glBegin(GL_POINTS);
nkjBresenhamCircleGeneration3f(400.0,300.0,150.0);
glEnd();
glFlush();
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(800,600);
glutCreateWindow("Session2, Exer4(nkj)");
glutDisplayFunc(nkjDisplay);
nkjInit();
glutMainLoop();

www.Ignousite.blogspot.com (ignou study helper)


}

Output:

o m
o t .c
sp
lo g
e . b
it
Exercise 9:

/*
u s
n o
* Problem definition:To draw a convex hall polygon

i g
* By: Prasun Pal(prsn)
* Date: 21/04/2009
*/

#include<windows.h>
#include<gl/gl.h>
#include<gl/glu.h>
#include<gl/glut.h>
#include<math.h>
#include<stdio.h>

void prsnInit()

www.Ignousite.blogspot.com (ignou study helper)


{
glClearColor(1.0,1.0,1.0,0.0); //Black background Color
glColor3f(0.0,0.0,0.0);//Drawing Color yellow
glPointSize(4);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,800,0.0,600.0);
}

void prsnConvexHallPolygon()
{
glVertex2i(200,200);
glVertex2i(150,250);

glVertex2i(150,250);
o m
glVertex2i(150,300);

o t.c
glVertex2i(150,300);
sp
glVertex2i(200,350);

lo g
glVertex2i(200,350);
e . b
glVertex2i(250,300);

s it
o u
glVertex2i(250,300);

i g n
glVertex2i(250,250);

glVertex2i(250,250);
glVertex2i(200,200);

void prsnDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINES);
prsnConvexHallPolygon();
glEnd();

www.Ignousite.blogspot.com (ignou study helper)


glFlush();
}

void main(int argc, char **argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(800,600);
glutCreateWindow("Session2, Exer4(prsn)");
glutDisplayFunc(prsnDisplay);
prsnInit();
glutMainLoop();

}
o m
o t.c
Output:
sp
lo g
e . b
s it
o u
i g n

www.Ignousite.blogspot.com (ignou study helper)


----Session 4----

Exercise 10:

#include<windows.h>
#include<gl/gl.h>
#include<gl/glu.h>

m
#include<gl/glut.h>
#include<iostream.h>

.c o
void display();
o t
void myInit();
sp
void main(int i,char **ptr)

lo g
{
glutInit(&i, ptr);
e . b
s it
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

u
glutInitWindowSize(800,600);

o
i g n
glutInitWindowPosition(100,100);
glutCreateWindow("OpenGL Window");
glutDisplayFunc(display);
myInit();
glutMainLoop();
}
void myInit()
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,800.0,0.0,800.0);

www.Ignousite.blogspot.com (ignou study helper)


}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINES);

glVertex2i(200,200);
glVertex2i(500,200);
glVertex2i(200,400);
glVertex2i(500,400);
glVertex2i(300,100);
glVertex2i(300,500);
glVertex2i(400,500);
glVertex2i(400,100);

o m
glEnd();
glFlush();
o t.c
}
sp
Output:

lo g
e . b
s it
o u
i g n

Exercise 11:

www.Ignousite.blogspot.com (ignou study helper)


#include<windows.h>
#include<gl/gl.h>
#include<gl/glu.h>
#include<gl/glut.h>
#include<iostream.h>

void display();
void myInit();
void main(int i,char **ptr)
{
glutInit(&i, ptr);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(800,600);
glutInitWindowPosition(100,100);
glutCreateWindow("OpenGL Window");
o m
glutDisplayFunc(display);
myInit();
o t.c
glutMainLoop();
sp
}

lo g
void myInit()
e . b
{

s it
o u
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);

i g n
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,800.0,0.0,800.0);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(4.0);
glBegin(GL_LINES);
glVertex2i(100,200);
glVertex2i(400,200);
glVertex2i(100,400);
glVertex2i(400,400);

www.Ignousite.blogspot.com (ignou study helper)


glVertex2i(200,100);
glVertex2i(200,500);
glVertex2i(300,500);
glVertex2i(300,100);
glEnd();

glEnable(GL_LINE_STIPPLE);
glLineStipple(1,0xAAAA);
glBegin(GL_LINES);

glVertex2i(450,200);
glVertex2i(750,200);
glVertex2i(450,400);
glVertex2i(750,400);
glVertex2i(550,100);
o m
glVertex2i(550,500);
glVertex2i(650,500);
o t.c
glVertex2i(650,100);
sp
glEnd();
lo g
e .
glDisable(GL_LINE_STIPPLE);
b
s it
}
o u
glFlush();

i g n
Output:

www.Ignousite.blogspot.com (ignou study helper)


o m
o t.c
sp
Exercise 12:

lo g
#include<windows.h>

e . b
#include<gl/gl.h>

s it
u
#include<gl/glu.h>

o
#include<gl/glut.h>

n
i g
void display();

void main(int argc, char *argv[])


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(50,50);
glutCreateWindow("EX-12 GRAPHICS");
glutDisplayFunc(display);

//init
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(1.0,1.0,0.0);
glPointSize(4);

www.Ignousite.blogspot.com (ignou study helper)


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,800.0,0.0,600.0);
glutMainLoop();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,1.0,1.0);
glBegin(GL_LINES);
glVertex2i(140,310);
glVertex2i(40,210);
glVertex2i(140,310);
glVertex2i(240,210);
glVertex2i(40,210);
o m
glVertex2i(40,10);
glVertex2i(240,210);
o t.c
glVertex2i(240,10);
sp
glVertex2i(40,180);
glVertex2i(240,180);
lo g
glVertex2i(240,210);
e . b
glVertex2i(240,310);

s it
o u
glVertex2i(240,310);
glVertex2i(220,310);

i g n
glVertex2i(220,310);
glVertex2i(220,230);
glVertex2i(120,180);
glVertex2i(120,10);
glVertex2i(120,10);
glVertex2i(160,10);
glVertex2i(160,10);
glVertex2i(160,180);
glVertex2i(40,10);
glVertex2i(240,10);
glEnd();

glBegin(GL_QUADS);

www.Ignousite.blogspot.com (ignou study helper)


glVertex2i(50,205);
glVertex2i(50,180);
glVertex2i(75,180);
glVertex2i(75,205);
glVertex2i(205,205);
glVertex2i(205,180);
glVertex2i(230,180);
glVertex2i(230,205);
glEnd();
glFlush();
}

o m
o t.c
sp
Output:

lo g
e . b
s it
o u
i g n

www.Ignousite.blogspot.com (ignou study helper)


o m
o t.c
sp
g
----Session 5----

lo
Exercise 13:
e . b
s it
u
#include<windows.h>

o
i n
#include<gl/gl.h>

g
#include<gl/glu.h>
#include<gl/glut.h>
#include<iostream.h>

void display();
void myInit();
void main(int i,char **ptr)
{
glutInit(&i, ptr);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(400,400);
glutInitWindowPosition(100,100);
glutCreateWindow("OpenGL Window");

www.Ignousite.blogspot.com (ignou study helper)


glutDisplayFunc(display);
myInit();
glutMainLoop();
}
void myInit()
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,800.0,0.0,800.0);
}
void display()
{
o m
glClear(GL_COLOR_BUFFER_BIT);
int i=0,j=0;
o t .c
int scale=100;
s p
for(i=0;i<8;i++)
{
lo g
for(j=0;j<8;j++)
e . b
{

s it
o u if((i+j)%2==0)
{

i g n }
glColor3f(0.0,0.0,0.0);

else
{
glColor3f(1.0,1.0,1.0);
}
glBegin(GL_POLYGON);
glVertex2i(j*scale,i*scale);
glVertex2i(j*scale,i*scale+scale);
glVertex2i(j*scale+scale,i*scale+scale);
glVertex2i(j*scale+scale,i*scale);
glEnd();
}
}

www.Ignousite.blogspot.com (ignou study helper)


glFlush();
}

Output:

o m
o t.c
sp
Exercise 15:

lo g
#include<windows.h>

e . b
#include<gl/gl.h>

s it
u
#include<gl/glu.h>

o
#include<gl/glut.h>

n
i g
#include<stdio.h>
#include<math.h>
#include<stdarg.h>

//function that implements Sutherand-Cohen algorithm


void nkjImpementsSutherlandCohen(int [], int , ... );
//function to deside visibiity of any line
int nkjDecideVisibility(int [],int *,int *,int *,int *);
//function to generate bit code of points
int nkjGenerateCode(int,int, int, int, int ,int);
//to perform swapping
void nkjSwap(int * , int *);

void nkjInit()

www.Ignousite.blogspot.com (ignou study helper)


{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(4);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,200.0,0.0,200.0);
}

void nkjDisplayLines()
{
int points[]={110,110,10,10};// points for window position
int xMax,yMax,xMin,yMin;
xMax=yMax=110;
xMin=yMin=10;
o m
glClear(GL_COLOR_BUFFER_BIT);

o t.c
//Drawing Window
sp
glBegin(GL_LINES);
glVertex2i(xMin,yMin);
lo g
e . b
glVertex2i(xMin,yMax);

s it
o u
glVertex2i(xMin,yMax);
glVertex2i(xMax,yMax);

i g n glVertex2i(xMax,yMax);
glVertex2i(xMax,yMin);

glVertex2i(xMax,yMin);
glVertex2i(xMin,yMin);

nkjImpementsSutherlandCohen(points,12,25,75,90,100,125,35,110,1
20,50,50,120,120);
glEnd();
glFlush();
}
void nkjImpementsSutherlandCohen(int polygonPoints[], int
vertexPoints, ... )

www.Ignousite.blogspot.com (ignou study helper)


{
int x1, y1, x2,y2;
int ind, total, decision;

va_list ptr;
va_start(ptr, vertexPoints);
if(vertexPoints%4!=0)
{
printf("nkjError Message! Wrong number of arguments
given........\n");
return;
}
total=vertexPoints/4;
glClear(GL_COLOR_BUFFER_BIT);

o m
.c
for(ind=0;ind<total;ind++)
{

o t
x1=va_arg(ptr,int);

sp
y1=va_arg(ptr,int);
x2=va_arg(ptr,int);
lo g
.
y2=va_arg(ptr,int);

e b
decision=

s it
nkjDecideVisibility(polygonPoints,&x1,&y1,&x2,&y2);

o u
if(decision!=-1)

i g n {
//this implies ine must be drawn and points are stored
//in the corresponding variables
glVertex2i(x1,y1);
glVertex2i(x2,y2);
}
}
}

int nkjDecideVisibility(int points[], int *x1,int *y1, int *x2, int


*y2)
{
int xMax,yMax,xMin,yMin;
int code1,code2;
xMax=points[0];

www.Ignousite.blogspot.com (ignou study helper)


yMax=points[1];
xMin=points[2];
yMin=points[3];

for(;;)
{
code1=nkjGenerateCode(xMax,yMax,xMin,yMin,*x1,*y1);
code2=nkjGenerateCode(xMax,yMax,xMin,yMin,*x2,*y2);

if(code1==0 && code2==0)


{
//this indicates line is totaly visible
return 1;
}
else if((code1 & code2)!=0)
o m
{

o t
//this implies line is totaly invisible.c
return -1;
s p
}
else
lo g
{
e . b
s it
if(*x1>xMax)

o u {
//finding intersection of line[(x1,y1),(x2,y2)] and xMax

i g n *y1=(((*y2-*y1)/(*x2-*x1))*(xMax-*x1)) + *y1;
*x1=xMax;
}
else if(*x1<xMin)
{
//finding intersection of line[(x1,y1),(x2,y2)] and xMin
*y1=(((*y2-*y1)/(*x2-*x1))*(xMin-*x1)) + *y1;
*x1=xMin;
}

if(*y1>yMax)
{
//finding intersection of line[(x1,y1),(x2,y2)] and yMax
*x1=((yMax-*y1)*((*x2-*x1)/(*y2-*y1))) + *x1;

www.Ignousite.blogspot.com (ignou study helper)


*y1=yMax;
}
else if(*y1<yMin)
{
//finding intersection of line[(x1,y1),(x2,y2)] and yMin
*x1=((yMin-*y1)*((*x2-*x1)/(*y2-*y1))) + *x1;
*y1=yMin;
}
}

//generating new code for the clipped points

code1=nkjGenerateCode(xMax,yMax,xMin,yMin,*x1,*y1);
if(code1==0)
{
o m
t .c
//interchange two points and respective flags
nkjSwap(x1,x2);
o
nkjSwap(y1,y2);
s p
}
nkjSwap(&code1,&code2);

lo g
}
e . b
it
return -1; //this will never execute, just to satisfy compiler

s
}

o u
i g n
int nkjGenerateCode(int xMax, int yMax, int xMin, int yMin, int x,
int y)
{
int code=0;
//code sequence UDLR
if(x>xMax)
code|=1;//0001 Right bit
else if(x<xMin)
code|=2;//0010 Left bit

if(y>yMax)
code|=8;//1000 Up/Top bit
else if(y<yMin)
code|=4;//0100 Down/Bottom nit

www.Ignousite.blogspot.com (ignou study helper)


return code;
}

void nkjSwap(int *x, int *y)


{
*x=*x^*y;
*y=*x^*y;
*x=*x^*y;
}

void main(int argc, char **argv)


{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(400,400);
o m
glutInitWindowPosition(10,10);

o t.c
glutCreateWindow("Sutherland-Cohen by Prasun Pal");
glutDisplayFunc(nkjDisplayLines);
sp
nkjInit();
glutMainLoop();
lo g
}
e . b
s it
Output:

o u
i g n

www.Ignousite.blogspot.com (ignou study helper)


----Session 6----

Exercise 16:

#include<windows.h>
#include<gl/gl.h>
#include<gl/glu.h>
#include<gl/glut.h>
#include<stdio.h>
#include<math.h>
#include<stdarg.h>

//function that implements Sutherand-Cohen algorithm


void nkjImpementsSutherlandCohen(int [], int , ... );
//function to deside visibiity of any line
o m
int nkjDecideVisibility(int [],int *,int *,int *,int *);

o t.c
p
//function to generate bit code of points
int nkjGenerateCode(int,int, int, int, int ,int);

g s
lo
//to perform swapping
void nkjSwap(int * , int *);

e . b
void nkjInit()
s it
{

o u
i g n
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(4);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,200.0,0.0,200.0);
}

void nkjDisplayLines()
{
int points[]={60,40,20,20};// points for window position xMax, yMax,
// xMin, yMin
int xMax,yMax,xMin,yMin;
xMax=60;

www.Ignousite.blogspot.com (ignou study helper)


yMax=40;
xMin=yMin=20;
glClear(GL_COLOR_BUFFER_BIT);

//Drawing Window
glBegin(GL_LINES);
glVertex2i(xMin,yMin);
glVertex2i(xMin,yMax);

glVertex2i(xMin,yMax);
glVertex2i(xMax,yMax);

glVertex2i(xMax,yMax);
glVertex2i(xMax,yMin);

o m
glVertex2i(xMax,yMin);
glVertex2i(xMin,yMin);
o t.c
sp
//Total 4 points two for p and two for q

lo g
nkjImpementsSutherlandCohen(points,4,40,80,120,30);
glEnd();

e . b
}
glFlush();

s it
o u
i n
void nkjImpementsSutherlandCohen(int polygonPoints[], int

g
vertexPoints, ... )
{
int x1, y1, x2,y2;
int ind, total, decision;

va_list ptr;
va_start(ptr, vertexPoints);
if(vertexPoints%4!=0)
{
printf("nkjError Message! Wrong number of arguments given......\n");
return;
}
total=vertexPoints/4;

www.Ignousite.blogspot.com (ignou study helper)


glClear(GL_COLOR_BUFFER_BIT);

for(ind=0;ind<total;ind++)
{
x1=va_arg(ptr,int);
y1=va_arg(ptr,int);
x2=va_arg(ptr,int);
y2=va_arg(ptr,int);
decision=
nkjDecideVisibility(polygonPoints,&x1,&y1,&x2,&y2);
if(decision!=-1)
{
//this implies ine must be drawn and points are stored
//in the corresponding variables
glVertex2i(x1,y1);

o m
.c
glVertex2i(x2,y2);
}

o t
}

sp
}

lo g
. b
int nkjDecideVisibility(int points[], int *x1,int *y1, int *x2, int

e
it
*y2)
{

u s
int xMax,yMax,xMin,yMin;

n o
int code1,code2;

i gxMax=points[0];
yMax=points[1];
xMin=points[2];
yMin=points[3];

for(;;)
{
code1=nkjGenerateCode(xMax,yMax,xMin,yMin,*x1,*y1);
code2=nkjGenerateCode(xMax,yMax,xMin,yMin,*x2,*y2);

if(code1==0 && code2==0)


{
//this indicates line is totaly visible

www.Ignousite.blogspot.com (ignou study helper)


return 1;
}
else if((code1 & code2)!=0)
{
//this implies line is totaly invisible
return -1;
}
else
{
if(*x1>xMax)
{
//finding intersection of line[(x1,y1),(x2,y2)] and xMax
*y1=(((*y2-*y1)/(*x2-*x1))*(xMax-*x1)) + *y1;
*x1=xMax;
}
o m
else if(*x1<xMin)
{
o t .c
s p
//finding intersection of line[(x1,y1),(x2,y2)] and xMin

*x1=xMin;
lo g
*y1=(((*y2-*y1)/(*x2-*x1))*(xMin-*x1)) + *y1;

}
e . b
s it
o u if(*y1>yMax)
{

i g n //finding intersection of line[(x1,y1),(x2,y2)] and yMax


*x1=((yMax-*y1)*((*x2-*x1)/(*y2-*y1))) + *x1;
*y1=yMax;
}
else if(*y1<yMin)
{
//finding intersection of line[(x1,y1),(x2,y2)] and yMin
*x1=((yMin-*y1)*((*x2-*x1)/(*y2-*y1))) + *x1;
*y1=yMin;
}
}

//generating new code for the clipped points

www.Ignousite.blogspot.com (ignou study helper)


code1=nkjGenerateCode(xMax,yMax,xMin,yMin,*x1,*y1);
if(code1==0)
{
//interchange two points and respective flags
nkjSwap(x1,x2);
nkjSwap(y1,y2);
nkjSwap(&code1,&code2);
}
}
return -1; //this will never execute, just to satisfy compiler
}

int nkjGenerateCode(int xMax, int yMax, int xMin, int yMin, int x,
int y)
{

o m
.c
int code=0;
//code sequence UDLR

o t
if(x>xMax)

sp
code|=1;//0001 Right bit
else if(x<xMin)
lo g
. b
code|=2;//0010 Left bit

e
if(y>yMax)
s it
o u
code|=8;//1000 Up/Top bit

i g n
else if(y<yMin)
code|=4;//0100 Down/Bottom nit

return code;
}

void nkjSwap(int *x, int *y)


{
*x=*x^*y;
*y=*x^*y;
*x=*x^*y;
}

void main(int argc, char **argv)

www.Ignousite.blogspot.com (ignou study helper)


{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(400,400);
glutInitWindowPosition(10,10);
glutCreateWindow("Sutherland-Cohen by Niraj Kumar Jha");
glutDisplayFunc(nkjDisplayLines);
nkjInit();
glutMainLoop();
}
Output:

o m
o t.c
sp
lo g
e . b
s it
o u
i g n
Output for the given window size, where line is totally invisible.

www.Ignousite.blogspot.com (ignou study helper)


o m
o t.c
sp
lo g
e . b
sit
o u
i g n

Das könnte Ihnen auch gefallen