ROOT logo
// @(#)root/gl:$Id$
// Author: Timur Pocheptsov

/*************************************************************************
 * Copyright (C) 1995-2007, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/
#include "TGLPlotCamera.h"
#include "TGLIncludes.h"
#include "TVirtualGL.h"
#include "TGLUtil.h"

//______________________________________________________________________________
//
// Camera for TGLPlotPainter and sub-classes.

ClassImp(TGLPlotCamera);

//______________________________________________________________________________
TGLPlotCamera::TGLPlotCamera() :
   fZoom(1.), fShift(1.5), fCenter(),
   fVpChanged(kFALSE)
{
   //Construct camera for plot painters.
   fOrthoBox[0] = 1.;
   fOrthoBox[1] = 1.;
   fOrthoBox[2] = -100.;
   fOrthoBox[3] = 100.;
}

//______________________________________________________________________________
void TGLPlotCamera::SetViewport(const TGLRect &vp)
{
   //Setup viewport, if it was changed, plus reset arcball.
   
   if (vp.Width() != fViewport.Width() || vp.Height() != fViewport.Height() ||
       vp.X() != fViewport.X() || vp.Y() != fViewport.Y())
   {
      fVpChanged = kTRUE;
      fArcBall.SetBounds(vp.Width(), vp.Height());
      fViewport = vp;
      
   } else
      fVpChanged = kFALSE;
}

//______________________________________________________________________________
void TGLPlotCamera::SetViewVolume(const TGLVertex3* /* box */)
{
   //'box' is the TGLPlotPainter's back box's coordinates.
/*   fCenter[0] = (box[0].X() + box[1].X()) / 2;
   fCenter[1] = (box[0].Y() + box[2].Y()) / 2;
   fCenter[2] = (box[0].Z() + box[4].Z()) / 2;
   const Double_t maxDim = box[1].X() - box[0].X();
   fOrthoBox[0] = maxDim;
   fOrthoBox[1] = maxDim;
   fOrthoBox[2] = -100 * maxDim;//100?
   fOrthoBox[3] = 100 * maxDim;
   fShift = maxDim * 1.5;*/
}

//______________________________________________________________________________
void TGLPlotCamera::StartRotation(Int_t px, Int_t py)
{
   //User clicks somewhere (px, py).
   fArcBall.Click(TPoint(px, py));
}

//______________________________________________________________________________
void TGLPlotCamera::RotateCamera(Int_t px, Int_t py)
{
   //Mouse movement.
   fArcBall.Drag(TPoint(px, py));
}

//______________________________________________________________________________
void TGLPlotCamera::StartPan(Int_t px, Int_t py)
{
   //User clicks somewhere (px, py).
   fMousePos.fX = px;
   fMousePos.fY = fViewport.Height() - py;
}

//______________________________________________________________________________
void TGLPlotCamera::Pan(Int_t px, Int_t py)
{
   //Pan camera.
   py = fViewport.Height() - py;

   //Extract gl matrices.
   Double_t mv[16] = {0.};
   glGetDoublev(GL_MODELVIEW_MATRIX, mv);
   Double_t pr[16] = {0.};
   glGetDoublev(GL_PROJECTION_MATRIX, pr);
   Int_t vp[] = {0, 0, fViewport.Width(), fViewport.Height()};
   //Adjust pan vector.
   TGLVertex3 start, end;
   gluUnProject(fMousePos.fX, fMousePos.fY, 1., mv, pr, vp, &start.X(), &start.Y(), &start.Z());
   gluUnProject(px, py, 1., mv, pr, vp, &end.X(), &end.Y(), &end.Z());
   fTruck += (start - end) /= 2.;
   //
   fMousePos.fX = px;
   fMousePos.fY = py;
}

//______________________________________________________________________________
void TGLPlotCamera::SetCamera()const
{
   //Viewport and projection.
   glViewport(fViewport.X(), fViewport.Y(), fViewport.Width(), fViewport.Height());

   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(
           -fOrthoBox[0] * fZoom,
            fOrthoBox[0] * fZoom,
           -fOrthoBox[1] * fZoom,
            fOrthoBox[1] * fZoom,
            fOrthoBox[2],
            fOrthoBox[3]
          );

   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
}

//______________________________________________________________________________
void TGLPlotCamera::Apply(Double_t phi, Double_t theta)const
{
   //Applies rotations and translations before drawing
   glTranslated(0., 0., -fShift);
   glMultMatrixd(fArcBall.GetRotMatrix());
   glRotated(theta - 90., 1., 0., 0.);
   glRotated(phi, 0., 0., 1.);
   glTranslated(-fTruck[0], -fTruck[1], -fTruck[2]);
//   glTranslated(-fCenter[0], -fCenter[1], -fCenter[2]);
}

//______________________________________________________________________________
Int_t TGLPlotCamera::GetX()const
{
   //viewport[0]
   return fViewport.X();
}

//______________________________________________________________________________
Int_t TGLPlotCamera::GetY()const
{
   //viewport[1]
   return fViewport.Y();
}

//______________________________________________________________________________
Int_t TGLPlotCamera::GetWidth()const
{
   //viewport[2]
   return Int_t(fViewport.Width());
}

//______________________________________________________________________________
Int_t TGLPlotCamera::GetHeight()const
{
   //viewport[3]
   return Int_t(fViewport.Height());
}

//______________________________________________________________________________
void TGLPlotCamera::ZoomIn()
{
   //Zoom in.
   fZoom /= 1.2;
}

//______________________________________________________________________________
void TGLPlotCamera::ZoomOut()
{
   //Zoom out.
   fZoom *= 1.2;
}
 TGLPlotCamera.cxx:1
 TGLPlotCamera.cxx:2
 TGLPlotCamera.cxx:3
 TGLPlotCamera.cxx:4
 TGLPlotCamera.cxx:5
 TGLPlotCamera.cxx:6
 TGLPlotCamera.cxx:7
 TGLPlotCamera.cxx:8
 TGLPlotCamera.cxx:9
 TGLPlotCamera.cxx:10
 TGLPlotCamera.cxx:11
 TGLPlotCamera.cxx:12
 TGLPlotCamera.cxx:13
 TGLPlotCamera.cxx:14
 TGLPlotCamera.cxx:15
 TGLPlotCamera.cxx:16
 TGLPlotCamera.cxx:17
 TGLPlotCamera.cxx:18
 TGLPlotCamera.cxx:19
 TGLPlotCamera.cxx:20
 TGLPlotCamera.cxx:21
 TGLPlotCamera.cxx:22
 TGLPlotCamera.cxx:23
 TGLPlotCamera.cxx:24
 TGLPlotCamera.cxx:25
 TGLPlotCamera.cxx:26
 TGLPlotCamera.cxx:27
 TGLPlotCamera.cxx:28
 TGLPlotCamera.cxx:29
 TGLPlotCamera.cxx:30
 TGLPlotCamera.cxx:31
 TGLPlotCamera.cxx:32
 TGLPlotCamera.cxx:33
 TGLPlotCamera.cxx:34
 TGLPlotCamera.cxx:35
 TGLPlotCamera.cxx:36
 TGLPlotCamera.cxx:37
 TGLPlotCamera.cxx:38
 TGLPlotCamera.cxx:39
 TGLPlotCamera.cxx:40
 TGLPlotCamera.cxx:41
 TGLPlotCamera.cxx:42
 TGLPlotCamera.cxx:43
 TGLPlotCamera.cxx:44
 TGLPlotCamera.cxx:45
 TGLPlotCamera.cxx:46
 TGLPlotCamera.cxx:47
 TGLPlotCamera.cxx:48
 TGLPlotCamera.cxx:49
 TGLPlotCamera.cxx:50
 TGLPlotCamera.cxx:51
 TGLPlotCamera.cxx:52
 TGLPlotCamera.cxx:53
 TGLPlotCamera.cxx:54
 TGLPlotCamera.cxx:55
 TGLPlotCamera.cxx:56
 TGLPlotCamera.cxx:57
 TGLPlotCamera.cxx:58
 TGLPlotCamera.cxx:59
 TGLPlotCamera.cxx:60
 TGLPlotCamera.cxx:61
 TGLPlotCamera.cxx:62
 TGLPlotCamera.cxx:63
 TGLPlotCamera.cxx:64
 TGLPlotCamera.cxx:65
 TGLPlotCamera.cxx:66
 TGLPlotCamera.cxx:67
 TGLPlotCamera.cxx:68
 TGLPlotCamera.cxx:69
 TGLPlotCamera.cxx:70
 TGLPlotCamera.cxx:71
 TGLPlotCamera.cxx:72
 TGLPlotCamera.cxx:73
 TGLPlotCamera.cxx:74
 TGLPlotCamera.cxx:75
 TGLPlotCamera.cxx:76
 TGLPlotCamera.cxx:77
 TGLPlotCamera.cxx:78
 TGLPlotCamera.cxx:79
 TGLPlotCamera.cxx:80
 TGLPlotCamera.cxx:81
 TGLPlotCamera.cxx:82
 TGLPlotCamera.cxx:83
 TGLPlotCamera.cxx:84
 TGLPlotCamera.cxx:85
 TGLPlotCamera.cxx:86
 TGLPlotCamera.cxx:87
 TGLPlotCamera.cxx:88
 TGLPlotCamera.cxx:89
 TGLPlotCamera.cxx:90
 TGLPlotCamera.cxx:91
 TGLPlotCamera.cxx:92
 TGLPlotCamera.cxx:93
 TGLPlotCamera.cxx:94
 TGLPlotCamera.cxx:95
 TGLPlotCamera.cxx:96
 TGLPlotCamera.cxx:97
 TGLPlotCamera.cxx:98
 TGLPlotCamera.cxx:99
 TGLPlotCamera.cxx:100
 TGLPlotCamera.cxx:101
 TGLPlotCamera.cxx:102
 TGLPlotCamera.cxx:103
 TGLPlotCamera.cxx:104
 TGLPlotCamera.cxx:105
 TGLPlotCamera.cxx:106
 TGLPlotCamera.cxx:107
 TGLPlotCamera.cxx:108
 TGLPlotCamera.cxx:109
 TGLPlotCamera.cxx:110
 TGLPlotCamera.cxx:111
 TGLPlotCamera.cxx:112
 TGLPlotCamera.cxx:113
 TGLPlotCamera.cxx:114
 TGLPlotCamera.cxx:115
 TGLPlotCamera.cxx:116
 TGLPlotCamera.cxx:117
 TGLPlotCamera.cxx:118
 TGLPlotCamera.cxx:119
 TGLPlotCamera.cxx:120
 TGLPlotCamera.cxx:121
 TGLPlotCamera.cxx:122
 TGLPlotCamera.cxx:123
 TGLPlotCamera.cxx:124
 TGLPlotCamera.cxx:125
 TGLPlotCamera.cxx:126
 TGLPlotCamera.cxx:127
 TGLPlotCamera.cxx:128
 TGLPlotCamera.cxx:129
 TGLPlotCamera.cxx:130
 TGLPlotCamera.cxx:131
 TGLPlotCamera.cxx:132
 TGLPlotCamera.cxx:133
 TGLPlotCamera.cxx:134
 TGLPlotCamera.cxx:135
 TGLPlotCamera.cxx:136
 TGLPlotCamera.cxx:137
 TGLPlotCamera.cxx:138
 TGLPlotCamera.cxx:139
 TGLPlotCamera.cxx:140
 TGLPlotCamera.cxx:141
 TGLPlotCamera.cxx:142
 TGLPlotCamera.cxx:143
 TGLPlotCamera.cxx:144
 TGLPlotCamera.cxx:145
 TGLPlotCamera.cxx:146
 TGLPlotCamera.cxx:147
 TGLPlotCamera.cxx:148
 TGLPlotCamera.cxx:149
 TGLPlotCamera.cxx:150
 TGLPlotCamera.cxx:151
 TGLPlotCamera.cxx:152
 TGLPlotCamera.cxx:153
 TGLPlotCamera.cxx:154
 TGLPlotCamera.cxx:155
 TGLPlotCamera.cxx:156
 TGLPlotCamera.cxx:157
 TGLPlotCamera.cxx:158
 TGLPlotCamera.cxx:159
 TGLPlotCamera.cxx:160
 TGLPlotCamera.cxx:161
 TGLPlotCamera.cxx:162
 TGLPlotCamera.cxx:163
 TGLPlotCamera.cxx:164
 TGLPlotCamera.cxx:165
 TGLPlotCamera.cxx:166
 TGLPlotCamera.cxx:167
 TGLPlotCamera.cxx:168
 TGLPlotCamera.cxx:169
 TGLPlotCamera.cxx:170
 TGLPlotCamera.cxx:171
 TGLPlotCamera.cxx:172
 TGLPlotCamera.cxx:173
 TGLPlotCamera.cxx:174
 TGLPlotCamera.cxx:175
 TGLPlotCamera.cxx:176
 TGLPlotCamera.cxx:177
 TGLPlotCamera.cxx:178
 TGLPlotCamera.cxx:179
 TGLPlotCamera.cxx:180
 TGLPlotCamera.cxx:181
 TGLPlotCamera.cxx:182