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-2011 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 00302 00305 template <typename T> 00306 class HashedVector 00307 { 00308 public: 00309 typedef std::vector<T, STLAllocator<T, GeneralAllocPolicy> > VectorImpl; 00310 protected: 00311 VectorImpl mList; 00312 mutable uint32 mListHash; 00313 mutable bool mListHashDirty; 00314 00315 void addToHash(const T& newPtr) const 00316 { 00317 mListHash = FastHash((const char*)&newPtr, sizeof(T), mListHash); 00318 } 00319 void recalcHash() const 00320 { 00321 mListHash = 0; 00322 for (const_iterator i = mList.begin(); i != mList.end(); ++i) 00323 addToHash(*i); 00324 mListHashDirty = false; 00325 00326 } 00327 00328 public: 00329 typedef typename VectorImpl::value_type value_type; 00330 typedef typename VectorImpl::pointer pointer; 00331 typedef typename VectorImpl::reference reference; 00332 typedef typename VectorImpl::const_reference const_reference; 00333 typedef typename VectorImpl::size_type size_type; 00334 typedef typename VectorImpl::difference_type difference_type; 00335 typedef typename VectorImpl::iterator iterator; 00336 typedef typename VectorImpl::const_iterator const_iterator; 00337 typedef typename VectorImpl::reverse_iterator reverse_iterator; 00338 typedef typename VectorImpl::const_reverse_iterator const_reverse_iterator; 00339 00340 void dirtyHash() 00341 { 00342 mListHashDirty = true; 00343 } 00344 bool isHashDirty() const 00345 { 00346 return mListHashDirty; 00347 } 00348 00349 iterator begin() 00350 { 00351 // we have to assume that hash needs recalculating on non-const 00352 dirtyHash(); 00353 return mList.begin(); 00354 } 00355 iterator end() { return mList.end(); } 00356 const_iterator begin() const { return mList.begin(); } 00357 const_iterator end() const { return mList.end(); } 00358 reverse_iterator rbegin() 00359 { 00360 // we have to assume that hash needs recalculating on non-const 00361 dirtyHash(); 00362 return mList.rbegin(); 00363 } 00364 reverse_iterator rend() { return mList.rend(); } 00365 const_reverse_iterator rbegin() const { return mList.rbegin(); } 00366 const_reverse_iterator rend() const { return mList.rend(); } 00367 size_type size() const { return mList.size(); } 00368 size_type max_size() const { return mList.max_size(); } 00369 size_type capacity() const { return mList.capacity(); } 00370 bool empty() const { return mList.empty(); } 00371 reference operator[](size_type n) 00372 { 00373 // we have to assume that hash needs recalculating on non-const 00374 dirtyHash(); 00375 return mList[n]; 00376 } 00377 const_reference operator[](size_type n) const { return mList[n]; } 00378 reference at(size_type n) 00379 { 00380 // we have to assume that hash needs recalculating on non-const 00381 dirtyHash(); 00382 return mList.const_iterator(n); 00383 } 00384 const_reference at(size_type n) const { return mList.at(n); } 00385 HashedVector() : mListHash(0), mListHashDirty(false) {} 00386 HashedVector(size_type n) : mList(n), mListHash(0), mListHashDirty(n > 0) {} 00387 HashedVector(size_type n, const T& t) : mList(n, t), mListHash(0), mListHashDirty(n > 0) {} 00388 HashedVector(const HashedVector<T>& rhs) 00389 : mList(rhs.mList), mListHash(rhs.mListHash), mListHashDirty(rhs.mListHashDirty) {} 00390 00391 template <class InputIterator> 00392 HashedVector(InputIterator a, InputIterator b) 00393 : mList(a, b), mListHashDirty(false) 00394 { 00395 dirtyHash(); 00396 } 00397 00398 ~HashedVector() {} 00399 HashedVector<T>& operator=(const HashedVector<T>& rhs) 00400 { 00401 mList = rhs.mList; 00402 mListHash = rhs.mListHash; 00403 mListHashDirty = rhs.mListHashDirty; 00404 return *this; 00405 } 00406 00407 void reserve(size_t t) { mList.reserve(t); } 00408 reference front() 00409 { 00410 // we have to assume that hash needs recalculating on non-const 00411 dirtyHash(); 00412 return mList.front(); 00413 } 00414 const_reference front() const { return mList.front(); } 00415 reference back() 00416 { 00417 // we have to assume that hash needs recalculating on non-const 00418 dirtyHash(); 00419 return mList.back(); 00420 } 00421 const_reference back() const { return mList.back(); } 00422 void push_back(const T& t) 00423 { 00424 mList.push_back(t); 00425 // Quick progressive hash add 00426 if (!isHashDirty()) 00427 addToHash(t); 00428 } 00429 void pop_back() 00430 { 00431 mList.pop_back(); 00432 dirtyHash(); 00433 } 00434 void swap(HashedVector<T>& rhs) 00435 { 00436 mList.swap(rhs.mList); 00437 dirtyHash(); 00438 } 00439 iterator insert(iterator pos, const T& t) 00440 { 00441 bool recalc = (pos != end()); 00442 iterator ret = mList.insert(pos, t); 00443 if (recalc) 00444 dirtyHash(); 00445 else 00446 addToHash(t); 00447 return ret; 00448 } 00449 00450 template <class InputIterator> 00451 void insert(iterator pos, 00452 InputIterator f, InputIterator l) 00453 { 00454 mList.insert(pos, f, l); 00455 dirtyHash(); 00456 } 00457 00458 void insert(iterator pos, size_type n, const T& x) 00459 { 00460 mList.insert(pos, n, x); 00461 dirtyHash(); 00462 } 00463 00464 iterator erase(iterator pos) 00465 { 00466 iterator ret = mList.erase(pos); 00467 dirtyHash(); 00468 return ret; 00469 } 00470 iterator erase(iterator first, iterator last) 00471 { 00472 iterator ret = mList.erase(first, last); 00473 dirtyHash(); 00474 return ret; 00475 } 00476 void clear() 00477 { 00478 mList.clear(); 00479 mListHash = 0; 00480 mListHashDirty = false; 00481 } 00482 00483 void resize(size_type n, const T& t = T()) 00484 { 00485 bool recalc = false; 00486 if (n != size()) 00487 recalc = true; 00488 00489 mList.resize(n, t); 00490 if (recalc) 00491 dirtyHash(); 00492 } 00493 00494 bool operator==(const HashedVector<T>& b) 00495 { return mListHash == b.mListHash; } 00496 00497 bool operator<(const HashedVector<T>& b) 00498 { return mListHash < b.mListHash; } 00499 00500 00502 uint32 getHash() const 00503 { 00504 if (isHashDirty()) 00505 recalcHash(); 00506 00507 return mListHash; 00508 } 00509 public: 00510 00511 00512 00513 }; 00514 00515 class Light; 00516 typedef HashedVector<Light*> LightList; 00517 00518 00519 00520 typedef map<String, bool>::type UnaryOptionList; 00521 typedef map<String, String>::type BinaryOptionList; 00522 00524 typedef map<String, String>::type NameValuePairList; 00525 00527 typedef map<String, String>::type AliasTextureNamePairList; 00528 00529 template< typename T > struct TRect 00530 { 00531 T left, top, right, bottom; 00532 TRect() : left(0), top(0), right(0), bottom(0) {} 00533 TRect( T const & l, T const & t, T const & r, T const & b ) 00534 : left( l ), top( t ), right( r ), bottom( b ) 00535 { 00536 } 00537 TRect( TRect const & o ) 00538 : left( o.left ), top( o.top ), right( o.right ), bottom( o.bottom ) 00539 { 00540 } 00541 TRect & operator=( TRect const & o ) 00542 { 00543 left = o.left; 00544 top = o.top; 00545 right = o.right; 00546 bottom = o.bottom; 00547 return *this; 00548 } 00549 T width() const 00550 { 00551 return right - left; 00552 } 00553 T height() const 00554 { 00555 return bottom - top; 00556 } 00557 bool isNull() const 00558 { 00559 return width() == 0 || height() == 0; 00560 } 00561 void setNull() 00562 { 00563 left = right = top = bottom = 0; 00564 } 00565 TRect & merge(const TRect& rhs) 00566 { 00567 if (isNull()) 00568 { 00569 *this = rhs; 00570 } 00571 else if (!rhs.isNull()) 00572 { 00573 left = std::min(left, rhs.left); 00574 right = std::max(right, rhs.right); 00575 top = std::min(top, rhs.top); 00576 bottom = std::max(bottom, rhs.bottom); 00577 } 00578 00579 return *this; 00580 00581 } 00582 TRect intersect(const TRect& rhs) const 00583 { 00584 TRect ret; 00585 if (isNull() || rhs.isNull()) 00586 { 00587 // empty 00588 return ret; 00589 } 00590 else 00591 { 00592 ret.left = std::max(left, rhs.left); 00593 ret.right = std::min(right, rhs.right); 00594 ret.top = std::max(top, rhs.top); 00595 ret.bottom = std::min(bottom, rhs.bottom); 00596 } 00597 00598 if (ret.left > ret.right || ret.top > ret.bottom) 00599 { 00600 // no intersection, return empty 00601 ret.left = ret.top = ret.right = ret.bottom = 0; 00602 } 00603 00604 return ret; 00605 00606 } 00607 00608 }; 00609 template<typename T> 00610 std::ostream& operator<<(std::ostream& o, const TRect<T>& r) 00611 { 00612 o << "TRect<>(l:" << r.left << ", t:" << r.top << ", r:" << r.right << ", b:" << r.bottom << ")"; 00613 return o; 00614 } 00615 00618 typedef TRect<float> FloatRect; 00619 00623 typedef TRect<Real> RealRect; 00624 00627 typedef TRect< long > Rect; 00628 00633 struct Box 00634 { 00635 size_t left, top, right, bottom, front, back; 00637 Box() 00638 : left(0), top(0), right(1), bottom(1), front(0), back(1) 00639 { 00640 } 00650 Box( size_t l, size_t t, size_t r, size_t b ): 00651 left(l), 00652 top(t), 00653 right(r), 00654 bottom(b), 00655 front(0), 00656 back(1) 00657 { 00658 assert(right >= left && bottom >= top && back >= front); 00659 } 00671 Box( size_t l, size_t t, size_t ff, size_t r, size_t b, size_t bb ): 00672 left(l), 00673 top(t), 00674 right(r), 00675 bottom(b), 00676 front(ff), 00677 back(bb) 00678 { 00679 assert(right >= left && bottom >= top && back >= front); 00680 } 00681 00683 bool contains(const Box &def) const 00684 { 00685 return (def.left >= left && def.top >= top && def.front >= front && 00686 def.right <= right && def.bottom <= bottom && def.back <= back); 00687 } 00688 00690 size_t getWidth() const { return right-left; } 00692 size_t getHeight() const { return bottom-top; } 00694 size_t getDepth() const { return back-front; } 00695 }; 00696 00697 00698 00710 int _OgreExport findCommandLineOpts(int numargs, char** argv, UnaryOptionList& unaryOptList, 00711 BinaryOptionList& binOptList); 00712 00714 enum ClipResult 00715 { 00717 CLIPPED_NONE = 0, 00719 CLIPPED_SOME = 1, 00721 CLIPPED_ALL = 2 00722 }; 00723 00725 struct RenderWindowDescription 00726 { 00727 String name; 00728 unsigned int width; 00729 unsigned int height; 00730 bool useFullScreen; 00731 NameValuePairList miscParams; 00732 }; 00733 00735 typedef vector<RenderWindowDescription>::type RenderWindowDescriptionList; 00736 00738 typedef vector<RenderWindow*>::type RenderWindowList; 00739 00741 class _OgreExport NameGenerator 00742 { 00743 protected: 00744 String mPrefix; 00745 unsigned long long int mNext; 00746 OGRE_AUTO_MUTEX 00747 public: 00748 NameGenerator(const NameGenerator& rhs) 00749 : mPrefix(rhs.mPrefix), mNext(rhs.mNext) {} 00750 00751 NameGenerator(const String& prefix) : mPrefix(prefix), mNext(1) {} 00752 00754 String generate() 00755 { 00756 OGRE_LOCK_AUTO_MUTEX 00757 std::ostringstream s; 00758 s << mPrefix << mNext++; 00759 return s.str(); 00760 } 00761 00763 void reset() 00764 { 00765 OGRE_LOCK_AUTO_MUTEX 00766 mNext = 1ULL; 00767 } 00768 00770 void setNext(unsigned long long int val) 00771 { 00772 OGRE_LOCK_AUTO_MUTEX 00773 mNext = val; 00774 } 00775 00777 unsigned long long int getNext() const 00778 { 00779 // lock even on get because 64-bit may not be atomic read 00780 OGRE_LOCK_AUTO_MUTEX 00781 return mNext; 00782 } 00783 00784 00785 00786 00787 }; 00788 00791 template <typename T> 00792 class Pool 00793 { 00794 protected: 00795 typedef typename list<T>::type ItemList; 00796 ItemList mItems; 00797 OGRE_AUTO_MUTEX 00798 public: 00799 Pool() {} 00800 virtual ~Pool() {} 00801 00805 virtual std::pair<bool, T> removeItem() 00806 { 00807 OGRE_LOCK_AUTO_MUTEX 00808 std::pair<bool, T> ret; 00809 if (mItems.empty()) 00810 { 00811 ret.first = false; 00812 } 00813 else 00814 { 00815 ret.first = true; 00816 ret.second = mItems.front(); 00817 mItems.pop_front(); 00818 } 00819 return ret; 00820 } 00821 00824 virtual void addItem(const T& i) 00825 { 00826 OGRE_LOCK_AUTO_MUTEX 00827 mItems.push_front(i); 00828 } 00830 virtual void clear() 00831 { 00832 OGRE_LOCK_AUTO_MUTEX 00833 mItems.clear(); 00834 } 00835 00836 00837 00838 }; 00841 } 00842 00843 #endif
Copyright © 2008 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Sat Jan 14 2012 18:40:43