Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

OgreRenderQueueSortingGrouping.h

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://ogre.sourceforge.net/
00006 
00007 Copyright © 2000-2002 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00025 #ifndef __RenderQueueSortingGrouping_H__
00026 #define __RenderQueueSortingGrouping_H__
00027 
00028 // Precompiler options
00029 #include "OgrePrerequisites.h"
00030 #include "OgreIteratorWrappers.h"
00031 #include "OgreMaterial.h"
00032 
00033 namespace Ogre {
00034 
00047     class RenderPriorityGroup
00048     {
00049         friend class Ogre::SceneManager;
00052         struct TransparentQueueItemLess
00053         {
00054             const Camera* camera;
00055 
00056             _OgreExport bool operator()(const Renderable* x, const Renderable* y) const
00057             {
00058                 // Sort DESCENDING by depth (ie far objects first)
00059                 if (x->getSquaredViewDepth(camera) > y->getSquaredViewDepth(camera))
00060                 {
00061                     return true;
00062                 }
00063                 else
00064                 {
00065                     return false;
00066                 }
00067 
00068             }
00069         };
00070     public:
00071         typedef std::vector<Renderable*> RenderableList;
00073         typedef std::map<Material*, RenderableList > MaterialGroupMap;
00075         typedef std::vector<Renderable*> TransparentObjectList;
00076     protected:
00077         MaterialGroupMap mMaterialGroups;
00078         TransparentObjectList mTransparentObjects;
00079 
00080     public:
00081         RenderPriorityGroup() {}
00082 
00083         ~RenderPriorityGroup() {}
00084 
00086         void addRenderable(Renderable* pRend)
00087         {
00088             std::pair<MaterialGroupMap::iterator, bool> retPair;
00089             RenderableList newList;
00090 
00091             Material* pMat = pRend->getMaterial();
00092 
00093             assert(pMat && "Can't add a renderable with a null material!");
00094 
00095             if (pMat->isTransparent())
00096             {
00097                 // Insert into transparent object vector
00098                 mTransparentObjects.push_back(pRend);
00099                 
00100             }
00101             else
00102             {
00103 
00104                 // Try to insert, if already there existing will be returned
00105                 retPair = mMaterialGroups.insert(MaterialGroupMap::value_type(pMat, newList));
00106 
00107                 // Insert new Renderable
00108                 // retPair.first is iterator on map (Material*, std::vector)
00109                 // retPair.first->second is the vector of renderables
00110                 retPair.first->second.push_back(pRend);
00111             }
00112 
00113         }
00114 
00117         void sortTransparentObjects(const Camera* cam)
00118         {
00119             TransparentQueueItemLess lessFunctor;
00120             lessFunctor.camera = cam;
00121 
00122             std::sort(mTransparentObjects.begin(), mTransparentObjects.end(), 
00123                 lessFunctor);
00124         }
00125          
00126 
00129         void clear(void)
00130         {
00131             mMaterialGroups.clear();
00132             mTransparentObjects.clear();
00133 
00134         }
00135 
00136 
00137 
00138     };
00139 
00140 
00150     class RenderQueueGroup
00151     {
00152     public:
00153         typedef std::map<ushort, RenderPriorityGroup*, std::less<ushort> > PriorityMap;
00154         typedef MapIterator<PriorityMap> PriorityMapIterator;
00155     protected:
00156         // Map of RenderPriorityGroup objects
00157         PriorityMap mPriorityGroups;
00158 
00159 
00160     public:
00161         RenderQueueGroup() {}
00162 
00163         ~RenderQueueGroup() {
00164             // destroy contents now
00165             PriorityMap::iterator i;
00166             for (i = mPriorityGroups.begin(); i != mPriorityGroups.end(); ++i)
00167             {
00168                 delete i->second;
00169             }
00170         }
00171 
00173         PriorityMapIterator getIterator(void)
00174         {
00175             return PriorityMapIterator(mPriorityGroups.begin(), mPriorityGroups.end());
00176         }
00177 
00179         void addRenderable(Renderable* pRend, ushort priority)
00180         {
00181             // Check if priority group is there
00182             PriorityMap::iterator i = mPriorityGroups.find(priority);
00183             RenderPriorityGroup* pPriorityGrp;
00184             if (i == mPriorityGroups.end())
00185             {
00186                 // Missing, create
00187                 pPriorityGrp = new RenderPriorityGroup();
00188                 mPriorityGroups.insert(PriorityMap::value_type(priority, pPriorityGrp));
00189             }
00190             else
00191             {
00192                 pPriorityGrp = i->second;
00193             }
00194 
00195             // Add
00196             pPriorityGrp->addRenderable(pRend);
00197 
00198         }
00199 
00206         void clear(void)
00207         {
00208             PriorityMap::iterator i, iend;
00209             iend = mPriorityGroups.end();
00210             for (i = mPriorityGroups.begin(); i != iend; ++i)
00211             {
00212                 i->second->clear();
00213             }
00214 
00215         }
00216 
00217 
00218     };
00219 
00220 
00221 
00222 }
00223 
00224 #endif
00225 
00226 

Copyright © 2002 by The OGRE Team