OgreCommon.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://www.ogre3d.org/
00006 
00007 Copyright (c) 2000-2012 Torus Knot Software Ltd
00008 
00009 Permission is hereby granted, free of charge, to any person obtaining a copy
00010 of this software and associated documentation files (the "Software"), to deal
00011 in the Software without restriction, including without limitation the rights
00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00013 copies of the Software, and to permit persons to whom the Software is
00014 furnished to do so, subject to the following conditions:
00015 
00016 The above copyright notice and this permission notice shall be included in
00017 all copies or substantial portions of the Software.
00018 
00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00025 THE SOFTWARE.
00026 -----------------------------------------------------------------------------
00027 */
00028 #ifndef __Common_H__
00029 #define __Common_H__
00030 // Common stuff
00031 
00032 #include "OgreString.h"
00033 
00034 #if defined ( OGRE_GCC_VISIBILITY )
00035 #   pragma GCC visibility push(default)
00036 #endif
00037 
00038 #include <utility>
00039 #include <sstream>
00040 
00041 #if defined ( OGRE_GCC_VISIBILITY )
00042 #   pragma GCC visibility pop
00043 #endif
00044 
00045 namespace Ogre {
00053 
00054     uint32 _OgreExport FastHash (const char * data, int len, uint32 hashSoFar = 0);
00056     template <typename T>
00057     uint32 HashCombine (uint32 hashSoFar, const T& data)
00058     {
00059         return FastHash((const char*)&data, sizeof(T), hashSoFar);
00060     }
00061 
00062 
00065     enum CompareFunction
00066     {
00067         CMPF_ALWAYS_FAIL,
00068         CMPF_ALWAYS_PASS,
00069         CMPF_LESS,
00070         CMPF_LESS_EQUAL,
00071         CMPF_EQUAL,
00072         CMPF_NOT_EQUAL,
00073         CMPF_GREATER_EQUAL,
00074         CMPF_GREATER
00075     };
00076 
00079     enum TextureFilterOptions
00080     {
00082         TFO_NONE,
00084         TFO_BILINEAR,
00086         TFO_TRILINEAR,
00088         TFO_ANISOTROPIC
00089     };
00090 
00091     enum FilterType
00092     {
00094         FT_MIN,
00096         FT_MAG,
00098         FT_MIP
00099     };
00101     enum FilterOptions
00102     {
00104         FO_NONE,
00106         FO_POINT,
00108         FO_LINEAR,
00110         FO_ANISOTROPIC
00111     };
00112 
00114     enum ShadeOptions
00115     {
00116         SO_FLAT,
00117         SO_GOURAUD,
00118         SO_PHONG
00119     };
00120 
00122     enum FogMode
00123     {
00125         FOG_NONE,
00127         FOG_EXP,
00129         FOG_EXP2,
00131         FOG_LINEAR
00132     };
00133 
00136     enum CullingMode
00137     {
00139         CULL_NONE = 1,
00141         CULL_CLOCKWISE = 2,
00143         CULL_ANTICLOCKWISE = 3
00144     };
00145 
00151     enum ManualCullingMode
00152     {
00154         MANUAL_CULL_NONE = 1,
00156         MANUAL_CULL_BACK = 2,
00158         MANUAL_CULL_FRONT = 3
00159     };
00160 
00162     enum WaveformType
00163     {
00165         WFT_SINE,
00167         WFT_TRIANGLE,
00169         WFT_SQUARE,
00171         WFT_SAWTOOTH,
00173         WFT_INVERSE_SAWTOOTH,
00176         WFT_PWM
00177     };
00178 
00180     enum PolygonMode
00181     {
00183         PM_POINTS = 1,
00185         PM_WIREFRAME = 2,
00187         PM_SOLID = 3
00188     };
00189 
00191     enum ShadowTechnique
00192     {
00194         SHADOWTYPE_NONE = 0x00,
00197         SHADOWDETAILTYPE_ADDITIVE = 0x01,
00200         SHADOWDETAILTYPE_MODULATIVE = 0x02,
00203         SHADOWDETAILTYPE_INTEGRATED = 0x04,
00206         SHADOWDETAILTYPE_STENCIL = 0x10,
00209         SHADOWDETAILTYPE_TEXTURE = 0x20,
00210         
00217         SHADOWTYPE_STENCIL_MODULATIVE = 0x12,
00225         SHADOWTYPE_STENCIL_ADDITIVE = 0x11,
00230         SHADOWTYPE_TEXTURE_MODULATIVE = 0x22,
00231         
00240         SHADOWTYPE_TEXTURE_ADDITIVE = 0x21,
00241 
00257         SHADOWTYPE_TEXTURE_ADDITIVE_INTEGRATED = 0x25,
00273         SHADOWTYPE_TEXTURE_MODULATIVE_INTEGRATED = 0x26
00274     };
00275 
00277     typedef int TrackVertexColourType;
00278     enum TrackVertexColourEnum {
00279         TVC_NONE        = 0x0,
00280         TVC_AMBIENT     = 0x1,        
00281         TVC_DIFFUSE     = 0x2,
00282         TVC_SPECULAR    = 0x4,
00283         TVC_EMISSIVE    = 0x8
00284     };
00285 
00287     enum SortMode
00288     {
00290         SM_DIRECTION,
00292         SM_DISTANCE
00293     };
00294 
00296     enum FrameBufferType {
00297         FBT_COLOUR  = 0x1,
00298         FBT_DEPTH   = 0x2,
00299         FBT_STENCIL = 0x4
00300     };
00301 
00303     enum InstanceManagerFlags
00304     {
00308         IM_USE16BIT     = 0x0001,
00309 
00312         IM_VTFBESTFIT   = 0x0002,
00313 
00316         IM_VTFBONEMATRIXLOOKUP = 0x0004,
00317 
00318         IM_USEBONEDUALQUATERNIONS = 0x0008,
00319 
00321         IM_USEONEWEIGHT = 0x0010,
00322 
00324         IM_FORCEONEWEIGHT = 0x0020,
00325 
00326         IM_USEALL       = IM_USE16BIT|IM_VTFBESTFIT|IM_USEONEWEIGHT
00327     };
00328     
00329     
00332     template <typename T>
00333     class HashedVector
00334     {
00335     public:
00336         typedef std::vector<T, STLAllocator<T, GeneralAllocPolicy> > VectorImpl;
00337     protected:
00338         VectorImpl mList;
00339         mutable uint32 mListHash;
00340         mutable bool mListHashDirty;
00341 
00342         void addToHash(const T& newPtr) const
00343         {
00344             mListHash = FastHash((const char*)&newPtr, sizeof(T), mListHash);
00345         }
00346         void recalcHash() const
00347         {
00348             mListHash = 0;
00349             for (const_iterator i = mList.begin(); i != mList.end(); ++i)
00350                 addToHash(*i);
00351             mListHashDirty = false;
00352             
00353         }
00354 
00355     public:
00356         typedef typename VectorImpl::value_type value_type;
00357         typedef typename VectorImpl::pointer pointer;
00358         typedef typename VectorImpl::reference reference;
00359         typedef typename VectorImpl::const_reference const_reference;
00360         typedef typename VectorImpl::size_type size_type;
00361         typedef typename VectorImpl::difference_type difference_type;
00362         typedef typename VectorImpl::iterator iterator;
00363         typedef typename VectorImpl::const_iterator const_iterator;
00364         typedef typename VectorImpl::reverse_iterator reverse_iterator;
00365         typedef typename VectorImpl::const_reverse_iterator const_reverse_iterator;
00366 
00367         void dirtyHash()
00368         {
00369             mListHashDirty = true;
00370         }
00371         bool isHashDirty() const
00372         {
00373             return mListHashDirty;
00374         }
00375 
00376         iterator begin() 
00377         { 
00378             // we have to assume that hash needs recalculating on non-const
00379             dirtyHash();
00380             return mList.begin(); 
00381         }
00382         iterator end() { return mList.end(); }
00383         const_iterator begin() const { return mList.begin(); }
00384         const_iterator end() const { return mList.end(); }
00385         reverse_iterator rbegin() 
00386         { 
00387             // we have to assume that hash needs recalculating on non-const
00388             dirtyHash();
00389             return mList.rbegin(); 
00390         }
00391         reverse_iterator rend() { return mList.rend(); }
00392         const_reverse_iterator rbegin() const { return mList.rbegin(); }
00393         const_reverse_iterator rend() const { return mList.rend(); }
00394         size_type size() const { return mList.size(); }
00395         size_type max_size() const { return mList.max_size(); }
00396         size_type capacity() const { return mList.capacity(); }
00397         bool empty() const { return mList.empty(); }
00398         reference operator[](size_type n) 
00399         { 
00400             // we have to assume that hash needs recalculating on non-const
00401             dirtyHash();
00402             return mList[n]; 
00403         }
00404         const_reference operator[](size_type n) const { return mList[n]; }
00405         reference at(size_type n) 
00406         { 
00407             // we have to assume that hash needs recalculating on non-const
00408             dirtyHash();
00409             return mList.const_iterator(n); 
00410         }
00411         const_reference at(size_type n) const { return mList.at(n); }
00412         HashedVector() : mListHash(0), mListHashDirty(false) {}
00413         HashedVector(size_type n) : mList(n), mListHash(0), mListHashDirty(n > 0) {}
00414         HashedVector(size_type n, const T& t) : mList(n, t), mListHash(0), mListHashDirty(n > 0) {}
00415         HashedVector(const HashedVector<T>& rhs) 
00416             : mList(rhs.mList), mListHash(rhs.mListHash), mListHashDirty(rhs.mListHashDirty) {}
00417 
00418         template <class InputIterator>
00419         HashedVector(InputIterator a, InputIterator b)
00420             : mList(a, b), mListHashDirty(false)
00421         {
00422             dirtyHash();
00423         }
00424 
00425         ~HashedVector() {}
00426         HashedVector<T>& operator=(const HashedVector<T>& rhs)
00427         {
00428             mList = rhs.mList;
00429             mListHash = rhs.mListHash;
00430             mListHashDirty = rhs.mListHashDirty;
00431             return *this;
00432         }
00433 
00434         void reserve(size_t t) { mList.reserve(t); }
00435         reference front() 
00436         { 
00437             // we have to assume that hash needs recalculating on non-const
00438             dirtyHash();
00439             return mList.front(); 
00440         }
00441         const_reference front() const { return mList.front(); }
00442         reference back()  
00443         { 
00444             // we have to assume that hash needs recalculating on non-const
00445             dirtyHash();
00446             return mList.back(); 
00447         }
00448         const_reference back() const { return mList.back(); }
00449         void push_back(const T& t)
00450         { 
00451             mList.push_back(t);
00452             // Quick progressive hash add
00453             if (!isHashDirty())
00454                 addToHash(t);
00455         }
00456         void pop_back()
00457         {
00458             mList.pop_back();
00459             dirtyHash();
00460         }
00461         void swap(HashedVector<T>& rhs)
00462         {
00463             mList.swap(rhs.mList);
00464             dirtyHash();
00465         }
00466         iterator insert(iterator pos, const T& t)
00467         {
00468             bool recalc = (pos != end());
00469             iterator ret = mList.insert(pos, t);
00470             if (recalc)
00471                 dirtyHash();
00472             else
00473                 addToHash(t);
00474             return ret;
00475         }
00476 
00477         template <class InputIterator>
00478         void insert(iterator pos,
00479             InputIterator f, InputIterator l)
00480         {
00481             mList.insert(pos, f, l);
00482             dirtyHash();
00483         }
00484 
00485         void insert(iterator pos, size_type n, const T& x)
00486         {
00487             mList.insert(pos, n, x);
00488             dirtyHash();
00489         }
00490 
00491         iterator erase(iterator pos)
00492         {
00493             iterator ret = mList.erase(pos);
00494             dirtyHash();
00495             return ret;
00496         }
00497         iterator erase(iterator first, iterator last)
00498         {
00499             iterator ret = mList.erase(first, last);
00500             dirtyHash();
00501             return ret;
00502         }
00503         void clear()
00504         {
00505             mList.clear();
00506             mListHash = 0;
00507             mListHashDirty = false;
00508         }
00509 
00510         void resize(size_type n, const T& t = T())
00511         {
00512             bool recalc = false;
00513             if (n != size())
00514                 recalc = true;
00515 
00516             mList.resize(n, t);
00517             if (recalc)
00518                 dirtyHash();
00519         }
00520 
00521         bool operator==(const HashedVector<T>& b)
00522         { return mListHash == b.mListHash; }
00523 
00524         bool operator<(const HashedVector<T>& b)
00525         { return mListHash < b.mListHash; }
00526 
00527 
00529         uint32 getHash() const 
00530         { 
00531             if (isHashDirty())
00532                 recalcHash();
00533 
00534             return mListHash; 
00535         }
00536     public:
00537 
00538 
00539 
00540     };
00541 
00542     class Light;
00543     typedef HashedVector<Light*> LightList;
00544 
00545 
00546 
00547     typedef map<String, bool>::type UnaryOptionList;
00548     typedef map<String, String>::type BinaryOptionList;
00549 
00551     typedef map<String, String>::type NameValuePairList;
00552 
00554     typedef map<String, String>::type AliasTextureNamePairList;
00555 
00556         template< typename T > struct TRect
00557         {
00558           T left, top, right, bottom;
00559           TRect() : left(0), top(0), right(0), bottom(0) {}
00560           TRect( T const & l, T const & t, T const & r, T const & b )
00561             : left( l ), top( t ), right( r ), bottom( b )
00562           {
00563           }
00564           TRect( TRect const & o )
00565             : left( o.left ), top( o.top ), right( o.right ), bottom( o.bottom )
00566           {
00567           }
00568           TRect & operator=( TRect const & o )
00569           {
00570             left = o.left;
00571             top = o.top;
00572             right = o.right;
00573             bottom = o.bottom;
00574             return *this;
00575           }
00576           T width() const
00577           {
00578             return right - left;
00579           }
00580           T height() const
00581           {
00582             return bottom - top;
00583           }
00584           bool isNull() const
00585           {
00586               return width() == 0 || height() == 0;
00587           }
00588           void setNull()
00589           {
00590               left = right = top = bottom = 0;
00591           }
00592           TRect & merge(const TRect& rhs)
00593           {
00594               if (isNull())
00595               {
00596                   *this = rhs;
00597               }
00598               else if (!rhs.isNull())
00599               {
00600                   left = std::min(left, rhs.left);
00601                   right = std::max(right, rhs.right);
00602                   top = std::min(top, rhs.top);
00603                   bottom = std::max(bottom, rhs.bottom);
00604               }
00605 
00606               return *this;
00607 
00608           }
00609           TRect intersect(const TRect& rhs) const
00610           {
00611               TRect ret;
00612               if (isNull() || rhs.isNull())
00613               {
00614                   // empty
00615                   return ret;
00616               }
00617               else
00618               {
00619                   ret.left = std::max(left, rhs.left);
00620                   ret.right = std::min(right, rhs.right);
00621                   ret.top = std::max(top, rhs.top);
00622                   ret.bottom = std::min(bottom, rhs.bottom);
00623               }
00624 
00625               if (ret.left > ret.right || ret.top > ret.bottom)
00626               {
00627                   // no intersection, return empty
00628                   ret.left = ret.top = ret.right = ret.bottom = 0;
00629               }
00630 
00631               return ret;
00632 
00633           }
00634 
00635         };
00636         template<typename T>
00637         std::ostream& operator<<(std::ostream& o, const TRect<T>& r)
00638         {
00639             o << "TRect<>(l:" << r.left << ", t:" << r.top << ", r:" << r.right << ", b:" << r.bottom << ")";
00640             return o;
00641         }
00642 
00645         typedef TRect<float> FloatRect;
00646 
00650         typedef TRect<Real> RealRect;
00651 
00654         typedef TRect< long > Rect;
00655 
00660         struct Box
00661         {
00662             size_t left, top, right, bottom, front, back;
00664             Box()
00665                 : left(0), top(0), right(1), bottom(1), front(0), back(1)
00666             {
00667             }
00677             Box( size_t l, size_t t, size_t r, size_t b ):
00678                 left(l),
00679                 top(t),   
00680                 right(r),
00681                 bottom(b),
00682                 front(0),
00683                 back(1)
00684             {
00685                 assert(right >= left && bottom >= top && back >= front);
00686             }
00698             Box( size_t l, size_t t, size_t ff, size_t r, size_t b, size_t bb ):
00699                 left(l),
00700                 top(t),   
00701                 right(r),
00702                 bottom(b),
00703                 front(ff),
00704                 back(bb)
00705             {
00706                 assert(right >= left && bottom >= top && back >= front);
00707             }
00708             
00710             bool contains(const Box &def) const
00711             {
00712                 return (def.left >= left && def.top >= top && def.front >= front &&
00713                     def.right <= right && def.bottom <= bottom && def.back <= back);
00714             }
00715             
00717             size_t getWidth() const { return right-left; }
00719             size_t getHeight() const { return bottom-top; }
00721             size_t getDepth() const { return back-front; }
00722         };
00723 
00724     
00725     
00737     int _OgreExport findCommandLineOpts(int numargs, char** argv, UnaryOptionList& unaryOptList, 
00738         BinaryOptionList& binOptList);
00739 
00741     enum ClipResult
00742     {
00744         CLIPPED_NONE = 0,
00746         CLIPPED_SOME = 1, 
00748         CLIPPED_ALL = 2
00749     };
00750 
00752     struct RenderWindowDescription
00753     {
00754         String              name;
00755         unsigned int        width;
00756         unsigned int        height;
00757         bool                useFullScreen;
00758         NameValuePairList   miscParams;
00759     };
00760 
00762     typedef vector<RenderWindowDescription>::type RenderWindowDescriptionList;
00763 
00765     typedef vector<RenderWindow*>::type RenderWindowList;
00766 
00768     class _OgreExport NameGenerator
00769     {
00770     protected:
00771         String mPrefix;
00772         unsigned long long int mNext;
00773         OGRE_AUTO_MUTEX
00774     public:
00775         NameGenerator(const NameGenerator& rhs)
00776             : mPrefix(rhs.mPrefix), mNext(rhs.mNext) {}
00777         
00778         NameGenerator(const String& prefix) : mPrefix(prefix), mNext(1) {}
00779 
00781         String generate()
00782         {
00783             OGRE_LOCK_AUTO_MUTEX
00784             std::ostringstream s;
00785             s << mPrefix << mNext++;
00786             return s.str();
00787         }
00788 
00790         void reset()
00791         {
00792             OGRE_LOCK_AUTO_MUTEX
00793             mNext = 1ULL;
00794         }
00795 
00797         void setNext(unsigned long long int val)
00798         {
00799             OGRE_LOCK_AUTO_MUTEX
00800             mNext = val;
00801         }
00802 
00804         unsigned long long int getNext() const
00805         {
00806             // lock even on get because 64-bit may not be atomic read
00807             OGRE_LOCK_AUTO_MUTEX
00808             return mNext;
00809         }
00810 
00811 
00812 
00813 
00814     };
00815 
00818     template <typename T>
00819     class Pool
00820     {
00821     protected:
00822         typedef typename list<T>::type ItemList;
00823         ItemList mItems;
00824         OGRE_AUTO_MUTEX
00825     public:
00826         Pool() {} 
00827         virtual ~Pool() {}
00828 
00832         virtual std::pair<bool, T> removeItem()
00833         {
00834             OGRE_LOCK_AUTO_MUTEX
00835             std::pair<bool, T> ret;
00836             if (mItems.empty())
00837             {
00838                 ret.first = false;
00839             }
00840             else
00841             {
00842                 ret.first = true;
00843                 ret.second = mItems.front();
00844                 mItems.pop_front();
00845             }
00846             return ret;
00847         }
00848 
00851         virtual void addItem(const T& i)
00852         {
00853             OGRE_LOCK_AUTO_MUTEX
00854             mItems.push_front(i);
00855         }
00857         virtual void clear()
00858         {
00859             OGRE_LOCK_AUTO_MUTEX
00860             mItems.clear();
00861         }
00862 
00863 
00864 
00865     };
00868 }
00869 
00870 #endif

Copyright © 2012 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Fri May 25 23:36:23 2012