Laboratory CG&IP
Laboratory CG&IP
#include<stdio.h>
#include <GL/glut.h>
#include <math.h>
void draw()
{
glClearColor(1,1,1,1); //Background Color(#,#,#) and Transparency (#)
glPointSize(4); //Point Size
gluOrtho2D(0,50,0,50); //Specifying the 2D Space with Start and End Cordinates
int x1=20,y1=10,x2=10,y2=30; //Coordinates
float x,y,dx,dy,m,p,temp;
glClear(GL_COLOR_BUFFER_BIT); //Clearing the previous BG
if(x1!=x2)
m=(y2-y1)/(x2-x1); //Finding Slope. If x1==x2, then slope is zero
else
m=999;
x=x1; //Updating Variable
y=y1; //Updating Variable
if(fabs(m)<1) //Positive Slope and zero slope
{
if(x1>x2)
{
temp=x1;x1=x2;x2=temp;
temp=y1;y1=y2;y2=temp;
}
dx=fabs(x2-x1);
dy=fabs(y2-y1);
x=x1;
y=y1;
p=2*dy-dx;
while(x<=x2)
{
glBegin(GL_POINTS);
glColor3f(1,0,0);
glVertex2f(x,y);
glEnd();
x=x+1;
if(p>=0)
{
if(m>=0&&m<1)
y=y+1;
else
y=y-1;
p=p+2*dy-2*dx;
}
else
p=p+2*dy;
}
}
if(fabs(m)>=1) //Negative and Infinite Slope
{
if(y1>y2)
{
temp=x1;x1=x2;x2=temp;
temp=y1;y1=y2;y2=temp;
}
dx=fabs(x2-x1);
dy=fabs(y2-y1);
x=x1;
y=y1;
p=2*dx-dy;
while(y<=y2)
{
glBegin(GL_POINTS);
glColor3f(1,0,0);
glVertex2f(x,y);
glEnd();
y=y+1;
if(p>=0)
{
if(m>=1)
x=x+1;
else
x=x-1;
p=p+2*dx-2*dy;
}
else
p=p+2*dx;
}
}
glFlush(); //Refreshing the window
}
int main(int C,char *V[])
{
glutInit(&C,V);
glutInitWindowSize(480,480);
glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
glutCreateWindow("Bresenham's Algorithm");
glutDisplayFunc(draw);
glutMainLoop();
return 0;
}
2. Develop a program to demonstrate basic geometric operations on the 2D object.
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
GLfloat d,r,xr,yr,sx=1,sy=1;
int i;
void draw()
glClearColor(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,0,0);
glRotatef(theta,0,0,1);
glTranslatef(dx,dy,0);
glScalef(sc_x,sc_y,0);
glBegin(GL_LINE_LOOP);
glVertex2fv(P[0]);
glVertex2fv(P[1]);
glVertex2fv(P[2]);
glEnd();
glFlush();
switch(key)
case 'r':theta=d;
dx=dy=0;
sc_x=1;sc_y=1;
break;
dx=dy=0;
sc_x=1;sc_y=1;
break;
dx=tx; dy=ty;
sc_x=1;sc_y=1;
break;
dx=-tx; dy=-ty;
sc_x=1;sc_y=1;
break;
sc_x=sx;sc_y=sy;
break;
dx=0; dy=0;
sc_x=1.0/sx;sc_y=1.0/sy;
break;
glutPostRedisplay();
glClearColor(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,0,0);
glPointSize(1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
{
printf("Enter the angle\n");
scanf("%f",&d);
// scanf("%f%f",&xr,&yr);
scanf("%f%f",&tx,&ty);
scanf("%f%f",&sx,&sy);
glutInit(&argc, argv);
glutInitWindowSize(640,480);
glutInitWindowPosition(0,0);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE );
glutCreateWindow("Triangle Rotation");
myInit();
glutKeyboardFunc(keyboard);
glutDisplayFunc(draw);
glutMainLoop();
return 0;
}
3. Develop a program to Demonstrate basic geometric operations on 3D object.
#include<stdio.h>
#include <GL/glut.h>
GLfloat d=0,dinc=1,ux=0,uy=0,uz=1;
void spin()
/****************Translation***********/
tx=tx+tinc;
if(tx>=0.5)
tinc=-0.01;
if(tx<-0.5)
tinc=0.01;
ty=ty+tinc;
if(ty>=0.5)
tinc=-0.01;
if(ty<0.0)
tinc=0.01;
tz=tz+tinc;
if(tz>=0.5)
tinc=-0.01;
if(tz<-0.5)
tinc=0.01;
/****************Rotation***********/
d=d+dinc;
if(d>=360)
dinc=-1;
if(d<0)
dinc=1;
ux=1;uy=0,uz=0;
d=d+dinc;
if(d>=360)
dinc=-1;
if(d<0)
dinc=1;
ux=0;uy=1,uz=0;
d=d+dinc;
if(d>=360)
dinc=-1;
else if(d<0)
dinc=1;
ux=0;uy=0,uz=1;
}
/****************Scaling***********/
sx=sx+sinc;
if(sx>=2)
sinc=-0.01;
if(sx<0.1)
sinc=0.01;
sy=sy+sinc;
if(sy>=2)
sinc=-0.01;
if(sy<0.1)
sinc=0.01;
sz=sz+sinc;
if(sz>=2)
sinc=-0.01;
if(sz<0.1)
sinc=0.01;
glutPostRedisplay();
void display()
GLfloat L[]={1,1,1};
GLfloat P[]={0,1,-1,0};
GLfloat D1[]={1,0,0};
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0,1,3,0,0,0,0,1,0);
glLightfv(GL_LIGHT0,GL_AMBIENT,L);
glLightfv(GL_LIGHT0,GL_POSITION,P);
//glTranslatef(tx,ty,tz);
glPushMatrix();
glTranslatef(0.0,-0.25,0.0);
glScalef(1,0.05,1);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();
glTranslatef(-0.45,-0.5,-0.45);
glScalef(0.05,0.60,0.05);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();
glTranslatef(-0.45,-0.5,0.45);
glScalef(0.05,0.60,0.05);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();
glTranslatef(0.45,-0.5,-0.45);
glScalef(0.05,0.60,0.05);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();
glTranslatef(0.45,-0.5,0.45);
glScalef(0.05,0.60,0.05);
glutSolidCube(1);
glPopMatrix();
glPushAttrib(GL_ALL_ATTRIB_BITS);
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,D1);
glTranslatef(tx,ty,tz);
glRotatef(d,ux,uy,uz);
glScalef(sx,sy,sz);
glPushMatrix();
glTranslatef(0,0,0);
glutSolidTeapot(0.20);
glPopMatrix();
glPopAttrib();
glutSwapBuffers();
key=key_pressed;
void init()
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1,1,-1,1,2,10);
glMatrixMode(GL_MODELVIEW);
printf("1.Translation\n2.Rotation\n3.Scaling\n");
scanf("%d",&choice);
glutInit(&argc, argv);
glutInitWindowSize(640,640);
glutInitWindowPosition(10,10);
init();
glutDisplayFunc(display);
glutIdleFunc(spin);
glutKeyboardFunc(keyboard);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glutMainLoop();
return 0;
}
4. Develop a program to demonstrate animation effects on simple objects.
#include<stdio.h>
#include <GL/glut.h>
#include <math.h>
#include <stdlib.h>
const double TWO_PI = 6.2831853;
GLsizei winWidth = 500, winHeight = 500; // Initial display window size.
GLuint regHex; // Define name for display list.
static GLfloat rotTheta = 0.0;
struct Pt {
GLint x, y;
};
glutMouseFunc (mouseFcn);
glutMainLoop ( );
}