Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
16TTimer timer(25);
18
20
21void 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, randGen.Rndm() * 4};
35 static Double_t hRotateStep = randGen.Rndm() * 0.025;
36 static Double_t vRotateStep = randGen.Rndm() * 0.05;
37
38 // move center
39 center[0] += centerStep[0];
40 center[1] += centerStep[1];
41 center[2] += centerStep[2];
42 Double_t mag = TMath::Sqrt(center[0] * center[0] + center[1] * center[1] + center[2] * center[2]);
43 if (mag > 500) {
44 centerStep[0] = -centerStep[0];
45 centerStep[1] = -centerStep[1];
46 centerStep[2] = -centerStep[2];
47 }
48
49 // rotate
52 if (vRotate >= TMath::TwoPi() || vRotate <= 0.0)
54
55 if (hRotate >= (TMath::PiOver2() - 0.02f) || hRotate <= (0.07f - TMath::PiOver2())) {
57 }
58
59 // dolly
61 if (dolly >= 2000.0 || dolly <= 1500.0)
63
64 // modify frustum
65 TGLViewer *v = gPad ? (TGLViewer *)gPad->GetViewer3D() : nullptr;
66 if (!v) return;
67 if (camera < 3) {
68 fov += fovStep;
69 if (fov > 130.0 || fov < 5.0)
71 } else {
72 zoom += zoomStep;
73 if (zoom > 4.0 || zoom < 0.25)
75 }
76
77 // apply
78 if (camera < 3)
79 v->SetPerspectiveCamera(camera, fov, dollyStep, center, hRotateStep, vRotateStep);
80 else
81 v->SetOrthoCamera(camera, zoom, dollyStep, center, hRotateStep, vRotateStep);
82
83 if (++moveCount % 10 == 0)
84 v->RefreshPadEditor(v);
85}
86
88{
89 gROOT->ProcessLine(".x nucleus.C");
90 TGLViewer *v = gPad ? (TGLViewer *)gPad->GetViewer3D() : nullptr;
91 if (!v) return;
92
93 // Random draw style
94 Int_t style = randGen.Integer(3);
95 switch (style) {
96 case 0: v->SetStyle(TGLRnrCtx::kFill); break;
97 case 1: v->SetStyle(TGLRnrCtx::kOutline); break;
98 case 2: v->SetStyle(TGLRnrCtx::kWireFrame); break;
99 }
100
101 // Lights - turn some off randomly
102 TGLLightSet *ls = v->GetLightSet();
103 if (randGen.Integer(2) == 0)
105 if (randGen.Integer(2) == 0)
107 if (randGen.Integer(2) == 0)
109 if (randGen.Integer(2) == 0)
111
112 // Random camera type
113 Int_t id = randGen.Integer(6);
115 v->SetCurrentCamera(camera);
116 v->CurrentCamera().SetExternalCenter(kTRUE);
117 if (id > 2) {
118 // 0, 1, and 2 - are different 'perspective' camers.
119 TGLOrthoCamera &o = static_cast<TGLOrthoCamera &>(v->CurrentCamera());
121 }
122
123 // Now animate the camera
124 TGLSAViewer *sav = dynamic_cast<TGLSAViewer *>(v);
125 if (sav)
126 sav->GetFrame()->Connect("CloseWindow()", "TTimer", &timer, "TurnOff()");
127 timer.SetCommand("AnimateCamera()");
128 timer.TurnOn();
129}
int Int_t
Definition RtypesCore.h:45
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
double Double_t
Definition RtypesCore.h:59
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t style
#define gROOT
Definition TROOT.h:414
#define gPad
Encapsulates a set of lights for OpenGL.
Definition TGLLightSet.h:22
void SetLight(ELight light, Bool_t on)
Set a light on/off.
Orthographic projection camera.
void SetEnableRotate(Bool_t x)
The top level standalone GL-viewer - created via plugin manager.
Definition TGLSAViewer.h:38
Base GL viewer object - used by both standalone and embedded (in pad) GL.
Definition TGLViewer.h:55
This is the base class for the ROOT Random number generators.
Definition TRandom.h:27
Handles synchronous and a-synchronous timer events.
Definition TTimer.h:51
constexpr Double_t PiOver2()
Definition TMath.h:52
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:668
constexpr Double_t TwoPi()
Definition TMath.h:45