CodeCodeCode

OpenGL Cube Render

Jan 7th, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.07 KB | None | 0 0
  1. #include "C:/Program Files (x86)/Microsoft Visual Studio 10.0/VC/include/GL/glew.h"
  2. #include "C:/Program Files (x86)/Microsoft Visual Studio 10.0/VC/include/GL/freeglut.h"
  3.  
  4. //Variables
  5. bool* KeyStates = new bool[256]; //Array of booleans for current key state
  6.                  // false = Not pressed
  7.                  // true = Pressed
  8. bool* SpecialKeyStates = new bool[256]; //Array of booleans for current special key state
  9.                     // false = Not pressed
  10.                     // true = Pressed
  11. bool CubeMovement = false; //Current cube movement direction
  12.                // false = down
  13.                // true = up
  14. float CubeVerticalLocation = 0.0f; //Location of Cube on y axis
  15. float CubeVerticalRotation = 0.0f; //Angle of rotation about y axis
  16.  
  17.  
  18. void Render_Primitive (void) {
  19.     //glEnable(GL_COLOR_MATERIAL); //For use with lightmaps
  20.  
  21.     //glBegin(GL_TRIANGLE_STRIP); // Start drawing a triangle strip primitive
  22.     //// The first triangle
  23.     //glVertex3f(-1.0f, -1.0f, 0.0f); // The bottom left corner
  24.     //glVertex3f(-1.0f, 1.0f, 0.0f); // The top left corner
  25.     //glVertex3f(1.0f, 1.0f, 0.0f); // The top right corner
  26.     //// The end of the second triangle
  27.     //glVertex3f(1.0f, -1.0f, 0.0f); // The bottom right corner
  28.     //glVertex3f(-1.0f, -1.0f, 0.0f); // The bottom left corner
  29.     //glEnd();
  30.  
  31.     //glBegin(GL_LINE_LOOP); // Start drawing a line primitive
  32.     //glVertex3f(-1.0f, -1.0f, 0.0f); // The bottom left corner
  33.     //glVertex3f(-1.0f, 1.0f, 0.0f); // The top left corner
  34.     //glVertex3f(1.0f, 1.0f, 0.0f); // The top right corner
  35.     //glVertex3f(1.0f, -1.0f, 0.0f); // The bottom right corner
  36.     //glEnd();
  37.  
  38.     //glPointSize(20.0f);
  39.     //glBegin(GL_POINTS); // Start drawing a point primitive
  40.     //glVertex3f(-1.0f, -1.0f, 0.0f); // The bottom left corner
  41.     //glVertex3f(-1.0f, 1.0f, 0.0f); // The top left corner
  42.     //glVertex3f(1.0f, 1.0f, 0.0f); // The top right corner
  43.     //glVertex3f(1.0f, -1.0f, 0.0f); // The bottom right corner
  44.     //glEnd();
  45.  
  46.     glBegin(GL_QUADS); //Start drawing square
  47.     glColor3f(0.0f, 1.0f, 0.0f); //Color vertex green
  48.     glVertex3f(-1.0f, -1.0f, 0.0f); //The bottom left corner
  49.     glColor3f(0.0f, 1.0f, 0.0f); //Color vertex green
  50.     glVertex3f(-1.0f, 1.0f, 0.0f); //The top left corner
  51.     glColor3f(0.0f, 0.0f, 1.0f);
  52.     glVertex3f(1.0f, 1.0f, 0.0f); //The top right corner
  53.     glColor3f(0.0f, 0.0f, 1.0f);
  54.     glVertex3f(1.0f, -1.0f, 0.0f); //The bottom right corner
  55.     glEnd();
  56. }
  57.  
  58. //Perform special key operations
  59. void Special_Key_Operations (void) {
  60.     if (SpecialKeyStates[GLUT_KEY_LEFT]) {
  61.         //stuff
  62.     }
  63. }
  64.  
  65. //Perform key operations
  66. void Key_Operations (void) {
  67.     if (KeyStates['a']) {
  68.         //stuff
  69.     }
  70. }
  71.  
  72. //Detect special key releases
  73. void Special_Key_Released (int key, int x, int y) {
  74.     SpecialKeyStates[key] = false; //Current special key is released
  75. }
  76.  
  77. //Detect key releases
  78. void Key_Released (unsigned char key, int x, int y) {
  79.     KeyStates[key] = false; //Current key is not pressed
  80. }
  81.  
  82. //Detect special key presses
  83. void Special_Key_Pressed (int key, int x, int y) {
  84.     SpecialKeyStates[key] = true; //Current special key is pressed
  85. }
  86.  
  87. //Detect key presses
  88. void Key_Pressed (unsigned char key, int x, int y) {
  89.     KeyStates[key] = true; //Current key is pressed
  90. }
  91.  
  92. //Control Window Reshape
  93. void Reshape (int width, int height) {
  94.     glViewport(0, 0, (GLsizei)width, (GLsizei)height); //Set viewport size
  95.     glMatrixMode(GL_PROJECTION); //Switch to projection matrix for scene render
  96.     glLoadIdentity(); //Reset projection matrix to identity
  97.  
  98.     //FOV, aspect ratio, render min, reder max
  99.     gluPerspective(60, ((GLfloat)width / (GLfloat)height), 1.0, 100.0);
  100.  
  101.     glMatrixMode(GL_MODELVIEW); //Switch back to model view matrix
  102. }
  103.  
  104. //Store drawing code
  105. void Display (void) {
  106.     Key_Operations(); //Get key processing
  107.  
  108.     //RGB ALPHA
  109.     glClearColor(1.0f, 0.0f, 0.0f, 1.0f); //Clear background of window
  110.     glClear(GL_COLOR_BUFFER_BIT); //Clear color buffer
  111.     glEnable(GL_BLEND); //Enable blending
  112.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //blend function
  113.     glLoadIdentity(); //Load Identity matrix to reset draw location
  114.  
  115.     glTranslatef(0.0f, 0.0f, -5.0f); //Push scene back 5 units along z
  116.     //Render_Primitive(); //Reder the primitive
  117.     glTranslatef(0.0f, CubeVerticalLocation, 0.0f); //translate vertically
  118.     glRotatef(CubeVerticalRotation, 0.0f, 1.0f, 0.0f); //Rotate object around
  119.     glColor4f(0.0f, 1.0f, 0.0f, 0.1f);
  120.     glScalef(2.0f, 0.5f, 1.0f);
  121.     glutSolidCube(2.0f);
  122.  
  123.     //glFlush(); //Flush OpenGL buffers to window
  124.     glutSwapBuffers(); //Swap the drawing buffer with the display buffer and flush contents
  125.  
  126.     if (CubeMovement == true) { //Cube moving up
  127.         CubeVerticalLocation -= 0.005f; //Move up
  128.     } else { //Cube moving down
  129.         CubeVerticalLocation += 0.005f; //Move down
  130.     }
  131.     if (CubeVerticalLocation < -3.0f) { //Gone too far up
  132.         CubeMovement = false; //Change direction to down
  133.     } else if (CubeVerticalLocation > 3.0f) { //GOne too far down
  134.         CubeMovement = true; //Change direction to up
  135.     }
  136.     CubeVerticalRotation += 0.010f; //Increment rotation
  137.     if (CubeVerticalRotation > 360.0f) { //Cube has rotated beyond 360
  138.         CubeVerticalRotation -= 360.0f; //restart counting
  139.     }
  140.  
  141.  
  142. }
  143.  
  144. int main (int argc, char **argv) {
  145.     //argc, argv required by GLUT; allow to add command line arguments
  146.  
  147.     glutInit(&argc, argv); //Initialize GLUT
  148.     //glutInitDisplayMode(GLUT_SINGLE); //Set up basic display buffer
  149.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); //Set up double buffering w/ alpha blending
  150.     glutInitWindowSize(500, 500); //Set width and height of window
  151.     glutInitWindowPosition(100,100); //Set Position of window
  152.     glutCreateWindow("You're first OpenGL Window"); //Set the title of the widow
  153.    
  154.     glutDisplayFunc(Display); //Tell GLUT to use Display method
  155.     glutIdleFunc(Display); //Tell GLUT to use Display as Idle method
  156.     glutReshapeFunc(Reshape); //Tell GLUT to use Reshape method
  157.     glutKeyboardFunc(Key_Pressed); //Tell GLUT to use Key_Pressed method
  158.     glutKeyboardUpFunc(Key_Released); //Tell GLUT to use Key_Released method
  159.     glutSpecialFunc(Special_Key_Pressed); //Tell GLUT to use Special_Key_Pressed method
  160.     glutSpecialUpFunc(Special_Key_Released); //Tell GLUT to use Special_Key_Released method
  161.  
  162.     glutMainLoop(); //Throw into graphics loop
  163. }
Advertisement
Add Comment
Please, Sign In to add comment