Advertisement
naeem043

opengl Keyboard with teapot

Jan 3rd, 2020
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.41 KB | None | 0 0
  1.  
  2. #include <cstdlib>          // standard definitions
  3. #include <iostream>         // C++ I/O
  4. #include <cstdio>           // C I/O (for sprintf)
  5. #include <cmath>            // standard definitions
  6.  
  7. #include <GL/glut.h>            // GLUT
  8. #include <GL/glu.h>         // GLU
  9. #include <GL/gl.h>          // OpenGL
  10.  
  11. using namespace std;    // make std accessible
  12.  
  13. float r = 1, g = 0, b = 0, m = 2;
  14.  
  15. //-----------------------------------------------------------------------
  16. // init
  17. //  Sets up some default OpenGL values.
  18. //-----------------------------------------------------------------------
  19.  
  20. void init()
  21. {
  22.     glClearColor(0, 0, 0, 0);       // background color
  23.     glClearDepth(1.0);          // background depth value
  24.  
  25.     glMatrixMode(GL_PROJECTION);
  26.     glLoadIdentity();
  27.     gluPerspective(60, 1, 1, 1000); // setup a perspective projection
  28.  
  29.     glMatrixMode(GL_MODELVIEW);
  30.     glLoadIdentity();
  31.  
  32.     gluLookAt(              // set up the camera
  33.         0.0, 0.0, 5.0,      // eye position
  34.         0.0, 0.0, 0.0,      // lookat position
  35.         0.0, 1.0, 0.0);     // up direction
  36.  
  37.     glEnable(GL_DEPTH_TEST);        // enable hidden surface removal
  38.  
  39.     glEnable(GL_LIGHTING);      // enable lighting
  40.     glEnable(GL_LIGHT0);        // enable
  41.    
  42.     float lpos[] = { 5, 5, 5, 0 };
  43.     glLightfv(GL_LIGHT0, GL_POSITION, lpos);
  44.  
  45.     // glShadeModel(GL_FLAT);       // flat shading
  46.     glShadeModel(GL_SMOOTH);        // smooth shading
  47. }
  48.  
  49. //-----------------------------------------------------------------------
  50. // display callback function
  51. //  This is called each time application needs to redraw itself.
  52. //  Most of the opengl work is done through this function.
  53. //-----------------------------------------------------------------------
  54.  
  55. void display()
  56. {
  57.     glClear(
  58.     GL_COLOR_BUFFER_BIT |       // clear the frame buffer (color)
  59.     GL_DEPTH_BUFFER_BIT);       // clear the depth buffer (depths)
  60.  
  61.     glPushMatrix();         // save the current camera transform
  62.  
  63.     glRotated(0, 1, 0, 0);  // rotate by rotAngle about y-axis
  64.  
  65.  
  66.     glEnable(GL_COLOR_MATERIAL);    // specify object color
  67.     glColor3f(r,g,b);       // redish
  68.    
  69.  
  70.     glutSolidTeapot(m);         // draw the teapot
  71.  
  72.     glPopMatrix();          // restore the modelview matrix
  73.     glFlush();              // force OpenGL to render now
  74.  
  75.     glutSwapBuffers();          // make the image visible
  76. }
  77.  
  78. void keyboard(unsigned char k, int x, int y)
  79. {
  80.     if(k == 'r')
  81.     {
  82.         r = 1;
  83.         g = 0;
  84.         b = 0;
  85.     }
  86.     else if(k == 'g')
  87.     {
  88.         r = 0;
  89.         g = 1;
  90.         b = 0;
  91.     }
  92.     else if(k == 'b')
  93.     {
  94.         r = 0;
  95.         g = 0;
  96.         b = 1;
  97.     }
  98.  
  99.  
  100.     else exit(0);
  101.    
  102.  
  103.     glutPostRedisplay();
  104. }
  105.  
  106.  
  107.  
  108. void usage()
  109. {
  110.  
  111.     cout.flush();
  112. }
  113.  
  114.  
  115.  
  116. void processSpecialKeys(int key, int x, int y)
  117. {
  118.     if(key == GLUT_KEY_LEFT)
  119.     {
  120.         glTranslatef(-1,0,0);
  121.     }
  122.  
  123.     else if(key == GLUT_KEY_RIGHT)
  124.     {
  125.         glTranslatef(1,0,0);
  126.     }
  127.    
  128.     else if(key == GLUT_KEY_F1)
  129.     {
  130.         m++;
  131.     }
  132.  
  133.     else if(key == GLUT_KEY_F2)
  134.     {
  135.         m--;
  136.     }
  137.    
  138.     glutPostRedisplay();
  139.  
  140. }
  141.  
  142.  
  143. int main()
  144. {
  145.     usage();                // explain how to use
  146.     glutInitDisplayMode(        // initialize GLUT
  147.     GLUT_DOUBLE |       // use double buffering
  148.     GLUT_DEPTH |        // request memory for z-buffer
  149.     GLUT_RGB );     // set RGB color mode
  150.    
  151.     glutCreateWindow("Task 3: Teapot Keyboard Effects");    // create the window
  152.  
  153.     glutDisplayFunc(display);       // call display() to redraw window
  154.     glutKeyboardFunc(keyboard);     // call keyboard() when key is hit
  155.     glutSpecialFunc(processSpecialKeys);
  156.  
  157.     init();             // our own initializations
  158.  
  159.     glutMainLoop();         // let GLUT take care of everything
  160.     return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement