Guest User

FreeGLUT Spinning Cube

a guest
Aug 23rd, 2012
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.97 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 <windows.h>
  12. #include <GL/glut.h>
  13.  
  14. const int WIDTH = 600;
  15. const int HEIGHT = 480;
  16.  
  17. /* Prototypes */
  18. void init();
  19. void display();
  20. GLvoid ReSizeGLScene(GLsizei width, GLsizei height);
  21.  
  22. /* Definitions */
  23.  
  24. /////////////////////////////////////////////////////////////////////////////////////////////////
  25. //                                      THE OPENGL INIT
  26. /////////////////////////////////////////////////////////////////////////////////////////////////
  27. int InitGL( )                                       // All Setup For OpenGL Goes Here
  28. {
  29.     glShadeModel(GL_SMOOTH);                            // Enable Smooth Shading
  30.     glClearColor(0.0f, 0.0f, 0.1f, 0.5f);               // Black Background
  31.     glClearDepth(1.0f);                                 // Depth Buffer Setup
  32.     glEnable(GL_DEPTH_TEST);                            // Enables Depth Testing
  33.     glDepthFunc(GL_LEQUAL);                             // The Type Of Depth Testing To Do
  34.     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);  // Really Nice Perspective Calculations
  35.     return TRUE;                                        // Initialization Went OK
  36. }
  37.  
  38. GLvoid IdleFunc( )
  39. {
  40.     glutPostRedisplay();
  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. /* Initializes the OpenGL state */
  129. void init() {
  130.     InitGL();
  131. }
  132.  
  133. /* Displays a black clear screen */
  134. void display() {
  135.     // Clear Screen And Depth Buffer
  136.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  137.  
  138.     draw_cube();
  139.  
  140.     glutSwapBuffers(); /* Double buffering */
  141. }
  142.  
  143. /* The main function */
  144. int main( int argc, char *argv[] ) {
  145.     /* Glut setup function calls */
  146.     glutInit( &argc, argv );
  147.     glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB ); /* Use double buffering and RGB colors */
  148.     glutInitWindowPosition( 100, 100 );
  149.     glutInitWindowSize( WIDTH, HEIGHT );
  150.     glutCreateWindow( argv[0] );
  151.     init();
  152.     glutDisplayFunc( display );  /* Call back display function */
  153.     glutReshapeFunc( ReSizeGLScene );
  154.     glutIdleFunc( IdleFunc );
  155.     glutMainLoop(); /* Continue drawing the scene */
  156.     return 0;
  157. }
Add Comment
Please, Sign In to add comment