1 #if defined(_MSC_VER) /* MSVC Compiler */
2 #pragma warning ( disable : 4305 )
3 #pragma warning ( disable : 4786 )
4 #endif
5
6 #include <float.h>
7 #include "qwt3d_extglwidget.h"
8
9 using namespace std;
10 using namespace Qwt3D;
11
12 #ifndef WHEEL_DELTA
13 #define WHEEL_DELTA 120
14 #endif
15
16
17 /*!
18 This should be the first call in your derived classes constructors.
19 */
ExtGLWidget(QWidget * parent,const QGLWidget * shareWidget)20 ExtGLWidget::ExtGLWidget( QWidget * parent, const QGLWidget * shareWidget)
21 : QGLWidget(parent, shareWidget)
22 {
23 initializedGL_ = false;
24 xRot_ = yRot_ = zRot_ = 0.0; // default object rotation
25
26 xShift_ = yShift_ = zShift_ = xVPShift_ = yVPShift_ = 0.0;
27 xScale_ = yScale_ = zScale_ = 1.0;
28 zoom_ = 1;
29 ortho_ = true;
30 lastMouseMovePosition_ = QPoint(0,0);
31 mpressed_ = false;
32 mouse_input_enabled_ = true;
33
34 kbd_input_enabled_ = true;
35
36 setFocusPolicy(Qt::StrongFocus);
37 assignMouse(Qt::LeftButton,
38 MouseState(Qt::LeftButton, Qt::ShiftModifier), Qt::LeftButton,
39 MouseState(Qt::LeftButton, Qt::AltModifier),
40 MouseState(Qt::LeftButton, Qt::AltModifier),
41 MouseState(Qt::LeftButton, Qt::AltModifier | Qt::ShiftModifier),
42 MouseState(Qt::LeftButton, Qt::AltModifier | Qt::ControlModifier),
43 MouseState(Qt::LeftButton, Qt::ControlModifier),
44 MouseState(Qt::LeftButton, Qt::ControlModifier));
45
46
47 assignKeyboard(Qt::Key_Down, Qt::Key_Up,
48 KeyboardState(Qt::Key_Right, Qt::ShiftModifier),
49 KeyboardState(Qt::Key_Left, Qt::ShiftModifier), Qt::Key_Right, Qt::Key_Left,
50 KeyboardState(Qt::Key_Right, Qt::AltModifier),
51 KeyboardState(Qt::Key_Left, Qt::AltModifier),
52 KeyboardState(Qt::Key_Down, Qt::AltModifier),
53 KeyboardState(Qt::Key_Up, Qt::AltModifier),
54 KeyboardState(Qt::Key_Down, Qt::AltModifier|Qt::ShiftModifier),
55 KeyboardState(Qt::Key_Up, Qt::AltModifier|Qt::ShiftModifier),
56 KeyboardState(Qt::Key_Down, Qt::AltModifier|Qt::ControlModifier),
57 KeyboardState(Qt::Key_Up, Qt::AltModifier|Qt::ControlModifier),
58 KeyboardState(Qt::Key_Right, Qt::ControlModifier),
59 KeyboardState(Qt::Key_Left, Qt::ControlModifier),
60 KeyboardState(Qt::Key_Down, Qt::ControlModifier),
61 KeyboardState(Qt::Key_Up, Qt::ControlModifier));
62
63 setKeySpeed(3,5,5);
64
65 lighting_enabled_ = false;
66 disableLighting();
67 lights_ = std::vector<Light>(8);
68 }
69
70 /*!
71 Set up ortogonal or perspective mode and updates widget
72 */
setOrtho(bool val)73 void ExtGLWidget::setOrtho( bool val )
74 {
75 if (val == ortho_) return;
76 ortho_ = val;
77 updateGL();
78
79 emit projectionChanged(val);
80 }
81
82 /*!
83 Set up the OpenGL rendering state
84 */
initializeGL()85 void ExtGLWidget::initializeGL()
86 {
87 glEnable( GL_BLEND );
88 glEnable(GL_DEPTH_TEST);
89 glShadeModel(GL_SMOOTH);
90
91 // Set up the lights
92 disableLighting();
93
94 GLfloat whiteAmb[4] = {1.0, 1.0, 1.0, 1.0};
95
96 setLightShift(0, 0, 3000);
97 glEnable(GL_COLOR_MATERIAL);
98
99 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
100 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, whiteAmb);
101
102 setMaterialComponent(GL_DIFFUSE, 1.0);
103 setMaterialComponent(GL_SPECULAR, 0.3);
104 setMaterialComponent(GL_SHININESS, 5.0);
105 setLightComponent(GL_DIFFUSE, 1.0);
106 setLightComponent(GL_SPECULAR, 1.0);
107
108 initializedGL_ = true;
109 }
110
applyModelViewAndProjection(Triple beg,Triple end)111 void ExtGLWidget::applyModelViewAndProjection(Triple beg, Triple end)
112 {
113 Triple center = beg + (end-beg) / 2;
114 double radius = (center-beg).length();
115
116 glLoadIdentity();
117
118 glRotatef( xRot_-90, 1.0, 0.0, 0.0 );
119 glRotatef( yRot_, 0.0, 1.0, 0.0 );
120 glRotatef( zRot_, 0.0, 0.0, 1.0 );
121 glScalef( zoom_ * xScale_, zoom_ * yScale_, zoom_ * zScale_ );
122 glTranslatef(xShift_-center.x, yShift_-center.y, zShift_-center.z);
123
124 glMatrixMode( GL_PROJECTION );
125 glLoadIdentity();
126
127 if (beg != end) {
128 if (ortho_) glOrtho( -radius, +radius, -radius, +radius, 0, 40 * radius);
129 else glFrustum( -radius, +radius, -radius, +radius, 5 * radius, 400 * radius );
130 } else {
131 if (ortho_) glOrtho( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
132 else glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
133 }
134
135 glTranslatef( xVPShift_ * 2 * radius , yVPShift_ * 2 * radius , -7 * radius );
136 }
137