ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
glViewerExercise.C
Go to the documentation of this file.
1 //script showing how to use the GL viewer API to animate a picture
2 //Author: Richard maunder
3 
4 #include "TGLViewer.h"
5 #include "TGLOrthoCamera.h"
6 #include "TTimer.h"
7 #include "TRandom.h"
8 #include "TVirtualPad.h"
9 
11 TTimer timer(25);
12 TRandom randGen(0);
13 
15 
17 {
18  // initialization
19  static Double_t fov = 30;
20  static Double_t zoom = 0.78;
21  static Double_t dolly = 1500.0;
22  static Double_t center[3] = {-164.0, -164.0, -180.0};
23  static Double_t hRotate = 0.0;
24  static Double_t vRotate = 0.0;
25  // steps
26  static Double_t fovStep = randGen.Rndm()*3.0 - 0.5;
27  static Double_t zoomStep = (20 - randGen.Rndm())/1000.;
28  static Double_t dollyStep = randGen.Rndm()*5.0 - 1.0;
29  static Double_t centerStep[3] = {randGen.Rndm()*4, randGen.Rndm()*4,
30  randGen.Rndm()*4 };
31  static Double_t hRotateStep = randGen.Rndm()*0.025;
32  static Double_t vRotateStep = randGen.Rndm()*0.05;
33 
34  // move center
35  center[0] += centerStep[0];
36  center[1] += centerStep[1];
37  center[2] += centerStep[2];
38  Double_t mag = TMath::Sqrt(center[0]*center[0] + center[1]*center[1] +
39  center[2]*center[2]);
40  if(mag > 500)
41  {
42  centerStep[0] = -centerStep[0];
43  centerStep[1] = -centerStep[1];
44  centerStep[2] = -centerStep[2];
45  }
46 
47  // rotate
48  hRotate += hRotateStep;
49  vRotate += vRotateStep;
50  if (vRotate >= TMath::TwoPi() || vRotate <= 0.0)
51  vRotateStep = -vRotateStep;
52 
53  if (hRotate >= (TMath::PiOver2()- 0.02f) ||
54  hRotate <= (0.07f -TMath::PiOver2())) {
55  hRotateStep = -hRotateStep;
56  }
57 
58  // dolly
59  dolly += dollyStep;
60  if (dolly >= 2000.0 || dolly <= 1500.0)
61  dollyStep = -dollyStep;
62 
63  // modify frustum
64  TGLViewer * v = (TGLViewer *)gPad->GetViewer3D();
65  if(camera < 3)
66  {
67  fov += fovStep;
68  if (fov > 130.0 || fov < 5.0)
69  fovStep = - fovStep; }
70  else
71  {
72  zoom += zoomStep;
73  if (zoom > 4.0 || zoom < 0.25)
74  zoomStep = - zoomStep;
75  }
76 
77  // apply
78  if(camera < 3)
79  v->SetPerspectiveCamera(camera, fov, dollyStep, center, hRotateStep,
80  vRotateStep);
81  else
82  v->SetOrthoCamera(camera, zoom, dollyStep, center, hRotateStep,
83  vRotateStep);
84 
85  if (++moveCount % 10 == 0)
86  v->RefreshPadEditor(v);
87 }
88 
90 {
91  gROOT->ProcessLine(".x nucleus.C");
92  TGLViewer * v = (TGLViewer *)gPad->GetViewer3D();
93 
94  // Random draw style
96  switch (style)
97  {
98  case 0: v->SetStyle(TGLRnrCtx::kFill); break;
99  case 1: v->SetStyle(TGLRnrCtx::kOutline); break;
100  case 2: v->SetStyle(TGLRnrCtx::kWireFrame); break;
101  }
102 
103  // Lights - turn some off randomly
104  TGLLightSet* ls = v->GetLightSet();
105  if (randGen.Integer(2) == 0)
107  if (randGen.Integer(2) == 0)
109  if (randGen.Integer(2) == 0)
111  if (randGen.Integer(2) == 0)
113 
114  // Random camera type
115  Int_t id = randGen.Integer(6);
119  if (id > 2) {
120  //0, 1, and 2 - are different 'perspective' camers.
121  TGLOrthoCamera& o = static_cast<TGLOrthoCamera &>(v->CurrentCamera());
123  }
124 
125  // Now animate the camera
126  TGLSAViewer* sav = dynamic_cast<TGLSAViewer*>(v);
127  if (sav)
128  sav->GetFrame()->Connect("CloseWindow()", "TTimer", &timer, "TurnOff()");
129  timer.SetCommand("AnimateCamera()");
130  timer.TurnOn();
131 }
132 
133 
void SetLight(ELight light, Bool_t on)
Set a light on/off.
Definition: TGLLightSet.cxx:55
virtual Double_t Rndm(Int_t i=0)
Machine independent random number generator.
Definition: TRandom.cxx:512
TRandom randGen(0)
mfile ls()
#define gROOT
Definition: TROOT.h:344
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: Rtypes.h:92
Encapsulates a set of lights for OpenGL.
Definition: TGLLightSet.h:21
TFile * f
void SetOrthoCamera(ECameraType camera, Double_t zoom, Double_t dolly, Double_t center[3], Double_t hRotate, Double_t vRotate)
Set an orthographic camera to supplied configuration - note this does not need to be the current came...
Definition: TGLViewer.cxx:1871
void SetEnableRotate(Bool_t x)
TGLViewer::ECameraType camera
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:29
virtual UInt_t Integer(UInt_t imax)
Returns a random integer on [ 0, imax-1 ].
Definition: TRandom.cxx:320
TTimer timer(25)
Int_t moveCount
TGCompositeFrame * GetFrame() const
Return the main-frame.
void AnimateCamera()
Double_t TwoPi()
Definition: TMath.h:45
The top level standalone GL-viewer - created via plugin manager.
Definition: TGLSAViewer.h:37
void SetPerspectiveCamera(ECameraType camera, Double_t fov, Double_t dolly, Double_t center[3], Double_t hRotate, Double_t vRotate)
Set a perspective camera to supplied configuration - note this does not need to be the current camera...
Definition: TGLViewer.cxx:1919
TGLLightSet * GetLightSet() const
Definition: TGLViewer.h:261
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:1135
SVector< double, 2 > v
Definition: Dict.h:5
TGLCamera & CurrentCamera() const
Definition: TGLViewer.h:267
void SetExternalCenter(Bool_t x)
Set camera center diffrent than scene center, if enable is kTRUE.
Definition: TGLCamera.cxx:825
Handles synchronous and a-synchronous timer events.
Definition: TTimer.h:57
Base GL viewer object - used by both standalone and embedded (in pad) GL.
Definition: TGLViewer.h:53
void SetCommand(const char *command)
Set the interpreter command to be executed at time out.
Definition: TTimer.cxx:172
double Double_t
Definition: RtypesCore.h:55
TCanvas * style()
Definition: style.C:1
void SetCurrentCamera(ECameraType camera)
Set current active camera - 'cameraType' one of: kCameraPerspX, kCameraPerspY, kCameraPerspZ, kCameraOrthoXOY, kCameraOrthoXOZ, kCameraOrthoZOY, kCameraOrthoXnOY, kCameraOrthoXnOZ, kCameraOrthoZnOY.
Definition: TGLViewer.cxx:1779
Orthographic projection camera.
virtual void RefreshPadEditor(TObject *obj=0)
Update GED editor if it is set.
Definition: TGLViewer.cxx:2248
Double_t PiOver2()
Definition: TMath.h:46
void glViewerExercise()
#define gPad
Definition: TVirtualPad.h:288
Double_t Sqrt(Double_t x)
Definition: TMath.h:464
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual void TurnOn()
Add the timer to the system timer list.
Definition: TTimer.cxx:239
void SetStyle(Short_t st)