Logo ROOT   6.14/05
Reference Guide
TVirtualViewer3D.cxx
Go to the documentation of this file.
1 // @(#)root/base:$Id$
2 // Author: Olivier Couet 05/10/2004
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 /** \class TVirtualViewer3D
13 \ingroup Base
14 
15 Abstract 3D shapes viewer.
16 
17 The concrete implementations are:
18 
19  - TViewerX3D : X3d viewer
20  - TGLViewer : OpenGL viewer
21 
22 ## 3D Viewer Infrastructure Overview
23 
24 The 3D Viewer infrastructure consists of:
25 
26  - TVirtualViewer3D interface: An abstract handle to the viewer, allowing
27  client to test preferences, add objects, control the viewer via scripting
28  (to be added) etc.
29  -TBuffer3D class hierarchy: Used to describe 3D objects
30  ("shapes")
31  - filled /added by negotiation with viewer via TVirtualViewer3D.
32 
33 
34 Together these allow clients to publish objects to any one of the 3D viewers
35  (currently OpenGL/x3d,TPad), free of viewer specific drawing code. They allow
36  our simple x3d viewer, and considerably more sophisticated OpenGL one to both
37  work with both geometry libraries (g3d and geom) efficiently.
38 
39 Publishing to a viewer consists of the following steps:
40 
41  1. Create / obtain viewer handle
42  2. Begin scene on viewer
43  3. Fill mandatory parts of TBuffer3D describing object
44  4. Add to viewer
45  5. Fill optional parts of TBuffer3D if requested by viewer, and add again
46  ... repeat 3/4/5 as required
47  6. End scene on viewer
48 
49 ## Creating / Obtaining Viewer
50 
51 Create/obtain the viewer handle via local/global pad - the viewer is always
52 bound to a TPad object at present [This may be removed as a restriction in
53 the future] . You should perform the publishing to the viewer described below
54 in the Paint() method of the object you attach to the pad (via Draw())
55 ~~~ {.cpp}
56 TVirtualViewer3D * v = gPad->GetViewer3D("xxxx");
57 ~~~
58 
59 " xxxx" is viewer type: OpenGL "ogl", X3D "x3d" or
60 Pad "pad" (default). The viewer is created via the plugin manager,
61 attached to pad, and the interface returned.
62 
63 ## Begin / End Scene
64 
65 Objects must be added to viewer between BeginScene/EndScene calls e.g.
66 ~~~ {.cpp}
67 v->BeginScene();
68 .....
69 v->AddObject(....);
70 v->AddObject(....);
71 .....
72 v->EndScene();
73 ~~~
74 
75 The BeginScene call will cause the viewer to suspend redraws etc, and after
76 the EndScene the viewer will reset the camera to frame the new scene and redraw.
77 [x3d viewer does not support changing of scenes - objects added after the
78 first Open/CloseScene pair will be ignored.]
79 
80 
81 ## Filling TBuffer3D and Adding to Viewer
82 
83 The viewers behind the TVirtualViewer3D interface differ greatly in their
84 capabilities e.g.
85 
86  - Some know how to draw certain shapes natively (e.g. spheres/tubes in
87  OpenGL) - others always require a raw tessellation description of points/lines/segments.
88  - Some need the 3D object positions in the global frame, others can cope with
89  local frames + a translation matrix - which can give considerable performance
90  benefits.
91 
92 To cope with these situations the object buffer is filled out in negotiation
93 with the viewer. TBuffer3D classes are conceptually divided into enumerated
94 sections Core, BoundingBox, Raw etc (see TBuffer3D.h for more details).
95 \image html base_tbuffer3d.png
96 
97 The SectionsValid() / SetSectionsValid / ClearSectionsValid() methods of TBuffer3D
98 are used to test/set/clear these section valid flags.
99 
100 The sections found in TBuffer3D (Core/BoundingBox/Raw Sizes/Raw)
101 are sufficient to describe any tessellated shape in a generic fashion.
102 An additional ShapeSpecific section
103 in derived shape specific classes allows a more abstract shape description
104 ("a sphere of inner radius x, outer radius y"). This enables a viewer
105 which knows how to draw (tessellate) the shape itself to do so, which can bring
106 considerable performance and quality benefits, while providing a generic fallback
107 suitable for all viewers.
108 
109 The rules for client negotiation with the viewer are:
110 
111  - If suitable specialized TBuffer3D class exists, use it, otherwise use TBuffer3D.
112  - Complete the mandatory Core section.
113  - Complete the ShapeSpecific section if applicable.
114  - Complete the BoundingBox if you can.
115  - Pass this buffer to the viewer using one of the AddObject() methods - see below.
116 
117 If the viewer requires more sections to be completed (Raw/RawSizes) AddObject()
118 will return flags indicating which ones, otherwise it returns kNone. You must
119 fill the buffer and mark these sections valid, and pass the buffer again. A
120 typical code snippet would be:
121 ~~~ {.cpp}
122 TBuffer3DSphere sphereBuffer;
123 // Fill out kCore...
124 // Fill out kBoundingBox...
125 // Fill out kShapeSpecific for TBuffer3DSphere
126 // Try first add to viewer
127 Int_t reqSections = viewer->AddObject(buffer);
128 if (reqSections != TBuffer3D::kNone) {
129  if (reqSections & TBuffer3D::kRawSizes) {
130  // Fill out kRawSizes...
131  }
132  if (reqSections & TBuffer3D::kRaw) {
133  // Fill out kRaw...
134  }
135  // Add second time to viewer - ignore return cannot do more
136  viewer->AddObject(buffer);
137  }
138 }
139 ~~~
140 
141 ShapeSpecific: If the viewer can directly display the buffer without
142 filling of the kRaw/kRawSizes section it will not need to request client side
143 tessellation. Currently we provide the following various shape specific classes,
144 which the OpenGL viewer can take advantage of (see TBuffer3D.h and TBuffer3DTypes.h)
145 
146  - TBuffer3DSphere - solid, hollow and cut spheres*
147  - TBuffer3DTubeSeg - angle tube segment
148  - TBuffer3DCutTube - angle tube segment with plane cut ends.
149 
150 *OpenGL only supports solid spheres at present - cut/hollow ones will be
151 requested tessellated.
152 
153 Anyone is free to add new TBuffer3D classes, but it should be clear that the
154 viewers require updating to be able to take advantage of them. The number of
155 native shapes in OpenGL will be expanded over time.
156 
157 BoundingBox: You are not obliged to complete this, as any viewer
158 requiring one internally (OpenGL) will build one for you if you do not provide.
159 However to do this the viewer will force you to provide the raw tessellation, and the
160 resulting box will be axis aligned with the overall scene, which is non-ideal
161 for rotated shapes.
162 
163 As we need to support orientated (rotated) bounding boxes, TBuffer3D requires
164  the 6 vertices of the box. We also provide a convenience function, SetAABoundingBox(),
165  for simpler case of setting an axis aligned bounding box.
166 
167 ## Master/Local Reference Frames
168 
169 The Core section of TBuffer3D contains two members relating to reference frames:
170 fLocalFrame & fLocalMaster. fLocalFrame indicates if any positions in the buffer
171 (bounding box and tessellation vertexes) are in local or master (world frame).
172 fLocalMaster is a standard 4x4 translation matrix (OpenGL column major ordering)
173 for placing the object into the 3D master frame.
174 
175 If fLocalFrame is kFALSE, fLocalMaster should contain an identity matrix. This
176 is set by default, and can be reset using SetLocalMasterIdentity() function.
177 
178 ### Logical & Physical Objects
179 
180 There are two cases of object addition:
181 
182  - Add this object as a single independent entity in the world reference frame.
183  - Add a physical placement (copy) of this logical object (described in local reference frame).
184 
185 The second case is very typical in geometry packages, GEANT4, where we have
186 very large number repeated placements of relatively few logical (unique) shapes.
187 Some viewers (OpenGL only at present) are able to take advantage of this by
188 identifying unique logical shapes from the fID logical ID member of
189 TBuffer3D. If repeated addition of the same fID is found, the shape
190 is cached already - and the costly tessellation does not need to be sent again.
191 The viewer can also perform internal GL specific caching with considerable performance gains
192 in these cases.
193 
194 For this to work correctly the logical object in must be described in TBuffer3D
195 in the local reference frame, complete with the local/master translation. The
196 viewer indicates this through the interface method
197 ~~~ {.cpp}
198 PreferLocalFrame()
199 ~~~
200 
201 If this returns kTRUE you can make repeated calls to AddObject(), with TBuffer3D
202 containing the same fID, and different fLocalMaster placements.
203 
204 For viewers supporting logical/physical objects, the TBuffer3D content refers
205 to the properties of logical object, with the fLocalMaster transform and the
206 fColor and fTransparency attributes, which can be varied for each physical
207 object.
208 
209 As a minimum requirement all clients must be capable of filling the raw tessellation
210 of the object buffer, in the master reference frame. Conversely viewers must
211 always be capable of displaying the object described by this buffer.
212 
213 ## Scene Rebuilds
214 
215 It should be understood that AddObject is not an explicit command to the viewer
216  - it may for various reasons decide to ignore it:
217 
218  - It already has the object internally cached .
219  - The object falls outside some 'interest' limits of the viewer camera.
220  - The object is too small to be worth drawing.
221 
222 In all these cases AddObject() returns kNone, as it does for successful addition,
223 simply indicating it does not require you to provide further information about
224 this object. You should not try to make any assumptions about what the viewer did with it.
225 
226 This enables the viewer to be connected to a client which sends potentially
227 millions of objects, and only accept those that are of interest at a certain
228 time, caching the relatively small number of CPU/memory costly logical shapes,
229 and retaining/discarding the physical placements as required. The viewer may
230 decide to force the client to rebuild (republish) the scene (via a TPad
231 repaint at present), and thus collect these objects if the
232 internal viewer state changes. It does this presently by forcing a repaint
233 on the attached TPad object - hence the reason for putting all publishing to
234 the viewer in the attached pad objects Paint() method. We will likely remove
235 this requirement in the future, indicating the rebuild request via a normal
236 ROOT signal, which the client can detect.
237 
238 ## Physical IDs
239 
240 TVirtualViewer3D provides for two methods of object addition:virtual Int_t AddObject(const
241 TBuffer3D & buffer, Bool_t * addChildren = 0)
242 
243 ~~~ {.cpp}
244 virtual Int_t AddObject(UInt_t physicalID, const TBuffer3D & buffer, Bool_t * addChildren = 0)
245 ~~~
246 
247 If you use the first (simple) case a viewer using logical/physical pairs
248 will generate IDs for each physical object internally. In the second you
249 can specify a unique identifier from the client, which allows the viewer to be more
250 efficient. It can now cache both logical and physical objects, and only discard
251 physical objects no longer of interest as part of scene rebuilds.
252 
253 ## Child Objects
254 
255 In many geometries there is a rigid containment hierarchy, and so if the viewer
256 is not interested in a certain object due to limits/size then it will also
257 not be interest in any of the contained branch of descendents. Both AddObject()
258 methods have an addChildren parameter. The viewer will complete this (if passed)
259 indicating if children (contained within the one just sent) are worth adding.
260 
261 ## Recycling TBuffer3D
262 
263 Once add AddObject() has been called, the contents are copied to the viewer
264 internally. You are free to destroy this object, or recycle it for the next
265 object if suitable.
266 */
267 
268 #include "TVirtualViewer3D.h"
269 #include "TVirtualPad.h"
270 #include "TPluginManager.h"
271 #include "TError.h"
272 #include "TClass.h"
273 
275 
276 // pin the vtable here.
278 
279 ////////////////////////////////////////////////////////////////////////////////
280 /// Create a Viewer 3D of specified type.
281 
283 {
284  TVirtualViewer3D *viewer = 0;
285  TPluginHandler *h;
286  if ((h = gPluginMgr->FindHandler("TVirtualViewer3D", type))) {
287  if (h->LoadPlugin() == -1)
288  return 0;
289 
290  if (!pad) {
291  viewer = (TVirtualViewer3D *) h->ExecPlugin(1, gPad);
292  } else {
293  viewer = (TVirtualViewer3D *) h->ExecPlugin(1, pad);
294  }
295  }
296  return viewer;
297 }
const char Option_t
Definition: RtypesCore.h:62
TPluginHandler * FindHandler(const char *base, const char *uri=0)
Returns the handler if there exists a handler for the specified URI.
Int_t LoadPlugin()
Load the plugin library for this handler.
Abstract 3D shapes viewer.
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition: TVirtualPad.h:49
R__EXTERN TPluginManager * gPluginMgr
Long_t ExecPlugin(int nargs, const T &... params)
static TVirtualViewer3D * Viewer3D(TVirtualPad *pad=0, Option_t *type="")
Create a Viewer 3D of specified type.
#define h(i)
Definition: RSha256.hxx:106
#define ClassImp(name)
Definition: Rtypes.h:359
int type
Definition: TGX11.cxx:120
#define gPad
Definition: TVirtualPad.h:285