Advertisement
Guest User

GLFW GLEW OpenGL Spinning Cube

a guest
Aug 23rd, 2012
795
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.32 KB | None | 0 0
  1. /* Demo.cpp
  2.  * A simple OpenGL program using GLUT.  It creates a blank black
  3.  * window which can be used as a starting point for OpenGL programming.
  4.  *
  5.  * Link in opengl32, glu32, and glut32 libraries and make sure to include
  6.  * windows.h if you are compiling with Eclipse in Windows.
  7.  *
  8.  * Author: Paul Solt  3-4-07
  9.  * Based on examples from the Red book OpenGL Programming Guide
  10.  */
  11. #include <GL/glew.h>
  12. #include <GL/glfw.h>
  13. #include <stdlib.h>
  14. #include <iostream>
  15.  
  16. using namespace std;
  17.  
  18. /*
  19. #ifndef GLEW_STATIC
  20. #define GLEW_STATIC
  21. #endif
  22. */
  23.  
  24. const int WIDTH = 600;
  25. const int HEIGHT = 480;
  26.  
  27. /* Definitions */
  28.  
  29. /////////////////////////////////////////////////////////////////////////////////////////////////
  30. //                                      THE OPENGL INIT
  31. /////////////////////////////////////////////////////////////////////////////////////////////////
  32. int InitGL( )                                       // All Setup For OpenGL Goes Here
  33. {
  34.     glShadeModel(GL_SMOOTH);                            // Enable Smooth Shading
  35.     glClearColor(0.0f, 0.0f, 0.1f, 0.5f);               // Black Background
  36.     glClearDepth(1.0f);                                 // Depth Buffer Setup
  37.     glEnable(GL_DEPTH_TEST);                            // Enables Depth Testing
  38.     glDepthFunc(GL_LEQUAL);                             // The Type Of Depth Testing To Do
  39.     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);  // Really Nice Perspective Calculations
  40.     return 1;                                           // Initialization Went OK
  41. }
  42.  
  43. void draw_triangle(){
  44.     glTranslatef(0.0f, 0.0f, -6.0f);        // Translate Into The Screen
  45.         glBegin(GL_TRIANGLES);          // Start Drawing A Triangle
  46.         glColor3f( 1.0f,0.0f,0.0f);     // Red Color
  47.         glVertex3f(0.0f, 1.0f, 0.0f);   // Top Color
  48.         glColor3f(0.0f,1.0f,0.0f);      // Green
  49.         glVertex3f(-1.0f,-1.0f, 0.0f);  // Bottom Left Vertex
  50.         glColor3f(0.0f,0.0f,1.0f);      // Blue Color
  51.         glVertex3f( 1.0f,-1.0f, 0.0f);  // Bottom Right Vertex
  52.         glEnd();                // Finished Drawing The Triangle
  53.  
  54.  
  55. }
  56.  
  57. float rotqube=0;
  58. void draw_cube(){
  59.  
  60.         // Reset The Current Modelview Matrix
  61.         glLoadIdentity();
  62.  
  63.     //NEW//////////////////NEW//////////////////NEW//////////////////NEW/////////////
  64.  
  65.       glTranslatef(0.0f, 0.0f,-7.0f);   // Translate Into The Screen 7.0 Units
  66.       glRotatef(rotqube,0.0f,1.0f,0.0f);    // Rotate The cube around the Y axis
  67.       glRotatef(rotqube,1.0f,1.0f,1.0f);
  68.       glBegin(GL_QUADS);        // Draw The Cube Using quads
  69.         glColor3f(0.0f,1.0f,0.0f);  // Color Blue
  70.         glVertex3f( 1.0f, 1.0f,-1.0f);  // Top Right Of The Quad (Top)
  71.         glVertex3f(-1.0f, 1.0f,-1.0f);  // Top Left Of The Quad (Top)
  72.         glVertex3f(-1.0f, 1.0f, 1.0f);  // Bottom Left Of The Quad (Top)
  73.         glVertex3f( 1.0f, 1.0f, 1.0f);  // Bottom Right Of The Quad (Top)
  74.         glColor3f(1.0f,0.5f,0.0f);  // Color Orange
  75.         glVertex3f( 1.0f,-1.0f, 1.0f);  // Top Right Of The Quad (Bottom)
  76.         glVertex3f(-1.0f,-1.0f, 1.0f);  // Top Left Of The Quad (Bottom)
  77.         glVertex3f(-1.0f,-1.0f,-1.0f);  // Bottom Left Of The Quad (Bottom)
  78.         glVertex3f( 1.0f,-1.0f,-1.0f);  // Bottom Right Of The Quad (Bottom)
  79.         glColor3f(1.0f,0.0f,0.0f);  // Color Red
  80.         glVertex3f( 1.0f, 1.0f, 1.0f);  // Top Right Of The Quad (Front)
  81.         glVertex3f(-1.0f, 1.0f, 1.0f);  // Top Left Of The Quad (Front)
  82.         glVertex3f(-1.0f,-1.0f, 1.0f);  // Bottom Left Of The Quad (Front)
  83.         glVertex3f( 1.0f,-1.0f, 1.0f);  // Bottom Right Of The Quad (Front)
  84.         glColor3f(1.0f,1.0f,0.0f);  // Color Yellow
  85.         glVertex3f( 1.0f,-1.0f,-1.0f);  // Top Right Of The Quad (Back)
  86.         glVertex3f(-1.0f,-1.0f,-1.0f);  // Top Left Of The Quad (Back)
  87.         glVertex3f(-1.0f, 1.0f,-1.0f);  // Bottom Left Of The Quad (Back)
  88.         glVertex3f( 1.0f, 1.0f,-1.0f);  // Bottom Right Of The Quad (Back)
  89.         glColor3f(0.0f,0.0f,1.0f);  // Color Blue
  90.         glVertex3f(-1.0f, 1.0f, 1.0f);  // Top Right Of The Quad (Left)
  91.         glVertex3f(-1.0f, 1.0f,-1.0f);  // Top Left Of The Quad (Left)
  92.         glVertex3f(-1.0f,-1.0f,-1.0f);  // Bottom Left Of The Quad (Left)
  93.         glVertex3f(-1.0f,-1.0f, 1.0f);  // Bottom Right Of The Quad (Left)
  94.         glColor3f(1.0f,0.0f,1.0f);  // Color Violet
  95.         glVertex3f( 1.0f, 1.0f,-1.0f);  // Top Right Of The Quad (Right)
  96.         glVertex3f( 1.0f, 1.0f, 1.0f);  // Top Left Of The Quad (Right)
  97.         glVertex3f( 1.0f,-1.0f, 1.0f);  // Bottom Left Of The Quad (Right)
  98.         glVertex3f( 1.0f,-1.0f,-1.0f);  // Bottom Right Of The Quad (Right)
  99.       glEnd();          // End Drawing The Cube
  100.  
  101.       rotqube +=0.9f;           // Increase Angle
  102. }
  103.  
  104.  
  105. /////////////////////////////////////////////////////////////////////////////////////////////////
  106. //                                      THE RESIZE GL SCENE
  107. /////////////////////////////////////////////////////////////////////////////////////////////////
  108. GLvoid ReSizeGLScene(GLsizei width, GLsizei height)     // Resize And Initialize The GL Window
  109. {
  110.     if (height==0)                                      // Prevent A Divide By Zero By
  111.     {
  112.         height=1;                                       // Making Height Equal One
  113.     }
  114.  
  115.     glViewport(0,0,width,height);                       // Reset The Current Viewport
  116.  
  117.     glMatrixMode(GL_PROJECTION);                        // Select The Projection Matrix
  118.     glLoadIdentity();                                   // Reset The Projection Matrix
  119.  
  120.     // Calculate The Aspect Ratio Of The Window
  121.     gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
  122.  
  123.     glMatrixMode(GL_MODELVIEW);                         // Select The Modelview Matrix
  124.     glLoadIdentity();                                   // Reset The Modelview Matrix
  125. }
  126.  
  127.  
  128. /* The main function */
  129. int main( int argc, char *argv[] ) {
  130.  
  131.     int running = GL_TRUE;
  132.  
  133.     //  Initialize GLEW.
  134.     GLenum err = glewInit();
  135.     if (GLEW_OK != err)
  136.     {
  137.       /* Problem: glewInit failed, something is seriously wrong. */
  138.         cout << "Error: " << glewGetErrorString(err) << endl;
  139.     }
  140.     cout << "Status: Using GLEW " <<  glewGetString(GLEW_VERSION) << endl;
  141.  
  142.     // Initialize GLFW
  143.     if( !glfwInit() )
  144.     {
  145.     exit( EXIT_FAILURE );
  146.     }
  147.     // Open an OpenGL window
  148.     if( !glfwOpenWindow( WIDTH,HEIGHT, 0,0,0,0,0,0, GLFW_WINDOW ) )
  149.     {
  150.     glfwTerminate();
  151.     exit( EXIT_FAILURE );
  152.     }
  153.  
  154.     //  Initialize OpenGL.
  155.     InitGL();
  156.  
  157.     //  Set the callback for when the window is resized.
  158.     glfwSetWindowSizeCallback( ReSizeGLScene );
  159.  
  160.     // Main loop
  161.     while( running )
  162.     {
  163.     // OpenGL rendering goes here...
  164.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  165.  
  166.         draw_cube();
  167.         //draw_triangle();
  168.  
  169.         // Swap front and back rendering buffers
  170.         glfwSwapBuffers();
  171.         // Check if ESC key was pressed or window was closed
  172.         running = !glfwGetKey( GLFW_KEY_ESC ) &&
  173.         glfwGetWindowParam( GLFW_OPENED );
  174.     }
  175.     // Close window and terminate GLFW
  176.     glfwTerminate();
  177.     // Exit program
  178.     exit( EXIT_SUCCESS );
  179.  
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement