Logo ROOT  
Reference Guide
glViewerExercise.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_gl
3 /// Script showing how to use the GL viewer API to animate a picture.
4 ///
5 /// \macro_code
6 ///
7 /// \author Richard Maunder
8 
9 #include "TGLViewer.h"
10 #include "TGLOrthoCamera.h"
11 #include "TTimer.h"
12 #include "TRandom.h"
13 #include "TVirtualPad.h"
14 
16 TTimer timer(25);
17 TRandom randGen(0);
18 
19 Int_t moveCount = 0;
20 
21 void AnimateCamera()
22 {
23  // initialization
24  static Double_t fov = 30;
25  static Double_t zoom = 0.78;
26  static Double_t dolly = 1500.0;
27  static Double_t center[3] = {-164.0, -164.0, -180.0};
28  static Double_t hRotate = 0.0;
29  static Double_t vRotate = 0.0;
30  // steps
31  static Double_t fovStep = randGen.Rndm()*3.0 - 0.5;
32  static Double_t zoomStep = (20 - randGen.Rndm())/1000.;
33  static Double_t dollyStep = randGen.Rndm()*5.0 - 1.0;
34  static Double_t centerStep[3] = {randGen.Rndm()*4, randGen.Rndm()*4,
35  randGen.Rndm()*4 };
36  static Double_t hRotateStep = randGen.Rndm()*0.025;
37  static Double_t vRotateStep = randGen.Rndm()*0.05;
38 
39  // move center
40  center[0] += centerStep[0];
41  center[1] += centerStep[1];
42  center[2] += centerStep[2];
43  Double_t mag = TMath::Sqrt(center[0]*center[0] + center[1]*center[1] +
44  center[2]*center[2]);
45  if(mag > 500)
46  {
47  centerStep[0] = -centerStep[0];
48  centerStep[1] = -centerStep[1];
49  centerStep[2] = -centerStep[2];
50  }
51 
52  // rotate
53  hRotate += hRotateStep;
54  vRotate += vRotateStep;
55  if (vRotate >= TMath::TwoPi() || vRotate <= 0.0)
56  vRotateStep = -vRotateStep;
57 
58  if (hRotate >= (TMath::PiOver2()- 0.02f) ||
59  hRotate <= (0.07f -TMath::PiOver2())) {
60  hRotateStep = -hRotateStep;
61  }
62 
63  // dolly
64  dolly += dollyStep;
65  if (dolly >= 2000.0 || dolly <= 1500.0)
66  dollyStep = -dollyStep;
67 
68  // modify frustum
69  TGLViewer * v = (TGLViewer *)gPad->GetViewer3D();
70  if(camera < 3)
71  {
72  fov += fovStep;
73  if (fov > 130.0 || fov < 5.0)
74  fovStep = - fovStep; }
75  else
76  {
77  zoom += zoomStep;
78  if (zoom > 4.0 || zoom < 0.25)
79  zoomStep = - zoomStep;
80  }
81 
82  // apply
83  if(camera < 3)
84  v->SetPerspectiveCamera(camera, fov, dollyStep, center, hRotateStep,
85  vRotateStep);
86  else
87  v->SetOrthoCamera(camera, zoom, dollyStep, center, hRotateStep,
88  vRotateStep);
89 
90  if (++moveCount % 10 == 0)
91  v->RefreshPadEditor(v);
92 }
93 
94 void glViewerExercise()
95 {
96  gROOT->ProcessLine(".x nucleus.C");
97  TGLViewer * v = (TGLViewer *)gPad->GetViewer3D();
98 
99  // Random draw style
100  Int_t style = randGen.Integer(3);
101  switch (style)
102  {
103  case 0: v->SetStyle(TGLRnrCtx::kFill); break;
104  case 1: v->SetStyle(TGLRnrCtx::kOutline); break;
105  case 2: v->SetStyle(TGLRnrCtx::kWireFrame); break;
106  }
107 
108  // Lights - turn some off randomly
109  TGLLightSet* ls = v->GetLightSet();
110  if (randGen.Integer(2) == 0)
112  if (randGen.Integer(2) == 0)
114  if (randGen.Integer(2) == 0)
116  if (randGen.Integer(2) == 0)
118 
119  // Random camera type
120  Int_t id = randGen.Integer(6);
121  camera = (TGLViewer::ECameraType)id;
122  v->SetCurrentCamera(camera);
123  v->CurrentCamera().SetExternalCenter(kTRUE);
124  if (id > 2) {
125  //0, 1, and 2 - are different 'perspective' camers.
126  TGLOrthoCamera& o = static_cast<TGLOrthoCamera &>(v->CurrentCamera());
128  }
129 
130  // Now animate the camera
131  TGLSAViewer* sav = dynamic_cast<TGLSAViewer*>(v);
132  if (sav)
133  sav->GetFrame()->Connect("CloseWindow()", "TTimer", &timer, "TurnOff()");
134  timer.SetCommand("AnimateCamera()");
135  timer.TurnOn();
136 }
137 
138 
TGLLightSet::SetLight
void SetLight(ELight light, Bool_t on)
Set a light on/off.
Definition: TGLLightSet.cxx:55
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TGLOrthoCamera::SetEnableRotate
void SetEnableRotate(Bool_t x)
Definition: TGLOrthoCamera.h:80
TGLSAViewer
The top level standalone GL-viewer - created via plugin manager.
Definition: TGLSAViewer.h:38
TMath::Sqrt
Double_t Sqrt(Double_t x)
Definition: TMath.h:691
TRandom.h
TMath::PiOver2
constexpr Double_t PiOver2()
Definition: TMath.h:51
Int_t
int Int_t
Definition: RtypesCore.h:45
TGLRnrCtx::kFill
@ kFill
Definition: TGLRnrCtx.h:46
TTimer.h
TGLOrthoCamera
Orthographic projection camera.
Definition: TGLOrthoCamera.h:36
v
@ v
Definition: rootcling_impl.cxx:3635
TGLLightSet::kLightTop
@ kLightTop
Definition: TGLLightSet.h:25
style
TCanvas * style()
Definition: style.C:1
TTimer
Handles synchronous and a-synchronous timer events.
Definition: TTimer.h:51
TGLViewer
Base GL viewer object - used by both standalone and embedded (in pad) GL.
Definition: TGLViewer.h:55
TRandom
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:27
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TVirtualPad.h
TGLViewer::ECameraType
ECameraType
Definition: TGLViewer.h:61
TGLViewer.h
TMath::TwoPi
constexpr Double_t TwoPi()
Definition: TMath.h:44
TGLSAViewer::GetFrame
TGCompositeFrame * GetFrame() const
Return the main-frame.
Definition: TGLSAViewer.cxx:325
Double_t
double Double_t
Definition: RtypesCore.h:59
TQObject::Connect
Bool_t Connect(const char *signal, const char *receiver_class, void *receiver, const char *slot)
Non-static method is used to connect from the signal of this object to the receiver slot.
Definition: TQObject.cxx:866
TGLLightSet::kLightRight
@ kLightRight
Definition: TGLLightSet.h:28
TGLLightSet::kLightLeft
@ kLightLeft
Definition: TGLLightSet.h:27
gPad
#define gPad
Definition: TVirtualPad.h:287
TGLLightSet::kLightBottom
@ kLightBottom
Definition: TGLLightSet.h:26
TGLOrthoCamera.h
TGLRnrCtx::kWireFrame
@ kWireFrame
Definition: TGLRnrCtx.h:48
gROOT
#define gROOT
Definition: TROOT.h:406
TGLLightSet
Encapsulates a set of lights for OpenGL.
Definition: TGLLightSet.h:22
TGLRnrCtx::kOutline
@ kOutline
Definition: TGLRnrCtx.h:47