Advertisement
xomidar

004_olympic_logo

Mar 4th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.85 KB | None | 0 0
  1. /*
  2.     Lab Assignment: Olympic Logo
  3.     Date:           February 22, 2015
  4. */
  5. #include "stdafx.h"
  6. #include <iostream>
  7. #include <glut.h>  // GLUT, include glu.h and gl.h
  8.  
  9. using namespace std;
  10.  
  11. /* Global variables */
  12. char title[] = "Olympic Logo";
  13.  
  14. /* Initialize OpenGL Graphics */
  15. void initGL()
  16. {
  17.    glClearColor(0.40f, 0.612f, 0.94f, 1.0f); // Set background color to white and opaque
  18.    glClearDepth(1.0f);                   // Set background depth to farthest
  19.    glEnable(GL_DEPTH_TEST);   // Enable depth testing for z-culling
  20.    glDepthFunc(GL_LEQUAL);    // Set the type of depth-test
  21.    glShadeModel(GL_SMOOTH);   // Enable smooth shading
  22.    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);  // Nice perspective corrections
  23. }
  24.  
  25. /* Handler for window-repaint event. Called back when the window first appears and
  26.    whenever the window needs to be re-painted. */
  27. void display()
  28. {
  29.    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth buffers
  30.    glMatrixMode(GL_MODELVIEW);     // To operate on model-view matrix
  31.  
  32.    // Render a color-cube consisting of 6 quads with different colors
  33.    glLoadIdentity();                 // Reset the model-view matrix
  34.    glTranslatef(-1.5f, 1.0f, -7.0f);  // Move right and into the screen
  35.  
  36.    // Your code here...
  37.    const int totalNumberOfRing = 5;
  38.    int breakingPoint = ceil(((float) totalNumberOfRing) / 2);
  39.  
  40.    float innerRadius = 0.05f,
  41.          outerRadius = 0.50f,
  42.          distance = (outerRadius / 100) * 60,
  43.          x = 0.0f,
  44.          y = 0.0f;
  45.  
  46.    float colors[totalNumberOfRing][3] = {
  47.                                             {0, 0, 1},
  48.                                             {0, 0, 0},
  49.                                             {1, 0, 0},
  50.                                             {1, 1, 0},
  51.                                             {0, 1, 0}
  52.                                        };
  53.  
  54.    for (int ringIndex = 1; ringIndex <= totalNumberOfRing; ringIndex++)
  55.    {
  56.         glTranslatef(x, y, 0.0f);
  57.         glColor3f(colors[ringIndex - 1][0], colors[ringIndex - 1][1], colors[ringIndex - 1][2]);
  58.         glutSolidTorus(0.05f, 0.50f, 32, 32);
  59.  
  60.         if (ringIndex == breakingPoint)
  61.         {
  62.             y -= (outerRadius * 2) - distance;
  63.             x -= (ringIndex * (outerRadius * 2) - distance) - (outerRadius * 2);
  64.         }
  65.         else
  66.         {
  67.             y = 0;
  68.             x = (outerRadius * 2) - distance;
  69.         }
  70.    }
  71.  
  72.    glutSwapBuffers();  // Swap the front and back frame buffers (double buffering)
  73. }
  74.  
  75. /* Handler for window re-size event. Called back when the window first appears and
  76.    whenever the window is re-sized with its new width and height */
  77. void reshape(GLsizei width, GLsizei height)
  78. {  // GLsizei for non-negative integer
  79.    // Compute aspect ratio of the new window
  80.    if (height == 0)
  81.    {
  82.        height = 1;                // To prevent divide by 0
  83.    }
  84.  
  85.    GLfloat aspect = (GLfloat)width / (GLfloat)height;
  86.  
  87.    // Set the viewport to cover the new window
  88.    glViewport(0, 0, width, height);
  89.  
  90.    // Set the aspect ratio of the clipping volume to match the viewport
  91.    glMatrixMode(GL_PROJECTION);  // To operate on the Projection matrix
  92.    glLoadIdentity();             // Reset
  93.    // Enable perspective projection with fovy, aspect, zNear and zFar
  94.    gluPerspective(45.0f, aspect, 0.1f, 100.0f);
  95. }
  96.  
  97. /* Main function: GLUT runs as a console application starting at main() */
  98. int main(int argc, char** argv)
  99. {
  100.    glutInit(&argc, argv);            // Initialize GLUT
  101.    glutInitDisplayMode(GLUT_DOUBLE); // Enable double buffered mode
  102.    glutInitWindowSize(640, 480);   // Set the window's initial width & height
  103.    glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
  104.    glutCreateWindow(title);          // Create window with the given title
  105.    glutDisplayFunc(display);       // Register callback handler for window re-paint event
  106.    glutReshapeFunc(reshape);       // Register callback handler for window re-size event
  107.    initGL();                       // Our own OpenGL initialization
  108.    glutMainLoop();                 // Enter the infinite event-processing loop
  109.    return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement