Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TGLTransManip.cxx
Go to the documentation of this file.
1 // @(#)root/gl:$Id$
2 // Author: Richard Maunder 16/09/2005
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2005, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #include "TGLTransManip.h"
13 #include "TGLPhysicalShape.h"
14 #include "TGLCamera.h"
15 #include "TGLIncludes.h"
16 
17 /** \class TGLTransManip
18 \ingroup opengl
19 Translation manipulator - attaches to physical shape and draws local
20 axes widgets with arrow heads. User can mouse over (turns yellow) and
21 L click/drag to translate along this axis.
22 Widgets use standard 3D package axes colours: X red, Y green, Z blue.
23 */
24 
25 ClassImp(TGLTransManip);
26 
27 ////////////////////////////////////////////////////////////////////////////////
28 /// Construct translation manipulator not bound to any physical shape.
29 
30 TGLTransManip::TGLTransManip()
31 {
32 }
33 
34 ////////////////////////////////////////////////////////////////////////////////
35 /// Construct translation manipulator, attached to supplied TGLViewer
36 /// 'viewer', bound to TGLPhysicalShape 'shape'.
37 
38 TGLTransManip::TGLTransManip(TGLPhysicalShape * shape) :
39  TGLManip(shape)
40 {
41 }
42 
43 ////////////////////////////////////////////////////////////////////////////////
44 /// Destroy the translation manipulator
45 
46 TGLTransManip::~TGLTransManip()
47 {
48 }
49 
50 ////////////////////////////////////////////////////////////////////////////////
51 /// Draw translation manipulator - tubes with arrow heads, in local axes of
52 /// attached shape, in red(X), green(Y) and blue(Z), with white center sphere.
53 /// If selected widget (mouse over) this is drawn in active colour (yellow).
54 
55 void TGLTransManip::Draw(const TGLCamera & camera) const
56 {
57  if (!fShape) {
58  return;
59  }
60 
61  // Get draw scales
62  const TGLBoundingBox & box = fShape->BoundingBox();
63  Double_t baseScale;
64  TGLVector3 axisScale[3];
65  CalcDrawScale(box, camera, baseScale, axisScale);
66 
67  // Get permitted manipulations on shape
68  TGLPhysicalShape::EManip manip = fShape->GetManip();
69 
70  glEnable(GL_BLEND);
71  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
72  glDisable(GL_CULL_FACE);
73 
74  // Draw three axis widgets out of bounding box where permitted
75  // Not drawing will prevent interaction
76  // GL name loading for hit testing - 0 reserved for no selection
77  if (manip & TGLPhysicalShape::kTranslateX) {
78  glPushName(1);
79  TGLUtil::DrawLine(box.Center(), axisScale[0], TGLUtil::kLineHeadArrow,
80  baseScale, ColorFor(1));
81  glPopName();
82  } else {
83  TGLUtil::DrawLine(box.Center(), axisScale[0], TGLUtil::kLineHeadArrow,
84  baseScale, TGLUtil::fgGrey);
85  }
86  if (manip & TGLPhysicalShape::kTranslateY) {
87  glPushName(2);
88  TGLUtil::DrawLine(box.Center(), axisScale[1], TGLUtil::kLineHeadArrow,
89  baseScale, ColorFor(2));
90  glPopName();
91  } else {
92  TGLUtil::DrawLine(box.Center(), axisScale[1], TGLUtil::kLineHeadArrow,
93  baseScale, TGLUtil::fgGrey);
94  }
95  if (manip & TGLPhysicalShape::kTranslateZ) {
96  glPushName(3);
97  TGLUtil::DrawLine(box.Center(), axisScale[2], TGLUtil::kLineHeadArrow,
98  baseScale, ColorFor(3));
99  glPopName();
100  } else {
101  TGLUtil::DrawLine(box.Center(), axisScale[2], TGLUtil::kLineHeadArrow,
102  baseScale, TGLUtil::fgGrey);
103  }
104  // Draw white center sphere
105  TGLUtil::DrawSphere(box.Center(), baseScale/2.0, TGLUtil::fgWhite);
106 
107  glEnable(GL_CULL_FACE);
108  glDisable(GL_BLEND);
109 }
110 
111 ////////////////////////////////////////////////////////////////////////////////
112 /// Handle mouse motion over manipulator - if active (selected
113 /// widget) translate physical along selected widget (axis) of the
114 /// manipulator, so it tracks mouse action. Returns kTRUE if redraw
115 /// required kFALSE otherwise.
116 
117 Bool_t TGLTransManip::HandleMotion(const Event_t & event,
118  const TGLCamera & camera)
119 {
120  if (fActive) {
121  // Find mouse delta projected into world at attached object center
122  TGLVector3 shift =
123  camera.ViewportDeltaToWorld( fShape->BoundingBox().Center(),
124  event.fX - fLastMouse.GetX(),
125  -event.fY + fLastMouse.GetY() ); // Y inverted
126 
127  // Now project this delta onto the current widget (axis) to give
128  // a constrained shift along this
129  UInt_t axisIndex = fSelectedWidget - 1; // Ugg sort out axis / widget id mapping
130  TGLVector3 widgetAxis = fShape->BoundingBox().Axis(axisIndex, kTRUE);
131  TGLVector3 constrainedShift = widgetAxis * Dot(shift, widgetAxis);
132  fShape->Translate(constrainedShift);
133 
134  fLastMouse.SetX(event.fX);
135  fLastMouse.SetY(event.fY);
136 
137  return kTRUE;
138  }
139  return kFALSE;
140 }
141