home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / qt3_emx.zip / examples / opengl / overlay_x11 / gearwidget.cpp next >
Encoding:
C/C++ Source or Header  |  2001-10-11  |  7.3 KB  |  270 lines

  1. /****************************************************************************
  2. ** $Id:  qt/gearwidget.cpp   3.0.0   edited Jul 30 23:08 $
  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. // A Qt OpenGL widget that draws a gear.
  12. //
  13. // Portions of this code has been borrowed from Brian Paul's Mesa
  14. // distribution.
  15. //
  16.  
  17. #include "gearwidget.h"
  18. #include <math.h>
  19. #if defined(Q_WS_X11)
  20. #include <X11/Xlib.h>
  21. #endif
  22.  
  23. #if defined(Q_CC_MSVC)
  24. #pragma warning(disable:4305) // init: truncation from const double to float
  25. #endif
  26.  
  27. /*
  28.  * Draw a gear wheel.  You'll probably want to call this function when
  29.  * building a display list since we do a lot of trig here.
  30.  *
  31.  * Input:  inner_radius - radius of hole at center
  32.  *       outer_radius - radius at center of teeth
  33.  *       width - width of gear
  34.  *       teeth - number of teeth
  35.  *       tooth_depth - depth of tooth
  36.  */
  37. static void gear( GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
  38.           GLint teeth, GLfloat tooth_depth )
  39. {
  40.     GLint i;
  41.     GLfloat r0, r1, r2;
  42.     GLfloat angle, da;
  43.     GLfloat u, v, len;
  44.  
  45.     r0 = inner_radius;
  46.     r1 = outer_radius - tooth_depth/2.0;
  47.     r2 = outer_radius + tooth_depth/2.0;
  48.  
  49.     const double pi = 3.14159264;
  50.     da = 2.0*pi / teeth / 4.0;
  51.  
  52.     glShadeModel( GL_FLAT );
  53.  
  54.     glNormal3f( 0.0, 0.0, 1.0 );
  55.  
  56.     /* draw front face */
  57.     glBegin( GL_QUAD_STRIP );
  58.     for (i=0;i<=teeth;i++) {
  59.     angle = i * 2.0*pi / teeth;
  60.     glVertex3f( r0*cos(angle), r0*sin(angle), width*0.5 );
  61.     glVertex3f( r1*cos(angle), r1*sin(angle), width*0.5 );
  62.     glVertex3f( r0*cos(angle), r0*sin(angle), width*0.5 );
  63.     glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), width*0.5 );
  64.     }
  65.     glEnd();
  66.  
  67.     /* draw front sides of teeth */
  68.     glBegin( GL_QUADS );
  69.     da = 2.0*pi / teeth / 4.0;
  70.     for (i=0;i<teeth;i++) {
  71.     angle = i * 2.0*pi / teeth;
  72.  
  73.     glVertex3f( r1*cos(angle),      r1*sin(angle),      width*0.5 );
  74.     glVertex3f( r2*cos(angle+da),   r2*sin(angle+da),      width*0.5 );
  75.     glVertex3f( r2*cos(angle+2*da), r2*sin(angle+2*da), width*0.5 );
  76.     glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), width*0.5 );
  77.     }
  78.     glEnd();
  79.  
  80.  
  81.     glNormal3f( 0.0, 0.0, -1.0 );
  82.  
  83.     /* draw back face */
  84.     glBegin( GL_QUAD_STRIP );
  85.     for (i=0;i<=teeth;i++) {
  86.     angle = i * 2.0*pi / teeth;
  87.     glVertex3f( r1*cos(angle), r1*sin(angle), -width*0.5 );
  88.     glVertex3f( r0*cos(angle), r0*sin(angle), -width*0.5 );
  89.     glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), -width*0.5 );
  90.     glVertex3f( r0*cos(angle), r0*sin(angle), -width*0.5 );
  91.     }
  92.     glEnd();
  93.  
  94.     /* draw back sides of teeth */
  95.     glBegin( GL_QUADS );
  96.     da = 2.0*pi / teeth / 4.0;
  97.     for (i=0;i<teeth;i++) {
  98.     angle = i * 2.0*pi / teeth;
  99.  
  100.     glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), -width*0.5 );
  101.     glVertex3f( r2*cos(angle+2*da), r2*sin(angle+2*da), -width*0.5 );
  102.     glVertex3f( r2*cos(angle+da),   r2*sin(angle+da),      -width*0.5 );
  103.     glVertex3f( r1*cos(angle),      r1*sin(angle),      -width*0.5 );
  104.     }
  105.     glEnd();
  106.  
  107.  
  108.     /* draw outward faces of teeth */
  109.     glBegin( GL_QUAD_STRIP );
  110.     for (i=0;i<teeth;i++) {
  111.     angle = i * 2.0*pi / teeth;
  112.  
  113.     glVertex3f( r1*cos(angle),      r1*sin(angle),       width*0.5 );
  114.     glVertex3f( r1*cos(angle),      r1*sin(angle),      -width*0.5 );
  115.     u = r2*cos(angle+da) - r1*cos(angle);
  116.     v = r2*sin(angle+da) - r1*sin(angle);
  117.     len = sqrt( u*u + v*v );
  118.     u /= len;
  119.     v /= len;
  120.     glNormal3f( v, -u, 0.0 );
  121.     glVertex3f( r2*cos(angle+da),   r2*sin(angle+da),       width*0.5 );
  122.     glVertex3f( r2*cos(angle+da),   r2*sin(angle+da),      -width*0.5 );
  123.     glNormal3f( cos(angle), sin(angle), 0.0 );
  124.     glVertex3f( r2*cos(angle+2*da), r2*sin(angle+2*da),  width*0.5 );
  125.     glVertex3f( r2*cos(angle+2*da), r2*sin(angle+2*da), -width*0.5 );
  126.     u = r1*cos(angle+3*da) - r2*cos(angle+2*da);
  127.     v = r1*sin(angle+3*da) - r2*sin(angle+2*da);
  128.     glNormal3f( v, -u, 0.0 );
  129.     glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da),  width*0.5 );
  130.     glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), -width*0.5 );
  131.     glNormal3f( cos(angle), sin(angle), 0.0 );
  132.     }
  133.  
  134.     glVertex3f( r1*cos(0.0), r1*sin(0.0), width*0.5 );
  135.     glVertex3f( r1*cos(0.0), r1*sin(0.0), -width*0.5 );
  136.  
  137.     glEnd();
  138.  
  139.  
  140.     glShadeModel( GL_SMOOTH );
  141.  
  142.     /* draw inside radius cylinder */
  143.     glBegin( GL_QUAD_STRIP );
  144.     for (i=0;i<=teeth;i++) {
  145.     angle = i * 2.0*pi / teeth;
  146.     glNormal3f( -cos(angle), -sin(angle), 0.0 );
  147.     glVertex3f( r0*cos(angle), r0*sin(angle), -width*0.5 );
  148.     glVertex3f( r0*cos(angle), r0*sin(angle), width*0.5 );
  149.     }
  150.     glEnd();
  151.  
  152. }
  153.  
  154.  
  155. static GLfloat view_rotx=20.0, view_roty=30.0, view_rotz=0.0;
  156. static GLint gear1, gear2, gear3;
  157. static GLfloat angle = 0.0;
  158.  
  159.  
  160. static void draw()
  161. {
  162.     angle += 2.0;
  163.     view_roty += 1.0;
  164.  
  165.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  166.  
  167.     glPushMatrix();
  168.     glRotatef( view_rotx, 1.0, 0.0, 0.0 );
  169.     glRotatef( view_roty, 0.0, 1.0, 0.0 );
  170.     glRotatef( view_rotz, 0.0, 0.0, 1.0 );
  171.  
  172.     glPushMatrix();
  173.     glTranslatef( -3.0, -2.0, 0.0 );
  174.     glRotatef( angle, 0.0, 0.0, 1.0 );
  175.     glCallList(gear1);
  176.     glPopMatrix();
  177.  
  178.     glPushMatrix();
  179.     glTranslatef( 3.1, -2.0, 0.0 );
  180.     glRotatef( -2.0*angle-9.0, 0.0, 0.0, 1.0 );
  181.     glCallList(gear2);
  182.     glPopMatrix();
  183.  
  184.     glPushMatrix();
  185.     glTranslatef( -3.1, 2.2, -1.8 );
  186.     glRotatef( 90.0, 1.0, 0.0, 0.0 );
  187.     glRotatef( 2.0*angle-2.0, 0.0, 0.0, 1.0 );
  188.     glCallList(gear3);
  189.     glPopMatrix();
  190.  
  191.     glPopMatrix();
  192. }
  193.  
  194.  
  195.  
  196. GearWidget::GearWidget( QWidget *parent, const char *name )
  197.      : QGLWidget( parent, name )
  198. {
  199. }
  200.  
  201. void GearWidget::initializeGL()
  202. {
  203.     static GLfloat pos[4] = {5.0, 5.0, 10.0, 1.0 };
  204.     static GLfloat redgear[4] = {0.8, 0.1, 0.0, 1.0 };
  205.     static GLfloat greengear[4] = {0.0, 0.8, 0.2, 1.0 };
  206.     static GLfloat bluegear[4] = {0.2, 0.2, 1.0, 1.0 };
  207.  
  208.     glLightfv( GL_LIGHT0, GL_POSITION, pos );
  209.     glEnable( GL_CULL_FACE );
  210.     glEnable( GL_LIGHTING );
  211.     glEnable( GL_LIGHT0 );
  212.     glEnable( GL_DEPTH_TEST );
  213.  
  214.     /* make the gears */
  215.     gear1 = glGenLists(1);
  216.     glNewList(gear1, GL_COMPILE);
  217.     glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, redgear );
  218.     gear( 1.0, 4.0, 1.0, 20, 0.7 );
  219.     glEndList();
  220.  
  221.     gear2 = glGenLists(1);
  222.     glNewList(gear2, GL_COMPILE);
  223.     glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, greengear );
  224.     gear( 0.5, 2.0, 2.0, 10, 0.7 );
  225.     glEndList();
  226.  
  227.     gear3 = glGenLists(1);
  228.     glNewList(gear3, GL_COMPILE);
  229.     glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, bluegear );
  230.     gear( 1.3, 2.0, 0.5, 10, 0.7 );
  231.     glEndList();
  232.  
  233.     glEnable( GL_NORMALIZE );
  234. }
  235.  
  236.  
  237. void GearWidget::resizeGL( int width, int height )
  238. {
  239.     GLfloat w = (float) width / (float) height;
  240.     GLfloat h = 1.0;
  241.  
  242.     glViewport( 0, 0, width, height );
  243.     glMatrixMode(GL_PROJECTION);
  244.     glLoadIdentity();
  245.     glFrustum( -w, w, -h, h, 5.0, 60.0 );
  246.     glMatrixMode(GL_MODELVIEW);
  247.     glLoadIdentity();
  248.     glTranslatef( 0.0, 0.0, -40.0 );
  249. }
  250.  
  251.  
  252. void GearWidget::paintGL()
  253. {
  254.     qDebug( "GearWidget: Doing GL rendering." );
  255. #if defined (Q_GLX)
  256.     static bool doneIt = FALSE;
  257.     if ( !doneIt ) {
  258.     doneIt = TRUE;
  259.     // Print out the Visual ID. Access to this will be made
  260.     // simpler in future versions of Qt!
  261.     XWindowAttributes a;
  262.     XGetWindowAttributes( x11Display(), winId(), &a );
  263.     qDebug( "QGLWidget: using Visual ID: 0x%x.",
  264.            (int)XVisualIDFromVisual( a.visual ) );
  265.     }
  266. #endif
  267.     draw();
  268. }
  269.  
  270.