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