scenePolyhedralObject.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _SCENEPOLYHEDRALOBJECT_H_
  23. #define _SCENEPOLYHEDRALOBJECT_H_
  24. #ifndef _MPOLYHEDRON_H_
  25. #include "math/mPolyhedron.h"
  26. #endif
  27. /// Shared interface for polyhedral objects.
  28. struct IScenePolyhedralObject
  29. {
  30. /// Convert the polyhedral object to a raw polyhedron.
  31. virtual AnyPolyhedron ToAnyPolyhedron() const = 0;
  32. };
  33. /// Helper template for mixing a polyhedral volume definition into
  34. /// the superclass hierarchy of a SceneSpace-derived class.
  35. template< typename Base, typename P = Polyhedron >
  36. class ScenePolyhedralObject : public Base, public IScenePolyhedralObject
  37. {
  38. public:
  39. typedef Base Parent;
  40. typedef P PolyhedronType;
  41. enum
  42. {
  43. MAX_PLANES = 256,
  44. MAX_POINTS = 256,
  45. MAX_EDGES = 256
  46. };
  47. protected:
  48. enum
  49. {
  50. PolyMask = Parent::NextFreeMask << 0,
  51. NextFreeMask = Parent::NextFreeMask << 1
  52. };
  53. /// Whether the polyhedron corresponds to the object box. If so,
  54. /// several things can be fast-tracked. For example, serializing the
  55. /// polyhedron is pointless as it can be easily reconstructed from the
  56. /// object box on load time. Also, certain operations like containment
  57. /// tests have significantly faster formulations for AABBs (given that the
  58. /// input data is transformed into object space) than for general
  59. /// polyhedrons.
  60. bool mIsBox;
  61. /// The polyhedron that defines the volume of the object.
  62. /// @note Defined in object space by default.
  63. PolyhedronType mPolyhedron;
  64. ///
  65. virtual void _renderObject( ObjectRenderInst* ri, SceneRenderState* state, BaseMatInstance* overrideMat );
  66. public:
  67. ScenePolyhedralObject()
  68. : mIsBox( true ) {}
  69. ScenePolyhedralObject( const PolyhedronType& polyhedron )
  70. : mIsBox( false ),
  71. mPolyhedron( polyhedron ) {}
  72. /// Return the polyhedron that describes the space.
  73. const PolyhedronType& getPolyhedron() const { return mPolyhedron; }
  74. // SimObject.
  75. virtual bool onAdd();
  76. virtual void writeFields( Stream& stream, U32 tabStop );
  77. virtual bool writeField( StringTableEntry name, const char* value );
  78. static void initPersistFields();
  79. // NetObject.
  80. virtual U32 packUpdate( NetConnection* connection, U32 mask, BitStream* stream );
  81. virtual void unpackUpdate( NetConnection* connection, BitStream* stream );
  82. // SceneObject.
  83. virtual bool containsPoint( const Point3F& point );
  84. // IScenePolyhedralObject.
  85. virtual AnyPolyhedron ToAnyPolyhedron() const { return getPolyhedron(); }
  86. private:
  87. static bool _setPlane( void* object, const char* index, const char* data );
  88. static bool _setPoint( void* object, const char* index, const char* data );
  89. static bool _setEdge( void* object, const char* index, const char* data );
  90. };
  91. #endif // !_SCENEPOLYHEDRALOBJECT_H_