home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / qt3_emx.zip / examples / demo / opengl / glbox.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-11  |  3.0 KB  |  134 lines

  1. /****************************************************************************
  2. ** $Id:  qt/glbox.cpp   3.0.0   edited Jun 1 18:44 $
  3. **
  4. ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
  5. **
  6. ** This file is part of an example program for Qt.  This example
  7. ** program may be used, distributed and modified without limitation.
  8. **
  9. *****************************************************************************/
  10.  
  11. /****************************************************************************
  12. **
  13. ** This is a simple QGLWidget displaying an openGL wireframe box
  14. **
  15. ** The OpenGL code is mostly borrowed from Brian Pauls "spin" example
  16. ** in the Mesa distribution
  17. **
  18. ****************************************************************************/
  19.  
  20. #include "glbox.h"
  21.  
  22. #if defined(Q_CC_MSVC)
  23. #pragma warning(disable:4305) // init: truncation from const double to float
  24. #endif
  25.  
  26. /*!
  27.   Create a GLBox widget
  28. */
  29.  
  30. GLBox::GLBox( QWidget* parent, const char* name, WFlags f )
  31.     : GLControlWidget( parent, name, 0, f )
  32. {
  33.     setAnimationDelay( -1 );
  34.     xRot = yRot = zRot = 0.0;        // default object rotation
  35.     scale = 1.25;            // default object scale
  36.     object = 0;
  37. }
  38.  
  39.  
  40. /*!
  41.   Release allocated resources
  42. */
  43.  
  44. GLBox::~GLBox()
  45. {
  46.     makeCurrent();
  47.     glDeleteLists( object, 1 );
  48. }
  49.  
  50.  
  51. /*!
  52.   Paint the box. The actual openGL commands for drawing the box are
  53.   performed here.
  54. */
  55.  
  56. void GLBox::paintGL()
  57. {
  58.     glClear( GL_COLOR_BUFFER_BIT );
  59.  
  60.     glLoadIdentity();
  61.     transform();
  62.     glCallList( object );
  63. }
  64.  
  65.  
  66. /*!
  67.   Set up the OpenGL rendering state, and define display list
  68. */
  69.  
  70. void GLBox::initializeGL()
  71. {
  72.     qglClearColor( black );         // Let OpenGL clear to black
  73.     object = makeObject();        // Generate an OpenGL display list
  74.     glShadeModel( GL_FLAT );
  75. }
  76.  
  77.  
  78.  
  79. /*!
  80.   Set up the OpenGL view port, matrix mode, etc.
  81. */
  82.  
  83. void GLBox::resizeGL( int w, int h )
  84. {
  85.     glViewport( 0, 0, (GLint)w, (GLint)h );
  86.     glMatrixMode( GL_PROJECTION );
  87.     glLoadIdentity();
  88.     glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 15.0 );
  89.     glMatrixMode( GL_MODELVIEW );
  90. }
  91.  
  92.  
  93. /*!
  94.   Generate an OpenGL display list for the object to be shown, i.e. the box
  95. */
  96.  
  97. GLuint GLBox::makeObject()
  98. {    
  99.     GLuint list;
  100.  
  101.     list = glGenLists( 1 );
  102.  
  103.     glNewList( list, GL_COMPILE );
  104.  
  105.     qglColor( white );              // Shorthand for glColor3f or glIndex
  106.  
  107.     glLineWidth( 2.0 );
  108.  
  109.     glBegin( GL_LINE_LOOP );
  110.     glVertex3f(  1.0,  0.5, -0.4 );
  111.     glVertex3f(  1.0, -0.5, -0.4 );
  112.     glVertex3f( -1.0, -0.5, -0.4 );
  113.     glVertex3f( -1.0,  0.5, -0.4 );
  114.     glEnd();
  115.  
  116.     glBegin( GL_LINE_LOOP );
  117.     glVertex3f(  1.0,  0.5, 0.4 );
  118.     glVertex3f(  1.0, -0.5, 0.4 );
  119.     glVertex3f( -1.0, -0.5, 0.4 );
  120.     glVertex3f( -1.0,  0.5, 0.4 );
  121.     glEnd();
  122.  
  123.     glBegin( GL_LINES );
  124.     glVertex3f(  1.0,  0.5, -0.4 );   glVertex3f(  1.0,  0.5, 0.4 );
  125.     glVertex3f(  1.0, -0.5, -0.4 );   glVertex3f(  1.0, -0.5, 0.4 );
  126.     glVertex3f( -1.0, -0.5, -0.4 );   glVertex3f( -1.0, -0.5, 0.4 );
  127.     glVertex3f( -1.0,  0.5, -0.4 );   glVertex3f( -1.0,  0.5, 0.4 );
  128.     glEnd();
  129.  
  130.     glEndList();
  131.  
  132.     return list;
  133. }
  134.