Lab 7,8,9
Lab 7,8,9
Lab Week 7
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:
objects
● Use of call back functions such as keyboard, menu functions and mouse functions.
Requirements:
1
Program 1: Transformations on 3D object & use of glut Menu call back function:
#include<glut.h>
float angle=0.0;
void myDisplay(void)
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glColor3f(red,green,blue);
glutSoldTeapot(0.5);
angle++;
glutSwapBuffers();
void init()
{ glClearColor(1.0,1.0,0.0,0.0); }
switch(option)
2
case 4: red=1.0, green=1.0, blue=1.0; break;
case 5: exit(0);
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>
void init(void)
glShadeModel (GL_SMOOTH);
void display(void)
glClear (GL_COLOR_BUFFER_BIT);
glPushMatrix();
glPopMatrix();
glutSwapBuffers();
4
}
glMatrixMode (GL_PROJECTION);
glMatrixMode(GL_MODELVIEW);
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
switch (key) {
case 'd': // rotate the planet about its axis in anti clockwise
day++;
glutPostRedisplay();
break;
day--;
glutPostRedisplay();
break;
case 'y': // rotate the planet around the sun in anti clockwise
year++;
glutPostRedisplay();
break;
year--;
glutPostRedisplay();
break;
5
default:
break;
glutInit(&argc, argv);
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>
void drawSphere(void);
void drawSphere(void)
glutWireSphere(6.0,20,20);
void drawCube(void)
glutWireCube(6.0);
void drawCone(void) {
void drawTorus(void) {
void drawTeapot(void) {
glutWireTeapot(6.0);
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 );
8
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
switch (k) {
case 'q':
break;
default:
break;
glutPostRedisplay();
9
}
void myIdleFunc(void) {
g_rotate += g_rotInc;
glutPostRedisplay();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(500, 500);
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutIdleFunc(myIdleFunc);
glutKeyboardFunc(myKey);
glutMouseFunc(myMouse);
glutCreateMenu(setPrim);
glutAddMenuEntry("Sphere", 1);
glutAddMenuEntry("Cube", 2);
glutAddMenuEntry("Cone", 3);
glutAddMenuEntry("Torus", 4);
glutAddMenuEntry("Teapot", 5);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop(); return 0;
10
Output:
Case 5: Teapot
Exercise:
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
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.
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