0% found this document useful (0 votes)
6 views

Laboratory CG&IP

The document contains multiple C programs using OpenGL and GLUT to demonstrate various graphical techniques. It includes implementations for Bresenham's line drawing algorithm, basic geometric operations on 2D and 3D objects, and animation effects on simple shapes like a rotating hexagon. Each program initializes a window, sets up the necessary transformations, and handles user input for interaction.

Uploaded by

nanditha7766
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Laboratory CG&IP

The document contains multiple C programs using OpenGL and GLUT to demonstrate various graphical techniques. It includes implementations for Bresenham's line drawing algorithm, basic geometric operations on 2D and 3D objects, and animation effects on simple shapes like a rotating hexagon. Each program initializes a window, sets up the necessary transformations, and handles user input for interaction.

Uploaded by

nanditha7766
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

1. Develop a program to draw a line using Bresenham’s line drawing techniques.

#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 P[3][2]={{10,10},{50,50},{90,10}}, NP[3][2];

GLfloat d,r,xr,yr,sx=1,sy=1;

GLfloat tx=0.0,ty=0.0, theta = 0, dx=0,dy=0, sc_x=1.0,sc_y=1.0;

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();

void keyboard(unsigned char key, int x, int y)

switch(key)

case 'r':theta=d;

dx=dy=0;

sc_x=1;sc_y=1;

break;

case 'R': theta=-d;

dx=dy=0;

sc_x=1;sc_y=1;

break;

case 't': theta=0;

dx=tx; dy=ty;

sc_x=1;sc_y=1;

break;

case 'T': theta=0;

dx=-tx; dy=-ty;

sc_x=1;sc_y=1;

break;

case 's': theta=0;


dx=0; dy=0;

sc_x=sx;sc_y=sy;

break;

case 'S': theta=0;

dx=0; dy=0;

sc_x=1.0/sx;sc_y=1.0/sy;

break;

glutPostRedisplay();

void myInit (void)

glClearColor(1,1,1,1);

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1,0,0);

glPointSize(1.0);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

// setting window dimension in X- and Y- direction

gluOrtho2D(0.0,200.0, 0.0, 200.0);

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

{
printf("Enter the angle\n");

scanf("%f",&d);

//printf("Enter the points for rotation and scaling\n");

// scanf("%f%f",&xr,&yr);

printf("Enter the translation distance in x and y direction \n");

scanf("%f%f",&tx,&ty);

printf("Enter the scaling factor in x and y direction \n");

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 tx=0,ty=0,tz=0, tinc=0.01;

GLfloat d=0,dinc=1,ux=0,uy=0,uz=1;

GLfloat sx=1.0, sy=1.0, sz=1.0,sinc=0.01;

GLint choice, key;

void spin()

/****************Translation***********/

if(choice == 1 && key=='x')

tx=tx+tinc;

if(tx>=0.5)

tinc=-0.01;

if(tx<-0.5)

tinc=0.01;

if(choice == 1 && key=='y')


{

ty=ty+tinc;

if(ty>=0.5)

tinc=-0.01;

if(ty<0.0)

tinc=0.01;

if(choice == 1 && key=='z')

tz=tz+tinc;

if(tz>=0.5)

tinc=-0.01;

if(tz<-0.5)

tinc=0.01;

/****************Rotation***********/

if(choice == 2 && key=='x')

d=d+dinc;

if(d>=360)

dinc=-1;

if(d<0)
dinc=1;

ux=1;uy=0,uz=0;

if(choice == 2 && key=='y')

d=d+dinc;

if(d>=360)

dinc=-1;

if(d<0)

dinc=1;

ux=0;uy=1,uz=0;

if(choice == 2 && key=='z')

d=d+dinc;

if(d>=360)

dinc=-1;

else if(d<0)

dinc=1;

ux=0;uy=0,uz=1;

}
/****************Scaling***********/

if(choice == 3 && key=='x')

sx=sx+sinc;

if(sx>=2)

sinc=-0.01;

if(sx<0.1)

sinc=0.01;

if(choice == 3 && key=='y')

sy=sy+sinc;

if(sy>=2)

sinc=-0.01;

if(sy<0.1)

sinc=0.01;

if(choice == 3 && key=='z')

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();

void keyboard(unsigned char key_pressed, int x, int y)


{

key=key_pressed;

void init()

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glFrustum(-1,1,-1,1,2,10);

glMatrixMode(GL_MODELVIEW);

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

printf("1.Translation\n2.Rotation\n3.Scaling\n");

printf("Enter your choice\n");

scanf("%d",&choice);

glutInit(&argc, argv);

glutInitWindowSize(640,640);

glutInitWindowPosition(10,10);

glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);


glutCreateWindow("3D Transformation");

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;
};

typedef struct Pt scrPt;

static void init (void)


{
scrPt hexVertex;
GLdouble hexTheta;
GLint k;
glClearColor (1.0, 1.0, 1.0, 0.0);
/* Set up a display list for a red regular hexagon.
* Vertices for the hexagon are six equally spaced
* points around the circumference of a circle.
*/
regHex = glGenLists (1);
glNewList (regHex, GL_COMPILE);
glColor3f (1.0, 0.0, 0.0);
glBegin (GL_POLYGON);
for (k = 0; k < 6; k++) {
hexTheta = TWO_PI * k / 6;
hexVertex.x = 150 + 100 * cos (hexTheta);
hexVertex.y = 150 + 100 * sin (hexTheta);
glVertex2i (hexVertex.x, hexVertex.y);
}
glEnd ( );
glEndList ( );
}

void displayHex (void)


{
glClear (GL_COLOR_BUFFER_BIT);
glPushMatrix ( );
glRotatef (rotTheta, 0.0, 0.0, 1.0);
glCallList (regHex);
glPopMatrix ( );
glutSwapBuffers ( );
glFlush ( );
}
void rotateHex (void)
{
rotTheta += 3.0;
if (rotTheta > 360.0)
rotTheta -= 360.0;
glutPostRedisplay ( );
}
void winReshapeFcn (GLint newWidth, GLint newHeight)
{
glViewport (0, 0, (GLsizei) newWidth, (GLsizei) newHeight);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ( );
gluOrtho2D (-320.0, 320.0, -320.0, 320.0);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ( );
glClear (GL_COLOR_BUFFER_BIT);
}

void mouseFcn (GLint button, GLint action, GLint x, GLint y)


{
switch (button) {
case GLUT_MIDDLE_BUTTON: // Start the rotation.
if (action == GLUT_DOWN)
glutIdleFunc (rotateHex);
break;
case GLUT_RIGHT_BUTTON: // Stop the rotation.
if (action == GLUT_DOWN)
glutIdleFunc (NULL);
break;
default:
break;
}
}

int main (int argc, char** argv)


{
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition (150, 150);
glutInitWindowSize (winWidth, winHeight);
glutCreateWindow ("Animation Example");
init ( );
glutDisplayFunc (displayHex);
glutReshapeFunc (winReshapeFcn);

glutMouseFunc (mouseFcn);
glutMainLoop ( );
}

You might also like