Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TGOSXGL.mm
Go to the documentation of this file.
1 #include <utility>
2 #include <cassert>
3 #include <vector>
4 
5 #include <Foundation/Foundation.h>
6 
7 #include "TVirtualViewer3D.h"
8 #include "TSeqCollection.h"
9 #include "TVirtualGL.h"
10 #include "TVirtualX.h"
11 #include "TGOSXGL.h"
12 #include "TROOT.h"
13 #include "TEnv.h"
14 
15 
16 ClassImp(TGOSXGLManager)
17 
18 //______________________________________________________________________________
19 TGOSXGLManager::TGOSXGLManager()
20 {
21  //Constructor.
22 
23  //gGLManager is a singleton, it's created by the plugin manager
24  //(either from TRootCanvas or TRootEmbeddedCanvas),
25  //never by user.
26 
27  assert(gGLManager == 0 && "TGOSXGLManager, gGLManager is initialized");
28  gGLManager = this;
29 
30  if (gROOT && gROOT->GetListOfSpecials())
31  gROOT->GetListOfSpecials()->Add(this);
32 }
33 
34 
35 //______________________________________________________________________________
36 TGOSXGLManager::~TGOSXGLManager()
37 {
38  //Destructor.
39 
40  if (gROOT && gROOT->GetListOfSpecials())
41  gROOT->GetListOfSpecials()->Remove(this);
42 }
43 
44 
45 //______________________________________________________________________________
46 Int_t TGOSXGLManager::InitGLWindow(Window_t parentID)
47 {
48  typedef std::pair<UInt_t, Int_t> component_type;
49 
50  std::vector<component_type> format;//Where is the hummer when you need one??? (I mean C++11 initializers '{xxx}').
51 
52  format.push_back(component_type(Rgl::kDoubleBuffer, 1));//1 means nothing, kDoubleBuffer is enough :)
53  format.push_back(component_type(Rgl::kStencil, 8));
54  format.push_back(component_type(Rgl::kDepth, 32));
55 
56  if (gEnv) {
57  const Int_t nSamples = gEnv->GetValue("OpenGL.Framebuffer.Multisample", 0);
58  if (nSamples > 0 && nSamples <= 8) //TODO: check the 'upper limit' using API, not hardcoded 8.
59  format.push_back(component_type(Rgl::kMultiSample, nSamples));
60  }
61 
62  //Now, the interface is quite ugly, that's why it's called TVirtualX :)
63  Int_t x = 0, y = 0;
64  UInt_t width = 0, height = 0;
65  gVirtualX->GetWindowSize(parentID, x, y, width, height);
66 
67  const Window_t glWin = gVirtualX->CreateOpenGLWindow(parentID, width, height, format);
68  if (glWin != kNone) {
69  //TRootCanvas/TRootEmbeddedCanvas never do this,
70  //so ...
71  gVirtualX->MapWindow(glWin);
72  }
73 
74  //Window_t is long, in principle it's a potential problem: do I need a mapping?
75  //But if you have billions of windows ... ;)
76  return Int_t(glWin);
77 }
78 
79 
80 //______________________________________________________________________________
81 Int_t TGOSXGLManager::CreateGLContext(Int_t winID)
82 {
83  //Called from TRootCanvas, it never shares :) -> the second parameter is kNone.
84  //Handle_t is long, I'm converting to int, which can be a problem if you ...
85  //have billions of gl contexts :)
86  const Handle_t ctx = gVirtualX->CreateOpenGLContext(winID, kNone);
87  fCtxToWin[ctx] = Window_t(winID);
88 
89  return Int_t(ctx);
90 }
91 
92 //______________________________________________________________________________
93 void TGOSXGLManager::DeleteGLContext(Int_t ctxInd)
94 {
95  //Just delegate.
96  gVirtualX->DeleteOpenGLContext(ctxInd);
97 }
98 
99 //______________________________________________________________________________
100 Bool_t TGOSXGLManager::MakeCurrent(Int_t ctxInd)
101 {
102  assert(fCtxToWin.find(Handle_t(ctxInd)) != fCtxToWin.end() &&
103  "MakeCurrent, window not found for a given context");
104 
105  return gVirtualX->MakeOpenGLContextCurrent(Handle_t(ctxInd), fCtxToWin[Handle_t(ctxInd)]);
106 }
107 
108 //______________________________________________________________________________
109 void TGOSXGLManager::Flush(Int_t ctxInd)
110 {
111  gVirtualX->FlushOpenGLBuffer(ctxInd);
112 }
113 
114 
115 //______________________________________________________________________________
116 Int_t TGOSXGLManager::GetVirtualXInd(Int_t ctxInd)
117 {
118  return ctxInd;
119 }
120 
121 //A bunch of (now) noop functions - this is a legacy from the time when
122 //we had a real off-screen OpenGL rendering. Nowadays we always do it "on-screen"
123 
124 //______________________________________________________________________________
125 Bool_t TGOSXGLManager::AttachOffScreenDevice(Int_t, Int_t, Int_t, UInt_t, UInt_t)
126 {
127  //NOOP.
128  return kFALSE;
129 }
130 
131 
132 //______________________________________________________________________________
133 Bool_t TGOSXGLManager::ResizeOffScreenDevice(Int_t, Int_t, Int_t, UInt_t, UInt_t)
134 {
135  //NOOP.
136  return kFALSE;
137 }
138 
139 
140 //______________________________________________________________________________
141 void TGOSXGLManager::SelectOffScreenDevice(Int_t)
142 {
143  //NOOP.
144 }
145 
146 
147 //______________________________________________________________________________
148 void TGOSXGLManager::MarkForDirectCopy(Int_t, Bool_t)
149 {
150  //NOOP.
151 }
152 
153 //______________________________________________________________________________
154 void TGOSXGLManager::ExtractViewport(Int_t, Int_t *)
155 {
156  //NOOP.
157 }
158 
159 //______________________________________________________________________________
160 void TGOSXGLManager::ReadGLBuffer(Int_t)
161 {
162  //NOOP.
163 }
164 
165 //These 'delegating' functions are legacy - were required (many years ago) on Windows.
166 
167 //______________________________________________________________________________
168 Bool_t TGOSXGLManager::SelectManip(TVirtualGLManip *manip, const TGLCamera *camera, const TGLRect *rect, const TGLBoundingBox *sceneBox)
169 {
170  //Why all this mess with pointers/references and not pointers/references everywhere???
171 
172  assert(manip != 0 && "SelectManip, parameter 'manip' is null");
173  assert(camera != 0 && "SelectManip, parameter 'camera' is null");
174  assert(rect != 0 && "SelectManip, parameter 'rect' is null");
175  assert(sceneBox != 0 && "SelectManip, parameter 'sceneBox' is null");
176 
177  // Select manipulator.
178  return manip->Select(*camera, *rect, *sceneBox);
179 }
180 
181 //______________________________________________________________________________
182 Bool_t TGOSXGLManager::PlotSelected(TVirtualGLPainter *plot, Int_t px, Int_t py)
183 {
184  //Analog of TObject::DistancetoPrimitive
185  assert(plot != 0 && "PlotSelected, parameter 'plot' is null");
186 
187  return plot->PlotSelected(px, py);
188 }
189 
190 //______________________________________________________________________________
191 char *TGOSXGLManager::GetPlotInfo(TVirtualGLPainter *plot, Int_t px, Int_t py)
192 {
193  //Analog of TObject::GetObjectInfo
194  assert(plot != 0 && "GetPlotInfo, parameter 'plot' is null");
195 
196  return plot->GetPlotInfo(px, py);
197 }
198 
199 //______________________________________________________________________________
200 void TGOSXGLManager::PaintSingleObject(TVirtualGLPainter *p)
201 {
202  // Paint a single object.
203  assert(p != 0 && "PaintSingleObject, parameter 'p' is null");
204 
205  p->Paint();
206 }
207 
208 //______________________________________________________________________________
209 void TGOSXGLManager::PanObject(TVirtualGLPainter *object, Int_t x, Int_t y)
210 {
211  // Pan objects.
212  assert(object != 0 && "PanObject, parameter 'object' is null");
213 
214  return object->Pan(x, y);
215 }
216 
217 //______________________________________________________________________________
218 void TGOSXGLManager::PrintViewer(TVirtualViewer3D *vv)
219 {
220  // Print viewer.
221  assert(vv != 0 && "PrintViewer, parameter 'vv' is null");
222 
223  vv->PrintObjects();
224 }