Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

OSGDrawActionBase.cpp

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *             Copyright (C) 2000-2002 by the OpenSG Forum                   *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  * This library is free software; you can redistribute it and/or modify it   *
00016  * under the terms of the GNU Library General Public License as published    *
00017  * by the Free Software Foundation, version 2.                               *
00018  *                                                                           *
00019  * This library is distributed in the hope that it will be useful, but       *
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00022  * Library General Public License for more details.                          *
00023  *                                                                           *
00024  * You should have received a copy of the GNU Library General Public         *
00025  * License along with this library; if not, write to the Free Software       *
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00027  *                                                                           *
00028 \*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*\
00030  *                                Changes                                    *
00031  *                                                                           *
00032  *                                                                           *
00033  *                                                                           *
00034  *                                                                           *
00035  *                                                                           *
00036  *                                                                           *
00037 \*---------------------------------------------------------------------------*/
00038 
00039 //---------------------------------------------------------------------------
00040 //  Includes
00041 //---------------------------------------------------------------------------
00042 
00043 #include <stdlib.h>
00044 #include <stdio.h>
00045 
00046 #include <OSGConfig.h>
00047 #include <OSGNodePtr.h>
00048 #include <OSGWindow.h>
00049 #include <OSGCamera.h>
00050 #include <OSGViewport.h>
00051 #include <OSGGeometry.h>
00052 #include <OSGDrawable.h>
00053 
00054 #include <OSGGL.h>
00055 #include <OSGVolumeDraw.h>
00056 
00057 #include <OSGDrawActionBase.h>
00058 
00059 OSG_USING_NAMESPACE
00060 
00061 
00062 /***************************************************************************\
00063  *                            Description                                  *
00064 \***************************************************************************/
00065 
00073 /***************************************************************************\
00074  *                           Class variables                               *
00075 \***************************************************************************/
00076 
00077 char DrawActionBase::cvsid[] = "@(#)$Id: $";
00078 
00079 StatElemDesc<StatTimeElem>  
00080 DrawActionBase::statTravTime ("travTime", "time for traversal");
00081 
00082 StatElemDesc<StatIntElem>   
00083 DrawActionBase::statCullTestedNodes("cullTestedNodes", "nodes tested");
00084 
00085 StatElemDesc<StatIntElem>   
00086 DrawActionBase::statCulledNodes("culledNodes", "nodes culled from frustum");
00087 
00088 /***************************************************************************\
00089  *                           Class methods                                 *
00090 \***************************************************************************/
00091 
00092 
00093 /*-------------------------------------------------------------------------*\
00094  -  public                                                                 -
00095 \*-------------------------------------------------------------------------*/
00096 
00097 
00098 /*-------------------------------------------------------------------------*\
00099  -  protected                                                              -
00100 \*-------------------------------------------------------------------------*/
00101 
00102 
00103 /*-------------------------------------------------------------------------*\
00104  -  private                                                                -
00105 \*-------------------------------------------------------------------------*/
00106 
00107 
00108 
00109 /***************************************************************************\
00110  *                           Instance methods                              *
00111 \***************************************************************************/
00112 
00113 /*-------------------------------------------------------------------------*\
00114  -  public                                                                 -
00115 \*-------------------------------------------------------------------------*/
00116 
00117 /*------------- constructors & destructors --------------------------------*/
00118 
00122 DrawActionBase::DrawActionBase(void) :
00123      Inherited     (     ),
00124     _camera        (NULL ),
00125     _background    (NULL ),
00126     _window        (NULL ),
00127     _viewport      (NULL ),
00128     _statistics    (NULL ),
00129     _ownStat       (false),
00130     _frustumCulling(true ),
00131     _volumeDrawing (false),
00132     _autoFrustum   (true ),
00133     _frustum       (     ),
00134     _pMaterial     (     ),
00135     _pMaterialNode (     ),
00136     _mCameraToWorld(     )
00137 {
00138 }
00139 
00140 
00141 DrawActionBase::DrawActionBase(const DrawActionBase &source) :
00142      Inherited     (source                 ),
00143     _camera        (source._camera         ),
00144     _background    (source._background     ),
00145     _window        (source._window         ),
00146     _viewport      (source._viewport       ),
00147     _statistics    (source._statistics     ),
00148     _ownStat       (source._ownStat        ),
00149     _frustumCulling(source._frustumCulling ),
00150     _volumeDrawing (source._volumeDrawing  ),
00151     _autoFrustum   (source._autoFrustum    ),
00152     _frustum       (source._frustum        ),
00153     _pMaterial     (source._pMaterial      ),
00154     _pMaterialNode (source._pMaterialNode  ),
00155     _mCameraToWorld(source._mCameraToWorld )
00156 {
00157 }
00158 
00162 DrawActionBase::~DrawActionBase(void)
00163 {
00164 #if 0 // Altered for last frame time
00165 
00166 #else
00167   if (_ownStat) {
00168      delete _statistics;
00169   }
00170 #endif
00171 }
00172 
00173 /*------------------------------ start -----------------------------------*/
00174 
00175 void DrawActionBase::setMaterial(Material *pMaterial, NodePtr node)
00176 {
00177     _pMaterial     = pMaterial;
00178     _pMaterialNode = node;
00179 }
00180 
00181 
00182 Action::ResultE DrawActionBase::start(void)
00183 {
00184     if(getFrustumCulling() == true &&
00185        getAutoFrustum   () == true &&
00186        getCamera        () != NULL &&
00187        getViewport      () != NULL)
00188     {
00189         getCamera()->getFrustum( _frustum, *getViewport() );
00190 //_frustum.dump();
00191     }
00192 
00193     if(_camera != NULL && getViewport() != NULL)
00194     {
00195         _camera->getViewing( _mCameraToWorld,
00196                              getViewport()->getPixelWidth(),
00197                              getViewport()->getPixelHeight() );
00198         _mCameraToWorld.invert();
00199     }
00200 
00201 //    cerr << "DA::start" << endl;
00202 
00203 //    cerr << _mCameraToWorld << endl << endl;
00204     
00205 
00206 #if 0 // Altered for last frame time
00207     if(_statistics == NULL)
00208     {
00209         _statistics = StatCollector::create();
00210         _ownStat = true;
00211     }
00212     else
00213     {
00214         _ownStat = false;        
00215     }
00216 #else
00217     if(_statistics == NULL)
00218     {
00219         _statistics = StatCollector::create();
00220         _ownStat = true;
00221     }
00222 #endif
00223 
00224     getStatistics()->getElem(statTravTime)->start();
00225     getStatistics()->getElem(statCullTestedNodes)->reset();
00226     getStatistics()->getElem(statCulledNodes)->reset();
00227    
00228     // this really doesn't belong here, but don't know a better place to put it
00229     if(getStatistics()->getElem(Drawable::statNTriangles,false))
00230     {
00231         getStatistics()->getElem(Drawable::statNTriangles)->set(0);
00232         getStatistics()->getElem(Drawable::statNLines)->set(0);
00233         getStatistics()->getElem(Drawable::statNPoints)->set(0);
00234         getStatistics()->getElem(Drawable::statNVertices)->set(0);
00235         getStatistics()->getElem(Drawable::statNPrimitives)->set(0);
00236     }
00237 
00238 //fprintf(stderr,"%p: start\n", Thread::getCurrent());
00239         
00240     return Action::Continue;
00241 }
00242 
00243 Action::ResultE DrawActionBase::stop(Action::ResultE res)
00244 {
00245     if ( getVolumeDrawing() )
00246         drawVolume( _frustum );  
00247  
00248     getStatistics()->getElem(statTravTime)->stop();
00249   
00250 #if 0 // Altered for last frame time
00251     if(_ownStat)
00252     {
00253         delete _statistics;
00254         _statistics = NULL;
00255     }
00256     else
00257     {
00258         _ownStat = false;        
00259     }
00260 #endif
00261 
00262     return res; 
00263 }
00264 
00265 /*------------------------------ access -----------------------------------*/
00266 
00267 void DrawActionBase::setViewport(Viewport *viewport)
00268 {
00269     _viewport = viewport;
00270 }
00271 
00272 void DrawActionBase::setCamera(Camera *cam)
00273 {
00274     _camera = cam;
00275 }
00276 
00277 void DrawActionBase::setBackground(Background *background)
00278 {
00279     _background = background;
00280 }
00281 
00282 void DrawActionBase::setWindow(Window *window)
00283 {
00284     _window = window;
00285 }
00286 
00287 void DrawActionBase::setStatistics(StatCollector *statistics)
00288 {
00289 #if 0 // Altered for last frame time
00290     _statistics = statistics;
00291     _ownStat = false;
00292 #else
00293     if (_ownStat) {
00294        delete _statistics;
00295     }
00296     _statistics = statistics;
00297     _ownStat = false;
00298 #endif
00299 }
00300 
00301 
00302 // do frustum culling at all?
00303 // default true
00304 
00305 void DrawActionBase::setFrustumCulling(bool frustumCulling)
00306 {
00307     _frustumCulling = frustumCulling;
00308 }
00309 
00310 // automatically calc the frustum at the beginning of the traversal
00311 // default true
00312 
00313 void DrawActionBase::setAutoFrustum(bool autoFrustum)
00314 {
00315     _autoFrustum = autoFrustum;
00316 }
00317 
00318 // draw the tested volumes
00319 // default false
00320 
00321 void DrawActionBase::setVolumeDrawing(bool volumeDrawing)
00322 {
00323     _volumeDrawing = volumeDrawing;
00324 }
00325 
00326 // explicitly set the frustum
00327 
00328 void DrawActionBase::setFrustum(FrustumVolume &frustum)
00329 {
00330     _frustum = frustum;
00331 }
00332 
00333 // select all visible nodes
00334 UInt32 DrawActionBase::selectVisibles(void)
00335 {
00336     if(getFrustumCulling() == false)
00337         return getNNodes();
00338 
00339     useNodeList();
00340 
00341     Color3f col;
00342     
00343     UInt32 count = 0;
00344     for ( UInt32 i = 0; i < getNNodes(); i++ )
00345     {
00346         if ( isVisible( getNode(i).getCPtr() ) )
00347         {
00348             col.setValuesRGB(0,1,0);
00349             addNode( getNode(i) );
00350             ++count;
00351         }
00352         else
00353             col.setValuesRGB(1,0,0);
00354         
00355         if(getVolumeDrawing())
00356         {
00357             dropVolume(this, getNode(i), col);
00358         }
00359     }
00360 
00361     return count;
00362 }
00363 
00364 

Generated on Thu Aug 25 04:03:08 2005 for OpenSG by  doxygen 1.4.3