Advertisement
WeltEnSTurm

Untitled

Aug 23rd, 2011
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.05 KB | None | 0 0
  1. /* Permission is hereby granted, free of charge, to any person obtaining a copy
  2.  * of this software and associated documentation files (the "Software"), to deal
  3.  * in the Software without restriction, including without limitation the rights
  4.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  5.  * copies of the Software, and to permit persons to whom the Software is
  6.  * furnished to do so, subject to the following conditions:
  7.  *
  8.  * The above notice and this permission notice shall be included in all copies
  9.  * or substantial portions of the Software.
  10.  *
  11.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17.  * SOFTWARE.
  18.  */
  19. /* File for "Basic Shapes" lesson of the OpenGL tutorial on
  20.  * www.videotutorialsrock.com
  21.  */
  22.  
  23.  
  24.  
  25. #include <iostream>
  26. #include <stdlib.h> //Needed for "exit" function
  27.  
  28. //Include OpenGL header files, so that we can use OpenGL
  29. #ifdef __APPLE__
  30. #include <OpenGL/OpenGL.h>
  31. #include <GLUT/glut.h>
  32. #else
  33. #include <GL/glut.h>
  34. #endif
  35.  
  36. using namespace std;
  37.  
  38. //Called when a key is pressed
  39. void handleKeypress(unsigned char key, //The key that was pressed
  40.                     int x, int y) {    //The current mouse coordinates
  41.     switch (key) {
  42.         case 27: //Escape key
  43.             exit(0); //Exit the program
  44.     }
  45. }
  46.  
  47. //Initializes 3D rendering
  48. void initRendering() {
  49.     //Makes 3D drawing work when something is in front of something else
  50.     glEnable(GL_DEPTH_TEST);
  51. }
  52.  
  53. //Called when the window is resized
  54. void handleResize(int w, int h) {
  55.     //Tell OpenGL how to convert from coordinates to pixel values
  56.     glViewport(0, 0, w, h);
  57.    
  58.     glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective
  59.    
  60.     //Set the camera perspective
  61.     glLoadIdentity(); //Reset the camera
  62.     gluPerspective(45.0,                  //The camera angle
  63.                    (double)w / (double)h, //The width-to-height ratio
  64.                    1.0,                   //The near z clipping coordinate
  65.                    200.0);                //The far z clipping coordinate
  66. }
  67.  
  68. //Draws the 3D scene
  69. void drawScene() {
  70.     //Clear information from last draw
  71.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  72.    
  73.     glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
  74.     glLoadIdentity(); //Reset the drawing perspective
  75.    
  76.     glBegin(GL_QUADS); //Begin quadrilateral coordinates
  77.    
  78.     //Trapezoid
  79.     glVertex3f(-0.7f, -1.5f, -5.0f);
  80.     glVertex3f(0.7f, -1.5f, -5.0f);
  81.     glVertex3f(0.4f, -0.5f, -5.0f);
  82.     glVertex3f(-0.4f, -0.5f, -5.0f);
  83.    
  84.     glEnd(); //End quadrilateral coordinates
  85.    
  86.     glBegin(GL_TRIANGLES); //Begin triangle coordinates
  87.    
  88.     //Pentagon
  89.     glVertex3f(0.5f, 0.5f, -5.0f);
  90.     glVertex3f(1.5f, 0.5f, -5.0f);
  91.     glVertex3f(0.5f, 1.0f, -5.0f);
  92.    
  93.     glVertex3f(0.5f, 1.0f, -5.0f);
  94.     glVertex3f(1.5f, 0.5f, -5.0f);
  95.     glVertex3f(1.5f, 1.0f, -5.0f);
  96.    
  97.     glVertex3f(0.5f, 1.0f, -5.0f);
  98.     glVertex3f(1.5f, 1.0f, -5.0f);
  99.     glVertex3f(1.0f, 1.5f, -5.0f);
  100.    
  101.     //Triangle
  102.     glVertex3f(-0.5f, 0.5f, -5.0f);
  103.     glVertex3f(-1.0f, 1.5f, -5.0f);
  104.     glVertex3f(-1.5f, 0.5f, -5.0f);
  105.    
  106.     glEnd(); //End triangle coordinates
  107.    
  108.     glutSwapBuffers(); //Send the 3D scene to the screen
  109. }
  110.  
  111. int main(int argc, char** argv) {
  112.     //Initialize GLUT
  113.     glutInit(&argc, argv);
  114.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  115.     glutInitWindowSize(400, 400); //Set the window size
  116.    
  117.     //Create the window
  118.     glutCreateWindow("Basic Shapes - videotutorialsrock.com");
  119.     initRendering(); //Initialize rendering
  120.    
  121.     //Set handler functions for drawing, keypresses, and window resizes
  122.     glutDisplayFunc(drawScene);
  123.     glutKeyboardFunc(handleKeypress);
  124.     glutReshapeFunc(handleResize);
  125.    
  126.     glutMainLoop(); //Start the main loop.  glutMainLoop doesn't return.
  127.     return 0; //This line is never reached
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement