Advertisement
Guest User

Tresh

a guest
Apr 25th, 2010
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.11 KB | None | 0 0
  1. main.cpp
  2. #include "main.h"
  3.  
  4. GLfloat angle = 0.0; //the rotation value
  5.  
  6. void cube (void) {
  7.     glRotatef(angle, 1.0, 0.0, 0.0); //rotate on the x axis
  8.     glRotatef(angle, 0.0, 1.0, 0.0); //rotate on the y axis
  9.     glRotatef(angle, 0.0, 0.0, 1.0); //rotate on the z axis
  10.     glColor3f(1.0, 0.0, 0.0);
  11.     glutWireCube(2);
  12. }
  13.  
  14. void display (void) {
  15.     keyOperations();
  16.     glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Clear the background of our window to ?
  17.     glClear (GL_COLOR_BUFFER_BIT); //Clear the colour buffer (more buffers later on)
  18.     glLoadIdentity();  // Load the Identity Matrix to reset our drawing locations
  19.     glTranslatef(0.0f, 0.0f, -5.0f); // Push eveything 5 units back into the scene,
  20.                                     //otherwise we won't see the primitive
  21.     cube();
  22.     glFlush(); // Flush the OpenGL buffers to the window
  23.     angle++; //update the angle of rotation
  24. }
  25.  
  26. void reshape (int width, int height) {
  27.     glViewport(0, 0, (GLsizei)width, (GLsizei)height); // Set our viewport to the size of our window
  28.     glMatrixMode(GL_PROJECTION); // Switch to the projection matrix so that we
  29.                                 // can manipulate how our scene is viewed
  30.     glLoadIdentity(); // Reset the projection matrix to the identity matrix so that we
  31.                         // don't get any artifacts (cleaning up)
  32.     gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100.0); // Set the Field of view angle
  33.                             // (in degrees), the aspect ratio of our window, and the new and far planes  
  34.     glMatrixMode(GL_MODELVIEW); // Switch back to the model view matrix, so that we
  35.                                 // can start drawing shapes correctly
  36. }
  37.  
  38. int main (int argc, char **argv) {
  39.     glutInit(&argc, argv); // Initialize GLUT
  40.     glutInitDisplayMode (GLUT_SINGLE); // Set up a basic display buffer (only single buffered for now)
  41.     glutInitWindowSize (500, 500); // Set the width and height of the window  
  42.     glutInitWindowPosition (100, 100); // Set the position of the window
  43.     glutCreateWindow ("Hello, World!"); // Set the title for the window
  44.    
  45.     glutDisplayFunc(display); // Tell GLUT to use the method "display" for rendering
  46.     glutIdleFunc (display); //change any idle values accordingly
  47.     glutReshapeFunc(reshape); // Tell GLUT to use the method "reshape" for rendering
  48.    
  49.     glutKeyboardFunc(keyPressed); // Tell GLUT to use the method "keyPressed" for key presses
  50.     glutKeyboardUpFunc(keyUp); // Tell GLUT to use the method "keyUp" for key up events
  51.     glutSpecialFunc(keySpecial); // Tell GLUT to use the method "keySpecial" for special key presses
  52.     glutSpecialUpFunc(keySpecialUp); // Tell GLUT to use the method "keySpecialUp" for special up key events
  53.  
  54.     glutMainLoop();
  55. }
  56. main.h
  57. #ifndef MAIN_H
  58. #define MAIN_H
  59.  
  60. #include "GL/glew.h"
  61. #include "GL/freeglut.h"
  62.  
  63. #include "keyOps.h"
  64.  
  65. #endif
  66. keyOps.cpp
  67. #include "keyOps.h"
  68.  
  69. bool* keyStates = new bool[256]; // Create an array of boolean values of length 256 (0-255)
  70. bool* keySpecialStates = new bool[246]; // Create an array of boolean values of length 256 (0-255)
  71.  
  72. void keyOperations () {  
  73.     if (keyStates['a']) { // If the 'a' key has been pressed  
  74.         //cout << "Test!" << endl;
  75.     }
  76. }
  77.  
  78. void keySpecialOperations() {  
  79.     if (keySpecialStates[GLUT_KEY_LEFT]) { // If the left arrow key has been pressed  
  80.         //cout << "Test!" << endl;
  81.     }  
  82. }
  83.  
  84. void keyPressed (unsigned char key, int x, int y) {
  85.     keyStates[key] = true; // Set the state of the current key to pressed
  86. }
  87.  
  88. void keyUp (unsigned char key, int x, int y) {  
  89.     keyStates[key] = false; // Set the state of the current key to not pressed
  90. }  
  91.  
  92. void keySpecial (int key, int x, int y) {  
  93.     keySpecialStates[key] = true; // Set the state of the current key to pressed
  94. }  
  95.  
  96. void keySpecialUp (int key, int x, int y) {  
  97.     keySpecialStates[key] = false; // Set the state of the current key to not pressed
  98. }  
  99. keyOps.h
  100. #ifndef KEYOPS_H
  101. #define KEYOPS_H
  102.  
  103. #include "GL/glew.h"
  104. #include "GL/freeglut.h"
  105. #include <iostream>
  106. using namespace std;
  107.  
  108. //PROTOTYPES
  109. void keyOperations ();
  110. void keySpecialOperations();
  111. void keyPressed (unsigned char key, int x, int y);
  112. void keyUp (unsigned char key, int x, int y);
  113. void keySpecial (int key, int x, int y);
  114. void keySpecialUp (int key, int x, int y);
  115.  
  116. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement