1 | #include "glcontrolwidget.h"
|
---|
2 |
|
---|
3 | #include <qcursor.h>
|
---|
4 | #include <qtimer.h>
|
---|
5 |
|
---|
6 | #include <math.h>
|
---|
7 |
|
---|
8 | GLControlWidget::GLControlWidget( QWidget *parent, const char *name, QGLWidget *share, WFlags f )
|
---|
9 | : QGLWidget( parent, name, share, f ),
|
---|
10 | xRot(0),yRot(0),zRot(0),xTrans(0),yTrans(0),zTrans(-10.0),scale(5.0), animation(TRUE), wasAnimated(FALSE), delay( 50 )
|
---|
11 | {
|
---|
12 | setCursor( pointingHandCursor );
|
---|
13 | timer = new QTimer( this );
|
---|
14 | connect( timer, SIGNAL(timeout()), SLOT(animate()) );
|
---|
15 | timer->start( delay );
|
---|
16 | }
|
---|
17 |
|
---|
18 | void GLControlWidget::transform()
|
---|
19 | {
|
---|
20 | glTranslatef( xTrans, yTrans, zTrans );
|
---|
21 | glScalef( scale, scale, scale );
|
---|
22 |
|
---|
23 | glRotatef( xRot, 1.0, 0.0, 0.0 );
|
---|
24 | glRotatef( yRot, 0.0, 1.0, 0.0 );
|
---|
25 | glRotatef( zRot, 0.0, 0.0, 1.0 );
|
---|
26 | }
|
---|
27 |
|
---|
28 | void GLControlWidget::drawText()
|
---|
29 | {
|
---|
30 | glPushAttrib( GL_LIGHTING_BIT | GL_TEXTURE_BIT );
|
---|
31 | glDisable( GL_LIGHTING );
|
---|
32 | glDisable( GL_TEXTURE_2D );
|
---|
33 | qglColor( white );
|
---|
34 | QString str( "Rendering text in OpenGL is easy with Qt" );
|
---|
35 | QFontMetrics fm( font() );
|
---|
36 | renderText( (width() - fm.width( str )) / 2, 15, str );
|
---|
37 | QFont f( "courier", 8 );
|
---|
38 | QFontMetrics fmc( f );
|
---|
39 | qglColor( QColor("skyblue") );
|
---|
40 | int x, y, z;
|
---|
41 | x = (xRot >= 0) ? (int) xRot % 360 : 359 - (QABS((int) xRot) % 360);
|
---|
42 | y = (yRot >= 0) ? (int) yRot % 360 : 359 - (QABS((int) yRot) % 360);
|
---|
43 | z = (zRot >= 0) ? (int) zRot % 360 : 359 - (QABS((int) zRot) % 360);
|
---|
44 | str.sprintf( "Rot X: %03d - Rot Y: %03d - Rot Z: %03d", x, y, z );
|
---|
45 | renderText( (width() - fmc.width( str )) / 2, height() - 15, str, f );
|
---|
46 | glPopAttrib();
|
---|
47 | }
|
---|
48 |
|
---|
49 | /*!
|
---|
50 | Set the rotation angle of the object to \e degrees around the X axis.
|
---|
51 | */
|
---|
52 | void GLControlWidget::setXRotation( double degrees )
|
---|
53 | {
|
---|
54 | xRot = (GLfloat)fmod(degrees, 360.0);
|
---|
55 | updateGL();
|
---|
56 | }
|
---|
57 |
|
---|
58 | /*!
|
---|
59 | Set the rotation angle of the object to \e degrees around the Y axis.
|
---|
60 | */
|
---|
61 | void GLControlWidget::setYRotation( double degrees )
|
---|
62 | {
|
---|
63 | yRot = (GLfloat)fmod(degrees, 360.0);
|
---|
64 | updateGL();
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 | /*!
|
---|
69 | Set the rotation angle of the object to \e degrees around the Z axis.
|
---|
70 | */
|
---|
71 | void GLControlWidget::setZRotation( double degrees )
|
---|
72 | {
|
---|
73 | zRot = (GLfloat)fmod(degrees, 360.0);
|
---|
74 | updateGL();
|
---|
75 | }
|
---|
76 |
|
---|
77 | void GLControlWidget::setScale( double s )
|
---|
78 | {
|
---|
79 | scale = s;
|
---|
80 | updateGL();
|
---|
81 | }
|
---|
82 |
|
---|
83 | void GLControlWidget::setXTrans( double x )
|
---|
84 | {
|
---|
85 | xTrans = x;
|
---|
86 | updateGL();
|
---|
87 | }
|
---|
88 |
|
---|
89 | void GLControlWidget::setYTrans( double y )
|
---|
90 | {
|
---|
91 | yTrans = y;
|
---|
92 | updateGL();
|
---|
93 | }
|
---|
94 |
|
---|
95 | void GLControlWidget::setZTrans( double z )
|
---|
96 | {
|
---|
97 | zTrans = z;
|
---|
98 | updateGL();
|
---|
99 | }
|
---|
100 |
|
---|
101 | void GLControlWidget::setRotationImpulse( double x, double y, double z )
|
---|
102 | {
|
---|
103 | setXRotation( xRot + 180*x );
|
---|
104 | setYRotation( yRot + 180*y );
|
---|
105 | setZRotation( zRot - 180*z );
|
---|
106 | }
|
---|
107 |
|
---|
108 | void GLControlWidget::setTranslationImpulse( double x, double y, double z )
|
---|
109 | {
|
---|
110 | setXTrans( xTrans + 2*x );
|
---|
111 | setYTrans( yTrans - 2*y );
|
---|
112 | setZTrans( zTrans + 2*z );
|
---|
113 | }
|
---|
114 |
|
---|
115 | void GLControlWidget::mousePressEvent( QMouseEvent *e )
|
---|
116 | {
|
---|
117 | e->accept();
|
---|
118 | oldPos = e->pos();
|
---|
119 | }
|
---|
120 |
|
---|
121 | void GLControlWidget::mouseReleaseEvent( QMouseEvent *e )
|
---|
122 | {
|
---|
123 | e->accept();
|
---|
124 | oldPos = e->pos();
|
---|
125 | }
|
---|
126 |
|
---|
127 | void GLControlWidget::mouseMoveEvent( QMouseEvent *e )
|
---|
128 | {
|
---|
129 | e->accept();
|
---|
130 | double dx = e->x() - oldPos.x();
|
---|
131 | double dy = e->y() - oldPos.y();
|
---|
132 |
|
---|
133 | oldPos = e->pos();
|
---|
134 |
|
---|
135 | double rx = dx / width();
|
---|
136 | double ry = dy / height();
|
---|
137 |
|
---|
138 | if ( e->state() == LeftButton )
|
---|
139 | setRotationImpulse( ry, rx, 0 );
|
---|
140 | else if ( e->state() == RightButton )
|
---|
141 | setRotationImpulse( ry, 0, rx );
|
---|
142 | else if ( e->state() == MidButton )
|
---|
143 | setTranslationImpulse( rx, ry, 0 );
|
---|
144 | else if ( e->state() == ( LeftButton | RightButton ) )
|
---|
145 | setTranslationImpulse( rx, 0, ry );
|
---|
146 | }
|
---|
147 |
|
---|
148 | void GLControlWidget::wheelEvent( QWheelEvent *e )
|
---|
149 | {
|
---|
150 | e->accept();
|
---|
151 | if ( scale <= ( (double)e->delta() / 1000 ) )
|
---|
152 | return;
|
---|
153 | setScale( scale - ( (double)e->delta() / 1000 ));
|
---|
154 | }
|
---|
155 |
|
---|
156 | void GLControlWidget::mouseDoubleClickEvent( QMouseEvent * )
|
---|
157 | {
|
---|
158 | if ( delay <= 0 )
|
---|
159 | return;
|
---|
160 |
|
---|
161 | animation = !animation;
|
---|
162 | if ( animation )
|
---|
163 | timer->start( delay );
|
---|
164 | else
|
---|
165 | timer->stop();
|
---|
166 | }
|
---|
167 |
|
---|
168 | void GLControlWidget::showEvent( QShowEvent *e )
|
---|
169 | {
|
---|
170 | if ( wasAnimated && !timer->isActive() )
|
---|
171 | timer->start( delay );
|
---|
172 |
|
---|
173 | QGLWidget::showEvent( e );
|
---|
174 | }
|
---|
175 |
|
---|
176 | void GLControlWidget::hideEvent( QHideEvent *e )
|
---|
177 | {
|
---|
178 | wasAnimated = timer->isActive();
|
---|
179 | timer->stop();
|
---|
180 | QGLWidget::hideEvent( e );
|
---|
181 | }
|
---|
182 |
|
---|
183 | void GLControlWidget::animate()
|
---|
184 | {
|
---|
185 | }
|
---|
186 |
|
---|
187 | void GLControlWidget::setAnimationDelay( int ms )
|
---|
188 | {
|
---|
189 | timer->stop();
|
---|
190 | delay = ms;
|
---|
191 | if ( animation ) {
|
---|
192 | wasAnimated = TRUE;
|
---|
193 | timer->start( delay );
|
---|
194 | }
|
---|
195 | }
|
---|