Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics
[Home] [Tutorials] [All Classes] [Grouped Classes]

X:/dropbox/visualizationlibrary/src/vlGraphics/RenderQueue.hpp

Go to the documentation of this file.
00001 /**************************************************************************************/
00002 /*                                                                                    */
00003 /*  Visualization Library                                                             */
00004 /*  http://www.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 RenderQueue_INCLUDE_ONCE
00033 #define RenderQueue_INCLUDE_ONCE
00034 
00035 #include <vlGraphics/RenderQueueSorter.hpp>
00036 
00037 namespace vl
00038 {
00039   //------------------------------------------------------------------------------
00040   // RenderQueue
00041   //------------------------------------------------------------------------------
00045   class RenderQueue: public Object
00046   {
00047     VL_INSTRUMENT_CLASS(vl::RenderQueue, Object)
00048 
00049   public:
00050     RenderQueue(): mSize(0), mSizeMP(0)
00051     {
00052       VL_DEBUG_SET_OBJECT_NAME()
00053       mList.reserve(100);
00054       mListMP.reserve(100);
00055     }
00056 
00057     const RenderToken* at(int i) const { return mList[i].get(); }
00058 
00059     RenderToken* at(int i) { return mList[i].get(); }
00060 
00061     RenderToken* newToken(bool multipass)
00062     {
00063       if (multipass)
00064       {
00065         ++mSizeMP;
00066         if ( mSizeMP > (int)mListMP.size() )
00067           mListMP.push_back( new RenderToken );
00068         return mListMP[mSizeMP-1].get();
00069       }
00070       else
00071       {
00072         ++mSize;
00073         if ( mSize > (int)mList.size() )
00074           mList.push_back( new RenderToken );
00075         return mList[mSize-1].get();
00076       }
00077     }
00078 
00079     void clear()
00080     {
00081       mSize   = 0;
00082       mSizeMP = 0;
00083     }
00084 
00085     bool empty()
00086     {
00087       return mSize == 0;
00088     }
00089 
00090     int size() const
00091     {
00092       return mSize;
00093     }
00094 
00095     void sort(RenderQueueSorter* sorter, Camera* camera)
00096     {
00097       if (sorter->mightNeedZCameraDistance())
00098       {
00099         for(int i=0; i<size(); ++i)
00100         {
00101           RenderToken* tok = at(i);
00102           vec3 center = tok->mRenderable->boundingBox().isNull() ? vec3(0,0,0) : tok->mRenderable->boundingBox().center();
00103           if ( sorter->confirmZCameraDistanceNeed(tok) )
00104           {
00105             if (tok->mActor->transform())
00106               // tok->mCameraDistance = ( camera->viewMatrix() * (tok->mActor->transform()->worldMatrix() * center) ).lengthSquared();
00107               tok->mCameraDistance = -( camera->viewMatrix() * (tok->mActor->transform()->worldMatrix() * center) ).z();
00108             else
00109               // tok->mCameraDistance = ( camera->viewMatrix() * /* I* */ center ).lengthSquared();
00110               tok->mCameraDistance = -( camera->viewMatrix() * /* I* */ center ).z();
00111           }
00112           else
00113             tok->mCameraDistance = 0;
00114         }
00115       }
00116 
00117       VL_CHECK( sorter )
00118       std::sort( mList.begin(), mList.begin() + size(), Sorter( sorter ) );
00119     }
00120 
00121   private:
00122     class Sorter
00123     {
00124     public:
00125       Sorter(const RenderQueueSorter* sorter): mRenderQueueSorter(sorter) {}
00126       bool operator()(const ref<RenderToken>& a, const ref<RenderToken>& b) const
00127       {
00128         VL_CHECK(a && b);
00129         return mRenderQueueSorter->operator()(a.get(), b.get());
00130       }
00131     protected:
00132       const RenderQueueSorter* mRenderQueueSorter;
00133     };
00134 
00135   protected:
00136     // mic fixme: would be nice not to allocate these dinamically. 
00137     // Necessary because:
00138     // 1- RenderToken.mNextPass: the pointer should be stable across std::vector reallocations. 
00139     //    Maybe we can use indices?
00140     // 2- The sorting sorts only pointers instead of whole structures.
00141     // Note: we need two lists because the sorting must still respect the multipassing order.
00142     std::vector< ref<RenderToken> > mList;
00143     std::vector< ref<RenderToken> > mListMP;
00144     int mSize;
00145     int mSizeMP;
00146   };
00147   //------------------------------------------------------------------------------
00148   typedef std::map< float, ref<RenderQueue> > TRenderQueueMap;
00149   //------------------------------------------------------------------------------
00150 }
00151 
00152 #endif

Visualization Library 2011.09.1160 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 2 2013 13:40:40.
Permission is granted to use this page to write and publish articles regarding Visualization Library.