Logo ROOT  
Reference Guide
glViewerExercise.C File Reference

Detailed Description

Script showing how to use the GL viewer API to animate a picture.

#include "TGLViewer.h"
#include "TGLOrthoCamera.h"
#include "TTimer.h"
#include "TRandom.h"
#include "TVirtualPad.h"
TTimer timer(25);
TRandom randGen(0);
Int_t moveCount = 0;
void AnimateCamera()
{
// initialization
static Double_t fov = 30;
static Double_t zoom = 0.78;
static Double_t dolly = 1500.0;
static Double_t center[3] = {-164.0, -164.0, -180.0};
static Double_t hRotate = 0.0;
static Double_t vRotate = 0.0;
// steps
static Double_t fovStep = randGen.Rndm()*3.0 - 0.5;
static Double_t zoomStep = (20 - randGen.Rndm())/1000.;
static Double_t dollyStep = randGen.Rndm()*5.0 - 1.0;
static Double_t centerStep[3] = {randGen.Rndm()*4, randGen.Rndm()*4,
randGen.Rndm()*4 };
static Double_t hRotateStep = randGen.Rndm()*0.025;
static Double_t vRotateStep = randGen.Rndm()*0.05;
// move center
center[0] += centerStep[0];
center[1] += centerStep[1];
center[2] += centerStep[2];
Double_t mag = TMath::Sqrt(center[0]*center[0] + center[1]*center[1] +
center[2]*center[2]);
if(mag > 500)
{
centerStep[0] = -centerStep[0];
centerStep[1] = -centerStep[1];
centerStep[2] = -centerStep[2];
}
// rotate
hRotate += hRotateStep;
vRotate += vRotateStep;
if (vRotate >= TMath::TwoPi() || vRotate <= 0.0)
vRotateStep = -vRotateStep;
if (hRotate >= (TMath::PiOver2()- 0.02f) ||
hRotate <= (0.07f -TMath::PiOver2())) {
hRotateStep = -hRotateStep;
}
// dolly
dolly += dollyStep;
if (dolly >= 2000.0 || dolly <= 1500.0)
dollyStep = -dollyStep;
// modify frustum
TGLViewer * v = (TGLViewer *)gPad->GetViewer3D();
if(camera < 3)
{
fov += fovStep;
if (fov > 130.0 || fov < 5.0)
fovStep = - fovStep; }
else
{
zoom += zoomStep;
if (zoom > 4.0 || zoom < 0.25)
zoomStep = - zoomStep;
}
// apply
if(camera < 3)
v->SetPerspectiveCamera(camera, fov, dollyStep, center, hRotateStep,
vRotateStep);
else
v->SetOrthoCamera(camera, zoom, dollyStep, center, hRotateStep,
vRotateStep);
if (++moveCount % 10 == 0)
v->RefreshPadEditor(v);
}
void glViewerExercise()
{
gROOT->ProcessLine(".x nucleus.C");
TGLViewer * v = (TGLViewer *)gPad->GetViewer3D();
// Random draw style
Int_t style = randGen.Integer(3);
switch (style)
{
case 0: v->SetStyle(TGLRnrCtx::kFill); break;
case 1: v->SetStyle(TGLRnrCtx::kOutline); break;
case 2: v->SetStyle(TGLRnrCtx::kWireFrame); break;
}
// Lights - turn some off randomly
TGLLightSet* ls = v->GetLightSet();
if (randGen.Integer(2) == 0)
if (randGen.Integer(2) == 0)
if (randGen.Integer(2) == 0)
if (randGen.Integer(2) == 0)
// Random camera type
Int_t id = randGen.Integer(6);
camera = (TGLViewer::ECameraType)id;
v->SetCurrentCamera(camera);
v->CurrentCamera().SetExternalCenter(kTRUE);
if (id > 2) {
//0, 1, and 2 - are different 'perspective' camers.
TGLOrthoCamera& o = static_cast<TGLOrthoCamera &>(v->CurrentCamera());
}
// Now animate the camera
TGLSAViewer* sav = dynamic_cast<TGLSAViewer*>(v);
if (sav)
sav->GetFrame()->Connect("CloseWindow()", "TTimer", &timer, "TurnOff()");
timer.SetCommand("AnimateCamera()");
timer.TurnOn();
}
Author
Richard Maunder

Definition in file glViewerExercise.C.

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
f
#define f(i)
Definition: RSha256.hxx:122
TGLSAViewer
Definition: TGLSAViewer.h:37
TMath::Sqrt
Double_t Sqrt(Double_t x)
Definition: TMath.h:691
TRandom.h
TMath::PiOver2
constexpr Double_t PiOver2()
Definition: TMath.h:57
Int_t
int Int_t
Definition: RtypesCore.h:45
TGLRnrCtx::kFill
@ kFill
Definition: TGLRnrCtx.h:46
TTimer.h
TGLOrthoCamera
Definition: TGLOrthoCamera.h:35
v
@ v
Definition: rootcling_impl.cxx:3635
TGLLightSet::kLightTop
@ kLightTop
Definition: TGLLightSet.h:25
style
TCanvas * style()
Definition: style.C:1
TTimer
Definition: TTimer.h:51
TTimer::SetCommand
void SetCommand(const char *command)
Set the interpreter command to be executed at time out.
Definition: TTimer.cxx:174
TGLViewer
Definition: TGLViewer.h:51
TRandom
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:50
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:864
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
TTimer::TurnOn
virtual void TurnOn()
Add the timer to the system timer list.
Definition: TTimer.cxx:241
TGLOrthoCamera.h
TGLRnrCtx::kWireFrame
@ kWireFrame
Definition: TGLRnrCtx.h:48
gROOT
#define gROOT
Definition: TROOT.h:406
TGLLightSet
Definition: TGLLightSet.h:21
TGLRnrCtx::kOutline
@ kOutline
Definition: TGLRnrCtx.h:47