00001 // @(#)root/base:$Id: TVirtualViewer3D.cxx 20877 2007-11-19 11:17:07Z rdm $ 00002 // Author: Olivier Couet 05/10/2004 00003 00004 /************************************************************************* 00005 * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers. * 00006 * All rights reserved. * 00007 * * 00008 * For the licensing terms see $ROOTSYS/LICENSE. * 00009 * For the list of contributors see $ROOTSYS/README/CREDITS. * 00010 *************************************************************************/ 00011 00012 ////////////////////////////////////////////////////////////////////////// 00013 // // 00014 // TVirtualViewer3D // 00015 // // 00016 // Abstract 3D shapes viewer. The concrete implementations are: // 00017 // // 00018 // TViewerX3D : X3d viewer // 00019 // TGLViewer : OpenGL viewer // 00020 // // 00021 ////////////////////////////////////////////////////////////////////////// 00022 //BEGIN_HTML <!-- 00023 /* --> 00024 <h4>3D Viewer Infrastructure Overview</h4> 00025 <p>The 3D Viewer infrastructure consists of:</p> 00026 <ul> 00027 <li> TVirtualViewer3D interface: An abstract handle to the viewer, allowing 00028 client to test preferences, add objects, control the viewer via scripting 00029 (to be added) etc.</li> 00030 <li>TBuffer3D class hierarchy: Used to describe 3D objects 00031 ("shapes") 00032 - filled /added by negotiation with viewer via TVirtualViewer3D.</li> 00033 </ul> 00034 <p>Together these allow clients to publish objects to any one of the 3D viewers 00035 (currently OpenGL/x3d,TPad), free of viewer specific drawing code. They allow 00036 our simple x3d viewer, and considerably more sophisticated OpenGL one to both 00037 work with both geometry libraries (g3d and geom) efficiently.</p> 00038 <p>Publishing to a viewer consists of the following steps:</p> 00039 <ol> 00040 <li> Create / obtain viewer handle</li> 00041 <li>Begin scene on viewer</li> 00042 <li>Fill mandatory parts of TBuffer3D describing object</li> 00043 <li>Add to viewer</li> 00044 <li>Fill optional parts of TBuffer3D if requested by viewer, and add again<br> 00045 ... repeat 3/4/5 00046 as required</li> 00047 <li>End scene on viewer</li> 00048 </ol> 00049 <h4>Creating / Obtaining Viewer</h4> 00050 <p>Create/obtain the viewer handle via local/global pad - the viewer is always 00051 bound to a TPad object at present [This may be removed as a restriction in 00052 the future] . You should perform the publishing to the viewer described below 00053 in the Paint() method of the object you attach to the pad (via Draw())</p> 00054 <pre>TVirtualViewer3D * v = gPad->GetViewer3D("xxxx");</pre> 00055 <p>" xxxx" is viewer type: OpenGL "ogl", X3D "x3d" or 00056 Pad "pad" (default). The viewer is created via the plugin manager, 00057 attached to pad, and the interface returned.</p> 00058 <h4> Begin / End Scene</h4> 00059 <p>Objects must be added to viewer between BeginScene/EndScene calls e.g.</p> 00060 <pre>v->BeginScene(); 00061 ..... 00062 v->AddObject(....); 00063 v->AddObject(....); 00064 ..... 00065 v->EndScene();</pre> 00066 <p>The BeginScene call will cause the viewer to suspend redraws etc, and after 00067 the EndScene the viewer will reset the camera to frame the new scene and redraw.</p> 00068 [x3d viewer does not support changing of scenes - objects added after the 00069 first Open/CloseScene pair will be ignored.]<br> 00070 <h4>Filling TBuffer3D and Adding to Viewer</h4> 00071 <p>The viewers behind the TVirtualViewer3D interface differ greatly in their 00072 capabilities e.g.</p> 00073 <ul> 00074 <li> Some know how to draw certain shapes natively (e.g. spheres/tubes in 00075 OpenGL) - others always require a raw tessellation description of points/lines/segments.</li> 00076 <li>Some 00077 need the 3D object positions in the global frame, others can cope with 00078 local frames + a translation matrix - which can give considerable performance 00079 benefits.</li> 00080 </ul> 00081 <p>To cope with these situations the object buffer is filled out in negotiation 00082 with the viewer. TBuffer3D classes are conceptually divided into enumerated 00083 sections Core, BoundingBox, Raw etc (see TBuffer3D.h for more details). </p> 00084 <p align="center"><img src="gif/TBuffer3D.gif" width="501" height="501"></p> 00085 <p>The<em> SectionsValid() / SetSectionsValid / ClearSectionsValid() </em>methods of TBuffer3D 00086 are used to test/set/clear these section valid flags.</p> 00087 <p>The sections found in TBuffer3D (<em>Core/BoundingBox/Raw Sizes/Raw</em>) 00088 are sufficient to describe any tessellated shape in a generic fashion. An additional <em>ShapeSpecific</em> section 00089 in derived shape specific classes allows a more abstract shape description 00090 ("a sphere of inner radius x, outer radius y"). This enables a viewer 00091 which knows how to draw (tessellate) the shape itself to do so, which can bring 00092 considerable performance and quality benefits, while providing a generic fallback 00093 suitable for all viewers.</p> 00094 <p>The rules for client negotiation with the viewer are:</p> 00095 <ul> 00096 <li> If suitable specialized TBuffer3D class exists, use it, otherwise use 00097 TBuffer3D.</li> 00098 <li>Complete the mandatory Core section.</li> 00099 <li>Complete the ShapeSpecific section 00100 if applicable.</li> 00101 <li>Complete the BoundingBox if you can.</li> 00102 <li>Pass this buffer to the viewer using 00103 one of the AddObject() methods - see below.</li> 00104 </ul> 00105 <p>If the viewer requires more sections to be completed (Raw/RawSizes) AddObject() 00106 will return flags indicating which ones, otherwise it returns kNone. You must 00107 fill the buffer and mark these sections valid, and pass the buffer again. A 00108 typical code snippet would be:</p> 00109 <pre>TBuffer3DSphere sphereBuffer; 00110 // Fill out kCore... 00111 // Fill out kBoundingBox... 00112 // Fill out kShapeSpecific for TBuffer3DSphere 00113 // Try first add to viewer 00114 Int_t reqSections = viewer->AddObject(buffer); 00115 if (reqSections != TBuffer3D::kNone) { 00116 if (reqSections & TBuffer3D::kRawSizes) { 00117 // Fill out kRawSizes... 00118 } 00119 if (reqSections & TBuffer3D::kRaw) { 00120 // Fill out kRaw... 00121 } 00122 // Add second time to viewer - ignore return cannot do more 00123 viewer->AddObject(buffer); 00124 } 00125 }</pre> 00126 <p><em>ShapeSpecific</em>: If the viewer can directly display the buffer without 00127 filling of the kRaw/kRawSizes section it will not need to request client side 00128 tessellation. 00129 Currently we provide the following various shape specific classes, which the 00130 OpenGL viewer can take advantage of (see TBuffer3D.h and TBuffer3DTypes.h)</p> 00131 <ul> 00132 <li>TBuffer3DSphere - solid, hollow and cut spheres*</li> 00133 <li>TBuffer3DTubeSeg - angle tube segment</li> 00134 <li>TBuffer3DCutTube - angle tube segment with plane cut ends.</li> 00135 </ul> 00136 <p>*OpenGL only supports solid spheres at present - cut/hollow ones will be 00137 requested tessellated.</p> 00138 <p>Anyone is free to add new TBuffer3D classes, but it should be clear that the 00139 viewers require updating to be able to take advantage of them. The number of 00140 native shapes in OpenGL will be expanded over time.</p> 00141 <p><em>BoundingBox: </em>You are not obliged to complete this, as any viewer 00142 requiring one internally (OpenGL) will build one for you if you do not provide. 00143 However 00144 to do this the viewer will force you to provide the raw tessellation, and the 00145 resulting box will be axis aligned with the overall scene, which is non-ideal 00146 for rotated shapes.</p> 00147 <p>As we need to support orientated (rotated) bounding boxes, TBuffer3D requires 00148 the 6 vertices of the box. We also provide a convenience function, SetAABoundingBox(), 00149 for simpler case of setting an axis aligned bounding box.</p> 00150 <h4> 00151 Master/Local Reference Frames</h4> 00152 The <em>Core</em> section of TBuffer3D contains two members relating to reference 00153 frames: 00154 <em>fLocalFrame</em> & <em>fLocalMaster</em>. <em>fLocalFrame</em> indicates 00155 if any positions in the buffer (bounding box and tessellation vertexes) are 00156 in local or master (world 00157 frame). <em>fLocalMaster</em> is a standard 4x4 translation matrix (OpenGL 00158 colum major ordering) for placing the object into the 3D master frame. 00159 <p>If <em>fLocalFrame</em> is kFALSE, <em>fLocalMaster</em> should contain an 00160 identity matrix. This is set by default, and can be reset using <em>SetLocalMasterIdentity()</em> function.<br> 00161 Logical & Physical Objects</p> 00162 <p>There are two cases of object addition:</p> 00163 <ul> 00164 <li> Add this object as a single independent entity in the world reference 00165 frame.</li> 00166 <li>Add 00167 a physical placement (copy) of this logical object (described in local 00168 reference frame).</li> 00169 </ul> 00170 <p>The second case is very typical in geometry packages, GEANT4, where we have 00171 very large number repeated placements of relatively few logical (unique) shapes. 00172 Some viewers (OpenGL only at present) are able to take advantage of this by 00173 identifying unique logical shapes from the <em>fID</em> logical ID member of 00174 TBuffer3D. If repeated addition of the same <em>fID</em> is found, the shape 00175 is cached already - and the costly tessellation does not need to be sent again. 00176 The viewer can 00177 also perform internal GL specific caching with considerable performance gains 00178 in these cases.</p> 00179 <p>For this to work correctly the logical object in must be described in TBuffer3D 00180 in the local reference frame, complete with the local/master translation. The 00181 viewer indicates this through the interface method</p> 00182 <pre>PreferLocalFrame()</pre> 00183 <p>If this returns kTRUE you can make repeated calls to AddObject(), with TBuffer3D 00184 containing the same fID, and different <em>fLocalMaster</em> placements.</p> 00185 <p>For viewers supporting logical/physical objects, the TBuffer3D content refers 00186 to the properties of logical object, with the <em>fLocalMaster</em> transform and the 00187 <em>fColor</em> and <em>fTransparency</em> attributes, which can be varied for each physical 00188 object.</p> 00189 <p>As a minimum requirement all clients must be capable of filling the raw tessellation 00190 of the object buffer, in the master reference frame. Conversely viewers must 00191 always be capable of displaying the object described by this buffer.</p> 00192 <h4> 00193 Scene Rebuilds</h4> 00194 <p>It should be understood that AddObject is not an explicit command to the viewer 00195 - it may for various reasons decide to ignore it:</p> 00196 <ul> 00197 <li> It already has the object internally cached .</li> 00198 <li>The object falls outside 00199 some 'interest' limits of the viewer camera.</li> 00200 <li>The object is too small to 00201 be worth drawing.</li> 00202 </ul> 00203 <p>In all these cases AddObject() returns kNone, as it does for successful addition, 00204 simply indicating it does not require you to provide further information about 00205 this object. You should 00206 not try to make any assumptions about what the viewer did with it.</p> 00207 <p>This enables the viewer to be connected to a client which sends potentially 00208 millions of objects, and only accept those that are of interest at a certain 00209 time, caching the relatively small number of CPU/memory costly logical shapes, 00210 and retaining/discarding the physical placements as required. The viewer may 00211 decide to force the client to rebuild (republish) the scene (via 00212 a TPad 00213 repaint 00214 at 00215 present), 00216 and 00217 thus 00218 collect 00219 these 00220 objects if 00221 the 00222 internal viewer state changes. It does this presently by forcing a repaint 00223 on the attached TPad object - hence the reason for putting all publishing to 00224 the viewer in the attached pad objects Paint() method. We will likely remove 00225 this requirement in the future, indicating the rebuild request via a normal 00226 ROOT signal, which the client can detect.</p> 00227 <h4> 00228 Physical IDs</h4> 00229 TVirtualViewer3D provides for two methods of object addition:virtual Int_t AddObject(const 00230 TBuffer3D & buffer, Bool_t * addChildren = 0)<br> 00231 <pre>virtual Int_t AddObject(UInt_t physicalID, const TBuffer3D & buffer, Bool_t * addChildren = 0)</pre> 00232 <p>If you use the first (simple) case a viewer using logical/physical pairs 00233 will generate IDs for each physical object internally. In the second you 00234 can specify 00235 a unique identifier from the client, which allows the viewer to be more 00236 efficient. It can now cache both logical and physical objects, and only discard 00237 physical 00238 objects no longer of interest as part of scene rebuilds.</p> 00239 <h4> 00240 Child Objects</h4> 00241 <p>In many geometries there is a rigid containment hierarchy, and so if the viewer 00242 is not interested in a certain object due to limits/size then it will also 00243 not be interest in any of the contained branch of descendents. Both AddObject() 00244 methods have an addChildren parameter. The viewer will complete this (if passed) 00245 indicating if children (contained within the one just sent) are worth adding.</p> 00246 <h4> 00247 Recyling TBuffer3D </h4> 00248 <p>Once add AddObject() has been called, the contents are copied to the viewer 00249 internally. You are free to destroy this object, or recycle it for the next 00250 object if suitable.</p> 00251 <!--*/ 00252 // -->END_HTML 00253 00254 00255 #include "TVirtualViewer3D.h" 00256 #include "TVirtualPad.h" 00257 #include "TPluginManager.h" 00258 #include "TError.h" 00259 #include "TClass.h" 00260 00261 ClassImp(TVirtualViewer3D) 00262 00263 //______________________________________________________________________________ 00264 TVirtualViewer3D* TVirtualViewer3D::Viewer3D(TVirtualPad *pad, Option_t *type) 00265 { 00266 // Create a Viewer 3D of specified type. 00267 00268 TVirtualViewer3D *viewer = 0; 00269 TPluginHandler *h; 00270 if ((h = gPluginMgr->FindHandler("TVirtualViewer3D", type))) { 00271 if (h->LoadPlugin() == -1) 00272 return 0; 00273 00274 if (!pad) { 00275 viewer = (TVirtualViewer3D *) h->ExecPlugin(1, gPad); 00276 } else { 00277 viewer = (TVirtualViewer3D *) h->ExecPlugin(1, pad); 00278 } 00279 } 00280 return viewer; 00281 }