Beruflich Dokumente
Kultur Dokumente
in
234 124
else
x=xa;
y=ya;
xend=xb;
putpixel(x,y,6);
while(x<xend)
{
x=x+1;
if(p<0)
{
p=p+2*dy;
}
else
{
y=y+1;
p=p+2*(dy-dx);
}
putpixel(x,y,6);
}
getch();
return(0);
engineerportal.blogspot.in
#include "stdio.h"
#include "conio.h"
#include "math.h"
#include "graphics.h"
main()
{
int gd=DETECT,gm;
int xcenter,ycenter,radius;
int p,x,y;
initgraph(&gd,&gm,"c:\\tc\\bgi");
x=0;
printf("Enter The Radius Value:\n");
scanf("%d",&radius);
y=radius;
printf("Enter The xcenter and
ycenter Values:\n");
scanf("%d%d",&xcenter,&ycenter);
plotpoints(xcenter,ycenter,x,y);
p=1-radius;
while(x<y)
{
if(p<0)
x=x+1;
OUTPUT :
Enter The Radius Value
: 80
Enter The xcenter and ycenter Values : 230 260
else
x=x+1;
y=y-1;
}
if(p<0)
p=p+2*x+1;
else
p=p+2*(x-y)+1;
plotpoints(xcenter,ycenter,x,y);
}
getch();
return(0);
}
int plotpoints(int xcenter,int ycenter,int
x,int y)
{
putpixel(xcenter+x,ycenter+y,1);
putpixel(xcenter-x,ycenter+y,1);
putpixel(xcenter+x,ycenter-y,1);
putpixel(xcenter-x,ycenter-y,1);
putpixel(xcenter+y,ycenter+x,1);
putpixel(xcenter-y,ycenter+x,1);
putpixel(xcenter+y,ycenter-x,1);
}
engineerportal.blogspot.in
engineerportal.blogspot.in
2D transformations
a.Translation, Reflection, and Shear
b. Rotation (With and without pivot
point), Scaling (With and without pivot
point)
c. Translation, Scaling, Rotation
d. fixed point scaling, fixed point
rotation
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void translation(x,y,x1,y1,x2,y2)
{
int tx,ty,xx,yy;
printf("Enter transformational values:");
scanf("%d%d",&tx,&ty);
line(x+tx,y+ty,x1+tx,y1+ty);
line(x1+tx,y1+ty,x2+tx,y2+ty);
line(x2+tx,y2+ty,x+tx,y+ty);
}
void scaling(x,y,x1,y1,x2,y2)
{
float sx,sy;
printf("Enter the scaling vertices:");
scanf("%f%f",&sx,&sy);
line(x*sx,y*sy,x1*sx,y1*sy);
line(x1*sx,y1*sy,x2*sx,y2*sy);
line(x2*sx,y2*sy,x*sx,y*sy);
}
void scalingpivot(x,y,x1,y1,x2,y2)
{
float sx,sy,xf,yf;
int xp,yp,xp1,yp1,xp2,yp2;
printf("Enter the scaling vertices:");
scanf("%f%f",&sx,&sy);
printf("Enter the scaling pivot points:");
scanf("%f%f",&xf,&yf);
xp=(x*sx)+xf*(1-sx);
yp=(y*sy)+yf*(1-sy);
xp1=(x1*sx)+xf*(1-sx);
yp1=(y1*sy)+yf*(1-sy);
xp2=(x2*sx)+xf*(1-sx);
yp2=(y2*sy)+yf*(1-sy);
line(x+xp,y+yp,x1+xp1,y1+yp1);
line(x1+xp1,y1+yp1,x2+xp2,y2+yp2);
line(x2+xp2,y2+yp2,x+xp,y+yp);
}
void rotation(x,y,x1,y1,x2,y2)
{
int the,xr,yr,xr1,yr1,xr2,yr2;
float rad;
printf("Enter the theta value:");
scanf("%d",&the);
rad=the*(3.14/180);
xr=x*cos(rad)-y*sin(rad);
yr=x*sin(rad)+y*cos(rad);
xr1=x1*cos(rad)-y1*sin(rad);
yr1=x1*sin(rad)+y1*cos(rad);
xr2=x2*cos(rad)-y2*sin(rad);
yr2=x2*sin(rad)+y2*cos(rad);
line(x+xr,y+yr,x1+xr1,y1+yr1);
line(x1+xr1,y1+yr1,x2+xr2,y2+yr2);
line(x2+xr2,y2+yr2,x+xr,y+yr);
}
void rotationpivot(x,y,x1,y1,x2,y2)
{
int the,xr,yr,xr1,yr1,xr2,yr2,xf,yf;
float rad;
printf("Enter the theta value:");
scanf("%d",&the);
printf("\n Enter the pivot points:");
scanf("%d%d",&xf,&yf);
rad=the*(3.14/180);
xr=xf+(x-xf)*cos(rad)-(y-yf)*sin(rad);
yr=yf+(x-xf)*sin(rad)-(y-yf)*cos(rad);
xr1=xf+(x1-xf)*cos(rad)-(y1yf)*sin(rad);
yr1=yf+(x1-xf)*sin(rad)-(y1yf)*cos(rad);
xr2=xf+(x2-xf)*cos(rad)-(y2yf)*sin(rad);
yr2=yf+(x2-xf)*sin(rad)-(y2yf)*cos(rad);
line(x+xr,y+yr,x1+xr1,y1+yr1);
engineerportal.blogspot.in
line(x1+xr1,y1+yr1,x2+xr2,y2+yr2);
line(x2+xr2,y2+yr2,x+xr,y+yr);
}
void reflection(x,y,x1,y1,x2,y2)
{
line(y,x,y1,x1);
line(y1,x1,y2,x2);
line(y2,x2,y,x);
case 1:
}
void shear(x,y,x1,y1,x2,y2)
{
int s;
//x+=100; y+=100;
//x1+=100; y1+=100;
//x2+=100; y2+=100;
printf("Enter the shear value about x:");
scanf("%d",&s);
line(x,y,x1+s,y1);
line(x1+s,y1,x2,y2);
line(x2,y2,x,y);
translation(x,y,x1,y1,x2,y2);
break;
case 2:
scaling(x,y,x1,y1,x2,y2);
break;
case 3:
scalingpivot(x,y,x1,y1,x2,y2);
break;
case 4:
rotation(x,y,x1,y1,x2,y2);
break;
case 5:
}
void main()
{
int gd,gm,x,y,c,tx,ty,x1,y1,x2,y2;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("Enter the 6 values:");
scanf("%d%d%d%d%d%d",&x,&y,&x1,
&y1,&x2,&y2);
printf("Actual polygon");
line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);
do
{
printf("\nMenu\n 1.Translation\n
2.Scaling\n 3.Scaling with pivot\n");
Ex number : 5-a,6-b,7-c,8-d
rotationpivot(x,y,x1,y1,x2,y2);
break;
case 6:
reflection(x,y,x1,y1,x2,y2);
break;
case 7:
shear(x,y,x1,y1,x2,y2);
break;
case 8:
exit(0);
break;
}
}while(c<8);
getch();
closegraph();
}
2 d trans
engineerportal.blogspot.in
else
{
ctemp.code[3]='0';
printf("\nbit4= %c",ctemp.code[3]);
}
ctemp.x=c.x;
ctemp.y=c.y;
return(ctemp);
engineerportal.blogspot.in
{
y=ymin;
}
if((c1.code[3]=='1')||(c1.code[2]=='1'))
{
m=(float)(c2.y-c1.y)/(c2.x-c1.x);
printf("\n slope value = %f",m);
k=(float)c1.x+(float)(y-c1.y)/m;
printf("\n x value = %f",k);
temp.x=k;
temp.y=y;
for(i=0;i<4;i++)
temp.code[i]=c1.code[i];
return(temp);
}
if(c1.code[1]=='1')
{
x=xmax;
}
if(c1.code[0]=='1')
{
x=xmin;
}
if((c1.code[1]=='1')||(c1.code[0]=='1'))
{
m=(float)(c2.y-c1.y)/(c2.x-c1.x);
printf("\n slope value = %f",m);
k=c1.y+(m*(x-c1.x));
printf("\n y value = %f",k);
temp.y=k;
temp.x=x;
for(i=0;i<4;i++)
temp.code[i]=c1.code[i];
return(temp);
}
void main()
{
int gd,gm,v;
int
xmin=100,xmax=500,ymin=150,ymax=400;
CR c1,c2,c3,ctemp;
OUTPUT:
engineerportal.blogspot.in
break;
break;
break;
}
case right:
if(s.x<clipbound)
pos=1;
case top:
if(s.y>clipbound)
pos=1;
case bottom:
if(s.y<clipbound)
pos=1;
}
return(pos);
engineerportal.blogspot.in
{
}
temp=intersect(xmin,s,p);
outvertex[max++]=temp;
}
n=max;
for(i=0;i<max;i++)
vertex[i]=outvertex[i];
max=0;
}
void sutherclip(int xmin,int xmax,int ymin,int
ymax)
{
clip(xmin,left);
clip(xmax,right);
clip(ymin,top);
clip(ymax,bottom);
}
void main()
{
int xmin,xmax,ymin,ymax,i;
int gd=DETECT,gm;
sin(1);
clrscr();
printf("Enter the co ordinates of
clipping window....\n");
scanf("%d %d %d %d"
,&xmin,&ymin,&xmax,&ymax);
printf("\nEnter the no of vertices of
the polygon....\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
Output:
engineerportal.blogspot.in
Implementation of 3D Transformations
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void draw(float x1,float y1,float
x2,float y2,float z)
{
bar3d(x1,y1,x2,y2,z,1);
}
void z_rotate(float x1,float y1,float
x2,float y2,float z,float r)
{
float a,b,c,d;
a=(x1*cos(r))-(y1*sin(r));
b=(x2*cos(r))-(y2*sin(r));
c=(x1*sin(r))+(y1*cos(r));
d=(x2*sin(r))+(y2*cos(r));
draw(a,c,b,d,z);
}
void main()
{
int gd,gm,s,ch;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"E:\\TC\\BGI");
do
{
float
x1=200,x2=300,y1=200,y2=300,z=1
00,tx,ty,sx,sy,sz,o,r;
clrscr();
printf("\n\n\n\t\t 3D
Transformations");
printf("\n\n1.3D
Translation\n2.3D Scaling\n3.3D
Rotation \n4.Exit");
printf("\n\nEnter Ur
Option:");
scanf("%d",&ch);
clrscr();
switch(ch)
{
case 1:
printf("\n\nEnter Translation
Values(tx,ty):");
scanf("%f %f",&tx,&ty);
printf("\nBefore Translation");
setcolor(10);
draw(x1,y1,x2,y2,z);
sleep(3);
printf("\nAfter Translation");
setcolor(12);
draw(x1+tx,y1+ty,x2+tx,y2+ty,z);
break;
case 2:
printf("\n\nEnter the scaling
Values(sx,sy,sz):");
scanf("%f %f %f",&sx,&sy,&sz);
printf("\nBefore Scaling");
setcolor(4);
draw(x1,y1,x2,y2,z);
sleep(3);
printf("\nAfter Scaling");
x1*=sx;
y1*=sy;
x2*=sx;
y2*=sy;
z*=sz;
setcolor(6);
engineerportal.blogspot.in
draw(x1,y1,x2,y2,z);
break;
case 4:
exit(0);
case 3:
printf("\n\nEnter the rotation
angle:");
scanf("%f",&o);
printf("\nBefore Rotation");
draw(x1,y1,x2,y2,z);
sleep(3);
printf("\nAfter Rotation");
r=(o*3.14)/180;
z_rotate(x1,y1,x2,y2,z,r);
break;
default:
printf("\n\nEnter
a valid choice");
}
printf("\n\nPress 1 to
continue:");
scanf("%d",&s);
}
while(s==1);
getch();
closegraph();
}
Output:
translation
Scaling
rotation
engineerportal.blogspot.in
tmp[r][c]=a[r][0]*b[0][c]+a[r][1]*b
[1][c]+a[r][2]*b[2][c];
}
}
for(r=0;r<3;r++)
for(c=0;c<3;c++)
b[r][c]=tmp[r][c];
z=z*a[2][2];
}
void triangle(struct pt *p1)
{
line(p1[0].x,p1[0].y,p1[1].x,p1[1].y);
line(p1[1].x,p1[1].y,p1[2].x,p1[2].y);
line(p1[2].x,p1[2].y,p1[0].x,p1[0].y);
}
void translate(int tx,int ty)
{
float m[3][3];
matrixsetidentity(m);
m[0][2]=tx;
m[1][2]=ty;
premul(m,thematrix);
}
void scaling()
{
float m[3][3],sx,sy,sz;
matrixsetidentity(m);
printf("\nEnter the scaling with respect to
x-axis:");
scanf("%f%f%f",&sx,&sy,&sz);
m[0][0]=sx;
m[1][1]=sy;
m[2][2]=sz;
premul(m,thematrix);
}
void rotation()
{
float m[3][3];
engineerportal.blogspot.in
float rad;
int a;
printf("\nEnter rotation angle");
scanf("%d",&a);
rad=a*(3.14/180);
matrixsetidentity(m);
m[0][0]=cos(rad);
m[0][1]=-sin(rad);
m[1][0]=sin(rad);
m[1][1]=cos(rad);
premul(m,thematrix);
}
void transformpts(int npts,struct pt *p1)
{
int k;
float tmp;
for(k=0;k<npts;k++)
{
tmp=thematrix[0][0]*p1[k].x+thematrix[0]
[1]*p1[k].y+thematrix[0][2];
p1[k].y=thematrix[1][0]*p1[k].x+thematri
x[1][1]*p1[k].y+thematrix[1][2];
p1[k].x=tmp;
p1[1].x=250.0;p1[1].y=150.0;
z=20;
do
{
printf("\n1.Translation\n2.Scaling\n3.Rotat
ion\n4.exit\n");
printf("Enter ur option");
scanf("%d",&ch);
switch(ch)
{
case 1:
cleardevice();
d3d(p1,z);
printf("Enter translation first
coordinates\n");
scanf("%d %d",&tx,&ty);
translate(tx,ty);
transformpts(3,p1);
d3d(p1,z);
printf("Enter translation second
coordinates\n");
scanf("%d %d",&tx1,&ty1);
translate(tx1,ty1);
transformpts(3,p1);
d3d(p1,z);
break;
}
}
void d3d(struct pt* p1,int z)
{
case 2:
cleardevice();
d3d(p1,z);
scaling();
transformpts(3,p1);
d3d(p1,z);
scaling();
transformpts(3,p1);
d3d(p1,z);
break;
bar3d(p1[0].x,p1[0].y,p1[1].x,p1[1].y,z,1);
}
void main()
{
int gd,gm;
int tx,ty,tx1,ty1,z;
int xf,yf,ch;
struct pt p1[3];
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"e:\\tc\\bgi");
matrixsetidentity(thematrix);
p1[0].x=150.0;p1[0].y=100.0;
case 3:
cleardevice();
d3d(p1,z);
rotation();
transformpts(3,p1);
d3d(p1,z);
rotation();
engineerportal.blogspot.in
transformpts(3,p1);
d3d(p1,z);
break;
case 4:
break;
Output:
}
}while(ch<4);
getch();
closegraph();
}
engineerportal.blogspot.in
Projection 3d
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd,gm;
int tlx,tly,brx,bry,d;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"e:\\tc\\
bgi");
printf("Enter the input for 3D
object");
scanf("%d %d %d %d
%d",&tlx,&tly,&brx,&bry,&d);
bar3d(tlx,tly,brx,bry,d,1);
Output:
sleep(3);
//getch();
cleardevice();
printf("Front View");
bar3d(tlx,tly,brx,bry,0,0);
sleep(3);
//getch();
cleardevice();
printf("Top view");
bar3d(tlx,tly-d,brx,tly,0,0);
sleep(3);
//getch();
cleardevice();
printf("Side View");
bar3d(brx,tly,brx+d,bry,0,0);
getch();
closegraph();
}
engineerportal.blogspot.in
//chain of diamonds
#include <gl/Glut.h>
typedef struct diamond
{
GLint x,y;
}center;
void myInit(void)
{
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,640.0,0.0,480.0);
}
void myDisplay(void)
{
int size=50,n=5,i;
glClear(GL_COLOR_BUFFER_BIT);
center cen;
cen.x=26,cen.y=30;
for(i=0;i<5;i++)
{
glBegin(GL_LINE_LOOP);
glVertex2i(cen.x-size/2,cen.y);
glVertex2i(cen.x,cen.y+size/2);
glVertex2i(cen.x+size/2,cen.y);
glVertex2i(cen.x,cen.y-size/2);
glEnd();
cen.x=cen.x+size;
}
glFlush();
}
void main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(10,150);
glutCreateWindow("MY First");
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
}
//output
engineerportal.blogspot.in
//chessboard
#include<GL/glut.h>
void myInit(void)
{
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,480.0,0.0,480.0);
}
void board(int size)
{
int i=0,k=0;
for(i=0;i<8;++i)
{
for(k=0;k<8;++k)
{
if((i+k)%2==0)
glColor3f(1.0,1.0,1.0);
else
glColor3f(0.0,0.0,0.0);
glRecti(i*size,k*size,(i+1)*size,(k+1)*
size);
}
}
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINE);
board(60);
glEnd();
glFlush();
}
engineerportal.blogspot.in
glVertex2i(pointx,pointy);
glEnd();
//j++;
}
CODING:
#include<stdlib.h>
#include<GL/glut.h>
void myInit(void)
{
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,640.0,0.0,480.0);
}
int random(int m)
{
return rand()%m;
}
void myDisplay(void)
{
//int i;
//int j=300,k=200;
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINE_LOOP);
glVertex2i(0,0);
glVertex2i(0,400);
glVertex2i(500,400);
glVertex2i(500,0);
glEnd();
glFlush();
}
int main(int argc, char** argv)
{
//Initialise GLUT with command-line parameters.
glutInit(&argc, argv);
//Set Display Mode
glutInitDisplayMode(GLUT_SINGLE |
GLUT_RGB);
//Set the window size
glutInitWindowSize(640,480);
//Set the window position
glutInitWindowPosition(100,100);
//Create the window
glutCreateWindow("CONSTELLATION OF 500
RANDOM DOTS WITHIN A WINDOW");
//Call init (initialise GLUT
//Call "display" function
glutDisplayFunc(myDisplay);
int tx[3];//={200,500,200};
int ty[3];//={400,50,50};
int index=random(500);
int pointx;//=tx[index];
int pointy;//=ty[index];
for(int i=0,j=0;i<500;i++)
{
pointx=random(500);
pointy=random(400);
glBegin(GL_POINTS);
OUTPUT:
myInit();
glViewport(0,0,640,480);
//Enter the GLUT event loop
glutMainLoop();
//exit(0);
return 0;
engineerportal.blogspot.in
IT);
for(int i=0;i<=20;i++)
{
centre.x=random(400);
centre.y=random(400);
GLfloat lev1=random(10)/10.0;
OUTPUT:
GLfloat lev2=random(10)/10.0;
GLfloat lev3=random(10)/10.0;
glColor3f(lev1,lev2,lev3);
glBegin(GL_LINE_LOOP);
glVertex2i(centre.xsize/2,centre.y);
2);
y);
size/2);
}
glVertex2i(centre.x,centre.y+size/
glVertex2i(centre.x+size/2,centre.
glVertex2i(centre.x,centre.yglEnd();
glFlush();
}
void main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SIN
GLE|GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,150);
glutCreateWindow("DIAMOND")
;
glutDisplayFunc(myDisplay);
myinit();
glutMainLoop();
}
engineerportal.blogspot.in
Output :
engineerportal.blogspot.in
Village of houses
#include<stdlib.h>
#include<GL/glut.h>
typedef struct village
{
int x,y;
}house;
house peak;
void myInit()
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(3.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.0);
}
int random(int r)
{
return rand()%r;
}
void parameterizedHouse(GLint width,GLint
height)
{
peak.x=random(300);
peak.y=random(500);
glBegin(GL_LINE_STRIP);
glVertex2i(peak.x-width/2,peak.y-height);
glVertex2i(peak.x-width/2,peak.y-3*height/8);
glVertex2i(peak.x,peak.y);
glVertex2i(peak.x+width/2,peak.y-3*height/8);
glVertex2i(peak.x+width/2,peak.y-height);
glVertex2i(peak.x-width/2,peak.y-height);
glEnd();
Output:
glBegin(GL_LINE_STRIP);
glVertex2i(((peak.x-width/2)+(.3*width)),peak.yheight);
glVertex2i(((peak.x-width/2)+(.3*width)),((peak.y3*height/8)-(.35*height)));
glVertex2i(((peak.x-width/2)+(.7*width)),((peak.y3*height/8)-(.35*height)));
glVertex2i(((peak.x-width/2)+(.7*width)),peak.yheight);
glEnd();
glBegin(GL_LINES);
glVertex2i(peak.x-width/2,peak.y-3*height/8);
glVertex2i(peak.x+width/2,peak.y-3*height/8);
glEnd();
}
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
for(int i=0;i<=5;i++)
{
int r1=random(200);
int r2=random(400);
parameterizedHouse(r1,r2);
}
glEnd();
glFlush();
}
void main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RG
B);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,150);
glutCreateWindow("Village of houses");
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
}
engineerportal.blogspot.in
3d objects
#include <gl.h>
#include<glu.h>
#include<freeglut.h>
#include<windows.h>
#include<process.h>
void axis(double length)
{
glPushMatrix();
glBegin(GL_LINES);
glVertex3d(0,0,0);
glVertex3d(0,0,length);
glEnd();
glTranslated(0,0,length-0.2);
glutWireCone(0.04,0.2,12,9);
glPopMatrix();
}
void display(void)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2.0*64/48.0,2.0*64/48.0,2.0,2.0,0.1,100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(2.0,2.0,2.0,0.0,0.0,0.0,0.0,1.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3d(0,0,0);
axis(0.5);
glPushMatrix();
glRotated(90,0,1.0,0);
axis(0.5);
glRotated(-90.0,1,0,0);
axis(0.5);
glPopMatrix();
//BIG CUBE
glPushMatrix();
glTranslated(0.5,0.5,0.5);
glutWireCube(1.0);
glPopMatrix();
glPushMatrix();
glTranslated(1,1,0);
glutWireSphere(0.25,10,8);
glPopMatrix();
glPushMatrix();
glTranslated(1,0,1);
glutWireCone(0.2,0.5,10,8);
glPopMatrix();
glPushMatrix();
glTranslated(1,1,1);
glutWireTeapot(0.1);
glPopMatrix();
glPushMatrix();
glTranslated(0,1,0);
glRotated(90,1,0,0);
glutWireTorus(0.1,0.3,10,10);
glPopMatrix();
glPushMatrix();
glTranslated(1,0,0);
glScaled(0.15,0.15,0.15);
glutWireDodecahedron();
glPopMatrix();
glPushMatrix();
glTranslated(0,1,1);
glutWireCube(0.25);
glPopMatrix();
glPushMatrix();
glTranslated(0,0,1);
GLUquadricObj *q;
q=gluNewQuadric();
gluQuadricDrawStyle(q,GLU_LINE);
gluCylinder(q,0.2,0.2,0.4,8,8);
glPopMatrix();
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE |
GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,100);
glutCreateWindow("OpenGL Windows
Application with GLUT");
glutDisplayFunc(display);
glClearColor(1.0,1.0,1.0,0.0);
glViewport(0,0,640,480);
glutMainLoop();
return 0;
}
engineerportal.blogspot.in
3d scenes
#include <GL/glut.h>
void wall(double thickness)
{
glPushMatrix();
glTranslated(0.5,0.5*thickness,0.5);
glScaled(1.0,thickness,1.0);
glutSolidCube(1.0);
glPopMatrix();
}
void tableleg(double thick,double len)
{
glPushMatrix();
glTranslated(0,len/2,0);
glScaled(thick,len,thick);
glutSolidCube(1.0);
glPopMatrix();
}
void jackpart()
{
glPushMatrix();
glScaled(0.2,0.2,1.0);
glutSolidSphere(1,15,15);
glPopMatrix();
glPushMatrix();
glTranslated(0,0,1.2);
glutSolidSphere(0.2,15,15);
glTranslated(0,0,-2.4);
glutSolidSphere(0.2,15,15);
glPopMatrix();
}
void jack()
{
glPushMatrix();
jackpart();
glRotated(90.0,0,1,0);
jackpart();
glRotated(90.0,1,0,0);
jackpart();
glPopMatrix();
}
void table(double topwid,double
topthick,double legthick,double leglen)
{
glPushMatrix();
glTranslated(0,leglen,0);
glScaled(topwid,topthick,topwid);
glutSolidCube(1.0);
glPopMatrix();
double dist=0.95*topwid/2.0-legthick/2.0;
glPushMatrix();
glTranslated(dist,0,dist);
tableleg(legthick,leglen);
glTranslated(0,0,-2*dist);
tableleg(legthick,leglen);
glTranslated(-2*dist,0,2*dist);
tableleg(legthick,leglen);
glTranslated(0,0,-2*dist);
tableleg(legthick,leglen);
glPopMatrix();
}
void display(void)
{
GLfloat
mat_ambient[]={0.7f,0.7f,0.7f,1.0f};
GLfloat mat_diffuse[]={0.6f,0.6f,0.6f,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,ma
t_specular);
glMaterialfv(GL_FRONT,GL_SHININESS,ma
t_shininess);
GLfloat
lightIntensity[]={0.7f,0.7f,0.7f,1.0f};
GLfloat
light_position[]={2.0f,6.0f,3.0f,0.0f};
glLightfv(GL_LIGHT0,GL_POSITION,light_p
osition);
glLightfv(GL_LIGHT0,GL_DIFFUSE,lightInte
nsity);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double winHt=1.0;
engineerportal.blogspot.in
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.4,0.4,0.6);
glRotated(45,0,0,1);
glScaled(0.08,0.08,0.08);
jack();
glPopMatrix();
glPushMatrix();
glTranslated(0.6,0.38,0.5);
glRotated(30,0,1,0);
glutSolidTeapot(0.08);
glPopMatrix();
glPushMatrix();
glTranslated(0.25,0.42,0.35);
glutSolidSphere(0.1,15,15);
glPopMatrix();
glPushMatrix();
glTranslated(0.4,0,0.4);
table(0.6,0.02,0.02,0.3);
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();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE |
GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,100);
glutCreateWindow("OpenGL Windows
Application with GLUT");
glutDisplayFunc(display);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glClearColor(1.0,1.0,1.0,0.0);
glViewport(0,0,640,480);
glutMainLoop();
return 0;
engineerportal.blogspot.in
Colour models
#include <GL/glut.h>
void display(void)
{
float r,g,b,c,m,y;
glClear(GL_COLOR_BUFFER_BIT);
r=1;
g=0;
b=0;
c=1-r;
m=1-g;
y=1-b;
glColor3f(r,g,b);
int i;
{
for(i=0;i<20;i++)
glRecti(100+i,200+i,150+i,250
+i);
}
glColor3f(c,m,y);
for(i=0;i<20;i++)
{
glRecti(200+i,300+i,250+i,350
+i);
}
glFlush();
Output :
}
void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0,
440.0);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE
| GLUT_RGB);
glutInitWindowSize(640,440);
glutInitWindowPosition(100,100);
glutCreateWindow("A Simple
OpenGL Windows Application with
GLUT");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
engineerportal.blogspot.in
Window to viewport
#include <GL/glut.h>
void display(void)
{
GLint i,j;
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0,0,640,440);
glBegin(GL_TRIANGLES);
glVertex2i(300,300);
glVertex2i(250,350);
glVertex2i(350,350);
glEnd();
glColor3f(1.0,0.0,0.0);
for(i=0;i<3;i++)
for(j=0;j<5;j++)
{
glViewport(i*64,j*44,64,44);
glBegin(GL_TRIANGLES);
glVertex2i(300,300);
glVertex2i(250,350);
glVertex2i(350,350);
glEnd();
}
glFlush();
}
}
int main(int argc, char** argv)
{
//Initialise GLUT with command-line
parameters.
glutInit(&argc, argv);
//Set Display Mode
glutInitDisplayMode(GLUT_SINGLE |
GLUT_RGB);
//Set the window size
glutInitWindowSize(640,440);
//Set the window position
glutInitWindowPosition(100,100);
//Create the window
glutCreateWindow("A Simple OpenGL
Windows Application with GLUT");
//Call init (initialise GLUT
init();
void init()
{
//select clearing (background) color
glClearColor(0.0, 0.0, 0.0, 0.0);
//initialize viewing values
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
engineerportal.blogspot.in
//JULIA sets
#include <GL/glut.h>
int dwell(double sx,double sy)
{
int iteration,itermax=100;
double dx,dy,tmp,fsq,cx=-0.5,cy=0.5;
dx = sx; dy = sy;
fsq=sx*sx+sy*sy;
for (iteration=1;iteration<=itermax &&
fsq<=4;iteration++)
{
tmp=dx;
dx = dx*dx-dy*dy+cx;
dy = 2.0*tmp*dy+cy;
fsq=dx*dx+dy*dy;
}
return iteration;
}
void color(int x,int y,int f)
void color(int x,int y,int f)
{
if(f==1)
{
glColor3f(1.0,0.0,0.0);
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}
else if(f==2)
{
glColor3f(0.0,1.0,0.0);
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}
glFlush();
}
void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
Output
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 440.0);
}
void display()
{
double sx,sy,tmp,zoom=1.5;
int iteration,hx,hy;
int itermax = 100;
int hxres = 200;
int hyres = 200;
for (hy=1;hy<=hyres;hy++)
{
sy=(hy-hyres/2)/(0.5*zoom*hyres);
for (hx=1;hx<=hxres;hx++)
{
sx=1.5*(hx-hxres/2)/(0.5*zoom*hxres);
iteration=dwell(sx,sy);
if (iteration<=itermax)
color(hx,hy,1);
else
color(hx,hy,2);
}
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE |
GLUT_RGB);
glutInitWindowSize(640,440);
glutInitWindowPosition(100,100);
glutCreateWindow("A Simple OpenGL
Windows Application with GLUT");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
engineerportal.blogspot.in
Mandelbort Set
#include <GL/glut.h>
int dwell(double cx,double cy)
{
int iteration,itermax=100;
double dx,dy,tmp,fsq;
dx = 0; dy = 0;
fsq=cx*cx+cy*cy;
for (iteration=1;iteration<=itermax &&
fsq<=4;iteration++)
{
tmp=dx;
dx = dx*dx-dy*dy+cx;
dy = 2.0*tmp*dy+cy;
fsq=dx*dx+dy*dy;
}
return iteration;
}
void color(int x,int y,int f) {
if(f==1)
{
glColor3f(1.0,0.0,0.0);
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}
else if(f==2)
{
glColor3f(0.0,1.0,0.0);
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}
glFlush();
}
void display()
{
double dx,tmp,dy,cx,cy;
int iteration,hx,hy;
int itermax = 100;
Output
if (iteration<=itermax)
color(hx,hy,1);
else
color(hx,hy,2);
}
void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 440.0);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE |
GLUT_RGB);
glutInitWindowSize(640,440);
glutInitWindowPosition(100,100);
glutCreateWindow("A Simple OpenGL Windows
Application with GLUT");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}