Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TGLCameraGuide.cxx
Go to the documentation of this file.
1 // @(#)root/eve:$Id$
2 // Author: Matevz Tadel 2007
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2007, 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 "TGLCameraGuide.h"
13 #include "TGLRnrCtx.h"
14 #include "TGLCamera.h"
15 #include "TGLUtil.h"
16 #include "TGLIncludes.h"
17 #include "TGLSelectRecord.h"
18 
19 #include "TMath.h"
20 
21 /** \class TGLCameraGuide
22 \ingroup opengl
23 Draws arrows showing camera orientation in the overlay.
24 X, Y position is in range 0, 1.
25 */
26 
27 ClassImp(TGLCameraGuide);
28 
29 ////////////////////////////////////////////////////////////////////////////////
30 /// Constructor.
31 
32 TGLCameraGuide::TGLCameraGuide(Float_t x, Float_t y, Float_t s,
33  ERole role, EState state) :
34  TGLOverlayElement(role, state),
35  fXPos(x), fYPos(y), fSize(s),
36  fSelAxis(-1), fInDrag(kFALSE)
37 {
38 }
39 
40 ////////////////////////////////////////////////////////////////////////////////
41 /// Mouse has entered overlay area.
42 
43 Bool_t TGLCameraGuide::MouseEnter(TGLOvlSelectRecord& /*rec*/)
44 {
45  return kTRUE;
46 }
47 
48 ////////////////////////////////////////////////////////////////////////////////
49 /// Handle overlay event.
50 /// Return TRUE if event was handled.
51 
52 Bool_t TGLCameraGuide::Handle(TGLRnrCtx& rnrCtx,
53  TGLOvlSelectRecord& selRec,
54  Event_t* event)
55 {
56  if (selRec.GetN() < 2) return kFALSE;
57  Int_t recID = selRec.GetItem(1);
58 
59  if (recID == 4)
60  fSelAxis = 4;
61  else
62  fSelAxis = 0;
63 
64  switch (event->fType)
65  {
66  case kButtonPress:
67  {
68  if (recID == 4)
69  fInDrag = kTRUE;
70  return kTRUE;
71  }
72  case kButtonRelease:
73  {
74  fInDrag = kFALSE;
75  return kTRUE;
76  }
77  case kMotionNotify:
78  {
79  if (fInDrag)
80  {
81  const TGLRect& vp = rnrCtx.RefCamera().RefViewport();
82  if (vp.Width() == 0 || vp.Height() == 0) return kFALSE;
83 
84  fXPos = TMath::Range(0.0f, 1.0f, (Float_t)(event->fX) / vp.Width());
85  fYPos = TMath::Range(0.0f, 1.0f, 1.0f - (Float_t)(event->fY) / vp.Height());
86  }
87  return kTRUE;
88  }
89  default:
90  {
91  return kFALSE;
92  }
93  }
94 }
95 
96 ////////////////////////////////////////////////////////////////////////////////
97 /// Mouse has left overlay area.
98 
99 void TGLCameraGuide::MouseLeave()
100 {
101  fSelAxis = -1;
102  fInDrag = kFALSE;
103 }
104 
105 ////////////////////////////////////////////////////////////////////////////////
106 /// Render the camera axis arrows.
107 
108 void TGLCameraGuide::Render(TGLRnrCtx& rnrCtx)
109 {
110  TGLCapabilitySwitch lgt_off(GL_LIGHTING, kFALSE);
111 
112  rnrCtx.ProjectionMatrixPushIdentity();
113  glPushMatrix();
114  glLoadIdentity();
115  glTranslatef(-1.0f, -1.0f, 0.0f);
116  glScalef(2.0f, 2.0f, -2.0f);
117  glTranslatef(fXPos, fYPos, -0.25f);
118 
119  Float_t aspect= rnrCtx.RefCamera().RefViewport().Aspect();
120  if (aspect > 1)
121  glScalef(1.0f / aspect, 1.0f, 1.0f);
122  else if (aspect < 1)
123  glScalef(1.0f, aspect, 1.0f);
124 
125  Float_t dr[2];
126  glGetFloatv(GL_DEPTH_RANGE, dr);
127  glDepthRange(0, 0.01);
128 
129  TGLVertex3 c;
130  TGLVector3 e;
131  const TGLMatrix &mv = rnrCtx.RefCamera().RefModelViewMatrix();
132 
133  glPushName(1);
134  mv.GetBaseVec(1, e);
135  e *= fSize;
136  TGLUtil::DrawLine(c, e, TGLUtil::kLineHeadArrow, 0.1*fSize,
137  fSelAxis == 1 ? TGLUtil::fgYellow : TGLUtil::fgRed);
138 
139  glLoadName(2);
140  mv.GetBaseVec(2, e);
141  e *= fSize;
142  TGLUtil::DrawLine(c, e, TGLUtil::kLineHeadArrow, 0.1*fSize,
143  fSelAxis == 2 ? TGLUtil::fgYellow : TGLUtil::fgGreen);
144 
145  glLoadName(3);
146  mv.GetBaseVec(3, e);
147  e *= fSize;
148  TGLUtil::DrawLine(c, e, TGLUtil::kLineHeadArrow, 0.1*fSize,
149  fSelAxis == 3 ? TGLUtil::fgYellow : TGLUtil::fgBlue);
150 
151  glLoadName(4);
152  TGLUtil::DrawSphere(c, 0.08*fSize,
153  fSelAxis == 4 ? TGLUtil::fgYellow : rnrCtx.ColorSet().Foreground().CArr());
154 
155  glPopName();
156 
157  glDepthRange(dr[0], dr[1]);
158 
159  glPopMatrix();
160  rnrCtx.ProjectionMatrixPop();
161 }