Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
4.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Revision: $
  6. // $NoKeywords: $
  7. //
  8. // The BSP tree leaf data system
  9. //
  10. //=============================================================================//
  11. #include "tier0/platform.h"
  12. #if !defined( BSPTREEDATA )
  13. #define BSPTREEDATA
  14. #ifdef _WIN32
  15. #pragma once
  16. #endif
  17. //-----------------------------------------------------------------------------
  18. // Forward declarations
  19. //-----------------------------------------------------------------------------
  20. class Vector;
  21. struct Ray_t;
  22. //-----------------------------------------------------------------------------
  23. // Handle to an renderable in the client leaf system
  24. //-----------------------------------------------------------------------------
  25. typedef unsigned short BSPTreeDataHandle_t;
  26. enum
  27. {
  28. TREEDATA_INVALID_HANDLE = (BSPTreeDataHandle_t)~0
  29. };
  30. //-----------------------------------------------------------------------------
  31. // Interface needed by tree data to do its job
  32. //
  33. // Note that anything that has convex spatial regions with leaves identified
  34. // by indices can implement the ISpatialQuery. All you have to do is to implement
  35. // a class that can answer the 5 questions in the Query interface about the
  36. // spatial subdivision. For example, a K-D tree or a BSP tree could implement
  37. // this interface
  38. //
  39. //-----------------------------------------------------------------------------
  40. abstract_class ISpatialLeafEnumerator
  41. {
  42. public:
  43. // call back with a leaf and a context
  44. // The context is completely user defined; it's passed into the enumeration
  45. // function of ISpatialQuery.
  46. // This gets called by the enumeration methods with each leaf
  47. // that passes the test; return true to continue enumerating,
  48. // false to stop
  49. virtual bool EnumerateLeaf( int leaf, int context ) = 0;
  50. };
  51. abstract_class ISpatialQuery
  52. {
  53. public:
  54. // Returns the number of leaves
  55. virtual int LeafCount() const = 0;
  56. // Enumerates the leaves along a ray, box, etc.
  57. virtual bool EnumerateLeavesAtPoint( Vector const& pt, ISpatialLeafEnumerator* pEnum, int context ) = 0;
  58. virtual bool EnumerateLeavesInBox( Vector const& mins, Vector const& maxs, ISpatialLeafEnumerator* pEnum, int context ) = 0;
  59. virtual bool EnumerateLeavesInSphere( Vector const& center, float radius, ISpatialLeafEnumerator* pEnum, int context ) = 0;
  60. virtual bool EnumerateLeavesAlongRay( Ray_t const& ray, ISpatialLeafEnumerator* pEnum, int context ) = 0;
  61. };
  62. //-----------------------------------------------------------------------------
  63. // Data associated with leaves.
  64. //
  65. // This is a parasitic class that attaches data to the leaves specified by the
  66. // ISpatialQuery sent in to the initialization function. It can't exist without
  67. // a spatial partition of some sort to hold onto.
  68. //-----------------------------------------------------------------------------
  69. abstract_class IBSPTreeDataEnumerator
  70. {
  71. public:
  72. // call back with a userId and a context
  73. virtual bool FASTCALL EnumerateElement( int userId, int context ) = 0;
  74. };
  75. abstract_class IBSPTreeData
  76. {
  77. public:
  78. // Add a virtual destructor so that the derived class destructors will
  79. // be called.
  80. virtual ~IBSPTreeData() {}
  81. // Initializes, shuts down
  82. virtual void Init( ISpatialQuery* pBSPTree ) = 0;
  83. virtual void Shutdown() = 0;
  84. // Adds and removes data from the leaf lists
  85. virtual BSPTreeDataHandle_t Insert( int userId, Vector const& mins, Vector const& maxs ) = 0;
  86. virtual void Remove( BSPTreeDataHandle_t handle ) = 0;
  87. // Call this when a element moves
  88. virtual void ElementMoved( BSPTreeDataHandle_t handle, Vector const& mins, Vector const& maxs ) = 0;
  89. // Enumerate elements in a particular leaf
  90. virtual bool EnumerateElementsInLeaf( int leaf, IBSPTreeDataEnumerator* pEnum, int context ) = 0;
  91. // Is the element in any leaves at all?
  92. virtual bool IsElementInTree( BSPTreeDataHandle_t handle ) const = 0;
  93. // NOTE: These methods call through to the functions in the attached
  94. // ISpatialQuery
  95. // For convenience, enumerates the leaves along a ray, box, etc.
  96. virtual bool EnumerateLeavesAtPoint( Vector const& pt, ISpatialLeafEnumerator* pEnum, int context ) = 0;
  97. virtual bool EnumerateLeavesInBox( Vector const& mins, Vector const& maxs, ISpatialLeafEnumerator* pEnum, int context ) = 0;
  98. virtual bool EnumerateLeavesInSphere( Vector const& center, float radius, ISpatialLeafEnumerator* pEnum, int context ) = 0;
  99. virtual bool EnumerateLeavesAlongRay( Ray_t const& ray, ISpatialLeafEnumerator* pEnum, int context ) = 0;
  100. };
  101. //-----------------------------------------------------------------------------
  102. // Class factory
  103. //-----------------------------------------------------------------------------
  104. IBSPTreeData* CreateBSPTreeData();
  105. void DestroyBSPTreeData( IBSPTreeData* pTreeData );
  106. #endif // BSPTREEDATA