naeem043

OPENGL Keyboard operation

Nov 1st, 2019
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.68 KB | None | 0 0
  1. //-----------------------------------------------------------------------
  2. // File: sample1.cpp
  3. // Description: A simple OpenGL sample program
  4. // Programmer: Pooja Nath
  5. // For: CMSC 427 - Computer Graphics
  6. // Date: Feb 2004
  7. //
  8. // This is a basic OpenGL/GLUT program, which renders a teapot.
  9. // Hitting the keyboard charatcers 'a' and 'l' rotate the teapot
  10. // about the Y (vertical) axis. Hitting 'q' quits the program.
  11. //-----------------------------------------------------------------------
  12.  
  13.  
  14. #include <cstdlib> // standard definitions
  15. #include <iostream> // C++ I/O
  16. #include <cstdio> // C I/O (for sprintf)
  17. #include <cmath> // standard definitions
  18.  
  19. #include <GL/glut.h> // GLUT
  20. #include <GL/glu.h> // GLU
  21. #include <GL/gl.h> // OpenGL
  22.  
  23. using namespace std; // make std accessible
  24.  
  25. float r = 1, g = 0, b = 0, m = 2;
  26.  
  27. //-----------------------------------------------------------------------
  28. // init
  29. // Sets up some default OpenGL values.
  30. //-----------------------------------------------------------------------
  31.  
  32. void init()
  33. {
  34. glClearColor(0, 0, 0, 0); // background color
  35. glClearDepth(1.0); // background depth value
  36.  
  37. glMatrixMode(GL_PROJECTION);
  38. glLoadIdentity();
  39. gluPerspective(60, 1, 1, 1000); // setup a perspective projection
  40.  
  41. glMatrixMode(GL_MODELVIEW);
  42. glLoadIdentity();
  43.  
  44. gluLookAt( // set up the camera
  45. 0.0, 0.0, 5.0, // eye position
  46. 0.0, 0.0, 0.0, // lookat position
  47. 0.0, 1.0, 0.0); // up direction
  48.  
  49. glEnable(GL_DEPTH_TEST); // enable hidden surface removal
  50.  
  51. glEnable(GL_LIGHTING); // enable lighting
  52. glEnable(GL_LIGHT0); // enable
  53.  
  54. float lpos[] = { 5, 5, 5, 0 };
  55. glLightfv(GL_LIGHT0, GL_POSITION, lpos);
  56.  
  57. // glShadeModel(GL_FLAT); // flat shading
  58. glShadeModel(GL_SMOOTH); // smooth shading
  59. }
  60.  
  61. //-----------------------------------------------------------------------
  62. // display callback function
  63. // This is called each time application needs to redraw itself.
  64. // Most of the opengl work is done through this function.
  65. //-----------------------------------------------------------------------
  66.  
  67. void display()
  68. {
  69. glClear(
  70. GL_COLOR_BUFFER_BIT | // clear the frame buffer (color)
  71. GL_DEPTH_BUFFER_BIT); // clear the depth buffer (depths)
  72.  
  73. glPushMatrix(); // save the current camera transform
  74.  
  75. glRotated(0, 1, 0, 0); // rotate by rotAngle about y-axis
  76.  
  77.  
  78. glEnable(GL_COLOR_MATERIAL); // specify object color
  79. glColor3f(r,g,b); // redish
  80.  
  81.  
  82. glutSolidTeapot(m); // draw the teapot
  83.  
  84. glPopMatrix(); // restore the modelview matrix
  85. glFlush(); // force OpenGL to render now
  86.  
  87. glutSwapBuffers(); // make the image visible
  88. }
  89.  
  90. //-----------------------------------------------------------------------
  91. // keyboard callback function
  92. // This is called whenever a keyboard key is hit.
  93. //-----------------------------------------------------------------------
  94.  
  95. void keyboard(unsigned char k, int x, int y)
  96. {
  97. if(k == 'r')
  98. {
  99. r = 1;
  100. g = 0;
  101. b = 0;
  102. }
  103. else if(k == 'g')
  104. {
  105. r = 0;
  106. g = 1;
  107. b = 0;
  108. }
  109. else if(k == 'b')
  110. {
  111. r = 0;
  112. g = 0;
  113. b = 1;
  114. }
  115.  
  116.  
  117.  
  118. else if((k == 'W') && (glutGetModifiers() == GLUT_ACTIVE_SHIFT))
  119. {
  120. r = 1;
  121. g = 1;
  122. b = 1;
  123. }
  124.  
  125. else if(k == 'c')
  126. {
  127. glRotatef(90,0,0,1);
  128. }
  129.  
  130. else if(k == 'a')
  131. {
  132. glRotatef(-90,0,0,1);
  133. }
  134.  
  135.  
  136.  
  137. else exit(0);
  138.  
  139.  
  140. glutPostRedisplay();
  141. }
  142.  
  143.  
  144.  
  145. void usage()
  146. {
  147. cout << "\n\
  148. -----------------------------------------------------------------------\n\
  149. CMSC 427 Sample Program.\n\
  150. Inputs:\n\
  151. a: Rotate counterclockwise\n\
  152. l: Rotate clockwise\n\
  153. q: Quit\n\
  154. You may need to place the cursor over the graphics window for\n\
  155. keyboard input to be processed.\n\
  156. -----------------------------------------------------------------------\n";
  157. cout.flush();
  158. }
  159.  
  160.  
  161.  
  162. void processSpecialKeys(int key, int x, int y)
  163. {
  164. /*if(key == GLUT_KEY_LEFT)
  165. {
  166. glTranslatef(-1, 0, 0);
  167. }
  168.  
  169. else if(key == GLUT_KEY_RIGHT)
  170. {
  171. glTranslatef(1, 0, 0);
  172. }
  173. if(key == GLUT_KEY_UP)
  174. {
  175. glTranslatef(0, 1, 0);
  176. }
  177.  
  178. else if(key == GLUT_KEY_DOWN)
  179. {
  180. glTranslatef(0, -1, 0);
  181. }*/
  182.  
  183. if((key == GLUT_KEY_UP) && (glutGetModifiers() == GLUT_ACTIVE_CTRL))
  184. {
  185. m++;
  186. }
  187.  
  188. else if((key == GLUT_KEY_DOWN) && (glutGetModifiers() == GLUT_ACTIVE_CTRL))
  189. {
  190. m--;
  191. }
  192.  
  193. if((glutGetModifiers() == GLUT_ACTIVE_SHIFT) && (key == GLUT_KEY_UP)) //color brightness incress
  194. {
  195. r += 0.1;
  196. }
  197. else if((glutGetModifiers() == GLUT_ACTIVE_SHIFT) && (key == GLUT_KEY_DOWN)) //color brightness incress
  198. {
  199. r -= 0.1;
  200.  
  201. }
  202.  
  203. glutPostRedisplay();
  204.  
  205. }
  206.  
  207.  
  208. void mouseClicks(int button, int state, int x, int y)
  209. {
  210.  
  211. }
  212.  
  213.  
  214. //-----------------------------------------------------------------------
  215. // main program
  216. // Where everything begins.
  217. //-----------------------------------------------------------------------
  218.  
  219. int main()
  220. {
  221. usage(); // explain how to use
  222. glutInitDisplayMode( // initialize GLUT
  223. GLUT_DOUBLE | // use double buffering
  224. GLUT_DEPTH | // request memory for z-buffer
  225. GLUT_RGB ); // set RGB color mode
  226.  
  227. printf("Press r for red, g for green and b for blue, SHIFT+W to white else any key for exit. Press Shift+Up Arrow for zooming in and Shift+Down for zooming out");
  228. glutCreateWindow("GLUT Example"); // create the window
  229.  
  230. glutDisplayFunc(display); // call display() to redraw window
  231. glutKeyboardFunc(keyboard); // call keyboard() when key is hit
  232. glutSpecialFunc(processSpecialKeys);
  233. glutMouseFunc(mouseClicks);
  234.  
  235. init(); // our own initializations
  236.  
  237. glutMainLoop(); // let GLUT take care of everything
  238. return 0;
  239. }
Advertisement
Add Comment
Please, Sign In to add comment