glViewerExercise.C

Go to the documentation of this file.
00001 //script showing how to use the GL viewer API to animate a picture
00002 //Author: Richard maunder
00003 
00004 #include "TGLViewer.h"
00005 #include "TGLPerspectiveCamera.h"
00006 #include "TTimer.h"
00007 #include "TRandom.h"
00008 #include "TVirtualPad.h"
00009 
00010 TGLViewer::ECameraType camera;
00011 TTimer timer(25);
00012 TRandom randGen(0);
00013 
00014 Int_t moveCount = 0;
00015 
00016 void AnimateCamera()
00017 {
00018    // initialization
00019    static Double_t fov = 30;
00020    static Double_t zoom = 0.78;
00021    static Double_t dolly = 1500.0;
00022    static Double_t center[3] = {-164.0, -164.0, -180.0};
00023    static Double_t hRotate = 0.0;
00024    static Double_t vRotate = 0.0;
00025    // steps
00026    static Double_t fovStep = randGen.Rndm()*3.0 - 0.5;
00027    static Double_t zoomStep = (20 - randGen.Rndm())/1000.;
00028    static Double_t dollyStep = randGen.Rndm()*5.0 - 1.0;
00029    static Double_t centerStep[3] = {randGen.Rndm()*4, randGen.Rndm()*4,
00030                                     randGen.Rndm()*4 };
00031    static Double_t hRotateStep = randGen.Rndm()*0.025;
00032    static Double_t vRotateStep = randGen.Rndm()*0.05;
00033 
00034    // move center
00035    center[0] += centerStep[0];
00036    center[1] += centerStep[1];
00037    center[2] += centerStep[2];
00038    Double_t mag = TMath::Sqrt(center[0]*center[0] + center[1]*center[1] +
00039                               center[2]*center[2]);
00040    if(mag > 500)
00041    {
00042       centerStep[0] = -centerStep[0];
00043       centerStep[1] = -centerStep[1];
00044       centerStep[2] = -centerStep[2];
00045    }
00046 
00047    // rotate
00048    hRotate += hRotateStep;
00049    vRotate += vRotateStep;
00050    if (vRotate >= TMath::TwoPi() || vRotate <= 0.0)
00051       vRotateStep = -vRotateStep;
00052 
00053    if (hRotate >= (TMath::PiOver2()- 0.02f) ||
00054        hRotate <= (0.07f -TMath::PiOver2())) {
00055       hRotateStep = -hRotateStep;
00056    }
00057 
00058    // dolly
00059    dolly += dollyStep;
00060    if (dolly >= 2000.0 || dolly <= 1500.0)
00061       dollyStep = -dollyStep;
00062 
00063    // modify frustum
00064    TGLViewer * v = (TGLViewer *)gPad->GetViewer3D();
00065    if(camera < 3)
00066    {
00067       fov += fovStep;
00068       if (fov > 130.0 || fov < 5.0)
00069          fovStep = - fovStep; }
00070    else
00071    {
00072       zoom += zoomStep;
00073       if (zoom > 4.0 || zoom < 0.25)
00074          zoomStep = - zoomStep;
00075    }
00076 
00077    // apply
00078    if(camera < 3)
00079       v->SetPerspectiveCamera(camera, fov, dollyStep, center, hRotateStep,
00080                               vRotateStep);
00081    else
00082       v->SetOrthoCamera(camera, zoom, dollyStep, center, hRotateStep,
00083                         vRotateStep);
00084 
00085    if (++moveCount % 10 == 0)
00086       v->RefreshPadEditor(v);
00087 }
00088 
00089 void glViewerExercise()
00090 {
00091    gROOT->ProcessLine(".x nucleus.C");
00092    TGLViewer * v = (TGLViewer *)gPad->GetViewer3D();
00093 
00094    // Random draw style
00095    Int_t style = randGen.Integer(3);
00096    switch (style)
00097    {
00098       case 0: v->SetStyle(TGLRnrCtx::kFill); break;
00099       case 1: v->SetStyle(TGLRnrCtx::kOutline); break;
00100       case 2: v->SetStyle(TGLRnrCtx::kWireFrame); break;
00101    }
00102 
00103    // Lights - turn some off randomly
00104    TGLLightSet* ls = v->GetLightSet();
00105    if (randGen.Integer(2) == 0)
00106       ls->SetLight(TGLLightSet::kLightLeft, kFALSE);
00107    if (randGen.Integer(2) == 0)
00108       ls->SetLight(TGLLightSet::kLightRight, kFALSE);
00109    if (randGen.Integer(2) == 0)
00110       ls->SetLight(TGLLightSet::kLightTop, kFALSE);
00111    if (randGen.Integer(2) == 0)
00112       ls->SetLight(TGLLightSet::kLightBottom, kFALSE);
00113 
00114    // Random camera type
00115    Int_t id = randGen.Integer(6);
00116    camera = (TGLViewer::ECameraType)id;
00117    v->SetCurrentCamera(camera);
00118    v->CurrentCamera().SetExternalCenter(kTRUE);
00119    if (id > 2) {
00120       TGLOrthoCamera& o = v->CurrentCamera();
00121       o.SetEnableRotate(kTRUE);
00122    }
00123 
00124    // Now animate the camera
00125    TGLSAViewer* sav = dynamic_cast<TGLSAViewer*>(v);
00126    if (sav)
00127      sav->GetFrame()->Connect("CloseWindow()", "TTimer", &timer, "TurnOff()");
00128    timer.SetCommand("AnimateCamera()");
00129    timer.TurnOn();
00130 }
00131 
00132 

Generated on Tue Jul 5 15:44:19 2011 for ROOT_528-00b_version by  doxygen 1.5.1