# Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics

[Home] [Tutorials] [All Classes] [Grouped Classes]

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/VL/BuildSystem/VL_DISTRO/Visualization_Library_SDK/src/vlGraphics/RenderQueueSorter.hpp

Go to the documentation of this file.

    00001 /**************************************************************************************/
    00002 /*                                                                                    */
    00003 /*  Visualization Library                                                             */
    00004 /*  http://www.visualizationlibrary.com                                               */
    00005 /*                                                                                    */
    00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
    00007 /*  All rights reserved.                                                              */
    00008 /*                                                                                    */
    00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
    00010 /*  are permitted provided that the following conditions are met:                     */
    00011 /*                                                                                    */
    00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
    00013 /*  list of conditions and the following disclaimer.                                  */
    00014 /*                                                                                    */
    00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
    00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
    00017 /*  other materials provided with the distribution.                                   */
    00018 /*                                                                                    */
    00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
    00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
    00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
    00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
    00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
    00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
    00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
    00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
    00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
    00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
    00029 /*                                                                                    */
    00030 /**************************************************************************************/
    00031
    00032 #ifndef RenderQueueSorter_INCLUDE_ONCE
    00033 #define RenderQueueSorter_INCLUDE_ONCE
    00034
    00035 #include <vlGraphics/RenderToken.hpp>
    00036
    00037 namespace vl
    00038 {
    00039   //------------------------------------------------------------------------------
    00040   // RenderQueueSorter
    00041   //------------------------------------------------------------------------------
    00045   class RenderQueueSorter: public Object
    00046   {
    00047   public:
    00048     virtual const char* className() { return "vl::RenderQueueSorter"; }
    00049     RenderQueueSorter()
    00050     {
    00051       VL_DEBUG_SET_OBJECT_NAME()
    00052     }
    00053     virtual bool operator()(const RenderToken* a, const RenderToken* b) const = 0;
    00054     virtual bool confirmZCameraDistanceNeed(const RenderToken*) const = 0;
    00055     virtual bool mightNeedZCameraDistance() const = 0;
    00056   };
    00057   //------------------------------------------------------------------------------
    00058   // RenderQueueSorterByShader
    00059   //------------------------------------------------------------------------------
    00061   // no z sort, no effect render rank, no actor render rank
    00062   class RenderQueueSorterByShader: public RenderQueueSorter
    00063   {
    00064   public:
    00065     virtual const char* className() { return "vl::RenderQueueSorterByShader"; }
    00066     RenderQueueSorterByShader()
    00067     {
    00068       VL_DEBUG_SET_OBJECT_NAME()
    00069     }
    00070     virtual bool mightNeedZCameraDistance() const { return false; }
    00071     virtual bool confirmZCameraDistanceNeed(const RenderToken*) const { return false; }
    00072     virtual bool operator()(const RenderToken* a, const RenderToken* b) const
    00073     {
    00074       return a->mShader < b->mShader;
    00075     }
    00076   };
    00077   //------------------------------------------------------------------------------
    00078   // RenderQueueSorterByRenderable
    00079   //------------------------------------------------------------------------------
    00081   // no z sort, no effect render rank, no actor render rank
    00082   class RenderQueueSorterByRenderable: public RenderQueueSorter
    00083   {
    00084   public:
    00085     virtual const char* className() { return "vl::RenderQueueSorterByRenderable"; }
    00086     RenderQueueSorterByRenderable()
    00087     {
    00088       VL_DEBUG_SET_OBJECT_NAME()
    00089     }
    00090     virtual bool mightNeedZCameraDistance() const { return false; }
    00091     virtual bool confirmZCameraDistanceNeed(const RenderToken*) const { return false; }
    00092     virtual bool operator()(const RenderToken* a, const RenderToken* b) const
    00093     {
    00094       return a->mRenderable < b->mRenderable;
    00095     }
    00096   };
    00097   //------------------------------------------------------------------------------
    00098   // RenderQueueSorterBasic
    00099   //------------------------------------------------------------------------------
    00101   class RenderQueueSorterBasic: public RenderQueueSorter
    00102   {
    00103   public:
    00104     virtual const char* className() { return "vl::RenderQueueSorterBasic"; }
    00105     RenderQueueSorterBasic()
    00106     {
    00107       VL_DEBUG_SET_OBJECT_NAME()
    00108     }
    00109     virtual bool confirmZCameraDistanceNeed(const RenderToken*) const { return false; }
    00110     virtual bool operator()(const RenderToken* a, const RenderToken* b) const
    00111     {
    00112       //  Actor's render-block
    00113       if (a->mActor->renderBlock() != b->mActor->renderBlock())
    00114         return a->mActor->renderBlock() < b->mActor->renderBlock();
    00115       else
    00116       // Effect's render-rank
    00117       if (a->mEffectRenderRank != b->mEffectRenderRank)
    00118         return a->mEffectRenderRank < b->mEffectRenderRank;
    00119       else
    00120       //  Actor's render-rank
    00121       if (a->mActor->renderRank() != b->mActor->renderRank())
    00122         return a->mActor->renderRank() < b->mActor->renderRank();
    00123       else
    00124       // shader sorting
    00125       if (a->mShader != b->mShader)
    00126         return a->mShader < b->mShader;
    00127       // renderable sorting
    00128       else
    00129         return a->mRenderable < b->mRenderable;
    00130     }
    00131   };
    00132   //------------------------------------------------------------------------------
    00133   // RenderQueueSorterStandard
    00134   //------------------------------------------------------------------------------
    00144   class RenderQueueSorterStandard: public RenderQueueSorter
    00145   {
    00146   public:
    00147     virtual const char* className() { return "vl::RenderQueueSorterStandard"; }
    00148     RenderQueueSorterStandard(): mDepthSortMode(AlphaDepthSort)
    00149     {
    00150       VL_DEBUG_SET_OBJECT_NAME()
    00151     }
    00152
    00153     virtual bool mightNeedZCameraDistance() const { return true; }
    00154     virtual bool confirmZCameraDistanceNeed(const RenderToken* a) const
    00155     {
    00156       return mDepthSortMode != NeverDepthSort && (mDepthSortMode == AlwaysDepthSort  ||
    00157       (a->mShader->blendingEnabled() && (mDepthSortMode == AlphaDepthSort)) );
    00158     }
    00159
    00160     virtual bool operator()(const RenderToken* a, const RenderToken* b) const
    00161     {
    00162       // --------------- user defined sorting ---------------
    00163
    00164       //  Actor's render-block
    00165       if (a->mActor->renderBlock() != b->mActor->renderBlock())
    00166         return a->mActor->renderBlock() < b->mActor->renderBlock();
    00167       else
    00168       // Effect's render-rank
    00169       if (a->mEffectRenderRank != b->mEffectRenderRank)
    00170         return a->mEffectRenderRank < b->mEffectRenderRank;
    00171       else
    00172       //  Actor's render-rank
    00173       if (a->mActor->renderRank() != b->mActor->renderRank())
    00174         return a->mActor->renderRank() < b->mActor->renderRank();
    00175       else
    00176
    00177       // --------------- shader based sorting ---------------
    00178
    00179       if ( mDepthSortMode != AlwaysDepthSort && (a->mShader->blendingEnabled() != b->mShader->blendingEnabled()))
    00180       {
    00181         // first render opaque objects
    00182         return !a->mShader->blendingEnabled();
    00183       }
    00184       // A/b->mShader->isEnabled(OGL_BLEND) are equal or AlwaysDepthSort
    00185       else
    00186       if ( confirmZCameraDistanceNeed(a) )
    00187       {
    00188         // render first far objects then the close ones
    00189         return a->mCameraDistance > b->mCameraDistance;
    00190       }
    00191       else
    00192
    00193       /*// glsl
    00194       if ( a->mShader->glslProgram() != b->mShader->glslProgram() )
    00195         return a->mShader->glslProgram() < b->mShader->glslProgram();
    00196       else*/
    00197
    00198       /*sort by textures: removed*/
    00199
    00200       // shader sorting
    00201       if (a->mShader != b->mShader)
    00202         return a->mShader < b->mShader;
    00203       // renderable sorting
    00204       else
    00205         return a->mRenderable < b->mRenderable;
    00206     }
    00207
    00208     EDepthSortMode depthSortMode() const { return mDepthSortMode; }
    00209     void setDepthSortMode(EDepthSortMode mode) { mDepthSortMode = mode; }
    00210
    00211   public:
    00212     EDepthSortMode mDepthSortMode;
    00213   };
    00214   //------------------------------------------------------------------------------
    00215   // RenderQueueSorterOcclusion
    00216   //------------------------------------------------------------------------------
    00225   class RenderQueueSorterOcclusion: public RenderQueueSorter
    00226   {
    00227   public:
    00228     virtual const char* className() { return "vl::RenderQueueSorterOcclusion"; }
    00229     RenderQueueSorterOcclusion()
    00230     {
    00231       VL_DEBUG_SET_OBJECT_NAME()
    00232     }
    00233
    00234     virtual bool mightNeedZCameraDistance() const { return true; }
    00235     virtual bool confirmZCameraDistanceNeed(const RenderToken*) const { return true; }
    00236
    00237     virtual bool operator()(const RenderToken* a, const RenderToken* b) const
    00238     {
    00239       // --------------- user defined sorting ---------------
    00240
    00241       //  Actor's render-block
    00242       if (a->mActor->renderBlock() != b->mActor->renderBlock())
    00243         return a->mActor->renderBlock() < b->mActor->renderBlock();
    00244       else
    00245       // Effect's render-rank
    00246       if (a->mEffectRenderRank != b->mEffectRenderRank)
    00247         return a->mEffectRenderRank < b->mEffectRenderRank;
    00248       else
    00249       //  Actor's render-rank
    00250       if (a->mActor->renderRank() != b->mActor->renderRank())
    00251         return a->mActor->renderRank() < b->mActor->renderRank();
    00252       else
    00253
    00254       // --------------- shader based sorting ---------------
    00255
    00256       if ( a->mShader->blendingEnabled() != b->mShader->blendingEnabled() )
    00257       {
    00258         // first render opaque objects
    00259         return !a->mShader->blendingEnabled();
    00260       }
    00261       else
    00262       // blending on: render first far objects then the close ones
    00263       if ( a->mShader->blendingEnabled() )
    00264       {
    00265         return a->mCameraDistance > b->mCameraDistance;
    00266       }
    00267       else
    00268       // blending off: render first close objects then far ones -> i.e. -> if ( !a->mShader->blendingEnabled() )
    00269       if (a->mCameraDistance != b->mCameraDistance)
    00270       {
    00271         return a->mCameraDistance < b->mCameraDistance;
    00272       }
    00273       else
    00274       // shader sorting
    00275       if (a->mShader != b->mShader)
    00276         return a->mShader < b->mShader;
    00277       // renderable sorting
    00278       else
    00279         return a->mRenderable < b->mRenderable;
    00280     }
    00281   };
    00282   //------------------------------------------------------------------------------
    00283   // RenderQueueSorterAggressive
    00284   //------------------------------------------------------------------------------
    00287   class RenderQueueSorterAggressive: public RenderQueueSorter
    00288   {
    00289   public:
    00290     virtual const char* className() { return "vl::RenderQueueSorterAggressive"; }
    00291     RenderQueueSorterAggressive(): mDepthSortMode(AlphaDepthSort)
    00292     {
    00293       VL_DEBUG_SET_OBJECT_NAME()
    00294     }
    00295
    00296     virtual bool mightNeedZCameraDistance() const { return true; }
    00297     virtual bool confirmZCameraDistanceNeed(const RenderToken* a) const
    00298     {
    00299       return mDepthSortMode != NeverDepthSort &&
    00300         (mDepthSortMode == AlwaysDepthSort  || (a->mShader->blendingEnabled() && (mDepthSortMode == AlphaDepthSort)) );
    00301     }
    00302
    00303     virtual bool operator()(const RenderToken* a, const RenderToken* b) const
    00304     {
    00305       // --------------- user defined sorting ---------------
    00306
    00307       //  Actor's render-block
    00308       if (a->mActor->renderBlock() != b->mActor->renderBlock())
    00309         return a->mActor->renderBlock() < b->mActor->renderBlock();
    00310       else
    00311       // Effect's render-rank
    00312       if (a->mEffectRenderRank != b->mEffectRenderRank)
    00313         return a->mEffectRenderRank < b->mEffectRenderRank;
    00314       else
    00315       //  Actor's render-rank
    00316       if (a->mActor->renderRank() != b->mActor->renderRank())
    00317         return a->mActor->renderRank() < b->mActor->renderRank();
    00318       else
    00319
    00320       // --------------- shader based sorting ---------------
    00321
    00322       if ( mDepthSortMode != AlwaysDepthSort && (a->mShader->blendingEnabled() != b->mShader->blendingEnabled()))
    00323       {
    00324         return !a->mShader->blendingEnabled(); // first render opaque objects
    00325       }
    00326       else // A/b->mShader->isEnabled(OGL_BLEND) are equal or AlwaysDepthSort
    00327       if ( confirmZCameraDistanceNeed(a) )
    00328       {
    00329         return a->mCameraDistance > b->mCameraDistance; // render first far objects then the close ones
    00330       }
    00331       else
    00332
    00333       // glsl
    00334       if ( a->mShader->glslProgram() != b->mShader->glslProgram() )
    00335         return a->mShader->glslProgram() < b->mShader->glslProgram();
    00336       else
    00337
    00338       // render state set
    00339       if ( a->mShader->getRenderStateSet() != b->mShader->getRenderStateSet() )
    00340         return a->mShader->getRenderStateSet() < b->mShader->getRenderStateSet();
    00341       else
    00342
    00343       // enables
    00344       if ( a->mShader->getEnableSet() != b->mShader->getEnableSet() )
    00345         return a->mShader->getEnableSet() < b->mShader->getEnableSet();
    00346       else
    00347
    00348       /*sort by textures: removed*/
    00349
    00350       /*sort by lights: removed*/
    00351
    00352       // shader sorting
    00353       if (a->mShader != b->mShader)
    00354         return a->mShader < b->mShader;
    00355       // renderable sorting
    00356       else
    00357         return a->mRenderable < b->mRenderable;
    00358     }
    00359
    00360     EDepthSortMode depthSortMode() const { return mDepthSortMode; }
    00361     void setDepthSortMode(EDepthSortMode mode) { mDepthSortMode = mode; }
    00362
    00363   public:
    00364     EDepthSortMode mDepthSortMode;
    00365   };
    00366 }
    00367
    00368 #endif

Visualization Library v2011.05.1142 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 26 2011 22:51:25.
Permission is granted to use this page to write and publish articles regarding Visualization Library.