Developer Documentation
TrackballNode.cc
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43  * *
44  * $Revision$ *
45  * $Author$ *
46  * $Date$ *
47  * *
48 \*===========================================================================*/
49 
50 
51 
52 
53 //=============================================================================
54 //
55 // CLASS TrackballNode - IMPLEMENTATION
56 //
57 //=============================================================================
58 
59 
60 //== INCLUDES =================================================================
61 
62 
63 #include "TrackballNode.hh"
64 
65 
66 //== NAMESPACES ===============================================================
67 
68 
69 namespace ACG {
70 namespace SceneGraph {
71 
72 
73 //== IMPLEMENTATION ==========================================================
74 
75 
76 void
77 TrackballNode::draw(GLState& /* _state */ , const DrawModes::DrawMode& /* _drawMode */ )
78 {
79  // draw the trackball
80  if (drawTrackball_)
81  {
82  ACG::GLState::disable(GL_LIGHTING);
83  ACG::GLState::shadeModel( GL_FLAT );
84  glPushMatrix();
85  glTranslatef(center()[0], center()[1], center()[2]);
86  glutWireSphere(radius_, 20, 20);
87  glPopMatrix();
88  }
89 
90 
91  // draw the coord axes
92  if (drawAxes_)
93  {
94  // store original settings
95  GLfloat backupColor[4], backupLineWidth;
96  glGetFloatv(GL_CURRENT_COLOR, backupColor);
97  glGetFloatv(GL_LINE_WIDTH, &backupLineWidth);
98 
99  ACG::GLState::disable(GL_LIGHTING);
100  ACG::GLState::shadeModel( GL_FLAT );
101  glLineWidth(3.0);
102 
103  glColor3f(1.0, 0.0, 0.0);
104  glBegin(GL_LINES);
105  glVertex(center());
106  glVertex(center() + radius_*xAxis_);
107  glEnd();
108 
109  glColor3f(0.0, 1.0, 0.0);
110  glBegin(GL_LINES);
111  glVertex(center());
112  glVertex(center() + radius_*yAxis_);
113  glEnd();
114 
115  glColor3f(0.0, 0.0, 1.0);
116  glBegin(GL_LINES);
117  glVertex(center());
118  glVertex(center() + radius_*zAxis_);
119  glEnd();
120 
121  // restore original settings
122  glColor4fv(backupColor);
123  glLineWidth(backupLineWidth);
124  }
125 }
126 
127 
128 //----------------------------------------------------------------------------
129 
130 
131 void
132 TrackballNode::mouseEvent(GLState& _state, QMouseEvent* _event)
133 {
134  Vec3d oldPoint3D;
135  Vec2i newPoint2D(_event->pos().x(), _event->pos().y());
136  Vec3d newPoint3D;
137 
138 
139  switch (_event->type())
140  {
141  case QEvent::MouseButtonPress:
142  {
143  break;
144  }
145 
146 
147  case QEvent::MouseButtonDblClick:
148  {
149  if (mapToSphere(_state, oldPoint2D_, oldPoint3D))
150  {
151  // toggle drawTrackball_
152  if (_event->button() == Qt::LeftButton)
153  drawTrackball_ = !drawTrackball_;
154 
155  // toggle drawAxes_
156  if (_event->button() == Qt::MidButton)
157  drawAxes_ = !drawAxes_;
158  }
159  break;
160  }
161 
162 
163  case QEvent::MouseMove:
164  {
165  bool hit0 = mapToSphere(_state, newPoint2D, newPoint3D);
166  bool hit1 = mapToSphere(_state, oldPoint2D_, oldPoint3D);
167 
168  if (hit0 && hit1)
169  {
170 
171  // scaling
172  if ((_event->button() & Qt::LeftButton) &&
173  (_event->button() & Qt::MidButton))
174  {
175  double s = 1.0 + ((double) (newPoint2D[1] - oldPoint2D_[1]) /
176  (double) _state.viewport_height());
177  scale(s);
178  }
179 
180 
181  // translation
182  else if (_event->button() & Qt::MidButton)
183  {
184  double value_x = (radius_ * ((newPoint2D[0] - oldPoint2D_[0]))
185  * 2.0 / (double) _state.viewport_width());
186 
187  double value_y = (radius_ * ((newPoint2D[1] - oldPoint2D_[1]))
188  * 2.0 / (double) _state.viewport_height());
189 
190 
191  /* need inverse transposed matrix in order to
192  transform direction vectors */
194  m.transpose();
195 
196 
197  Vec3d dx, dy;
198 
199  // axis aligned
200  if (drawAxes_)
201  {
202  dx = m.transform_point(xAxis_);
203  dy = m.transform_point(yAxis_);
204  }
205 
206  // screen space translation
207  else
208  {
209  dx = m.transform_vector(_state.right());
210  dy = m.transform_vector(_state.up());
211  }
212 
213  dx.normalize();
214  dy.normalize();
215  translate(value_x*dx - value_y*dy);
216  }
217 
218 
219 
220  // rotation
221  else if (_event->button() & Qt::LeftButton)
222  {
223  Vec3d axis = oldPoint3D % newPoint3D;
224  double cos_angle = ( oldPoint3D | newPoint3D );
225 
226 
227  if (fabs(cos_angle) < 1.0)
228  {
229  // rotate coord axes
230  if (_event->modifiers() & Qt::AltModifier)
231  {
232  GLMatrixd mat;
233 
234  mat.identity();
235  mat.rotate(acos(cos_angle)*180.0/M_PI, axis);
236 
237  /* transform_point works since only
238  rotations and no translations are
239  involved */
240  xAxis_ = mat.transform_point(xAxis_);
241  yAxis_ = mat.transform_point(yAxis_);
242  zAxis_ = mat.transform_point(zAxis_);
243  }
244 
245  // normal rotation
246  else rotate(acos(cos_angle)*180.0/M_PI, axis);
247  }
248  }
249  }
250 
251 
252  break;
253  }
254 
255  default: // avoid warning
256  break;
257  }
258 
259 
260  // store 2D point
261  oldPoint2D_ = newPoint2D;
262 }
263 
264 
265 //----------------------------------------------------------------------------
266 
267 
268 bool
270  const Vec2i& _v2,
271  Vec3d& _v3 )
272 {
273  // Qt -> GL coordinate systems
274  unsigned int x = _v2[0];
275  unsigned int y = _state.context_height() - _v2[1];
276 
277 
278  // get ray from eye through pixel (trackball coords)
279  Vec3d origin, direction;
280  _state.viewing_ray(x, y, origin, direction);
281 
282 
283  // translate and scale trackball to unit sphere
284  origin -= center();
285  origin /= radius_;
286 
287 
288  // calc sphere-ray intersection
289  // (sphere is centered at origin, has radius 1)
290  double a = direction.sqrnorm(),
291  b = 2.0 * (origin | direction),
292  c = origin.sqrnorm() - 1.0,
293  d = b*b - 4.0*a*c,
294  t;
295 
296  if (d < 0.0) return false;
297  else if (d == 0.0) t = -b / (2.0*a);
298  else
299  {
300  // t1 = (-b - sqrt(d)) / (2.0*a), t2 = (-b + sqrt(d)) / (2.0*a)
301  a = 1.0 / (2.0*a);
302  d = sqrt(d);
303  double t1 = (-b - d) * a;
304  double t2 = (-b + d) * a;
305  t = (t1 < t2) ? t1 : t2;
306  }
307 
308  _v3 = origin + direction*t;
309 
310  return true;
311 }
312 
313 
314 //=============================================================================
315 } // namespace SceneGraph
316 } // namespace ACG
317 //=============================================================================
void transpose()
transpose matrix
Definition: Matrix4x4T.cc:272
VectorT< T, 3 > transform_point(const VectorT< T, 3 > &_v) const
transform point (x&#39;,y&#39;,z&#39;,1) = M * (x,y,z,1)
Definition: Matrix4x4T.cc:202
int context_height() const
get gl context height
Definition: GLState.hh:833
void viewing_ray(int _x, int _y, Vec3d &_origin, Vec3d &_direction) const
Definition: GLState.cc:926
bool mapToSphere(GLState &_state, const Vec2i &_v2, Vec3d &_v3)
Map 2D-screen-coords to trackball.
int viewport_width() const
get viewport width
Definition: GLState.hh:825
void glVertex(const Vec2i &_v)
Wrapper: glVertex for Vec2i.
Definition: gl.hh:97
void rotate(Scalar angle, Scalar x, Scalar y, Scalar z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
Definition: GLMatrixT.cc:161
Vec3d up() const
get up-vector w.r.t. camera coordinates
Definition: GLState.cc:902
void identity()
setup an identity matrix
Definition: Matrix4x4T.cc:256
const Vec3d & center() const
get center
const GLMatrixd & scale() const
return scale matrix
static void disable(GLenum _cap)
replaces glDisable, but supports locking
Namespace providing different geometric functions concerning angles.
Definition: DBSCANT.cc:51
auto normalize() -> decltype(*this/=std::declval< VectorT< S, DIM >>().norm())
Definition: Vector11T.hh:428
decltype(std::declval< S >()*std::declval< S >()) sqrnorm() const
compute squared euclidean norm
Definition: Vector11T.hh:396
void rotate(double _angle, const Vec3d &_axis)
void translate(const Vec3d &_v)
Add a translation to the current Transformation.
const GLMatrixd & inverse_matrix() const
return inverse matrix
int viewport_height() const
get viewport height
Definition: GLState.hh:827
Vec3d right() const
get right-vector w.r.t. camera coordinates
Definition: GLState.cc:914
virtual void mouseEvent(GLState &_state, QMouseEvent *_event)
get mouse events
VectorT< T, 3 > transform_vector(const VectorT< T, 3 > &_v) const
transform vector (x&#39;,y&#39;,z&#39;,0) = A * (x,y,z,0)
Definition: Matrix4x4T.cc:225
void draw(GLState &_state, const DrawModes::DrawMode &_drawMode)
Draw the trackball + axes (if enabled)
static void shadeModel(GLenum _mode)
replaces glShadeModel, supports locking