0% found this document useful (0 votes)
25 views12 pages

Lab 7,8,9

Lab

Uploaded by

oqbaalazab
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)
25 views12 pages

Lab 7,8,9

Lab

Uploaded by

oqbaalazab
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/ 12

281CSS-3 Computer Graphics

Lab Week 7

Name of the experiment: Animation with Geometric Transformations using 3D


objects.

Introduction:

OpenGL is a graphics package that uses the graphic library called as GLUT (Graphics Library Utility
Toolkit). GLUT contains OpenGL commands through which graphics are drawn.

Objective:

The objective of this lab work is as follows:

● Applying Geometric Transformations such as Translation, Scaling and Rotation to an object on 3D

objects

● Displaying the object with continuous transformations to show animation effect.

● Use of call back functions such as keyboard, menu functions and mouse functions.

Requirements:

● Computer having Operating System such a Windows

● Microsoft Visual Studio 2010 Professional

● Other files such as glut.h, glut32.dll, glut32.lib

1
Program 1: Transformations on 3D object & use of glut Menu call back function:

#include<glut.h>

float angle=0.0;

float red=1.0, blue=1.0, green=1.0;

void myDisplay(void)

glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glRotatef(angle, 1.0, 1.0, 1.0);

glColor3f(red,green,blue);

glutSoldTeapot(0.5);

angle++;

glutSwapBuffers();

void init()

{ glClearColor(1.0,1.0,0.0,0.0); }

void menu(int option)

switch(option)

case 1: red=1.0; green=0.0, blue=0.0; break;

case 2: red=0.0, green=1.0, blue=0.0; break;

case 3: red=0.0, green=0.0, blue=1.0; break;

2
case 4: red=1.0, green=1.0, blue=1.0; break;

case 5: exit(0);

int main(int argc, char **argv)

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);

glutInitWindowPosition(100,100);

glutInitWindowSize(320,320);

glutCreateWindow("Menu Test");

init();

glutDisplayFunc(myDisplay);

glutIdleFunc(myDisplay);

glutCreateMenu(menu);

glutAddMenuEntry("Red",1);

glutAddMenuEntry("Green",2);

glutAddMenuEntry("Blue",3);

glutAddMenuEntry("White",4);

glutAddMenuEntry("Close",5);

glutAttachMenu(GLUT_RIGHT_BUTTON);

glutMainLoop();

return 0;

OUTPUT:

3
Program 2: Transformations using gl() functions and use of keyboard call back functions

#include <glut.h>

static int year = 0, day = 0;

void init(void)

glClearColor (0.0, 0.0, 0.0, 0.0);

glShadeModel (GL_SMOOTH);

void display(void)

glClear (GL_COLOR_BUFFER_BIT);

glColor3f (1.0, 1.0, 0.0);

glPushMatrix();

glutSolidSphere(1.0, 20, 16); /* draw sun */

glRotatef ((GLfloat) year, 0.0, 1.0, 0.0);

glTranslatef (2.0, 0.0, 0.0);

glRotatef ((GLfloat) day, 0.0, 1.0, 0.0);

glColor3f (0.0, 0.0, 1.0);

glutWireSphere(0.2, 10, 8); /* draw smaller planet */

glPopMatrix();

glutSwapBuffers();

4
}

void reshape (int w, int h)

glViewport (0, 0, (GLsizei) w, (GLsizei) h);

glMatrixMode (GL_PROJECTION);

gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);

glMatrixMode(GL_MODELVIEW);

gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

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

switch (key) {

case 'd': // rotate the planet about its axis in anti clockwise

day++;

glutPostRedisplay();

break;

case 'D': // rotate the planet about its axis in clockwise

day--;

glutPostRedisplay();

break;

case 'y': // rotate the planet around the sun in anti clockwise

year++;

glutPostRedisplay();

break;

case 'Y': // rotate the planet around the sun in clockwise

year--;

glutPostRedisplay();

break;

5
default:

exit(0); // to quit the program

break;

int main(int argc, char** argv)

glutInit(&argc, argv);

glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);

glutInitWindowSize (500, 500);

glutInitWindowPosition (100, 100);

glutCreateWindow (argv[0]);

init ();

glutDisplayFunc(display);

glutReshapeFunc(reshape);

glutKeyboardFunc(keyboard);

glutMainLoop();

return 0;

OUTPUT:

Program 3: To create the 3D object and apply continuous rotation using the interactive
mouse and keyboard call back functions.

6
#include <stdlib.h>

#include <stdio.h>

#include <math.h>

#include <glut.h>

#define ROT_INC 0.1

void drawSphere(void);

static GLfloat g_rotate = 0;

static GLfloat g_rotInc = ROT_INC;

static void (*drawPrimP)(void) = drawSphere;

void drawSphere(void)

glutWireSphere(6.0,20,20);

void drawCube(void)

glutWireCube(6.0);

void drawCone(void) {

glutWireCone(6.0, 8.0, 30, 20);

void drawTorus(void) {

glutWireTorus(1.0, 6.0, 10, 20);

void drawTeapot(void) {

glutWireTeapot(6.0);

void setPrim(int value) {

switch(value) {

7
case 1:

drawPrimP = drawSphere;

break;

case 2:

drawPrimP = drawCube;

break;

case 3:

drawPrimP = drawCone;

break;

case 4:

drawPrimP = drawTorus;

break;

case 5:

drawPrimP = drawTeapot;

break;

void display(void)

glClear( GL_COLOR_BUFFER_BIT );

/* set matrix mode to modelview */ glMatrixMode(GL_MODELVIEW);

/* save matrix */ glPushMatrix();

/* global rotation */ glRotatef(g_rotate,1.0,1.0,1.0);

/* draw the geometry */ (*drawPrimP)();

/* restore matrix */ glPopMatrix();

/* swap buffers to display the frame */ glutSwapBuffers();

void myReshape(int w, int h)

8
{

glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION);

glLoadIdentity(); /* init projection matrix */

/* perspective parameters: field of view, aspect, near clip, far clip */

gluPerspective( 60.0, (GLdouble)w/(GLdouble)h, 0.1, 40.0);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

gluLookAt(0.0, 0.0, 20.0, /* eye at (0,0,20) */

0.0, 0.0, 0.0, /* lookat point */

0.0, 1.0, 0.0); /* up is in +ive y */

void myKey(unsigned char k, int x, int y)

switch (k) {

case 'q':

case 'Q': exit(0);

break;

default:

printf("Unknown keyboard command \'%c\'.\n", k);

break;

void myMouse(int btn, int state, int x, int y)

if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) g_rotInc += ROT_INC;

if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) g_rotInc = ROT_INC;

glutPostRedisplay();

9
}

void myIdleFunc(void) {

g_rotate += g_rotInc;

glutPostRedisplay();

int main(int argc, char **argv)

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);

glutInitWindowSize(500, 500);

glutCreateWindow("3D Shapes Test");

glutReshapeFunc(myReshape);

glutDisplayFunc(display);

glutIdleFunc(myIdleFunc);

glutKeyboardFunc(myKey);

glutMouseFunc(myMouse);

/* set up right menu */

glutCreateMenu(setPrim);

glutAddMenuEntry("Sphere", 1);

glutAddMenuEntry("Cube", 2);

glutAddMenuEntry("Cone", 3);

glutAddMenuEntry("Torus", 4);

glutAddMenuEntry("Teapot", 5);

glutAttachMenu(GLUT_RIGHT_BUTTON);

/* set clear colour */ glClearColor(1.0, 1.0, 1.0, 1.0);

/* set current colour to black */ glColor3f(0.0, 0.0, 0.0);

glutMainLoop(); return 0;

10
Output:

Case 1: Sphere Case 2: Cube

Case 3: Cone Case 4: Torus

Case 5: Teapot
Exercise:

Modify the menu to display the following additional polyhedras:

● Dodecahedron (12 sided )


● Icosahedron (20 sided )
● Octahedron ( 8 sided )
● Tetrahedron (4 sided )

Exercises :
1. in program 1, use different polyhedra objects instead of tea pot.

11
2. do the rotation in different axis individually. use the following format

3. Modify the menu in program 3 to display the following additional polyhedras:


● Dodecahedron (12 sided )
● Icosahedron (20 sided )
● Octahedron ( 8 sided )
● Tetrahedron (4 sided )

Hint:

Include the items that indicate these primitive names in the menu and associate the case
number with each added menu item. Use the case numbers in the switch statement to call
the function that draws the respective primitives.

a) Cube -glutWireCube(size); (or) glutSolidCube


b) Cone -glutWireCone(rbase, height, longitudes, latitudes);
c) Sphere -glutWireSphere(radius, longitudes, latitudes);
d) Hexahedron -glutWireHexahedron();
e) Dodecahedron -glutWireDodecahedron() (or) glutSolidDodecahedron()
f) Icosahedron -glutWire Icosahedron () (or) glutSolid Icosahedron ()
g) Octahedron -glutWireOctahedron() (or) glutSolidOctahedron()
h) Tetrahedron -glutWireTetrahedron() (or) glutSolidTetrahedron()

Rotation:
glRotatef(angle,x-axis,y-axis,z-axis);
For example, to rotate by x-axis glRotatef(angle, 1.0,0.0,0.0);
To rotate by y-axis glRotate(angle,0.0,1.0,0.0);
To rotate both x-axis and y-axis glRotate(angle,1.0,1.0,0.0);
Similarly for z-axis also changes the last parameter.

12

You might also like