Tuesday, July 3, 2012

Movement of Teapot - OpenGL

An openGL program to illustrate the movement of teapot:

#include <cstdlib>
#include <iostream> // C++ I/O
#include <cstdio> // C I/O (for sprintf)
#include <cmath> // standard definitions
#include <GL/glut.h> // GLUT
#include <GL/glu.h> // GLU
#include <GL/gl.h> // OpenGL


using namespace std; // make std accessible
double rotAngle = 10; // rotation angle (BEWARE: Global)
void init()
{
glClearColor(0.96, 0.98, 0.03, 0); // background color
glClearDepth(1.0); // background depth value
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// gluPerspective(70, 1, 1, 1000); // setup a perspective projection
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/* gluLookAt( // set up the camera
0.0, 0.0, 10.0, // eye position
0.0, 0.0, 0.0, // lookat position
0.0, 1.0, 0.0); */ // up direction
glEnable(GL_DEPTH_TEST); // enable hidden surface removal
glEnable(GL_LIGHTING); // enable lighting
glEnable(GL_LIGHT0); // enable
float lpos[] = { 5, 5, 5, 0 };
glLightfv(GL_LIGHT0, GL_POSITION, lpos);
//glShadeModel(GL_FLAT); // flat shading
glShadeModel(GL_SMOOTH); // smooth shading
}
//-----------------------------------------------------------------------
// display callback function
//-----------------------------------------------------------------------
void display()
{
glClear(
GL_COLOR_BUFFER_BIT | // clear the frame buffer (color)
GL_DEPTH_BUFFER_BIT); // clear the depth buffer (depths)
glPushMatrix(); // save the current camera transform
glRotated(rotAngle, 0, 1, 0); // rotate by rotAngle about y-axis
glEnable(GL_COLOR_MATERIAL); // specify object color
glColor3f(0.07, 0.37, 0.85); // redish

glutSolidTeapot(1); // draw the teapot
glPopMatrix(); // restore the modelview matrix
glFlush(); // force OpenGL to render now
glutSwapBuffers(); // make the image visible
}
//-----------------------------------------------------------------------
// keyboard callback function
//-----------------------------------------------------------------------
void keyboard(unsigned char k, int x, int y)
{
switch (k)
{
case 'a':
rotAngle += 5; // increase rotation by 5 degrees
break;
case 's':
rotAngle -= 5; // decrease rotation by 5 degrees
break;
case 'd':
glTranslatef(0.5,0.5,0);
break;
case 'f':
glTranslatef(-0.5,-0.5,0);
break;
case 'g':
glScalef(0.5,0.5,0.5);
break;
case 'h':
glScalef(2.0,2.0,2.0);
break;
case 'q':
exit(0); // exit
}
glutPostRedisplay(); // redraw the image now
}
void usage()
{
cout << "\n\
-----------------------------------------------------------------------\n\
Menu\n\
Inputs keybord:\n\
a: Rotate counterclockwise\n\
s: Rotate clockwise\n\
d: Translate with x=1 y=1 z=0\n\
f: Translate with x= -1 y= -1 z =0 \n\
g: Zoom Out\n\
h: Zoom in\n\
q: Quit\n\
You may need to place the cursor over the graphics window for\n\
keyboard input to be processed.\n\
-----------------------------------------------------------------------\n";
cout.flush();
}
void menu(int value) {
switch (value) {
case 1 : rotAngle +=10;
break;
case 2 : rotAngle -=10;
break;
case 3 : glTranslatef(0.5,0.5,0);
break;
case 4 : glTranslatef(-0.5,-0.5,0);
break;
case 5 : glScalef(0.5,0.5,0.5);
break;
case 6 : glScalef(2.0,2.0,2.0);
break;
case 7 : exit(0);
}
glutPostRedisplay();
}
int main(int argc, char **argv)
{
usage(); // explain how to use
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH |GLUT_RGB );
glutCreateWindow("GLUT Example"); // create the window
glutCreateMenu(menu);
glutAddMenuEntry("Clockwise",1);
glutAddMenuEntry("Anticlockwise",2);
glutAddMenuEntry("Translate",3);
glutAddMenuEntry("AntiTranslate",4);
glutAddMenuEntry("Zoom out",5);
glutAddMenuEntry("Zoom in",6);
glutAddMenuEntry("Quit",7);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutDisplayFunc(display); // call display() to redraw window
glutKeyboardFunc(keyboard); // call keyboard() when key is hit
init(); // our own initializations
glutMainLoop(); // let GLUT take care of everything
return 0;
}

3 comments:

  1. How to fix this? Where to get GL Libraries?

    teapot.cpp:5:29: fatal error: GL/glut.h: No such file or directory
    compilation terminated.

    ReplyDelete
  2. just install openGL libraries using synaptic manager

    ReplyDelete