Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib> // standard definitions
- #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
- float r = 1, g = 0, b = 0, m = 2;
- //-----------------------------------------------------------------------
- // init
- // Sets up some default OpenGL values.
- //-----------------------------------------------------------------------
- void init()
- {
- glClearColor(0, 0, 0, 0); // background color
- glClearDepth(1.0); // background depth value
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(60, 1, 1, 1000); // setup a perspective projection
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- gluLookAt( // set up the camera
- 0.0, 0.0, 5.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
- // This is called each time application needs to redraw itself.
- // Most of the opengl work is done through this 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(0, 1, 0, 0); // rotate by rotAngle about y-axis
- glEnable(GL_COLOR_MATERIAL); // specify object color
- glColor3f(r,g,b); // redish
- glutSolidTeapot(m); // draw the teapot
- glPopMatrix(); // restore the modelview matrix
- glFlush(); // force OpenGL to render now
- glutSwapBuffers(); // make the image visible
- }
- void keyboard(unsigned char k, int x, int y)
- {
- if(k == 'r')
- {
- r = 1;
- g = 0;
- b = 0;
- }
- else if(k == 'g')
- {
- r = 0;
- g = 1;
- b = 0;
- }
- else if(k == 'b')
- {
- r = 0;
- g = 0;
- b = 1;
- }
- else exit(0);
- glutPostRedisplay();
- }
- void usage()
- {
- cout.flush();
- }
- void processSpecialKeys(int key, int x, int y)
- {
- if(key == GLUT_KEY_LEFT)
- {
- glTranslatef(-1,0,0);
- }
- else if(key == GLUT_KEY_RIGHT)
- {
- glTranslatef(1,0,0);
- }
- else if(key == GLUT_KEY_F1)
- {
- m++;
- }
- else if(key == GLUT_KEY_F2)
- {
- m--;
- }
- glutPostRedisplay();
- }
- int main()
- {
- usage(); // explain how to use
- glutInitDisplayMode( // initialize GLUT
- GLUT_DOUBLE | // use double buffering
- GLUT_DEPTH | // request memory for z-buffer
- GLUT_RGB ); // set RGB color mode
- glutCreateWindow("Task 3: Teapot Keyboard Effects"); // create the window
- glutDisplayFunc(display); // call display() to redraw window
- glutKeyboardFunc(keyboard); // call keyboard() when key is hit
- glutSpecialFunc(processSpecialKeys);
- init(); // our own initializations
- glutMainLoop(); // let GLUT take care of everything
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement