#include "TGLViewer.h"
#include "TGLIncludes.h"
#include "TGLStopwatch.h"
#include "TGLRnrCtx.h"
#include "TGLSelectBuffer.h"
#include "TGLLightSet.h"
#include "TGLClip.h"
#include "TGLManipSet.h"
#include "TGLCameraOverlay.h"
#include "TGLScenePad.h"
#include "TGLLogicalShape.h"
#include "TGLPhysicalShape.h"
#include "TGLObject.h"
#include "TGLStopwatch.h"
#include "TBuffer3D.h"
#include "TBuffer3DTypes.h"
#include "TGLOutput.h"
#include "TVirtualPad.h"
#include "TAtt3D.h"
#include "TVirtualX.h"
#include "TMath.h"
#include "TColor.h"
#include "TError.h"
#include "TClass.h"
#include "TROOT.h"
#include "Buttons.h"
#include "GuiTypes.h"
#include "TVirtualGL.h"
#include "TGLWidget.h"
#include "TGLViewerEditor.h"
#include "KeySymbols.h"
#include "TContextMenu.h"
#include "TImage.h"
#ifndef GL_BGRA
#define GL_BGRA GL_BGRA_EXT
#endif
ClassImp(TGLViewer);
TGLViewer::TGLViewer(TVirtualPad * pad, Int_t x, Int_t y,
Int_t width, Int_t height) :
fPad(pad),
fContextMenu(0),
fPerspectiveCameraXOZ(TGLVector3(-1.0, 0.0, 0.0), TGLVector3(0.0, 1.0, 0.0)),
fPerspectiveCameraYOZ(TGLVector3( 0.0,-1.0, 0.0), TGLVector3(1.0, 0.0, 0.0)),
fPerspectiveCameraXOY(TGLVector3(-1.0, 0.0, 0.0), TGLVector3(0.0, 0.0, 1.0)),
fOrthoXOYCamera (TGLOrthoCamera::kXOY, TGLVector3( 0.0, 0.0, 1.0), TGLVector3(0.0, 1.0, 0.0)),
fOrthoXOZCamera (TGLOrthoCamera::kXOZ, TGLVector3( 0.0,-1.0, 0.0), TGLVector3(0.0, 0.0, 1.0)),
fOrthoZOYCamera (TGLOrthoCamera::kZOY, TGLVector3(-1.0, 0.0, 0.0), TGLVector3(0.0, 1.0, 0.0)),
fOrthoXnOYCamera(TGLOrthoCamera::kXnOY, TGLVector3( 0.0, 0.0,-1.0), TGLVector3(0.0, 1.0, 0.0)),
fOrthoXnOZCamera(TGLOrthoCamera::kXnOZ, TGLVector3( 0.0, 1.0, 0.0), TGLVector3(0.0, 0.0, 1.0)),
fOrthoZnOYCamera(TGLOrthoCamera::kZnOY, TGLVector3( 1.0, 0.0, 0.0), TGLVector3(0.0, 1.0, 0.0)),
fCurrentCamera(&fPerspectiveCameraXOZ),
fLightSet (0),
fClipSet (0),
fSelectedPShapeRef (0),
fCurrentOvlElm (0),
fEventHandler(0),
fPushAction(kPushStd), fDragAction(kDragNone),
fRedrawTimer(0),
fMaxSceneDrawTimeHQ(5000),
fMaxSceneDrawTimeLQ(100),
fClearColor(1),
fAxesType(TGLUtil::kAxesNone),
fAxesDepthTest(kTRUE),
fReferenceOn(kFALSE),
fReferencePos(0.0, 0.0, 0.0),
fDrawCameraCenter(kFALSE),
fCameraOverlay(0),
fInitGL(kFALSE),
fSmartRefresh(kFALSE),
fDebugMode(kFALSE),
fIsPrinting(kFALSE),
fPictureFileName("viewer.jpg"),
fFader(0),
fGLWidget(0),
fGLDevice(-1),
fGLCtxId(0),
fIgnoreSizesOnUpdate(kFALSE),
fResetCamerasOnUpdate(kTRUE),
fResetCamerasOnNextUpdate(kFALSE),
fResetCameraOnDoubleClick(kTRUE)
{
InitSecondaryObjects();
SetViewport(x, y, width, height);
}
TGLViewer::TGLViewer(TVirtualPad * pad) :
fPad(pad),
fContextMenu(0),
fPerspectiveCameraXOZ(TGLVector3(-1.0, 0.0, 0.0), TGLVector3(0.0, 1.0, 0.0)),
fPerspectiveCameraYOZ(TGLVector3( 0.0,-1.0, 0.0), TGLVector3(1.0, 0.0, 0.0)),
fPerspectiveCameraXOY(TGLVector3(-1.0, 0.0, 0.0), TGLVector3(0.0, 0.0, 1.0)),
fOrthoXOYCamera (TGLOrthoCamera::kXOY, TGLVector3( 0.0, 0.0, 1.0), TGLVector3(0.0, 1.0, 0.0)),
fOrthoXOZCamera (TGLOrthoCamera::kXOZ, TGLVector3( 0.0,-1.0, 0.0), TGLVector3(0.0, 0.0, 1.0)),
fOrthoZOYCamera (TGLOrthoCamera::kZOY, TGLVector3(-1.0, 0.0, 0.0), TGLVector3(0.0, 1.0, 0.0)),
fOrthoXnOYCamera(TGLOrthoCamera::kXnOY, TGLVector3( 0.0, 0.0,-1.0), TGLVector3(0.0, 1.0, 0.0)),
fOrthoXnOZCamera(TGLOrthoCamera::kXnOZ, TGLVector3( 0.0, 1.0, 0.0), TGLVector3(0.0, 0.0, 1.0)),
fOrthoZnOYCamera(TGLOrthoCamera::kZnOY, TGLVector3( 1.0, 0.0, 0.0), TGLVector3(0.0, 1.0, 0.0)),
fCurrentCamera(&fPerspectiveCameraXOZ),
fLightSet (0),
fClipSet (0),
fSelectedPShapeRef (0),
fCurrentOvlElm (0),
fEventHandler(0),
fPushAction(kPushStd), fDragAction(kDragNone),
fRedrawTimer(0),
fMaxSceneDrawTimeHQ(5000),
fMaxSceneDrawTimeLQ(100),
fClearColor(1),
fAxesType(TGLUtil::kAxesNone),
fAxesDepthTest(kTRUE),
fReferenceOn(kFALSE),
fReferencePos(0.0, 0.0, 0.0),
fDrawCameraCenter(kFALSE),
fCameraOverlay(0),
fInitGL(kFALSE),
fSmartRefresh(kFALSE),
fDebugMode(kFALSE),
fIsPrinting(kFALSE),
fPictureFileName("viewer.jpg"),
fFader(0),
fGLWidget(0),
fGLDevice(fPad->GetGLDevice()),
fGLCtxId(0),
fIgnoreSizesOnUpdate(kFALSE),
fResetCamerasOnUpdate(kTRUE),
fResetCamerasOnNextUpdate(kFALSE),
fResetCameraOnDoubleClick(kTRUE)
{
InitSecondaryObjects();
if (fGLDevice != -1) {
fGLCtxId = new TGLContextIdentity;
fGLCtxId->AddRef(0);
Int_t viewport[4] = {0};
gGLManager->ExtractViewport(fGLDevice, viewport);
SetViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
}
}
void TGLViewer::InitSecondaryObjects()
{
fLightSet = new TGLLightSet;
fClipSet = new TGLClipSet; fOverlay.push_back(fClipSet);
fSelectedPShapeRef = new TGLManipSet; fOverlay.push_back(fSelectedPShapeRef);
fSelectedPShapeRef->SetDrawBBox(kTRUE);
fCameraOverlay = new TGLCameraOverlay(kFALSE, kFALSE);
AddOverlayElement(fCameraOverlay);
fRedrawTimer = new TGLRedrawTimer(*this);
}
TGLViewer::~TGLViewer()
{
delete fLightSet;
delete fClipSet;
delete fSelectedPShapeRef;
delete fCameraOverlay;
delete fContextMenu;
delete fRedrawTimer;
if (fEventHandler) {
if (fGLWidget)
fGLWidget->SetEventHandler(0);
delete fEventHandler;
}
if (fPad)
fPad->ReleaseViewer3D();
if (fGLDevice != -1)
fGLCtxId->Release(0);
}
void TGLViewer::PadPaint(TVirtualPad* pad)
{
TGLScenePad* scenepad = 0;
for (SceneInfoList_i si = fScenes.begin(); si != fScenes.end(); ++si)
{
scenepad = dynamic_cast<TGLScenePad*>((*si)->GetScene());
if (scenepad && scenepad->GetPad() == pad)
break;
scenepad = 0;
}
if (scenepad == 0)
{
scenepad = new TGLScenePad(pad);
AddScene(scenepad);
}
scenepad->PadPaintFromViewer(this);
PostSceneBuildSetup(fResetCamerasOnNextUpdate || fResetCamerasOnUpdate);
fResetCamerasOnNextUpdate = kFALSE;
RequestDraw();
}
void TGLViewer::UpdateScene()
{
fRedrawTimer->Stop();
for (SceneInfoList_i si = fScenes.begin(); si != fScenes.end(); ++si)
{
TGLScenePad* scenepad = dynamic_cast<TGLScenePad*>((*si)->GetScene());
if (scenepad)
scenepad->PadPaintFromViewer(this);
}
PostSceneBuildSetup(fResetCamerasOnNextUpdate || fResetCamerasOnUpdate);
fResetCamerasOnNextUpdate = kFALSE;
RequestDraw();
}
void TGLViewer::ResetCurrentCamera()
{
MergeSceneBBoxes(fOverallBoundingBox);
CurrentCamera().Setup(fOverallBoundingBox, kTRUE);
}
void TGLViewer::SetupCameras(Bool_t reset)
{
if (IsLocked()) {
Error("TGLViewer::SetupCameras", "expected kUnlocked, found %s", LockName(CurrentLock()));
return;
}
const TGLBoundingBox & box = fOverallBoundingBox;
if (!box.IsEmpty()) {
fPerspectiveCameraYOZ.Setup(box, reset);
fPerspectiveCameraXOZ.Setup(box, reset);
fPerspectiveCameraXOY.Setup(box, reset);
fOrthoXOYCamera.Setup(box, reset);
fOrthoXOZCamera.Setup(box, reset);
fOrthoZOYCamera.Setup(box, reset);
fOrthoXnOYCamera.Setup(box, reset);
fOrthoXnOZCamera.Setup(box, reset);
fOrthoZnOYCamera.Setup(box, reset);
}
}
void TGLViewer::PostSceneBuildSetup(Bool_t resetCameras)
{
MergeSceneBBoxes(fOverallBoundingBox);
SetupCameras(resetCameras);
fReferencePos.Set(fOverallBoundingBox.Center());
RefreshPadEditor(this);
}
void TGLViewer::InitGL()
{
if (fInitGL) {
Error("TGLViewer::InitGL", "GL already initialised");
}
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glClearColor(0.f, 0.f, 0.f, 1.f);
glClearDepth(1.0);
glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
glMaterialf(GL_BACK, GL_SHININESS, 0.0);
glPolygonMode(GL_FRONT, GL_FILL);
glDisable(GL_BLEND);
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
Float_t lmodelAmb[] = {0.5f, 0.5f, 1.f, 1.f};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodelAmb);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
TGLUtil::CheckError("TGLViewer::InitGL");
fInitGL = kTRUE;
}
void TGLViewer::RequestDraw(Short_t LODInput)
{
fRedrawTimer->Stop();
if (!fGLWidget && fGLDevice == -1) {
fRedrawTimer->RequestDraw(100, LODInput);
return;
}
if ( ! TakeLock(kDrawLock)) {
if (gDebug>3) {
Info("TGLViewer::RequestDraw", "viewer locked - requesting another draw.");
}
fRedrawTimer->RequestDraw(100, LODInput);
return;
}
fLOD = LODInput;
if (!gVirtualX->IsCmdThread())
gROOT->ProcessLineFast(Form("((TGLViewer *)0x%lx)->DoDraw()", this));
else
DoDraw();
}
void TGLViewer::PreRender()
{
fCamera = fCurrentCamera;
fClip = fClipSet->GetCurrentClip();
if (fGLDevice != -1)
{
fRnrCtx->SetGLCtxIdentity(fGLCtxId);
fGLCtxId->DeleteGLResources();
}
TGLViewerBase::PreRender();
fLightSet->StdSetupLights(fOverallBoundingBox, *fCamera, fDebugMode);
fClipSet->SetupClips(fOverallBoundingBox);
}
void TGLViewer::DoDraw()
{
fRedrawTimer->Stop();
if (CurrentLock() != kDrawLock) {
if ( ! TakeLock(kDrawLock)) {
Error("TGLViewer::DoDraw", "viewer is %s", LockName(CurrentLock()));
return;
}
}
if (fGLDevice == -1 && (fViewport.Width() <= 1 || fViewport.Height() <= 1)) {
ReleaseLock(kDrawLock);
if (gDebug > 2) {
Info("TGLViewer::DoDraw()", "zero surface area, draw skipped.");
}
return;
}
if (fGLDevice != -1) {
Int_t viewport[4] = {};
gGLManager->ExtractViewport(fGLDevice, viewport);
SetViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
}
TGLStopwatch timer;
if (gDebug>2) {
timer.Start();
}
fRnrCtx->SetRenderTimeOut(fLOD == TGLRnrCtx::kLODHigh ?
fMaxSceneDrawTimeHQ :
fMaxSceneDrawTimeLQ);
fRnrCtx->StartStopwatch();
if (!fIsPrinting) PreDraw();
PreRender();
if (fFader < 1)
{
RenderNonSelected();
DrawGuides();
glClear(GL_DEPTH_BUFFER_BIT);
RenderSelected();
glClear(GL_DEPTH_BUFFER_BIT);
RenderOverlay();
DrawDebugInfo();
}
PostRender();
if (fFader > 0)
{
FadeView(fFader);
}
PostDraw();
fRnrCtx->StopStopwatch();
ReleaseLock(kDrawLock);
if (gDebug>2) {
Info("TGLViewer::DoDraw()", "Took %f msec", timer.End());
}
if (CurrentCamera().UpdateInterest(kFALSE)) {
ResetSceneInfos();
fRedrawTimer->RequestDraw(0, fLOD);
}
if (fLOD != TGLRnrCtx::kLODHigh &&
(fDragAction < kDragCameraRotate || fDragAction > kDragCameraDolly))
{
fRedrawTimer->RequestDraw(100, TGLRnrCtx::kLODHigh);
}
}
Bool_t TGLViewer::SavePicture(const TString &fileName)
{
if (fileName.EndsWith(".gif") || fileName.Contains("gif+") ||
fileName.EndsWith(".jpg") || fileName.EndsWith(".png"))
{
if ( ! TakeLock(kDrawLock)) {
Error("TGLViewer::SavePicture", "viewer locked - try later.");
return kFALSE;
}
std::auto_ptr<TImage>gif(TImage::Create());
fRnrCtx->SetGrabImage(kTRUE);
fLOD = TGLRnrCtx::kLODHigh;
if (!gVirtualX->IsCmdThread())
gROOT->ProcessLineFast(Form("((TGLViewer *)0x%lx)->DoDraw()", this));
else
DoDraw();
gif->FromGLBuffer(fRnrCtx->GetGrabbedImage(), fViewport.Width(), fViewport.Height());
fRnrCtx->SetGrabImage(kFALSE);
delete [] fRnrCtx->GetGrabbedImage();
fRnrCtx->SetGrabbedImage(0);
gif->WriteImage(fileName.Data());
}
else if (fileName.EndsWith(".eps"))
{
TGLOutput::Capture(*this, TGLOutput::kEPS_BSP, fileName.Data());
}
else if (fileName.EndsWith(".pdf"))
{
TGLOutput::Capture(*this, TGLOutput::kPDF_BSP, fileName.Data());
}
else
{
Warning("TGLViewer::SavePicture",
"file %s cannot be saved with this extension.", fileName.Data());
return kFALSE;
}
return kTRUE;
}
Bool_t TGLViewer::SavePicture()
{
return SavePicture(fPictureFileName);
}
void TGLViewer::DrawGuides()
{
Bool_t disabled = kFALSE;
if (fReferenceOn)
{
glDisable(GL_DEPTH_TEST);
TGLUtil::DrawReferenceMarker(*fCamera, fReferencePos);
disabled = kTRUE;
}
if (fDrawCameraCenter)
{
glDisable(GL_DEPTH_TEST);
Float_t radius = fCamera->ViewportDeltaToWorld(TGLVertex3(fCamera->GetCenterVec()), 3, 3).Mag();
const Float_t rgba[4] = { 0, 1, 1, 1.0 };
TGLUtil::DrawSphere(fCamera->GetCenterVec(), radius, rgba);
disabled = kTRUE;
}
if (fAxesDepthTest && disabled)
{
glEnable(GL_DEPTH_TEST);
disabled = kFALSE;
}
else if (fAxesDepthTest == kFALSE && disabled == kFALSE)
{
glDisable(GL_DEPTH_TEST);
disabled = kTRUE;
}
TGLUtil::DrawSimpleAxes(*fCamera, fOverallBoundingBox, fAxesType);
if (disabled)
glEnable(GL_DEPTH_TEST);
}
void TGLViewer::DrawDebugInfo()
{
if (fDebugMode)
{
glDisable(GL_LIGHTING);
CurrentCamera().DrawDebugAids();
glColor3d(0.0, 1.0, 0.0);
fOverallBoundingBox.Draw();
glDisable(GL_DEPTH_TEST);
Double_t size = fOverallBoundingBox.Extents().Mag() / 200.0;
static Float_t white[4] = {1.0, 1.0, 1.0, 1.0};
TGLUtil::DrawSphere(TGLVertex3(0.0, 0.0, 0.0), size, white);
static Float_t green[4] = {0.0, 1.0, 0.0, 1.0};
const TGLVertex3 & center = fOverallBoundingBox.Center();
TGLUtil::DrawSphere(center, size, green);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
}
}
void TGLViewer::PreDraw()
{
MakeCurrent();
if (!fInitGL) {
InitGL();
}
Color_t ci = (fGLDevice != -1) ? gPad->GetFillColor() : fClearColor;
TColor *color = gROOT->GetColor(ci);
if (color)
color->GetRGB(fClearColorRGB[0], fClearColorRGB[1], fClearColorRGB[2]);
else
fClearColorRGB[0] = fClearColorRGB[1] = fClearColorRGB[2] = 1.0f;
glClearColor(fClearColorRGB[0], fClearColorRGB[1], fClearColorRGB[2], 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
TGLUtil::CheckError("TGLViewer::PreDraw");
}
void TGLViewer::PostDraw()
{
glFlush();
if (fRnrCtx->GetGrabImage())
{
UChar_t* xx = new UChar_t[4 * fViewport.Width() * fViewport.Height()];
glReadBuffer(GL_BACK);
glPixelStorei(GL_PACK_ALIGNMENT,1);
glReadPixels(0, 0, fViewport.Width(), fViewport.Height(),
GL_BGRA, GL_UNSIGNED_BYTE, xx);
fRnrCtx->SetGrabbedImage(xx);
}
SwapBuffers();
TGLUtil::CheckError("TGLViewer::PostDraw");
}
void TGLViewer::FadeView(Float_t alpha)
{
static const Float_t z = -1.0f;
glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity();
glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity();
{
TGLCapabilitySwitch blend(GL_BLEND, kTRUE);
TGLCapabilitySwitch light(GL_LIGHTING, kFALSE);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4f(fClearColorRGB[0], fClearColorRGB[1], fClearColorRGB[2], alpha);
glBegin(GL_QUADS);
glVertex3f(-1, -1, z); glVertex3f( 1, -1, z);
glVertex3f( 1, 1, z); glVertex3f(-1, 1, z);
glEnd();
}
glMatrixMode(GL_PROJECTION); glPopMatrix();
glMatrixMode(GL_MODELVIEW); glPopMatrix();
}
void TGLViewer::MakeCurrent() const
{
if (fGLDevice == -1)
fGLWidget->MakeCurrent();
else
gGLManager->MakeCurrent(fGLDevice);
}
void TGLViewer::SwapBuffers() const
{
if ( ! IsDrawOrSelectLock()) {
Error("TGLViewer::SwapBuffers", "viewer is %s", LockName(CurrentLock()));
}
if (fGLDevice == -1)
fGLWidget->SwapBuffers();
else {
gGLManager->ReadGLBuffer(fGLDevice);
gGLManager->Flush(fGLDevice);
gGLManager->MarkForDirectCopy(fGLDevice, kFALSE);
}
}
Bool_t TGLViewer::RequestSelect(Int_t x, Int_t y, Bool_t trySecSel)
{
if ( ! TakeLock(kSelectLock)) {
return kFALSE;
}
if (!gVirtualX->IsCmdThread())
return Bool_t(gROOT->ProcessLineFast(Form("((TGLViewer *)0x%lx)->DoSelect(%d, %d, %s)", this, x, y, trySecSel ? "kTRUE" : "kFALSE")));
else
return DoSelect(x, y, trySecSel);
}
Bool_t TGLViewer::DoSelect(Int_t x, Int_t y, Bool_t trySecSel)
{
if (CurrentLock() != kSelectLock) {
Error("TGLViewer::DoSelect", "expected kSelectLock, found %s", LockName(CurrentLock()));
return kFALSE;
}
MakeCurrent();
fRnrCtx->BeginSelection(x, y, 3);
glRenderMode(GL_SELECT);
PreRender();
Render();
PostRender();
Int_t nHits = glRenderMode(GL_RENDER);
fRnrCtx->EndSelection(nHits);
if (gDebug > 0) Info("TGLViewer::DoSelect", "Primary select nHits=%d.", nHits);
if (nHits > 0)
{
Int_t idx = 0;
if (FindClosestRecord(fSelRec, idx))
{
if (fSelRec.GetTransparent())
{
TGLSelectRecord opaque;
if (FindClosestOpaqueRecord(opaque, ++idx))
fSelRec = opaque;
}
if (gDebug > 1) fSelRec.Print();
}
} else {
fSelRec.Reset();
}
if ( ! trySecSel)
{
ReleaseLock(kSelectLock);
return ! TGLSelectRecord::AreSameSelectionWise(fSelRec, fCurrentSelRec);
}
{
if ( nHits < 1 || ! fSelRec.GetSceneInfo() || ! fSelRec.GetPhysShape() ||
! fSelRec.GetPhysShape()->GetLogical()->SupportsSecondarySelect())
{
if (gDebug > 0)
Info("TGLViewer::DoSelect", "Skipping secondary selection "
"(nPrimHits=%d, sinfo=0x%lx, pshape=0x%lx).\n",
nHits, fSelRec.GetSceneInfo(), fSelRec.GetPhysShape());
ReleaseLock(kSelectLock);
fSecSelRec.Reset();
return kFALSE;
}
TGLSceneInfo* sinfo = fSelRec.GetSceneInfo();
TGLSceneBase* scene = sinfo->GetScene();
TGLPhysicalShape* pshp = fSelRec.GetPhysShape();
SceneInfoList_t foo;
foo.push_back(sinfo);
fScenes.swap(foo);
fRnrCtx->BeginSelection(x, y, 3);
fRnrCtx->SetSecSelection(kTRUE);
glRenderMode(GL_SELECT);
PreRender();
fRnrCtx->SetSceneInfo(sinfo);
scene->PreRender(*fRnrCtx);
fRnrCtx->SetDrawPass(TGLRnrCtx::kPassFill);
fRnrCtx->SetShapeLOD(TGLRnrCtx::kLODHigh);
glPushName(pshp->ID());
pshp->Draw(*fRnrCtx);
glPopName();
scene->PostRender(*fRnrCtx);
fRnrCtx->SetSceneInfo(0);
PostRender();
Int_t nSecHits = glRenderMode(GL_RENDER);
fRnrCtx->EndSelection(nSecHits);
fScenes.swap(foo);
if (gDebug > 0) Info("TGLViewer::DoSelect", "Secondary select nSecHits=%d.", nSecHits);
ReleaseLock(kSelectLock);
if (nSecHits > 0)
{
fSecSelRec = fSelRec;
fSecSelRec.SetRawOnly(fRnrCtx->GetSelectBuffer()->RawRecord(0));
if (gDebug > 1) fSecSelRec.Print();
return kTRUE;
} else {
fSecSelRec.Reset();
return kFALSE;
}
}
}
void TGLViewer::ApplySelection()
{
fCurrentSelRec = fSelRec;
TGLPhysicalShape *selPhys = fSelRec.GetPhysShape();
fSelectedPShapeRef->SetPShape(selPhys);
SelectionChanged();
RequestDraw(TGLRnrCtx::kLODHigh);
}
Bool_t TGLViewer::RequestOverlaySelect(Int_t x, Int_t y)
{
if ( ! TakeLock(kSelectLock)) {
return kFALSE;
}
if (!gVirtualX->IsCmdThread())
return Bool_t(gROOT->ProcessLineFast(Form("((TGLViewer *)0x%lx)->DoSelect(%d, %d)", this, x, y)));
else
return DoOverlaySelect(x, y);
}
Bool_t TGLViewer::DoOverlaySelect(Int_t x, Int_t y)
{
if (CurrentLock() != kSelectLock) {
Error("TGLViewer::DoOverlaySelect", "expected kSelectLock, found %s", LockName(CurrentLock()));
return kFALSE;
}
MakeCurrent();
fRnrCtx->BeginSelection(x, y, 3);
glRenderMode(GL_SELECT);
PreRenderOverlaySelection();
RenderOverlay();
PostRenderOverlaySelection();
Int_t nHits = glRenderMode(GL_RENDER);
fRnrCtx->EndSelection(nHits);
TGLOverlayElement * selElm = 0;
if (nHits > 0)
{
Int_t idx = 0;
while (idx < nHits && FindClosestOverlayRecord(fOvlSelRec, idx))
{
TGLOverlayElement* el = fOvlSelRec.GetOvlElement();
if (el == fCurrentOvlElm)
{
if (el->MouseStillInside(fOvlSelRec))
{
selElm = el;
break;
}
}
else if (el->MouseEnter(fOvlSelRec))
{
selElm = el;
break;
}
++idx;
}
}
else
{
fOvlSelRec.Reset();
}
ReleaseLock(kSelectLock);
if (fCurrentOvlElm != selElm)
{
if (fCurrentOvlElm) fCurrentOvlElm->MouseLeave();
fCurrentOvlElm = selElm;
return kTRUE;
}
else
{
return kFALSE;
}
}
void TGLFaderHelper::MakeFadeStep()
{
Float_t fade = fViewer->GetFader();
if (fade == fFadeTarget) {
delete this; return;
}
if (TMath::Abs(fFadeTarget - fade) < 1e-3) {
fViewer->SetFader(fFadeTarget);
fViewer->RequestDraw(TGLRnrCtx::kLODHigh);
delete this;
return;
}
Float_t dt = fTime/fNSteps;
Float_t df = (fFadeTarget - fade)/fNSteps;
fViewer->SetFader(fade + df);
fViewer->RequestDraw(TGLRnrCtx::kLODHigh);
fTime -= dt; --fNSteps;
TTimer::SingleShot(TMath::CeilNint(1000*dt),
"TGLFaderHelper", this, "MakeFadeStep()");
}
void TGLViewer::AutoFade(Float_t fade, Float_t time, Int_t steps)
{
TGLFaderHelper* fh = new TGLFaderHelper(this, fade, time, steps);
fh->MakeFadeStep();
}
void TGLViewer::SetViewport(Int_t x, Int_t y, Int_t width, Int_t height)
{
if (IsLocked() && fGLDevice == -1) {
Error("TGLViewer::SetViewport", "expected kUnlocked, found %s", LockName(CurrentLock()));
return;
}
if (fViewport.X() == x && fViewport.Y() == y &&
fViewport.Width() == width && fViewport.Height() == height) {
return;
}
fViewport.Set(x, y, width, height);
fCurrentCamera->SetViewport(fViewport);
if (gDebug > 2) {
Info("TGLViewer::SetViewport", "updated - corner %d,%d dimensions %d,%d", x, y, width, height);
}
}
TGLCamera& TGLViewer::RefCamera(ECameraType cameraType)
{
switch(cameraType) {
case kCameraPerspXOZ:
return fPerspectiveCameraXOZ;
case kCameraPerspYOZ:
return fPerspectiveCameraYOZ;
case kCameraPerspXOY:
return fPerspectiveCameraXOY;
case kCameraOrthoXOY:
return fOrthoXOYCamera;
case kCameraOrthoXOZ:
return fOrthoXOZCamera;
case kCameraOrthoZOY:
return fOrthoZOYCamera;
case kCameraOrthoXnOY:
return fOrthoXnOYCamera;
case kCameraOrthoXnOZ:
return fOrthoXnOZCamera;
case kCameraOrthoZnOY:
return fOrthoZnOYCamera;
default:
Error("TGLViewer::SetCurrentCamera", "invalid camera type");
return *fCurrentCamera;
}
}
void TGLViewer::SetCurrentCamera(ECameraType cameraType)
{
if (IsLocked()) {
Error("TGLViewer::SetCurrentCamera", "expected kUnlocked, found %s", LockName(CurrentLock()));
return;
}
switch(cameraType) {
case kCameraPerspXOZ: {
fCurrentCamera = &fPerspectiveCameraXOZ;
break;
}
case kCameraPerspYOZ: {
fCurrentCamera = &fPerspectiveCameraYOZ;
break;
}
case kCameraPerspXOY: {
fCurrentCamera = &fPerspectiveCameraXOY;
break;
}
case kCameraOrthoXOY: {
fCurrentCamera = &fOrthoXOYCamera;
break;
}
case kCameraOrthoXOZ: {
fCurrentCamera = &fOrthoXOZCamera;
break;
}
case kCameraOrthoZOY: {
fCurrentCamera = &fOrthoZOYCamera;
break;
}
case kCameraOrthoXnOY: {
fCurrentCamera = &fOrthoXnOYCamera;
break;
}
case kCameraOrthoXnOZ: {
fCurrentCamera = &fOrthoXnOZCamera;
break;
}
case kCameraOrthoZnOY: {
fCurrentCamera = &fOrthoZnOYCamera;
break;
}
default: {
Error("TGLViewer::SetCurrentCamera", "invalid camera type");
break;
}
}
fCurrentCamera->SetViewport(fViewport);
RefreshPadEditor(this);
RequestDraw(TGLRnrCtx::kLODHigh);
}
void TGLViewer::SetOrthoCamera(ECameraType camera,
Double_t zoom, Double_t dolly,
Double_t center[3],
Double_t hRotate, Double_t vRotate)
{
switch(camera) {
case kCameraOrthoXOY: {
fOrthoXOYCamera.Configure(zoom, dolly, center, hRotate, vRotate);
if (fCurrentCamera == &fOrthoXOYCamera) {
RequestDraw(TGLRnrCtx::kLODHigh);
}
break;
}
case kCameraOrthoXOZ: {
fOrthoXOZCamera.Configure(zoom, dolly, center, hRotate, vRotate);
if (fCurrentCamera == &fOrthoXOZCamera) {
RequestDraw(TGLRnrCtx::kLODHigh);
}
break;
}
case kCameraOrthoZOY: {
fOrthoZOYCamera.Configure(zoom, dolly, center, hRotate, vRotate);
if (fCurrentCamera == &fOrthoZOYCamera) {
RequestDraw(TGLRnrCtx::kLODHigh);
}
break;
}
default: {
Error("TGLViewer::SetOrthoCamera", "invalid camera type");
break;
}
}
}
void TGLViewer::SetPerspectiveCamera(ECameraType camera,
Double_t fov, Double_t dolly,
Double_t center[3],
Double_t hRotate, Double_t vRotate)
{
switch(camera) {
case kCameraPerspXOZ: {
fPerspectiveCameraXOZ.Configure(fov, dolly, center, hRotate, vRotate);
if (fCurrentCamera == &fPerspectiveCameraXOZ) {
RequestDraw(TGLRnrCtx::kLODHigh);
}
break;
}
case kCameraPerspYOZ: {
fPerspectiveCameraYOZ.Configure(fov, dolly, center, hRotate, vRotate);
if (fCurrentCamera == &fPerspectiveCameraYOZ) {
RequestDraw(TGLRnrCtx::kLODHigh);
}
break;
}
case kCameraPerspXOY: {
fPerspectiveCameraXOY.Configure(fov, dolly, center, hRotate, vRotate);
if (fCurrentCamera == &fPerspectiveCameraXOY) {
RequestDraw(TGLRnrCtx::kLODHigh);
}
break;
}
default: {
Error("TGLViewer::SetPerspectiveCamera", "invalid camera type");
break;
}
}
}
void TGLViewer::GetGuideState(Int_t & axesType, Bool_t & axesDepthTest, Bool_t & referenceOn, Double_t referencePos[3]) const
{
axesType = fAxesType;
axesDepthTest = fAxesDepthTest;
referenceOn = fReferenceOn;
referencePos[0] = fReferencePos.X();
referencePos[1] = fReferencePos.Y();
referencePos[2] = fReferencePos.Z();
}
void TGLViewer::SetGuideState(Int_t axesType, Bool_t axesDepthTest, Bool_t referenceOn, const Double_t referencePos[3])
{
fAxesType = axesType;
fAxesDepthTest = axesDepthTest;
fReferenceOn = referenceOn;
if (referencePos)
fReferencePos.Set(referencePos[0], referencePos[1], referencePos[2]);
if (fGLDevice != -1)
gGLManager->MarkForDirectCopy(fGLDevice, kTRUE);
RequestDraw();
}
void TGLViewer::SetDrawCameraCenter(Bool_t x)
{
fDrawCameraCenter = x;
RequestDraw();
}
const TGLPhysicalShape * TGLViewer::GetSelected() const
{
return fSelectedPShapeRef->GetPShape();
}
void TGLViewer::SelectionChanged()
{
Emit("SelectionChanged()");
}
void TGLViewer::OverlayDragFinished()
{
Emit("OverlayDragFinished()");
}
void TGLViewer::MouseOver(TGLPhysicalShape *shape)
{
Emit("MouseOver(TGLPhysicalShape*)", (Long_t)shape);
}
void TGLViewer::MouseOver(TGLPhysicalShape *shape, UInt_t state)
{
Long_t args[2];
args[0] = (Long_t)shape;
args[1] = state;
Emit("MouseOver(TGLPhysicalShape*,UInt_t)", args);
}
void TGLViewer::Clicked(TObject *obj)
{
Emit("Clicked(TObject*)", (Long_t)obj);
}
void TGLViewer::Clicked(TObject *obj, UInt_t button, UInt_t state)
{
Long_t args[3];
args[0] = (Long_t)obj;
args[1] = button;
args[2] = state;
Emit("Clicked(TObject*,UInt_t,UInt_t)", args);
}
void TGLViewer::MouseIdle(TGLPhysicalShape *shape, UInt_t posx, UInt_t posy)
{
Long_t args[3];
static UInt_t oldx = 0, oldy = 0;
if (oldx != posx || oldy != posy) {
args[0] = (Long_t)shape;
args[1] = posx;
args[2] = posy;
Emit("MouseIdle(TGLPhysicalShape*,UInt_t,UInt_t)", args);
oldx = posx;
oldy = posy;
}
}
Int_t TGLViewer::DistancetoPrimitive(Int_t , Int_t )
{
gPad->SetSelected(this);
return 0;
}
void TGLViewer::ExecuteEvent(Int_t event, Int_t px, Int_t py)
{
if (fEventHandler)
return fEventHandler->ExecuteEvent(event, px, py);
}
void TGLViewer::PrintObjects()
{
TGLOutput::Capture(*this);
}
void TGLViewer::SetEventHandler(TGEventHandler *handler)
{
if (fEventHandler)
delete fEventHandler;
fEventHandler = handler;
if (fGLWidget)
fGLWidget->SetEventHandler(fEventHandler);
}
void TGLViewer::ClearCurrentOvlElm()
{
if (fCurrentOvlElm)
{
fCurrentOvlElm->MouseLeave();
fCurrentOvlElm = 0;
RequestDraw();
}
}
Last change: Mon Dec 8 10:00:29 2008
Last generated: 2008-12-08 10:00
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.