Visualization Library v1.0.3A lightweight C++ OpenGL middleware for 2D/3D graphics |
[Download] [Tutorials] [All Classes] [Grouped Classes] |
00001 /**************************************************************************************/ 00002 /* */ 00003 /* Visualization Library */ 00004 /* http://visualizationlibrary.org */ 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 VL_INSTRUMENT_ABSTRACT_CLASS(vl::RenderQueueSorter, Object) 00048 00049 public: 00050 RenderQueueSorter() 00051 { 00052 VL_DEBUG_SET_OBJECT_NAME() 00053 } 00054 virtual bool operator()(const RenderToken* a, const RenderToken* b) const = 0; 00055 virtual bool confirmZCameraDistanceNeed(const RenderToken*) const = 0; 00056 virtual bool mightNeedZCameraDistance() const = 0; 00057 }; 00058 //------------------------------------------------------------------------------ 00059 // RenderQueueSorterByShader 00060 //------------------------------------------------------------------------------ 00062 // no z sort, no effect render rank, no actor render rank 00063 class RenderQueueSorterByShader: public RenderQueueSorter 00064 { 00065 VL_INSTRUMENT_CLASS(vl::RenderQueueSorterByShader, RenderQueueSorter) 00066 00067 public: 00068 RenderQueueSorterByShader() 00069 { 00070 VL_DEBUG_SET_OBJECT_NAME() 00071 } 00072 virtual bool mightNeedZCameraDistance() const { return false; } 00073 virtual bool confirmZCameraDistanceNeed(const RenderToken*) const { return false; } 00074 virtual bool operator()(const RenderToken* a, const RenderToken* b) const 00075 { 00076 return a->mShader < b->mShader; 00077 } 00078 }; 00079 //------------------------------------------------------------------------------ 00080 // RenderQueueSorterByRenderable 00081 //------------------------------------------------------------------------------ 00083 // no z sort, no effect render rank, no actor render rank 00084 class RenderQueueSorterByRenderable: public RenderQueueSorter 00085 { 00086 VL_INSTRUMENT_CLASS(vl::RenderQueueSorterByRenderable, RenderQueueSorter) 00087 00088 public: 00089 RenderQueueSorterByRenderable() 00090 { 00091 VL_DEBUG_SET_OBJECT_NAME() 00092 } 00093 virtual bool mightNeedZCameraDistance() const { return false; } 00094 virtual bool confirmZCameraDistanceNeed(const RenderToken*) const { return false; } 00095 virtual bool operator()(const RenderToken* a, const RenderToken* b) const 00096 { 00097 return a->mRenderable < b->mRenderable; 00098 } 00099 }; 00100 //------------------------------------------------------------------------------ 00101 // RenderQueueSorterBasic 00102 //------------------------------------------------------------------------------ 00104 class RenderQueueSorterBasic: public RenderQueueSorter 00105 { 00106 VL_INSTRUMENT_CLASS(vl::RenderQueueSorterBasic, RenderQueueSorter) 00107 00108 public: 00109 RenderQueueSorterBasic() 00110 { 00111 VL_DEBUG_SET_OBJECT_NAME() 00112 } 00113 virtual bool mightNeedZCameraDistance() const { return true; } 00114 virtual bool confirmZCameraDistanceNeed(const RenderToken*) const { return false; } 00115 virtual bool operator()(const RenderToken* a, const RenderToken* b) const 00116 { 00117 // Actor's render-block 00118 if (a->mActor->renderBlock() != b->mActor->renderBlock()) 00119 return a->mActor->renderBlock() < b->mActor->renderBlock(); 00120 else 00121 // Effect's render-rank 00122 if (a->mEffectRenderRank != b->mEffectRenderRank) 00123 return a->mEffectRenderRank < b->mEffectRenderRank; 00124 else 00125 // Actor's render-rank 00126 if (a->mActor->renderRank() != b->mActor->renderRank()) 00127 return a->mActor->renderRank() < b->mActor->renderRank(); 00128 else 00129 // shader sorting 00130 if (a->mShader != b->mShader) 00131 return a->mShader < b->mShader; 00132 // renderable sorting 00133 else 00134 return a->mRenderable < b->mRenderable; 00135 } 00136 }; 00137 //------------------------------------------------------------------------------ 00138 // RenderQueueSorterStandard 00139 //------------------------------------------------------------------------------ 00149 class RenderQueueSorterStandard: public RenderQueueSorter 00150 { 00151 VL_INSTRUMENT_CLASS(vl::RenderQueueSorterStandard, RenderQueueSorter) 00152 00153 public: 00154 RenderQueueSorterStandard(): mDepthSortMode(AlphaDepthSort) 00155 { 00156 VL_DEBUG_SET_OBJECT_NAME() 00157 } 00158 00159 virtual bool mightNeedZCameraDistance() const { return true; } 00160 virtual bool confirmZCameraDistanceNeed(const RenderToken* a) const 00161 { 00162 return mDepthSortMode != NeverDepthSort && (mDepthSortMode == AlwaysDepthSort || 00163 (a->mShader->isBlendingEnabled() && (mDepthSortMode == AlphaDepthSort)) ); 00164 } 00165 00166 virtual bool operator()(const RenderToken* a, const RenderToken* b) const 00167 { 00168 // --------------- user defined sorting --------------- 00169 00170 // Actor's render-block 00171 if (a->mActor->renderBlock() != b->mActor->renderBlock()) 00172 return a->mActor->renderBlock() < b->mActor->renderBlock(); 00173 else 00174 // Effect's render-rank 00175 if (a->mEffectRenderRank != b->mEffectRenderRank) 00176 return a->mEffectRenderRank < b->mEffectRenderRank; 00177 else 00178 // Actor's render-rank 00179 if (a->mActor->renderRank() != b->mActor->renderRank()) 00180 return a->mActor->renderRank() < b->mActor->renderRank(); 00181 else 00182 00183 // --------------- shader based sorting --------------- 00184 00185 if ( mDepthSortMode != AlwaysDepthSort && (a->mShader->isBlendingEnabled() != b->mShader->isBlendingEnabled())) 00186 { 00187 // first render opaque objects 00188 return !a->mShader->isBlendingEnabled(); 00189 } 00190 // A/b->mShader->isEnabled(OGL_BLEND) are equal or AlwaysDepthSort 00191 else 00192 if ( confirmZCameraDistanceNeed(a) ) 00193 { 00194 // render first far objects then the close ones 00195 return a->mCameraDistance > b->mCameraDistance; 00196 } 00197 else 00198 00199 /*// glsl 00200 if ( a->mShader->glslProgram() != b->mShader->glslProgram() ) 00201 return a->mShader->glslProgram() < b->mShader->glslProgram(); 00202 else*/ 00203 00204 /*sort by textures: removed*/ 00205 00206 // shader sorting 00207 if (a->mShader != b->mShader) 00208 return a->mShader < b->mShader; 00209 // renderable sorting 00210 else 00211 return a->mRenderable < b->mRenderable; 00212 } 00213 00214 EDepthSortMode depthSortMode() const { return mDepthSortMode; } 00215 void setDepthSortMode(EDepthSortMode mode) { mDepthSortMode = mode; } 00216 00217 public: 00218 EDepthSortMode mDepthSortMode; 00219 }; 00220 //------------------------------------------------------------------------------ 00221 // RenderQueueSorterOcclusion 00222 //------------------------------------------------------------------------------ 00231 class RenderQueueSorterOcclusion: public RenderQueueSorter 00232 { 00233 VL_INSTRUMENT_CLASS(vl::RenderQueueSorterOcclusion, RenderQueueSorter) 00234 00235 public: 00236 RenderQueueSorterOcclusion() 00237 { 00238 VL_DEBUG_SET_OBJECT_NAME() 00239 } 00240 00241 virtual bool mightNeedZCameraDistance() const { return true; } 00242 virtual bool confirmZCameraDistanceNeed(const RenderToken*) const { return true; } 00243 00244 virtual bool operator()(const RenderToken* a, const RenderToken* b) const 00245 { 00246 // --------------- user defined sorting --------------- 00247 00248 // Actor's render-block 00249 if (a->mActor->renderBlock() != b->mActor->renderBlock()) 00250 return a->mActor->renderBlock() < b->mActor->renderBlock(); 00251 else 00252 // Effect's render-rank 00253 if (a->mEffectRenderRank != b->mEffectRenderRank) 00254 return a->mEffectRenderRank < b->mEffectRenderRank; 00255 else 00256 // Actor's render-rank 00257 if (a->mActor->renderRank() != b->mActor->renderRank()) 00258 return a->mActor->renderRank() < b->mActor->renderRank(); 00259 else 00260 00261 // --------------- shader based sorting --------------- 00262 00263 if ( a->mShader->isBlendingEnabled() != b->mShader->isBlendingEnabled() ) 00264 { 00265 // first render opaque objects 00266 return !a->mShader->isBlendingEnabled(); 00267 } 00268 else 00269 // blending on: render first far objects then the close ones 00270 if ( a->mShader->isBlendingEnabled() ) 00271 { 00272 return a->mCameraDistance > b->mCameraDistance; 00273 } 00274 else 00275 // blending off: render first close objects then far ones -> i.e. -> if ( !a->mShader->isBlendingEnabled() ) 00276 if (a->mCameraDistance != b->mCameraDistance) 00277 { 00278 return a->mCameraDistance < b->mCameraDistance; 00279 } 00280 else 00281 // shader sorting 00282 if (a->mShader != b->mShader) 00283 return a->mShader < b->mShader; 00284 // renderable sorting 00285 else 00286 return a->mRenderable < b->mRenderable; 00287 } 00288 }; 00289 //------------------------------------------------------------------------------ 00290 // RenderQueueSorterAggressive 00291 //------------------------------------------------------------------------------ 00294 class RenderQueueSorterAggressive: public RenderQueueSorter 00295 { 00296 VL_INSTRUMENT_CLASS(vl::RenderQueueSorterAggressive, RenderQueueSorter) 00297 00298 public: 00299 RenderQueueSorterAggressive(): mDepthSortMode(AlphaDepthSort) 00300 { 00301 VL_DEBUG_SET_OBJECT_NAME() 00302 } 00303 00304 virtual bool mightNeedZCameraDistance() const { return true; } 00305 virtual bool confirmZCameraDistanceNeed(const RenderToken* a) const 00306 { 00307 return mDepthSortMode != NeverDepthSort && 00308 (mDepthSortMode == AlwaysDepthSort || (a->mShader->isBlendingEnabled() && (mDepthSortMode == AlphaDepthSort)) ); 00309 } 00310 00311 virtual bool operator()(const RenderToken* a, const RenderToken* b) const 00312 { 00313 // --------------- user defined sorting --------------- 00314 00315 // Actor's render-block 00316 if (a->mActor->renderBlock() != b->mActor->renderBlock()) 00317 return a->mActor->renderBlock() < b->mActor->renderBlock(); 00318 else 00319 // Effect's render-rank 00320 if (a->mEffectRenderRank != b->mEffectRenderRank) 00321 return a->mEffectRenderRank < b->mEffectRenderRank; 00322 else 00323 // Actor's render-rank 00324 if (a->mActor->renderRank() != b->mActor->renderRank()) 00325 return a->mActor->renderRank() < b->mActor->renderRank(); 00326 else 00327 00328 // --------------- shader based sorting --------------- 00329 00330 if ( mDepthSortMode != AlwaysDepthSort && (a->mShader->isBlendingEnabled() != b->mShader->isBlendingEnabled())) 00331 { 00332 return !a->mShader->isBlendingEnabled(); // first render opaque objects 00333 } 00334 else // A/b->mShader->isEnabled(OGL_BLEND) are equal or AlwaysDepthSort 00335 if ( confirmZCameraDistanceNeed(a) ) 00336 { 00337 return a->mCameraDistance > b->mCameraDistance; // render first far objects then the close ones 00338 } 00339 else 00340 00341 // glsl 00342 if ( a->mShader->glslProgram() != b->mShader->glslProgram() ) 00343 return a->mShader->glslProgram() < b->mShader->glslProgram(); 00344 else 00345 00346 // render state set 00347 if ( a->mShader->getRenderStateSet() != b->mShader->getRenderStateSet() ) 00348 return a->mShader->getRenderStateSet() < b->mShader->getRenderStateSet(); 00349 else 00350 00351 // enables 00352 if ( a->mShader->getEnableSet() != b->mShader->getEnableSet() ) 00353 return a->mShader->getEnableSet() < b->mShader->getEnableSet(); 00354 else 00355 00356 /*sort by textures: removed*/ 00357 00358 /*sort by lights: removed*/ 00359 00360 // shader sorting 00361 if (a->mShader != b->mShader) 00362 return a->mShader < b->mShader; 00363 // renderable sorting 00364 else 00365 return a->mRenderable < b->mRenderable; 00366 } 00367 00368 EDepthSortMode depthSortMode() const { return mDepthSortMode; } 00369 void setDepthSortMode(EDepthSortMode mode) { mDepthSortMode = mode; } 00370 00371 public: 00372 EDepthSortMode mDepthSortMode; 00373 }; 00374 } 00375 00376 #endif