TranslationManipulatorNode.cc 64.9 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
2
3
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
Jan Möbius's avatar
Jan Möbius committed
4
 *      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen      *
Jan Möbius's avatar
Jan Möbius committed
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
 *                           www.openflipper.org                             *
 *                                                                           *
 *---------------------------------------------------------------------------*
 *  This file is part of OpenFlipper.                                        *
 *                                                                           *
 *  OpenFlipper is free software: you can redistribute it and/or modify      *
 *  it under the terms of the GNU Lesser General Public License as           *
 *  published by the Free Software Foundation, either version 3 of           *
 *  the License, or (at your option) any later version with the              *
 *  following exceptions:                                                    *
 *                                                                           *
 *  If other files instantiate templates or use macros                       *
 *  or inline functions from this file, or you compile this file and         *
 *  link it with other files to produce an executable, this file does        *
 *  not by itself cause the resulting executable to be covered by the        *
 *  GNU Lesser General Public License. This exception does not however       *
 *  invalidate any other reasons why the executable file might be            *
 *  covered by the GNU Lesser General Public License.                        *
 *                                                                           *
 *  OpenFlipper is distributed in the hope that it will be useful,           *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
 *  GNU Lesser General Public License for more details.                      *
 *                                                                           *
 *  You should have received a copy of the GNU LesserGeneral Public          *
 *  License along with OpenFlipper. If not,                                  *
 *  see <http://www.gnu.org/licenses/>.                                      *
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
37
 *   $Revision$                                                       *
Jan Möbius's avatar
Jan Möbius committed
38
39
40
41
 *   $Author$                                                      *
 *   $Date$                   *
 *                                                                           *
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
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




//=============================================================================
//
//  CLASS TranslationManipulatorNode - IMPLEMENTATION
//
//=============================================================================


//== INCLUDES =================================================================


#include "TranslationManipulatorNode.hh"
#include "../Math/GLMatrixT.hh"
#include "../GL/gl.hh"

#include <QMouseEvent>
#include <QEvent>
#include <float.h>

#include <Math_Tools/Math_Tools.hh>
#include <OpenMesh/Core/Geometry/MathDefs.hh>

//== NAMESPACES ===============================================================


namespace ACG {
namespace SceneGraph {


//== IMPLEMENTATION ==========================================================

Mike Kremer's avatar
Mike Kremer committed
76
77
78
79
// How many pixels should the cursor be away from the dragging center
// for the translation operation to affect the geometry's position
#define SNAP_PIXEL_TOLERANCE 30

80
// Node colors (normal, over, clicked, occluded normal, occluded over, occluded clicked)
Jan Möbius's avatar
 
Jan Möbius committed
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
const Vec4f colors[4][6] = {
  // origin
  {
    // normal
    Vec4f(0.2,0.2,0.2,1.0), Vec4f(0.5,0.5,0.5,1.0), Vec4f(0.8,0.8,0.8,1.0),
    // occluded
    Vec4f(0.2,0.2,0.2,0.2), Vec4f(0.5,0.5,0.5,0.4), Vec4f(0.8,0.8,0.8,0.6)
  },
  // X
  {
    // normal
    Vec4f(0.2,0.0,0.0,1.0), Vec4f(0.5,0.0,0.0,1.0), Vec4f(0.8,0.0,0.0,1.0),
    // occluded
    Vec4f(0.3,0.1,0.1,0.2), Vec4f(0.5,0.2,0.2,0.4), Vec4f(0.8,0.4,0.4,0.6)
  },
  // Y
  {
    // normal
    Vec4f(0.0,0.2,0.0,1.0), Vec4f(0.0,0.5,0.0,1.0), Vec4f(0.0,0.8,0.0,1.0),
    // occluded
    Vec4f(0.1,0.3,0.1,0.2), Vec4f(0.2,0.5,0.2,0.4), Vec4f(0.4,0.8,0.4,0.6)
  },
  // Z
  {
    // normal
    Vec4f(0.0,0.0,0.2,1.0), Vec4f(0.0,0.0,0.5,1.0), Vec4f(0.0,0.0,0.8,1.0),
    // occluded
    Vec4f(0.1,0.1,0.3,0.2), Vec4f(0.2,0.2,0.5,0.4), Vec4f(0.4,0.4,0.8,0.6)
  }
};
Jan Möbius's avatar
 
Jan Möbius committed
112
113
114
115


//----------------------------------------------------------------------------

116
117
118
119
120
121
122
123
124
125
126
TranslationManipulatorNode::Element::Element () :
  active_target_color_ (0.0, 0.0, 0.0, 1.0),
  active_current_color_ (0.0, 0.0, 0.0, 1.0),
  inactive_target_color_ (0.0, 0.0, 0.0, 1.0),
  inactive_current_color_ (0.0, 0.0, 0.0, 1.0),
  clicked_ (false),
  over_ (false)
{
}

//----------------------------------------------------------------------------
Jan Möbius's avatar
 
Jan Möbius committed
127
128
129
130
131
132
133
134
135

TranslationManipulatorNode::
TranslationManipulatorNode( BaseNode* _parent, const std::string& _name )
  : TransformNode(_parent, _name),
    draw_manipulator_(false),
    dirX_(1.0,0.0,0.0),
    dirY_(0.0,1.0,0.0),
    dirZ_(0.0,0.0,1.0),
    axis_(0),
136
137
    circle_(0),
    sphere_(0),
Jan Möbius's avatar
 
Jan Möbius committed
138
139
140
141
142
143
144
145
146
    manipulator_radius_(20.0),
    manipulator_height_(20),
    manipulator_slices_(10),
    manipulator_stacks_(10),
    any_axis_clicked_(false),
    any_top_clicked_(false),
    outer_ring_clicked_(false),
    any_axis_over_(false),
    any_top_over_(false),
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
147
    outer_ring_over_(false),
148
149
150
    resize_current_ (0.0),
    mode_ (TranslationRotation),
    ignoreTime_ (false),
Mike Kremer's avatar
Mike Kremer committed
151
    dragging_(false),
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
152
153
    auto_size_(TranslationManipulatorNode::Never),
    auto_size_length_(1.0)
Jan Möbius's avatar
 
Jan Möbius committed
154
155
{
  localTransformation_.identity();
156
157

  // Create quadrics
Jan Möbius's avatar
 
Jan Möbius committed
158
  axis_ = gluNewQuadric();
159
160
161
  circle_ = gluNewQuadric();
  sphere_ = gluNewQuadric();

Jan Möbius's avatar
 
Jan Möbius committed
162
163
164
165
166
  setTraverseMode( BaseNode::ChildrenFirst );

  updateTargetColors ();
  for (unsigned int i = 0; i < TranslationManipulatorNode::NumElements; i++)
  {
167
168
    element_[i].active_current_color_ = element_[i].active_target_color_;
    element_[i].inactive_current_color_ = element_[i].inactive_target_color_;
Jan Möbius's avatar
 
Jan Möbius committed
169
  }
170
171
172
173
174
175
176
/*  
  setMultipassNode(0);
  multipassNodeSetActive(2,true);
  
  setMultipassStatus(0);
  multipassStatusSetActive(1,true);
  multipassStatusSetActive(2,true);*/
Jan Möbius's avatar
 
Jan Möbius committed
177
178
179
180
181
182

}


//----------------------------------------------------------------------------

183
184
185
186
187
188
189
190
191
192
193
194
195
TranslationManipulatorNode::~TranslationManipulatorNode() {

	if (axis_) {
		gluDeleteQuadric(axis_);
	}

	if (circle_) {
		gluDeleteQuadric(circle_);
	}

	if (sphere_) {
		gluDeleteQuadric(sphere_);
	}
Jan Möbius's avatar
 
Jan Möbius committed
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
}


//----------------------------------------------------------------------------


void
TranslationManipulatorNode::
setIdentity()
{
  TransformNode::setIdentity();
}


//----------------------------------------------------------------------------
void
TranslationManipulatorNode::
update_manipulator_system(GLState& _state)
{
  _state.translate(center()[0], center()[1], center()[2]); // Follow translation of parent node
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
216
  _state.mult_matrix(inverse_scale (), scale ()); // Adapt scaling
Jan Möbius's avatar
 
Jan Möbius committed
217
218

  update_rotation(_state);
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
219
  updateSize (_state);
Jan Möbius's avatar
 
Jan Möbius committed
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
}

//----------------------------------------------------------------------------
// Local rotation of the manipulator
void
TranslationManipulatorNode::update_rotation(GLState& _state){

  // Get global transformation
  glMatrixMode(GL_MODELVIEW);
  GLMatrixd model = _state.modelview();

  // revert global transformation as we want to use our own
  model *= inverse_rotation();

  // apply local transformation to adjust our coordinate system
  model *= localTransformation_;

  // And update current matrix
  _state.set_modelview(model);

}

//----------------------------------------------------------------------------

void TranslationManipulatorNode::updateTargetColors ()
{
246
  // reset all color to default values
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
  element_[Origin].active_target_color_ = colors[0][0];
  element_[XAxis].active_target_color_ = colors[1][0];
  element_[YAxis].active_target_color_ = colors[2][0];
  element_[ZAxis].active_target_color_ = colors[3][0];
  element_[XTop].active_target_color_ = colors[1][0];
  element_[YTop].active_target_color_ = colors[2][0];
  element_[ZTop].active_target_color_ = colors[3][0];
  element_[XRing].active_target_color_ = colors[1][0];
  element_[YRing].active_target_color_ = colors[2][0];
  element_[ZRing].active_target_color_ = colors[3][0];

  element_[Origin].inactive_target_color_ = colors[0][3];
  element_[XAxis].inactive_target_color_ = colors[1][3];
  element_[YAxis].inactive_target_color_ = colors[2][3];
  element_[ZAxis].inactive_target_color_ = colors[3][3];
  element_[XTop].inactive_target_color_ = colors[1][3];
  element_[YTop].inactive_target_color_ = colors[2][3];
  element_[ZTop].inactive_target_color_ = colors[3][3];
  element_[XRing].inactive_target_color_ = colors[1][3];
  element_[YRing].inactive_target_color_ = colors[2][3];
  element_[ZRing].inactive_target_color_ = colors[3][3];

269
270
271
272
273
274
275

  // blending is enabled for ring so we have to set alpha correctly
  element_[XRing].active_target_color_[3] = 1.0;
  element_[YRing].active_target_color_[3] = 1.0;
  element_[ZRing].active_target_color_[3] = 1.0;

  // hide rings in resize mode
276
  if (mode_ == Resize || mode_ == Place)
277
278
279
280
281
282
    for (unsigned int i = 0; i < 3; i++)
    {
      element_[XRing + i].active_target_color_[3] = 0.0;
      element_[XRing + i].inactive_target_color_[3] = 0.0;
    }

Jan Möbius's avatar
 
Jan Möbius committed
283
  // set colors according to current (clicked/over/none) state
284
  if(element_[Origin].clicked_){
285
286
    element_[Origin].active_target_color_ = colors[0][2];
    element_[Origin].inactive_target_color_ = colors[0][5];
287
288
    for (unsigned int i = 1; i < NumElements - 3; i++)
    {
289
290
      element_[i].active_target_color_ = (colors[0][2] * 0.5) + (colors[((i-1)%3) + 1][2] * 0.5);
      element_[i].inactive_target_color_ = (colors[0][5] * 0.5) + (colors[((i-1)%3) + 1][5] * 0.5);
291
292
293
    }
    return;
  } else if(element_[Origin].over_){
294
295
    element_[Origin].active_target_color_ = colors[0][1];
    element_[Origin].inactive_target_color_ = colors[0][4];
296
297
    for (unsigned int i = 1; i < NumElements - 3; i++)
    {
298
299
      element_[i].active_target_color_ = (colors[0][1] * 0.5) + (colors[((i-1)%3) + 1][1] * 0.5);
      element_[i].inactive_target_color_ = (colors[0][4] * 0.5) + (colors[((i-1)%3) + 1][4] * 0.5);
300
301
    }
    return;
Jan Möbius's avatar
 
Jan Möbius committed
302
303
  }

304
305
306
  for (unsigned int i = 0; i < 3; i++)
    if (element_[i + XTop].clicked_)
    {
307
308
      element_[i + XTop].active_target_color_ = colors[i+1][2];
      element_[i + XTop].inactive_target_color_ = colors[i+1][5];
309
310
      if (mode_ != TranslationRotation)
      {
311
312
        element_[i + XAxis].active_target_color_ = colors[i+1][2];
        element_[i + XAxis].inactive_target_color_ = colors[i+1][5];
313
314
      }
      if (mode_ != Resize) {
315
316
        element_[i + XRing].active_target_color_ = colors[i+1][2];
        element_[i + XRing].inactive_target_color_ = colors[i+1][5];
317
318
319
320
      }
      return;
    } else if (element_[i + XTop].over_)
    {
321
322
      element_[i + XTop].active_target_color_ = colors[i+1][1];
      element_[i + XTop].inactive_target_color_ = colors[i+1][4];
323
324
      if (mode_ != TranslationRotation)
      {
325
326
        element_[i + XAxis].active_target_color_ = colors[i+1][1];
        element_[i + XAxis].inactive_target_color_ = colors[i+1][4];
327
328
      }
      if (mode_ != Resize) {
329
330
        element_[i + XRing].active_target_color_ = colors[i+1][1];
        element_[i + XRing].inactive_target_color_ = colors[i+1][4];
331
332
333
      }
      return;
    }
Jan Möbius's avatar
 
Jan Möbius committed
334

335
336
337
  for (unsigned int i = 0; i < 3; i++)
    if (element_[i + XAxis].clicked_)
    {
338
339
340
341
      element_[i + XTop].active_target_color_ = colors[i+1][2];
      element_[i + XTop].inactive_target_color_ = colors[i+1][5];
      element_[i + XAxis].active_target_color_ = colors[i+1][2];
      element_[i + XAxis].inactive_target_color_ = colors[i+1][5];
342
      if (mode_ == LocalRotation) {
343
344
        element_[i + XRing].active_target_color_ = colors[i+1][2];
        element_[i + XRing].inactive_target_color_ = colors[i+1][5];
345
346
347
348
      }
      return;
    } else if (element_[i + XAxis].over_)
    {
349
350
351
352
      element_[i + XTop].active_target_color_ = colors[i+1][1];
      element_[i + XTop].inactive_target_color_ = colors[i+1][4];
      element_[i + XAxis].active_target_color_ = colors[i+1][1];
      element_[i + XAxis].inactive_target_color_ = colors[i+1][4];
353
      if (mode_ == LocalRotation) {
354
355
        element_[i + XRing].active_target_color_ = colors[i+1][1];
        element_[i + XRing].inactive_target_color_ = colors[i+1][4];
356
357
358
359
360
361
362
363
      }
      return;
    }

  if (mode_ != Resize)
  for (unsigned int i = 0; i < 3; i++)
    if (element_[i + XRing].clicked_)
    {
364
365
      element_[i + XRing].active_target_color_ = colors[i+1][2];
      element_[i + XRing].inactive_target_color_ = colors[i+1][5];
366
367
368
      return;
    } else if (element_[i + XRing].over_)
    {
369
370
      element_[i + XRing].active_target_color_ = colors[i+1][1];
      element_[i + XRing].inactive_target_color_ = colors[i+1][4];
371
372
      return;
    }
Jan Möbius's avatar
 
Jan Möbius committed
373
374
375
376
377
378
379
380
381
382
383

}

//----------------------------------------------------------------------------

bool TranslationManipulatorNode::updateCurrentColors (GLState& _state)
{
    bool rv = false;

    float value = (float)_state.msSinceLastRedraw () / 1000.0;

384
    if (ignoreTime_)
Jan Möbius's avatar
 
Jan Möbius committed
385
    {
386
387
388
389
390
391
392
393
      value = 0;
      ignoreTime_ = false;
    }

    for (unsigned int i = 0; i < NumElements; i++)
    {
	Vec4f diff = element_[i].active_target_color_ -
                     element_[i].active_current_color_;
Jan Möbius's avatar
 
Jan Möbius committed
394
395
396
397
398
	for (unsigned int j = 0; j < 4; j++)
	    if (diff[j] > value)
		diff[j] = value;
	    else if (diff[j] < -value)
		diff[j] = -value;
399
400
401
	element_[i].active_current_color_ += diff;
	diff = element_[i].inactive_target_color_ -
               element_[i].inactive_current_color_;
Jan Möbius's avatar
 
Jan Möbius committed
402
403
404
405
406
	for (unsigned int j = 0; j < 4; j++)
	    if (diff[j] > value)
		diff[j] = value;
	    else if (diff[j] < -value)
		diff[j] = -value;
407
	element_[i].inactive_current_color_ += diff;
Jan Möbius's avatar
 
Jan Möbius committed
408

409
410
	rv |= element_[i].active_target_color_ != element_[i].active_current_color_ ||
	      element_[i].inactive_target_color_ != element_[i].inactive_current_color_;
Jan Möbius's avatar
 
Jan Möbius committed
411
    }
412
413
414
415
416
417
418
419
420

    float diff = ((mode_ == Resize) ? 1.0 : 0.0) - resize_current_;
    if (diff > value)
      diff = value;
    else if (diff < -value)
      diff = -value;
    resize_current_ += diff;
    rv |= resize_current_ != ((mode_ == Resize) ? 1.0 : 0.0);

Jan Möbius's avatar
 
Jan Möbius committed
421
422
423
424
425
    return rv;
}

//----------------------------------------------------------------------------

Mike Kremer's avatar
Mike Kremer committed
426
void TranslationManipulatorNode::drawManipulator (GLState& _state, bool _active)
Jan Möbius's avatar
 
Jan Möbius committed
427
{
428
  glPushAttrib(GL_ENABLE_BIT );
429
  ACG::GLState::disable( GL_CULL_FACE );
430

Jan Möbius's avatar
 
Jan Möbius committed
431
432
433
434
435
436
437
  // Save modelview matrix
  _state.push_modelview_matrix();

  //================================================================================================
  // Sphere
  //================================================================================================

438
439
440
//     ACG::GLState::disable(GL_ALPHA_TEST);
//     ACG::GLState::enable(GL_BLEND);
//     ACG::GLState::enable(GL_CULL_FACE);
Jan Möbius's avatar
 
Jan Möbius committed
441
//     glColorMaterial ( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE ) ;
442
//     ACG::GLState::enable(GL_COLOR_MATERIAL);
Jan Möbius's avatar
 
Jan Möbius committed
443
//
444
//     ACG::GLState::blendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_DST_ALPHA);
Jan Möbius's avatar
 
Jan Möbius committed
445
446
447
//
//     glColor4f( 1.0,0.0,0.0,0.3);
//     gluSphere( axis_, manipulator_height_ * 1.5 ,  manipulator_slices_ * 2, manipulator_stacks_ * 2);
448
//     ACG::GLState::disable(GL_COLOR_MATERIAL);
Jan Möbius's avatar
 
Jan Möbius committed
449
450
//
//
451
452
//     ACG::GLState::enable(GL_ALPHA_TEST);
//     ACG::GLState::disable(GL_BLEND);
Jan Möbius's avatar
 
Jan Möbius committed
453
454
455
456
457
458
459
460

  //================================================================================================
  // Z-Axis
  //================================================================================================
  // gluCylinder draws into z direction so z-Axis first

  if (_active)
  {
461
462
    _state.set_diffuse_color(element_[ZAxis].active_current_color_);
    _state.set_specular_color(element_[ZAxis].active_current_color_);
Jan Möbius's avatar
 
Jan Möbius committed
463
  } else {
464
465
    _state.set_diffuse_color(element_[ZAxis].inactive_current_color_);
    _state.set_specular_color(element_[ZAxis].inactive_current_color_);
Jan Möbius's avatar
 
Jan Möbius committed
466
467
  }

468
469
470
471
472
473
474
475
476
477
478
  // Draw Bottom of z-axis
  gluCylinder(axis_,
              (1.0 - resize_current_) * manipulator_radius_,
              (1.0 + resize_current_) * manipulator_radius_,
              manipulator_height_/2,
              manipulator_slices_,
              manipulator_stacks_);

  // Draw center of z-axis
  _state.translate(0.0, 0.0, manipulator_height_/2);

Jan Möbius's avatar
 
Jan Möbius committed
479
480
481
  gluCylinder(axis_,
              manipulator_radius_,
              manipulator_radius_,
482
              manipulator_height_/2,
Jan Möbius's avatar
 
Jan Möbius committed
483
484
485
              manipulator_slices_,
              manipulator_stacks_);

486

Jan Möbius's avatar
 
Jan Möbius committed
487
  // Draw Top of z-axis
488
489
490
491
492
493
494
495
  if (_active)
  {
    _state.set_diffuse_color(element_[ZTop].active_current_color_);
    _state.set_specular_color(element_[ZTop].active_current_color_);
  } else {
    _state.set_diffuse_color(element_[ZTop].inactive_current_color_);
    _state.set_specular_color(element_[ZTop].inactive_current_color_);
  }
Jan Möbius's avatar
 
Jan Möbius committed
496

497
  _state.translate(0.0, 0.0, manipulator_height_/2);
Jan Möbius's avatar
 
Jan Möbius committed
498
499
500
501
502
503
504
505
506
507
508
509
510
511
  gluCylinder(axis_,
              manipulator_radius_*2,
              0,
              manipulator_height_/2,
              manipulator_slices_,
              manipulator_stacks_);
  _state.translate(0.0, 0.0, -manipulator_height_);

  //================================================================================================
  // Y-Axis
  //================================================================================================
  _state.rotate(-90, 1.0, 0.0, 0.0);
  if (_active)
  {
512
513
    _state.set_diffuse_color(element_[YAxis].active_current_color_);
    _state.set_specular_color(element_[YAxis].active_current_color_);
Jan Möbius's avatar
 
Jan Möbius committed
514
  } else {
515
516
    _state.set_diffuse_color(element_[YAxis].inactive_current_color_);
    _state.set_specular_color(element_[YAxis].inactive_current_color_);
Jan Möbius's avatar
 
Jan Möbius committed
517
518
  }

519
520
521
522
523
524
525
526
527
528
529
  // Draw Bottom of z-axis
  gluCylinder(axis_,
              (1.0 - resize_current_) * manipulator_radius_,
              (1.0 + resize_current_) * manipulator_radius_,
              manipulator_height_/2,
              manipulator_slices_,
              manipulator_stacks_);

  // Draw center of z-axis
  _state.translate(0.0, 0.0, manipulator_height_/2);

Jan Möbius's avatar
 
Jan Möbius committed
530
531
532
  gluCylinder(axis_,
              manipulator_radius_,
              manipulator_radius_,
533
              manipulator_height_/2,
Jan Möbius's avatar
 
Jan Möbius committed
534
535
536
              manipulator_slices_,
              manipulator_stacks_);

537
538
539
540
541
542
543
544
545
546
547
548

  // Draw Top of z-axis
  if (_active)
  {
    _state.set_diffuse_color(element_[YTop].active_current_color_);
    _state.set_specular_color(element_[YTop].active_current_color_);
  } else {
    _state.set_diffuse_color(element_[YTop].inactive_current_color_);
    _state.set_specular_color(element_[YTop].inactive_current_color_);
  }

  _state.translate(0.0, 0.0, manipulator_height_/2);
Jan Möbius's avatar
 
Jan Möbius committed
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
  gluCylinder(axis_,
              manipulator_radius_*2,
              0,
              manipulator_height_/2,
              manipulator_slices_,
              manipulator_stacks_);
  _state.translate(0.0, 0.0, -manipulator_height_);


  //================================================================================================
  // X-Axis
  //================================================================================================
  _state.rotate(90, 0.0, 1.0, 0.0);
  if (_active)
  {
564
565
    _state.set_diffuse_color(element_[XAxis].active_current_color_);
    _state.set_specular_color(element_[XAxis].active_current_color_);
Jan Möbius's avatar
 
Jan Möbius committed
566
  } else {
567
568
    _state.set_diffuse_color(element_[XAxis].inactive_current_color_);
    _state.set_specular_color(element_[XAxis].inactive_current_color_);
Jan Möbius's avatar
 
Jan Möbius committed
569
570
  }

571
572
573
574
575
576
577
578
579
580
581
  // Draw Bottom of z-axis
  gluCylinder(axis_,
              (1.0 - resize_current_) * manipulator_radius_,
              (1.0 + resize_current_) * manipulator_radius_,
              manipulator_height_/2,
              manipulator_slices_,
              manipulator_stacks_);

  // Draw center of z-axis
  _state.translate(0.0, 0.0, manipulator_height_/2);

Jan Möbius's avatar
 
Jan Möbius committed
582
583
584
  gluCylinder(axis_,
              manipulator_radius_,
              manipulator_radius_,
585
              manipulator_height_/2,
Jan Möbius's avatar
 
Jan Möbius committed
586
587
588
              manipulator_slices_,
              manipulator_stacks_);

589
590
591
592
593
594
595
596
597
598
599
600

  // Draw Top of z-axis
  if (_active)
  {
    _state.set_diffuse_color(element_[XTop].active_current_color_);
    _state.set_specular_color(element_[XTop].active_current_color_);
  } else {
    _state.set_diffuse_color(element_[XTop].inactive_current_color_);
    _state.set_specular_color(element_[XTop].inactive_current_color_);
  }

  _state.translate(0.0, 0.0, manipulator_height_/2);
Jan Möbius's avatar
 
Jan Möbius committed
601
602
603
604
605
606
607
608
609
610
611
612
613
  gluCylinder(axis_,
              manipulator_radius_*2,
              0,
              manipulator_height_/2,
              manipulator_slices_,
              manipulator_stacks_);
  _state.translate(0.0, 0.0, -manipulator_height_);

  //=================================================================================================
  // Sphere
  //=================================================================================================
  if (_active)
  {
614
615
    _state.set_diffuse_color(element_[Origin].active_current_color_);
    _state.set_specular_color(element_[Origin].active_current_color_);
Jan Möbius's avatar
 
Jan Möbius committed
616
  } else {
617
618
    _state.set_diffuse_color(element_[Origin].inactive_current_color_);
    _state.set_specular_color(element_[Origin].inactive_current_color_);
Jan Möbius's avatar
 
Jan Möbius committed
619
620
  }

621
  gluSphere( sphere_, manipulator_radius_*2, manipulator_slices_, manipulator_stacks_ );
Jan Möbius's avatar
 
Jan Möbius committed
622
623
624
625
626

  //=================================================================================================
  // Outer-Rings
  //=================================================================================================

627
  ACG::GLState::enable (GL_BLEND);
Jan Möbius's avatar
 
Jan Möbius committed
628
  glPushAttrib(GL_LIGHTING_BIT);
629
  ACG::GLState::disable(GL_LIGHTING);
Jan Möbius's avatar
 
Jan Möbius committed
630
631
632

  if (_active)
  {
633
634
635
636
    glColor4f(element_[XRing].active_current_color_[0],
	      element_[XRing].active_current_color_[1],
	      element_[XRing].active_current_color_[2],
	      element_[XRing].active_current_color_[3]);
Jan Möbius's avatar
 
Jan Möbius committed
637
  } else {
638
639
640
641
    glColor4f(element_[XRing].inactive_current_color_[0],
	      element_[XRing].inactive_current_color_[1],
	      element_[XRing].inactive_current_color_[2],
	      element_[XRing].inactive_current_color_[3]);
Jan Möbius's avatar
 
Jan Möbius committed
642
643
644
645
646
647
  }

  drawCircle(2*manipulator_height_, 2*manipulator_height_ - manipulator_height_/4.0);

  if (_active)
  {
648
649
650
651
    glColor4f(element_[YRing].active_current_color_[0],
              element_[YRing].active_current_color_[1],
              element_[YRing].active_current_color_[2],
              element_[YRing].active_current_color_[3]);
Jan Möbius's avatar
 
Jan Möbius committed
652
  } else {
653
654
655
656
    glColor4f(element_[YRing].inactive_current_color_[0],
              element_[YRing].inactive_current_color_[1],
              element_[YRing].inactive_current_color_[2],
              element_[YRing].inactive_current_color_[3]);
Jan Möbius's avatar
 
Jan Möbius committed
657
658
659
660
661
662
663
  }

  _state.rotate(90, 0.0, 1.0, 0.0);
  drawCircle(2*manipulator_height_, 2*manipulator_height_ - manipulator_height_/4.0);

  if (_active)
  {
664
665
666
667
    glColor4f(element_[ZRing].active_current_color_[0],
              element_[ZRing].active_current_color_[1],
              element_[ZRing].active_current_color_[2],
              element_[ZRing].active_current_color_[3]);
Jan Möbius's avatar
 
Jan Möbius committed
668
  } else {
669
670
671
672
    glColor4f(element_[ZRing].inactive_current_color_[0],
              element_[ZRing].inactive_current_color_[1],
              element_[ZRing].inactive_current_color_[2],
              element_[ZRing].inactive_current_color_[3]);
Jan Möbius's avatar
 
Jan Möbius committed
673
674
675
676
677
  }

  _state.rotate(90, 1.0, 0.0, 0.0);
  drawCircle(2*manipulator_height_, 2*manipulator_height_ - manipulator_height_/4.0);

678
  glPopAttrib(); // ENABLE_BIT
Jan Möbius's avatar
 
Jan Möbius committed
679
680
  glPopAttrib(); // LIGHTING_BIT

681

Jan Möbius's avatar
 
Jan Möbius committed
682
683
684
685
686
687
688
  _state.pop_modelview_matrix();
}

//----------------------------------------------------------------------------


void
689
TranslationManipulatorNode::draw(GLState& _state, const DrawModes::DrawMode& /* _drawMode */ )
Jan Möbius's avatar
 
Jan Möbius committed
690
{
691
692
   GLenum prev_depth = _state.depthFunc();
    
Jan Möbius's avatar
 
Jan Möbius committed
693
   if (draw_manipulator_) {
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
694

Jan Möbius's avatar
 
Jan Möbius committed
695
696
    // Store lighting bits and enable lighting
    glPushAttrib(GL_LIGHTING_BIT);
697
698
    ACG::GLState::enable(GL_LIGHTING);
    ACG::GLState::shadeModel(GL_SMOOTH);
Jan Möbius's avatar
 
Jan Möbius committed
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720

    // Store original depth status
    glPushAttrib(GL_DEPTH_BUFFER_BIT);


    // Alpha Test things
    glPushAttrib(GL_COLOR_BUFFER_BIT);

    glPushAttrib(GL_ENABLE_BIT);

    // backup colors
    Vec4f backup_diffuse  = _state.diffuse_color();
    Vec4f backup_specular = _state.specular_color();


    // Correctly align nodes local coordinate system
    update_manipulator_system(_state);

    updateTargetColors ();
    if (updateCurrentColors (_state))
      setDirty ();

721
722
    ACG::GLState::enable(GL_DEPTH_TEST);
    ACG::GLState::depthFunc(GL_GREATER);
Jan Möbius's avatar
 
Jan Möbius committed
723
    glDepthMask(GL_FALSE);
724
725
    ACG::GLState::enable (GL_BLEND);
    ACG::GLState::blendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mike Kremer's avatar
Mike Kremer committed
726
    drawManipulator(_state, false);
727
    ACG::GLState::disable (GL_BLEND);
Jan Möbius's avatar
 
Jan Möbius committed
728

729
    ACG::GLState::depthFunc(GL_LEQUAL);
Jan Möbius's avatar
 
Jan Möbius committed
730
    glDepthMask(GL_TRUE);
Mike Kremer's avatar
Mike Kremer committed
731
    drawManipulator(_state, true);
Jan Möbius's avatar
 
Jan Möbius committed
732
733
734
735
736
737
738
739
740
741

    // Restore old attributes and modelview
    glPopAttrib();
    glPopAttrib();
    glPopAttrib();
    glPopAttrib();

    // restore active colors
    _state.set_diffuse_color(backup_diffuse);
    _state.set_specular_color(backup_specular);
742
    
743
    ACG::GLState::depthFunc(prev_depth);
Jan Möbius's avatar
 
Jan Möbius committed
744
745
746
747
748
749
750
751
752
753
  }
}


//----------------------------------------------------------------------------

/// Draws a circle with given radius
void
TranslationManipulatorNode::drawCircle(const float outerRadius, const float innerRadius)
{
754
  gluDisk(circle_, innerRadius, outerRadius, 30, 30);
Jan Möbius's avatar
 
Jan Möbius committed
755
756
757
758
759
}

//----------------------------------------------------------------------------

// void
760
// TranslationManipulatorNode::leave(GLState& _state, const DrawModes::DrawMode& /* _drawMode */ )
Jan Möbius's avatar
 
Jan Möbius committed
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
// {

// }

//----------------------------------------------------------------------------

void
TranslationManipulatorNode::mouseEvent(GLState& _state, QMouseEvent* _event)
{
  if(!draw_manipulator_) return; // Avoid transformation of object while transformator is invisible

  Vec3d         oldPoint3D;
  Vec2i         newPoint2D(_event->pos().x(), _event->pos().y());
  Vec3d         newPoint3D;
  double        old_axis_hit, new_axis_hit, new_axis_over;
776
777
  bool          rot[3], trans[3];
  unsigned int  i;
778
  bool lockOldPoint = false;
779

Jan Möbius's avatar
Dennis:    
Jan Möbius committed
780
  updateSize (_state);
Jan Möbius's avatar
 
Jan Möbius committed
781
782
783
784
785
786

  switch (_event->type()) {

    // If mouse is pressed check if any part of the manipulator is hit and store that state
    case QEvent::MouseButtonPress:
    {
787
788
789
      for (i = 0; i < NumElements; i++)
        element_[i].clicked_ = false;

Mike Kremer's avatar
Mike Kremer committed
790
791
792
793
794
795
      // Start dragging process
      if(!dragging_) {
    	  draggingOrigin3D_ = center();
    	  dragging_ = true;
      }

Jan Möbius's avatar
 
Jan Möbius committed
796
      // hit origin ?
797
798
799
800
      if (mode_ != LocalRotation)
        element_[Origin].clicked_ = hitSphere(_state, newPoint2D);
      else
        element_[Origin].clicked_ = false;
Jan Möbius's avatar
 
Jan Möbius committed
801
802
803
804
805
806
807
808

      // hit any top ?
      any_top_clicked_ = mapToCylinderTop(_state, newPoint2D, new_axis_hit, Click);

      // hit any axis ?
      any_axis_clicked_ = mapToCylinder(_state, newPoint2D, new_axis_hit, Click);

      // hit one of the outer rings ?
809
810
811
812
      if (mode_ != Resize)
        outer_ring_clicked_ = mapToSphere(_state, newPoint2D, newPoint3D, Click);
      else
        outer_ring_clicked_ = false;
Jan Möbius's avatar
 
Jan Möbius committed
813
814

      // origin has been hit, ignore all other parts
815
816
817
      if (element_[Origin].clicked_) {
        for (i = 1; i < NumElements; i++)
          element_[i].clicked_ = false;
Jan Möbius's avatar
 
Jan Möbius committed
818
819
820
821
        any_axis_clicked_   = false;
        any_top_clicked_    = false;
        outer_ring_clicked_ = false;
      } else if ( any_top_clicked_ ) { // tops override the axes
822
823
        for (i = XAxis; i < NumElements; i++)
          element_[i].clicked_ = false;
Jan Möbius's avatar
 
Jan Möbius committed
824
825
826
        any_axis_clicked_ = false;
        outer_ring_clicked_ = false;
      } else if ( any_axis_clicked_ ) { // axes have been hit, disable rest ... should not be required
827
828
        for (i = XRing; i < NumElements; i++)
          element_[i].clicked_ = false;
Jan Möbius's avatar
 
Jan Möbius committed
829
830
        outer_ring_clicked_ = false;
      } else if ( outer_ring_clicked_ ) { // Nothing except the outer ring has been hit
831
832
        for (i = 0; i < XRing; i++)
          element_[i].clicked_ = false;
Jan Möbius's avatar
 
Jan Möbius committed
833
834
835
836
837
838
839
840
841
        any_axis_clicked_   = false;
        any_top_clicked_    = false;
      }

      // Reset local transformation:
      if( _event->modifiers() & Qt::ControlModifier && _event->modifiers() & Qt::AltModifier) {
        localTransformation_.identity();
      }

842
      oldPoint2D_ = newPoint2D;
843
      currentScale_ = Vec3d (1.0, 1.0, 1.0);
844
      ignoreTime_ = true;
Jan Möbius's avatar
 
Jan Möbius committed
845
846
847
848
849
850
851
852
      break;
    }


    // Reset all states as we stopped manipulating
    case QEvent::MouseButtonRelease:
    {

853
      for (i = 0; i < NumElements; i++) {
854
        element_[i].clicked_ = false;
855
      }
Jan Möbius's avatar
 
Jan Möbius committed
856
857
858
      any_axis_clicked_   = false;
      any_top_clicked_    = false;
      outer_ring_clicked_ = false;
859
      ignoreTime_ = true;
Mike Kremer's avatar
Mike Kremer committed
860
      dragging_ = false;
Jan Möbius's avatar
 
Jan Möbius committed
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
      break;
    }


    case QEvent::MouseButtonDblClick:
    {
      draw_manipulator_ = !draw_manipulator_;
      break;
    }


    // All real manipulation is done here
    case QEvent::MouseMove:
    {
      if(!draw_manipulator_) return; // Avoid manipulation if manipulator is invisible

877
878
879
      // Get pressed modifiers
      Qt::KeyboardModifiers mods = Qt::ShiftModifier | Qt::ControlModifier;

880
881
      for (i = 0; i < NumElements; i++)
        element_[i].over_ = false;
Jan Möbius's avatar
 
Jan Möbius committed
882
883
884
885
      any_axis_over_   = false;
      any_top_over_    = false;
      outer_ring_over_ = false;

886
      if (!(element_[Origin].clicked_ || any_top_clicked_ || any_axis_clicked_ ||
Jan Möbius's avatar
 
Jan Möbius committed
887
888
889
	    outer_ring_clicked_))
      {
        // over origin ?
890
891
892
893
        if (mode_ != LocalRotation)
          element_[Origin].over_ = hitSphere(_state, newPoint2D);
        else
          element_[Origin].over_ = false;
Jan Möbius's avatar
 
Jan Möbius committed
894
895

        // over any top ?
896
897
898
        if(mode_ != Place) {
        	any_top_over_ = mapToCylinderTop(_state, newPoint2D, new_axis_over, Over);
        }
Jan Möbius's avatar
 
Jan Möbius committed
899
900

        // over any axis ?
901
902
903
        if(mode_ != Place) {
        	any_axis_over_ = mapToCylinder(_state, newPoint2D, new_axis_over, Over);
        }
Jan Möbius's avatar
 
Jan Möbius committed
904
905

        // over one of the outer rings ?
906
        if (mode_ != Resize) {
907
          outer_ring_over_ = mapToSphere(_state, newPoint2D, newPoint3D, Over);
908
909
        }
        else {
910
          outer_ring_over_ = false;
911
        }
Jan Möbius's avatar
 
Jan Möbius committed
912
913

        // origin has been hit, ignore all other parts
914
915
916
        if (element_[Origin].over_) {
          for (i = 1; i < NumElements; i++)
            element_[i].over_ = false;
Jan Möbius's avatar
 
Jan Möbius committed
917
918
919
920
          any_axis_over_   = false;
          any_top_over_    = false;
          outer_ring_over_ = false;
        } else if ( any_top_over_ ) { // tops override the axes
921
922
          for (i = XAxis; i < NumElements; i++)
            element_[i].over_ = false;
Jan Möbius's avatar
 
Jan Möbius committed
923
924
925
          any_axis_over_ = false;
          outer_ring_over_ = false;
        } else if ( any_axis_over_ ) { // axes have been hit, disable rest ... should not be required
926
927
          for (i = XRing; i < NumElements; i++)
            element_[i].over_ = false;
Jan Möbius's avatar
 
Jan Möbius committed
928
929
          outer_ring_over_ = false;
        } else if ( outer_ring_over_ ) { // Nothing except the outer ring has been hit
930
931
          for (i = 0; i < XRing; i++)
            element_[i].over_ = false;
Jan Möbius's avatar
 
Jan Möbius committed
932
933
934
935
936
          any_axis_over_   = false;
          any_top_over_    = false;
        }
      }

937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
      // set action for the different modes
      switch (mode_)
      {
        case TranslationRotation:
          for (i = 0; i < 3; i++)
          {
            rot[i] = element_[XTop + i].clicked_ || element_[XRing + i].clicked_;
            trans[i] = element_[XAxis + i].clicked_;
          }
          break;
        case LocalRotation:
          for (i = 0; i < 3; i++)
          {
            rot[i] = element_[XTop + i].clicked_ || element_[XRing + i].clicked_ || element_[XAxis + i].clicked_;
            trans[i] = false;
          }
          break;
954
955
        case Place:
          break;
956
957
958
959
960
961
962
963
964
        case Resize:
          for (i = 0; i < 3; i++)
          {
            rot[i] = false;
            trans[i] = element_[XTop + i].clicked_ || element_[XAxis + i].clicked_;
          }
          break;
      }

Jan Möbius's avatar
 
Jan Möbius committed
965
      // If origin was clicked on
966
      if(element_[Origin].clicked_) {
Jan Möbius's avatar
 
Jan Möbius committed
967

Mike Kremer's avatar
Mike Kremer committed
968
969
970
971
972
973
974
975
976
    	// translate along screen plane ( unproject to get world coords for translation and apply them )

		Vec3d d = _state.project(center());
		Vec3d d_origin = _state.project(draggingOrigin3D_);

		// Snap back to origin position if mouse cursor
		// is near enough
		if (abs(d_origin[0] - newPoint2D[0]) < SNAP_PIXEL_TOLERANCE &&
				abs(_state.context_height() - d_origin[1] - newPoint2D[1]) < SNAP_PIXEL_TOLERANCE
977
		       && _event->modifiers() & Qt::AltModifier) {
Mike Kremer's avatar
Mike Kremer committed
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
			newPoint2D = oldPoint2D_;
			Vec3d backtrans = draggingOrigin3D_ - center();
			if (mode_ != Resize) {
				translate(backtrans);
			}
		}

		// translate along screen plane ( unproject to get world coords for translation and apply them )
		Vec3d oldvec = _state.unproject(Vec3d(oldPoint2D_[0], (_state.context_height() - oldPoint2D_[1]), d[2]));
		Vec3d newvec = _state.unproject(Vec3d(newPoint2D[0], (_state.context_height() - newPoint2D[1]), d[2]));
		Vec3d ntrans = newvec - oldvec;

		if (mode_ != Resize)
			translate(ntrans);
		else
		{
994
995
996
997
998
999
1000
                  // revert last scale
                  scale(Vec3d (1.0 / currentScale_[0], 1.0 / currentScale_[1], 1.0 / currentScale_[2]));
		  double positive = -1;
		  if (newPoint2D[0] - oldPoint2D_[0] + oldPoint2D_[1] - newPoint2D[1] > 0)
		  positive = 1;

                  Vec2d div = Vec2d (newPoint2D[0], newPoint2D[1]) - Vec2d (oldPoint2D_[0], oldPoint2D_[1]);