Lab Manual
Lab Manual
Lab Manual
VERSION-4
CS6513 COMPUTER GRAPHICS LABORATORY LTPC
0032
OBJECTIVES:
The student should be made to:
Understand graphics programming
Be exposed to creation of 3D graphical scenes using open graphics library suits
Be familiar with image manipulation, enhancement
Learn to create animations
To create a multimedia presentation/Game/Project.
LIST OF EXPERIMENTS:
IMPLEMENT THE EXERCISES USING C / OPENGL / JAVA
S.No TOPICS
6 3D PROJECTIONS
8 2D ANIMATION
1. Open TC folder ------ after opening , you will see a folder with name BGI
2. Open BGI folder ----- copy EGAVGA.BGI and CGA.BGI
3. Paste these files into bin folder.
4. After that, open TC program--- click on “option” ---click on “linker”---
select “libraries”----click “graphics library”
5. Run the program.
DIGITAL DIFFERENTIAL ANALYZER (DDA) ALGORITHM
EX NO: 1a
Aim :
Algorithm:
Step 1: Initialize graphics variables and graphics mode using initgraph() function.
Step 2: Input the line endpoints and store the left endpoint in (x1, y1) and right endpoint
in(x2, y2)
Step 3 : Calculate the difference in x values and y values for finding no of times to plot the pixel
dx = x2 – x1, dy = y2 – y1
Step 4 :If the values of dx >dy,assign values of steps as dx ,otherwise the values of steps
as dy
Step 5 :Assign the value x= x1 and y = y1 and draw the initial point .
Step 6: Calculate the values of X increment and Y incrementby dividing dx and dy values
bystepsforfinding the next point (X,Y) to be plotted by
X = X + X increment , Y= Y + Y increment
PROGRAM :
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
void main()
{
int i,steps,x1,x2,y1,y2;
floatx,y,xinc,yinc,dx,dy;
charmsg[86];
intgdriver = DETECT,gmode,errorcode;
clrscr();
initgraph(&gdriver,&gmode,"f:\\tc");
printf("\n Enter the co ordinates ");
scanf("%d%d%d%d",&x1,&x2,&y1,&y2);
cleardevice();
outtextxy(200,4,"Draw Using DDA");
dx = x2 - x1;
dy = y2 - y1;
if(abs(dx) > abs(dy))
steps = abs(dx);
else
steps = abs(dy);
xinc = (float)dx/steps ;
yinc = (float)dy/steps ;
y = y1;
x = x1;
putpixel(ceil(x),ceil(y),20);
for(i = 0;i <= steps ;i++)
{
x += xinc ;
y += yinc ;
putpixel(x,y,2);
delay(45);
}
getch();
}
OUTPUT :
RESULT:
Thus the above program has been executed and output is verified.
Aim :
Algorithm:
Step 1: Initialize graphics variables and graphics mode using initgraph() function.
Step 3: Calculate the difference in x values and in y values to find the starting value of decision
parameter pwhich determines next position to be plotted as
dx=x1-x2
dy=y1-y2
p=2*dy-dx.
Step 4: If dx is not equal to zero and if x1 is greater than x2 then start to plot from end
point(x2,y2) and assignxend value to x1 to identify the place where to stop the plotting,otherwise
plot from initial point(x1,y1) and assign xend value to x2.
Step 5: If p<0, the next point to plot is (x++,y) andnext decision parameter p+=2*dy, otherwise
p+=2*(dy-dx).
Step 7:If p<0, the next point to plot is (x,y++) anddecision parameter p+=2*dx, otherwise
p+=2*(dx-dy).
PROGRAM :
#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int x1,x2,y1,y2;
intgd=DETECT,gm;
voidlinebres(int,int,int,int);
printf("Enter the two end points:");
scanf("%d%d%d%d",&x1,&x2,&y1,&y2);
initgraph(&gd,&gm,"");
cleardevice();
linebres(x1,y1,x2,y2);
getch();
line(x1,y1,x2,y2);
getch();
closegraph();
}
voidlinebres(int x1,int y1,int x2,int y2)
{
int dx=abs(x1-x2),dy=abs(y1-y2);
intp,x,y,i,xend,yend;
if(dx!=0)
{
p=2*dy-dx;
if(x1>x2)
{
x=x2;
y=y2;
xend=x1;
}
else
{
x=x1;
y=y1;
xend=x2;
}
putpixel(x,y,2);
for(i=x;i<xend;i++)
{
x+=1;
if(p<0)
p+=2*dy;
else
p+=2*(dy-dx);
}
putpixel(x,y,2);
}
else
{
p=2*dx-dy;
if(y1>y2)
{
x=x2;
y=y2;
yend=y2;
}
putpixel(x,y,2);
for(i=y;i<yend;i++)
{
y+=1;
if(p<0)
p+=2*dx;
else
{
x+=1;
p+=2*(dx-dy);
}
putpixel(x,y,2);
}
}
}
OUTPUT :
RESULT:
Thus the above program has been executed and output is verified.
BRESENHAM’S CIRCLE DRAWING ALGORITHM
EX NO: 1c
Aim :
Algorithm:
Step 1: Initialize graphics variables and graphics mode using initgraph() function.
Step 2: Input radius r and the midpoint of the circle (x,y) and obtain the first point on the
circumference for the circle as (0,r).
Step 3: Calculate the initial value of the decision parameter for determining the next
Positionas p=1-r.
Step 4: Calculate the next position in the circle circumference to be plotted bychecking
a) If p<0 then x=x+1 and next decision parameter p+=2*x+1
b) Else assign x=x+1, y=y-1 and p+=2*(x-y)+1.
Step 6: Move each calculated pixel position (x,y) onto the circular path centered on
(xc,yc) and plot the coordinate value as x=x+xc and y=y+yc.
PROGRAM :
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
intgd=DETECT,gm;
intx,y,r;
voidcir(int,int,int);
printf("Enter the Mid points and Radious:");
scanf("%d%d%d",&x,&y,&r);
initgraph(&gd,&gm,"");
cir(x,y,r);
getch();
closegraph();
}
voidcir(int x1,int y1,int r)
{
int x=0,y=r,p=1-r;
voidcliplot(int,int,int,int);
cliplot(x1,y1,x,y);
while(x<y)
{
x++;
if(p<0)
p+=2*x+1;
else
{
y--;
p+=2*(x-y)+1;
}
cliplot(x1,y1,x,y);
}
}
voidcliplot(intxctr,intyctr,intx,int y)
{
putpixel(xctr +x,yctr +y,1);
putpixel(xctr -x,yctr +y,1);
putpixel(xctr +x,yctr - y,1);
putpixel(xctr -x,yctr -y,1);
putpixel(xctr +y, yctr +x,1);
putpixel(xctr -y,yctr +x,1);
putpixel(xctr +y, yctr -x,1);
putpixel(xctr -y,yctr -x,1);
getch(); }
OUTPUT:
Enter the Mid points and Radius:100 100 50
RESULT:
Thus the above program has been executed and output is verified.
Aim :
Algorithm:
Step 1: Initialize graphics variables and graphics mode using initgraph() function.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
#include<stdlib.h>
void menu();
void input();
void output();
void translation();
void rotation();
void scaling();
void shearing();
void reflection();
int a[10][2],i,x,option,temp,angle,tx,ty,fx,fy,sh,k,n,axis,y;
floatsx,sy;
void menu()
{
printf("menu\n");
printf("1.Translation\n");
printf("2.rotation\n");
printf("3.scaling\n");
printf("4.shearing\n");
printf("5.reflection\n");
printf("6.exit\n");
printf("enter the choice:");
scanf("%d",&option);
switch(option)
{
case 1:
input();
translation();
break;
case 2:
input();
rotation();
break;
case 3:
input();
scaling();
break;
case 4 :
input();
shearing();
break;
case 5:
input();
reflection();
break;
case 6:
exit(0);
break;
}
}
void input()
{
printf("enter the number of vertices:" );
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the coordinates:");
scanf("%d%d%d%d",&a[i][0],&a[i][1],&a[i+1][0],&a[i+1][1]);
}
}
void output()
{
cleardevice();
for(i=0;i<n;i++)
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
}
void translation()
{
output();
printf("enter the tranformation vertex tx,ty:\n");
scanf("%d%d",&tx,&ty);
for(i=0;i<=n;i++)
{
a[i][0]=a[i][0]+tx;
a[i][1]=a[i][1]+ty;
}
output();
delay(10);
menu();
}
void rotation()
{
output();
printf("enter the rotating angle:");
scanf("%d",&y);
printf("enter the pivot point:");
scanf("%d%d",&fx,&fy);
k=(y*3.14)/180;
for(i=0;i<=n;i++)
{
a[i][0]=fx+(a[i][0]-fx)*cos(k)-(a[i][1]-fy)*sin(k);
a[i][1]=fy+(a[i][0]-fx)*sin(k)-(a[i][1]-fy)*cos(k);
}
output();
delay(10);
menu();
}
void scaling()
{
output();
printf("enter the scaling factor\n");
scanf("%f%f",&sx,&sy);
printf("enter the fixed point:");
scanf("%d%d",&fx,&fy);
for(i=0;i<=n;i++)
{
a[i][0]=a[i][0]*sx+fy*(1-sx);
a[i][1]=a[i][1]*sy+fy*(1-sy);
}
output();
delay(10);
menu();
}
void shearing()
{
output();
printf("enter the shear value:");
scanf("%d",&sh);
printf("enter the fixed point:");
scanf("%d%d",&fx,&fy);
printf("enter the axis for shearing if x-axis then 1 if y-axis then 0:");
scanf("%d",&axis);
for(i=0;i<=n;i++)
{
if(axis==1)
{
a[i][0]=a[i][0]+sh*(a[i][1]-fy);
}
else
{
a[i][1]=a[i][1]+sh*(a[i][0]-fx);
}
}
output();
delay(10);
menu();
}
void reflection()
{
output();
for(i=0;i<=n;i++)
{
temp=a[i][0];
a[i][0]=a[i][1];
a[i][1]=temp;
}
output();
delay(10);
menu();
}
void main()
{
intgd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
menu();
getch(); }
OUTPUT:
Menu
1. Translation
2. rotation
3. scaling
4. shearing
5. reflection
6. exit
ROTATION
Enter the choice : 2
Enter the number of Vertices: 3
Enter the coordinates : 30 150 10 200
Enter the coordinates : 10 200 60 200
Enter the coordinates : 60 200 30 150
SCALING
Enter the choice : 3
Enter the number of Vertices: 3
Enter the coordinates : 30 150 10 200
Enter the coordinates : 10 200 60 200
Enter the coordinates : 60 200 30 150
Enter the scaling Factor : 0.3 0.4
Enter the Fixed Point : 100 200
SHEARING
Enter the choice : 4
Enter the number of Vertices: 3
Enter the coordinates : 30 150 10 200
Enter the coordinates : 10 200 60 200
Enter the coordinates : 60 200 30 150
REFLECTION
Enter the choice : 5
Enter the number of Vertices: 3
Enter the coordinates : 30 150 10 200
Enter the coordinates : 10 200 60 200
Enter the coordinates : 60 200 30 150
RESULT:
Thus the above program has been executed and output is verified.
WINDOWING TO VIEWPORT MAPPING
EX NO: 2b
Aim :
To write a C program to clip a Window to Viewport Mapping.
Algorithm:
Step 1: Initialize graphics variables and graphics mode using initgraph() function.
Step 4: Calculate the co-ordinates of a point(sx,sy) by fitting window in viewport by the formula
sx=(v3-v1)/(w3-w1)
sy=(v4-v2)/(w4-w2)
Step 5: Calculate the transformed coordinates of the original point(x1,y1) for triangle by
x1=v1+(((x1-w1)*sx)+0.5)
y1=v2+(((y1-w2)*sy)+0.5)
Step 6: Repeat step 5 for the other points namely (x2,y2) and (x3,y3)
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
main()
{
floatsx,sy;
int w1,w2,w3,w4,x1,x2,x3,x4,y1,y2,y3, y4,v1,v2,v3,v4;
intgd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("Enter The Coordinate x1,y1,x2,y2,x3,y3\n");
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
cleardevice();
w1=5;
w2=5;
w3=635;
w4=465;
rectangle(w1,w2,w3,w4);
line(x1,y1,x2, y2);
line(x2,y2,x3, y3);
line(x3,y3,x1, y1);
getch();
v1=425;
v2=75;
v3=550;
v4=250;
sx=(float)(v3-v1)/(w3-w1);
sy=(float)(v4-v2)/(w4-w2);
rectangle(v1,v2,v3,v4);
x1=v1+floor(((float)(x1-w1)*sx)+.5);
x2=v1+floor(((float)(x2-w1)*sx)+.5);
x3=v1+floor(((float)(x3-w1)*sx)+.5);
y1=v2+floor(((float)(y1-w2)*sy)+.5);
y2=v2+floor(((float)(y2-w2)*sy)+.5);
y3=v2+floor(((float)(y3-w2)*sy)+.5);
line(x1,y1,x2, y2);
line(x2,y2,x3, y3);
line(x3,y3,x1, y1);
getch();
return 0;
}
OUTPUT
100
200
300
400
500
350
RESULT:
Thus the above program has been executed and output is verified.
COMPOSITE TWO – DIMENSIONAL TRANSFORMATION
EX NO: 3
Aim :
Algorithm:
Step 1: Initialize graphics variables and graphics mode using initgraph() function.
Step 3: Translation
a) Enter the translation factors tx and ty.
b) Move the original coordinate position (x,y) to a new position(x1,y1) using
x1=tx+x, y1=ty+y.
Step 4: Rotation
a) Enter the radian for rotation angle Ө and reference point(xf,yf) .
a) Rotate a point at position (x,y) through an angleӨ about the given reference point
to the new position(x1,y1) as
x1=xf+(x-xf)*cosӨ – (y-yf)*sinӨ,
y1=yf+(x-xf)*sinӨ -(y-yf)*cosӨ .
Step 5: Scaling
a) Input the scaled factors sx and syand reference point(rx,ry).
b) The transformed coordinates (x1,y1) about the given reference point will be calculated
as
x1=x.sx+rx*(1-sx)
y1=y.sy+ry*(1-sy).
Step 5: Reflection
Reflection can be performed about x axis and y axis.
a) Reflection about x axis : The transformed coordinates are x1=x and y1=-y.
Step 6: Reflection about y axis : The transformed coordinates are x1=-x and y1=y.
Step 7: Shearing
a) Input the shearing factors shx and shy.
b) Shearing related to x axis :The transformed coordinates are
x1=x+shx*y
y1=y.
c) Shearing related to y axis : The transformed coordinates are
x1=x
y1=y+shy*x.
d) Input the xref and yref values.
e) X axis shear related to the reference line y=yref is
x1=x+shx(y-yref)
y1=y.
f) Y axis shear related to the reference line x=xref is
x1=x
y1=y+shy(x-xref)
Step 8: Finally display the transformed object after the successive transformations.
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<stdlib.h>
void main()
{
int gd,gm,n,i,xa[10],ya[10],op,tx,ty,xa1[10],ya1[10],theta,xf,yf,rx,ry,
sx,sy,shx,shy,xref,yref;
char d;
gd=DETECT;
initgraph(&gd,&gm,"");
cout<<"enter the no of points";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"enter the coordinates"<<i+1;
cin>>xa[i]>>ya[i];
}
do
{
cout<<"menu";
cout<<"\n1.translation\n2.rotation\n3.scaling\n4.shearing\n5.reflection\n6.exit";
cin>>op;
switch(op)
{
case 1:
cout<<"enter the translation vector";
cin>>tx>>ty;
for(i=0;i<n;i++)
{
xa1[i]=xa[i]+tx;
ya1[i]=ya[i]+ty;
}
cleardevice();
cout<<"before translation";
for(i=0;i<n;i++)
{
line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);
}
cout<<"after translation";
for(i=0;i<n;i++)
{
line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]);
}
getch();
cleardevice();
break;
case 2:
cout<<"enter the rotation angle";
cin>>theta;
theta=(theta*3.14)/180;
cout<<"enter the reference points";
cin>>xf>>yf;
for(i=0;i<n;i++)
{
xa1[i]=xf+(xa[i]-xf)*cos(theta)-(ya[i]-yf)*sin(theta);
ya1[i]=yf+(xa[i]-xf)*sin(theta)-(ya[i]-yf)*cos(theta);
}
cleardevice();
cout<<"before rotation";
for(i=0;i<n;i++)
{
line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);
}
cout<<"after rotation";
for(i=0;i<n;i++)
{
line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]);
}
getch();
cleardevice();
break;
case 3:
cout<<"enter the scaling factor";
cin>>sx>>sy;
cout<<"enter the reference point";
cin>>rx>>ry;
for(i=0;i<n;i++)
{
xa1[i]=xa[i]*sx+rx*(1-sx);
ya1[i]=ya[i]*sy+ry*(1-sy);
}
cleardevice();
cout<<"before scaling";
for(i=0;i<n;i++)
{
line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);
}
cout<<"after scaling";
for(i=0;i<n;i++)
{
line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]);
}
getch();
cleardevice();
break;
case 4:
cout<<"enter the shear value";
cin>>shx>>shy;
cout<<"enter the reference point";
cin>>xref>>yref;
cout<<"enter the shear direction x or y";
cin>>d;
if(d=='x')
{
for(i=0;i<n;i++)
{
xa1[i]=xa[i]+shx*(ya[i]-yref);
ya1[i]=ya[i];
}
}
cleardevice();
cout<<"before shearing";
for(i=0;i<n;i++)
{
line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);
}
cout<<"after shearing";
for(i=0;i<n;i++)
{
line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]);
}
getch();
cleardevice();
break;
case 5:
cout<<"before reflection";
for(i=0;i<n;i++)
{
line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);
}
cout<<"after reflection";
for(i=0;i<n;i++)
{
line(ya[i],xa[i],ya[(i+1)%n],xa[(i+1)%n]);
}
getch();
cleardevice();
break;
case 6:
exit(0);
break;
}
}while(op!=6);
}
OUTPUT :
menu
1. translation
2. rotation
3. scaling
4.shearing
5.reflection
6.exit
1
enter the translation vector: 30 40
menu
1. translation
2. rotation
3. scaling
4.shearing
5.reflection
6.exit
2
enter the rotation angle: 60
AFTER
menu
1. translation
2. rotation
3. scaling
4.shearing
5.reflection
6.exit
menu
1. translation
2. rotation
3. scaling
4.shearing
5.reflection
6.exit
4
Enter the shear value: 3 4
Enter the reference point: 20 30
Enter the shear direction x or y: X
BEFORE
AFTER
menu
1. translation
2. rotation
3. scaling
4.shearing
5.reflection
6.exit
BEFORE
AFTER
RESULT:
Thus the above program has been executed and output is verified.
COHEN SUTHERLAND 2D LINE CLIPPING
EX NO: 4
Aim :
Algorithm:
Step 1: Initialize graphics variables and graphics mode using initgraph() function.
Step 2: Enter the line end points(x1,y1,x2,y2) and the window coordinates(cxl,cyb,cxr,cyt) for
Left, Bottom, Right, Top of window..
Step 3: Every line end point is assigned a code cthat identified the location of the point
relative to the boundaries of the clipping rectangle.Consider one of the Line point
is (x1,y1),do the following
a) If (x1<cxl) then c1=1
b) If (x1>cxr) then c1=2
c) If (y1<cyb) then c1=c|8
d) If (y1>cyt) then c1=c|4;
Step 4: Repeat step 3 for other end point(x2,y2) and will generate another code c2.
Step 5: Check whether the line lies inside the window by checking the condition (c1&c2)!=0
then it is entirely drawn.
Step 5: Check whether the line lies outside the window using condition (c1==c2) then it is
entirely clipped.
#include<stdio.h>
#include<math.h>
#include<graphics.h>
#include<conio.h>
float cxl, cxr,cyt,cyb;
code(float,float);
void clip(float,float,float,float);
voidrect(float,float,float,float);
void main()
{
float x1,y1,x2,y2;
int g=0,d;
initgraph(&g,&d,"c:\\tc\\bin");
settextstyle(1,0,1);
outtextxy(40,15,"BEFORE CLIPPING");
printf("\n\n\n please enter the left,bottom,right,top,of clip window");
scanf("%f%f%f%f",&cxl,&cyt,&cxr,&cyt);
rect(cxl,cyb,cxr,cyt);
getch();
printf("\n please enter the line(x1,y1,x2,y2):");
scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
line(x1,y1,x2,y2);
getch();
cleardevice();
settextstyle(1,0,1);
outtextxy(40,15,"after clipping");
clip(x1,y1,x2,y2);
getch();
closegraph();
}
void clip(float x1,float y1,float x2,float y2)
{
int c,c1,c2;
floatx,y;
c1=code(x1,y1);
c2=code(x2,y2);
getch();
while((c1!=0)||(c2!=0))
{
if((c1&c2)!=0)
goto out;
c=c1;
if(c==0)
c=c2;
if((c&1)==1)
{
y=y1+(y2-y1)*(cxl-x1)/(x2-x1);
x=cxl;
}
else
if((c&2)==2)
{
y=y1+(y2-y1)*(cxl-x1)/(x2-x1);
x=cxr;
}
else
if((c&8)==8)
{
x=x1+(x2-x1)*(cyb- y1)/(y2-y1);
y=cyb;
}
else
if((c&4)==4)
{
x=x1+(x2-x1)*(cyt-y1)/(y2-y1);
y=cyt;
}
if(c==c1)
{
x1=x;
y1=y;
c1=code(x,y);
else
{
x2=x;
y2=y;
c2=code(x,y);
}
}
out :rect(cxl,cyb,cxr,cyt);
line(x1,y1,x2,y2);
}
code(float x,float y)
{
int c=0;
if(x<cxl)
c=1;
else
if(x>cxr)
c=2;
if(y<cyb)
c=c|8;
else
if(y>cyt)
c=c|4;
return c;
}
voidrect(float xl,floatyb,floatxr,floatyt)
{
line(xl,yb,xr,yb);
line(xr,yb,xr,yt);
line(xr,yt,xl,yt);
line(xl,yt,xl,yb);
}
OUTPUT
250
300
400
450
Before Clipping:
After Clipping
RESULT:
Thus the above program has been executed and output is verified.
THREE – DIMENSIONAL TRANSFORMATION
EX NO: 5
Aim :
Algorithm:
Step 1: Initialize graphics variables and graphics mode using initgraph() function.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
intmaxx,maxy,midx,midy;
void axis()
{
getch();
cleardevice();
line(midx,0,midx,maxy);
line(0,mid y,maxx,midy);
}
void main()
{
int gd,gm,x,y,z,o,x1,x2,y1,y2;
detectgraph(&gd,&gm);
initgraph(&gd,&gm," ");
setfillstyle(0,getmaxcolor());
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
axis();
bar3d(midx+50,midy-100,midx+60,midy-90,5,1);
printf("Enter Translation Factor");
scanf("%d%d%d",&x,&y,&z);
axis();
printf("after translation");
bar3d(midx+(x+50),midy-(y+100),midx+x+60,midy-(y+90),5,1);
axis();
bar3d(midx+50,midy+100,midx+60,midy-90,5,1);
printf("Enter Scaling Factor");
scanf("%d%d%d",&x,&y,&z);
axis();
printf("After Scaling");
bar3d(midx+(x*50),midy-(y*100),midx+(x*60),midy-( y*90),5*z,1);
axis();
bar3d(midx+50,midy-100,midx+60,midy-90,5,1);
printf("Enter Rotating Angle");
scanf("%d",&o);
x1=50*cos(o*3.14/180)-100*sin(o*3.14/180);
y1=50*cos(o*3.14/180)+100*sin(o*3.14/180);
x2=60*sin(o*3.14/180)-90*cos(o*3.14/180);
y2=60*sin(o*3.14/180)+90*cos(o*3.14/180);
axis();
printf("After Rotation about Z Axis");
bar3d(midx+x1,midy-y1,midx+x2,midy- y2,5,1);
axis();
printf("After Rotation about X Axis");
bar3d(midx+50,midy-x1,midx+60,midy-x2,5,1);
axis();
printf("After Rotation about Y Axis");
bar3d(midx+x1,midy-100,midx+x2,midy-90,5,1);
getch();
closegraph();
}
OUTPUT:
Translation
Enter Translation Factor : 50 60 70
Before Translation
After Translation
Scaling
Rotation
Enter Rotating Angle : 60
RESULT:
Thus the above program has been executed and output is verified.
3D PROJECTIONS
EX NO: 6
Aim :
Algorithm:
Step 1: Initialize graphics variables and graphics mode using initgraph() function.
Step 2: Input the object coordinatesand depth values for drawing3D objects.
Step 3: Draw top view, side view and front view of parallel projections.
Program:
#include <stdio.h>
#include <stdlib.h>
#include<graphics.h>
#include<conio.h>
void draw3d(ints,int x[20],int y[20],int d);
void main()
{
intgd=DETECT,gm;
int x[20],y[20],i,s,d;
initgraph(&gd,&gm,"");
printf("Enter the No of sides : ");
scanf("%d",&s);
for(i=0;i<s;i++)
{
printf("(x%d,y%d) :",i,i);
scanf("%d%d",&x[i],&y[i]);
}
printf("Depth :");
scanf("%d",&d);
draw3d(s,x,y,d);
getch();
setcolor(14);
for(i=0;i<s-1;i++)
{
line(x[i]+200,y[i],x[i+1]+200,y[i+1]);
}
line(x[i]+200,y[i],x[0]+200,y[0]);
getch();//top view
for(i=0;i<s-1;i++)
{
line(x[i],300,x[i+1],300);
line(x[i],300+d*2,x[i+1],300+d*2);
line(x[i],300,x[i],300+d*2);
line(x[i+1],300,x[i+1],300+d*2);
}
getch();//side view
for(i=0;i<s-1;i++)
{
line(10,y[i],10,y[i+1]);
line(10+d*2,y[i],10+d*2,y[i+1]);
line(10,y[i],10+d*2,y[i]);
line(10,y[i+1],10+d*2,y[i+1]);
}
getch();
closegraph();
}
void draw3d(ints,int x[20],int y[20],int d)
{
inti,j,k=0;
for(j=0;j<2;j++)
{
for(i=0;i<s-1;i++)
line(x[i]+k,y[i]-k,x[i+1]+k,y[i+1]-k);
line(x[i]+k,y[i]-k,x[0]+k,y[0]-k);
k=d;
}
for(i=0;i<s;i++)
line(x[i],y[i],x[i]+d,y[i]-d);
}
OUTPUT
RESULT:
Thus the above program has been executed and output is verified.
DRAWING 3D OBJECTS AND SCENES
EX NO: 7
Aim :
Algorithm:
Step 2: Initialize graphics mode and window of specified size & position.
Step 3: Specify the material properties and light position & intensities.
Step 4: Set matrix mode to projection initially and get the orthogonal values for model view.
Step 5: Generate solid teapot, solid sphere and wall appearance using OPENGL commands.
Step 6: Finally apply different types of shading model for getting reality in appearance.
Program:
#include<iostream.h>
#include<windows.h>
#include<gl/Gl.h>
#include<gl/Glu.h>
#include<gl/glut.h>
void wall(double th)
{
glPushMatrix();
glTranslated(0.5,0.5*th,0.5);
glScaled(1.0,th,1.0);
glutSolidCube(1.0);
glPopMatrix();
}
voiddisplaySolid(void)
{
GLfloatmat_amb[]={0.7f,0.7f,0.7f,1.0f};
GLfloatmat_dif[]={0.6f,0.6f,0.6f,1.0f};
GLfloatmat_spec[]={1.0f,1.0f,1.0f,1.0f};
GLfloatmat_shine[]={50.0f};
glMaterialfv(GL_FRONT,GL_AMBIENT,mat_amb);
glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_dif);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_spec);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shine);
GLfloatlightIntensity[]={0.7f,0.7f,0.7f,1.0f};
GLfloatlight_position[]={0.2f,6.0f,3.0f,0.0f};
glLightfv(GL_LIGHT0,GL_POSITION,light_position);
glLightfv(GL_LIGHT0,GL_DIFFUSE,lightIntensity);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
doublewinHt=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.25,0,0.0,1.0,0.0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslated(0.6,0.39,0.7);
glRotated(30,0,1,0);
glutSolidTeapot(0.08);
glPopMatrix();
glPushMatrix();
glTranslated(0.9,0.40,0.70);
glutSolidSphere(0.1,15,15);
glPopMatrix();
wall(0.02);
glPushMatrix();
glRotated(90.0,0.0,0.0,1.0);
wall(0.02);
glPopMatrix();
glPushMatrix();
glRotated(-90.0,1.0,0.0,0.0);
wall(0.02);
glPopMatrix();
glFlush();
}
void main(intargc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,100);
glutCreateWindow("Shaded example-3D scene");
glutDisplayFunc(displaySolid);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glClearColor(0.1f,0.1f,0.1f,0.0f);
glViewport(0,0,640,480);
glutMainLoop();
}
OUTPUT:
RESULT:
Thus the above program has been executed and output is verified.
2D ANIMATION
EX NO: 8
Aim :
Algorithm:
Step 1: Initialize graphics variables and graphics mode using initgraph() function.
Step 3: After drawing object, rotate that object by getting angle of rotation value and
coordinates for center of rotation.
Program:
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
void rotate( int figure[], int edges, double angle, int cx, int cy )
{
double x, y;
angle = -1 * (angle*3.14/180);
doublecos_a = cos(angle);
doublesin_a = sin(angle);
void main()
{
int figure[20], edges; // A Figure with Max 10 edges.
double angle;
int cx=0, cy=0;
intgd = DETECT, gm;
initgraph(&gd, &gm, "" );
intmax_y = getmaxy();
clrscr();
cleardevice();
printf( "Number of edges: " );
scanf( "%d", &edges );
Thus the above program has been executed and output is verified.
GENERATING FRACTAL IMAGES
EX NO: 9
Aim :
Algorithm:
Step 2: Initialize graphics mode and window of specified size & position.
Step 3: Specify the material properties and light position & intensities.
Step 4: Set matrix mode to projection initially and get the orthogonal values for model view.
Step 5: Generate the fractal image by drawing the dots at randomly generated point positions
namely X and Y.
Step 6: Repeat step 5 for specified number of times to get desired image.
Program:
#include<iostream.h>
#include<windows.h>
#include<gl/Gl.h>
#include<gl/Glu.h>
#include<gl/glut.h>
struct GLintPoint
{
GLint x,y;
};
int random(int m)
{
return rand() %m;
}
void drawDot(GLint x,GLint y)
{
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}
void myInit()
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(2.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,800.0,0.0,600.0);
}
void sierpinski_render()
{
glClear(GL_COLOR_BUFFER_BIT);
GLintPoint T[3]={{10,10},{600,10},{300,600}};
int index=random(3);
GLintPoint point=T[index];
drawDot(point.x,point.y);
for(int i=0;i<55000;i++)
{
index=random(3);
point.x=(point.x + T[index].x)/2;
point.y=(point.y + T[index].y)/2;
drawDot(point.x,point.y);
}
glFlush();
}
void main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(800,600);
glutInitWindowPosition(100,150);
glutCreateWindow("sierpinski window");
glutDisplayFunc(sierpinski_render);
myInit();
glutMainLoop();
}
OUTPUT:
RESULT:
Thus the above program has been executed and output is verified.
OPEN GL PROGRAMS USING VC++
PROCEDURE:
Before executing the programs you have to place three header files (dll, Header, Lib) in the
following locations
Header:
C:\Program Files\Microsoft Visual Studio\VC98\Include\GL
Lib:
C:\Program Files\Microsoft Visual Studio\VC98\Lib
Dll:
C:\WINDOWS\system32
Go to Start Programs Microsoft Visual Studio 6.0 Microsoft Visual C++ 6.0
File New...
New dialog boxes opens in that select the win32console application from the projects tab
and give the name for the project and click on the ok and finish button.
Now select project menu and select the settings from the drop down menu.
In that select the directories tab and select the browse button as given in the below screenshot.
Select the path for the three header files that you have pasted in different locations and
click on the ok button.
FileNew...
Select the C++ source file from the files tab and give the name for the file and click ok
button .
Now select project menu and select the settings from the drop down menu
In the project settings dialog box click on the link tab and type the three file names at the
end of the object/library modules text box.
File names: opengl32.lib glu32.lib glut32.lib
After entering the file names click on the C/C++ tab and select C++ Language from the
category drop down list and click on the ok button
After performing these steps type the required program code save the program and click
on the build button.