Don't like ads? PRO users don't see any ads ;-)
Guest

Toggle glPolygonMode( ..., GL_FILL/GL_LINE) [w/GLUT]

By: a guest on Mar 25th, 2012  |  syntax: C++  |  size: 4.21 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //      ------------------------------------------------------ #include
  2. #ifdef MacOSX
  3.         #include <GLUT/glut.h>
  4. #else
  5.         #include <GL/glut.h>
  6. #endif
  7.  
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <iostream>
  11.  
  12. using namespace std ;
  13.  
  14. //      ------------------------------------------------------ Global Vars
  15.  
  16. static int resX = 640 ;
  17. static int resY = 480 ;
  18. static GLenum fillMode = GL_FILL ;
  19.  
  20. /*
  21.         ------------------------------------------------------
  22.         **OnDraw() ;
  23.  
  24.         **This is where we draw all the stuff.
  25.         ------------------------------------------------------ OnDraw()
  26. */
  27. void OnDraw() {
  28.  
  29.         /* Clear screen and depth buffer. */
  30.         glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT ) ;
  31.  
  32.         /* clear the previous transform */
  33.         glLoadIdentity() ;
  34.  
  35.         /*
  36.                 Set the camera position.
  37.                 gluLookAt( eye3, aim3, up3 ) ;
  38.         */
  39.         gluLookAt(      1,1,10, 0,0,0, 0,1,0) ;
  40.  
  41.         /* Set shading mode - Wire Frame/Flat Shaded/etc. */
  42.         //glPolygonMode( GL_FRONT_AND_BACK, fillMode ) ;
  43.  
  44.         /* Draw the Utah teapot */
  45.         glutWireTeapot( 1 ) ;
  46.  
  47.         // currently we've been drawing to the back buffer, we need
  48.         // to swap the back buffer with the front one to make the image visible
  49.         glutSwapBuffers();
  50. }
  51.  
  52. /*
  53.         **OnReshape() ;
  54.  
  55.         **Window resize and related stuff is handeled here.
  56.         ------------------------------------------------------ OnReshape()
  57. */
  58. void OnReshape( int w, int h ) {
  59.         /* Thou shalt not divide by zero! */
  60.         if ( h == 0 ) {
  61.                 h = 1 ;
  62.         }
  63.  
  64.         /* Set the drawable region of the window. */
  65.         glViewport( 0, 0, w, h) ;
  66.  
  67.         /* Set the projection matrix. */
  68.         glMatrixMode( GL_PROJECTION ) ;
  69.         glLoadIdentity() ;
  70.  
  71.         /*
  72.                 Use perspective camera.
  73.                 gluPersp...( fov, aspect, near, far ) ;
  74.         */
  75.         gluPerspective( 45, (float) w/h, 0.1, 100);
  76.  
  77.         /* Set modelview matrix. */
  78.         glMatrixMode( GL_MODELVIEW ) ;
  79. }
  80.  
  81. /*
  82.         ------------------------------------------------------
  83.         **OnKeyboard() ;
  84.  
  85.         **All Keyboard interaction is handeled here.
  86.         ------------------------------------------------------ OnKeyboard()
  87. */
  88. void OnKeyboard( unsigned char key, int x, int y ) {
  89.  
  90.         switch( key ) {
  91.                 case 27 :                       /* 27 is the code for ESC. */
  92.                         cout << "ESC was pressed" << endl ;
  93.                         exit( 0 ) ;             /* Quit applicatio.n */
  94.                         break ;
  95.  
  96.                 case 'w' :                      /* Toggle wire-frame/flat-shade. */
  97.                         cout << "w was pressed" << endl ;
  98.                         fillMode = !fillMode ;
  99.                 glPolygonMode (GL_FRONT_AND_BACK, fillMode ? GL_FILL : GL_LINE) ;
  100.                         break ;
  101.  
  102.                 case 'f' :                      /* Translate camera forward. */
  103.                         cout << "f was pressed" << endl ;
  104.                         break ;
  105.  
  106.                 case 'b' :                      /* Translate camera backward. */
  107.                         cout << "b was pressed" << endl ;
  108.                         break ;
  109.  
  110.                 case 'l' :                      /* Rotate camera left. */
  111.                         cout << "l was pressed" << endl ;
  112.                         break ;
  113.  
  114.                 case 'r' :                      /* Rotate camera right. */
  115.                         cout << "r was pressed" << endl ;
  116.                         break ;
  117.  
  118.                 default:
  119.                         /* Do nothing. */
  120.                         break ;
  121.         }
  122.  
  123.         glutPostRedisplay() ;
  124. }
  125.  
  126. /*
  127.         ------------------------------------------------------ OnInit()
  128. */
  129. void OnInit() {
  130.         /* Enable dept testing */
  131.         glEnable( GL_DEPTH_TEST ) ;
  132. }
  133.  
  134. /*
  135.         ------------------------------------------------------ OnExit()
  136. */
  137. void OnExit() {
  138.         /* Print exit message */
  139.         cout << "Have a nice day :)" << endl ;
  140. }
  141.  
  142.  
  143. /*
  144.         ------------------------------------------------------ main()
  145. */
  146. int main( int argc, char* argv[] ) {
  147.  
  148.         /* ------------- Initialisation ------------- */
  149.         /* initialise glut */
  150.         glutInit( &argc, argv ) ;
  151.  
  152.         /* Set display mode. */
  153.         glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH) ;
  154.  
  155.         /* Set polygon mode. */
  156.         glPolygonMode( GL_FRONT_AND_BACK, GL_FILL ) ;
  157.         /* Initial window size. */
  158.         glutInitWindowSize( resX, resY ) ;
  159.  
  160.         /* Create Window */
  161.         glutCreateWindow(" a1214415::assign1 ");
  162.  
  163.  
  164.         /* --------------- Call Backs --------------- */
  165.         /* Set function for drawing. */
  166.         glutDisplayFunc( OnDraw ) ;
  167.  
  168.         /* Set function for screen resize. */
  169.         glutReshapeFunc( OnReshape ) ;
  170.  
  171.         /* Pass keyboad function to GLUT */
  172.         glutKeyboardFunc( OnKeyboard ) ;
  173.  
  174.  
  175.         /* --------------- Prog. Init. -------------- */
  176.         /* Use custom OnInit(). */
  177.         OnInit() ;
  178.  
  179.         /* Call this function on exit. */
  180.         atexit(OnExit) ;
  181.  
  182.         /* ------------------- GO! ------------------ */
  183.         /* Start the main while loop. */
  184.         glutMainLoop() ;
  185.  
  186.         return 0 ;
  187.  
  188. }