1 | /****************************************************************************
|
---|
2 | ** $Id: glbox.cpp 160 2006-12-11 20:15:57Z dmik $
|
---|
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 | drawText();
|
---|
64 | qglColor( green );
|
---|
65 | glLineWidth( 1.0 );
|
---|
66 | glBegin( GL_LINES );
|
---|
67 | {
|
---|
68 | glVertex3f( 0.0, 0.0, 0.0 );
|
---|
69 | glVertex3f( 0.98, 0.98, 0.98 );
|
---|
70 | }
|
---|
71 | glEnd();
|
---|
72 | renderText( 1.0, 1.0, 1.0, "Wirebox", QFont( "helvetica", 12, QFont::Bold, TRUE ) );
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | /*!
|
---|
77 | Set up the OpenGL rendering state, and define display list
|
---|
78 | */
|
---|
79 |
|
---|
80 | void GLBox::initializeGL()
|
---|
81 | {
|
---|
82 | qglClearColor( black ); // Let OpenGL clear to black
|
---|
83 | object = makeObject(); // Generate an OpenGL display list
|
---|
84 | glShadeModel( GL_FLAT );
|
---|
85 | }
|
---|
86 |
|
---|
87 |
|
---|
88 |
|
---|
89 | /*!
|
---|
90 | Set up the OpenGL view port, matrix mode, etc.
|
---|
91 | */
|
---|
92 |
|
---|
93 | void GLBox::resizeGL( int w, int h )
|
---|
94 | {
|
---|
95 | glViewport( 0, 0, (GLint)w, (GLint)h );
|
---|
96 | glMatrixMode( GL_PROJECTION );
|
---|
97 | glLoadIdentity();
|
---|
98 | glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 15.0 );
|
---|
99 | glMatrixMode( GL_MODELVIEW );
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | /*!
|
---|
104 | Generate an OpenGL display list for the object to be shown, i.e. the box
|
---|
105 | */
|
---|
106 |
|
---|
107 | GLuint GLBox::makeObject()
|
---|
108 | {
|
---|
109 | GLuint list;
|
---|
110 |
|
---|
111 | list = glGenLists( 1 );
|
---|
112 |
|
---|
113 | glNewList( list, GL_COMPILE );
|
---|
114 |
|
---|
115 | qglColor( white ); // Shorthand for glColor3f or glIndex
|
---|
116 |
|
---|
117 | glLineWidth( 2.0 );
|
---|
118 |
|
---|
119 | glBegin( GL_LINE_LOOP );
|
---|
120 | glVertex3f( 1.0, 0.5, -0.4 );
|
---|
121 | glVertex3f( 1.0, -0.5, -0.4 );
|
---|
122 | glVertex3f( -1.0, -0.5, -0.4 );
|
---|
123 | glVertex3f( -1.0, 0.5, -0.4 );
|
---|
124 | glEnd();
|
---|
125 |
|
---|
126 | glBegin( GL_LINE_LOOP );
|
---|
127 | glVertex3f( 1.0, 0.5, 0.4 );
|
---|
128 | glVertex3f( 1.0, -0.5, 0.4 );
|
---|
129 | glVertex3f( -1.0, -0.5, 0.4 );
|
---|
130 | glVertex3f( -1.0, 0.5, 0.4 );
|
---|
131 | glEnd();
|
---|
132 |
|
---|
133 | glBegin( GL_LINES );
|
---|
134 | glVertex3f( 1.0, 0.5, -0.4 ); glVertex3f( 1.0, 0.5, 0.4 );
|
---|
135 | glVertex3f( 1.0, -0.5, -0.4 ); glVertex3f( 1.0, -0.5, 0.4 );
|
---|
136 | glVertex3f( -1.0, -0.5, -0.4 ); glVertex3f( -1.0, -0.5, 0.4 );
|
---|
137 | glVertex3f( -1.0, 0.5, -0.4 ); glVertex3f( -1.0, 0.5, 0.4 );
|
---|
138 | glEnd();
|
---|
139 |
|
---|
140 | glEndList();
|
---|
141 |
|
---|
142 | return list;
|
---|
143 | }
|
---|