Cafu Engine
BspTreeNode.hpp
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
6 
7 #ifndef CAFU_SCENEGRAPH_BSPTREENODE_HPP_INCLUDED
8 #define CAFU_SCENEGRAPH_BSPTREENODE_HPP_INCLUDED
9 
10 #include "Node.hpp"
11 #include "Math3D/Brush.hpp"
12 #include "Math3D/Polygon.hpp"
13 
14 #if defined(_WIN32) && _MSC_VER<1600
15 #include "pstdint.h" // Paul Hsieh's portable implementation of the stdint.h header.
16 #else
17 #include <stdint.h>
18 #endif
19 
20 
21 namespace cf
22 {
23  namespace SceneGraph
24  {
25  class FaceNodeT;
26 
27 
28  /// The class represents a BSP Tree node, implementing the Composite design pattern.
29  /// BSP tree nodes are special group nodes that implement acceleration structures.
30  class BspTreeNodeT : public GenericNodeT
31  {
32  public:
33 
34  struct NodeT
35  {
36  Plane3T<double> Plane;
37  unsigned long FrontChild;
38  unsigned long BackChild;
39  bool FrontIsLeaf;
40  bool BackIsLeaf;
41  };
42 
43 
44  struct LeafT
45  {
46  // These are just *indices* into the FaceChildren and OtherChildren arrays (that are "global" to the BSP tree) rather than *pointers* to
47  // cf::SceneGraph::FaceNodeT and cf::SceneGraph::GenericNodeT because having the indices allows for a better implementation of some pieces of code,
48  // e.g. when we want to keep track if one of the children has already been marked as being inside the PVS of a leaf.
49  ArrayT<unsigned long> FaceChildrenSet;
50  ArrayT<unsigned long> OtherChildrenSet; ///< The contents of this leaf (bezier patches, terrains, etc. ... everything but faces).
51  ArrayT< Polygon3T<double> > Portals;
53  bool IsInnerLeaf;
54  };
55 
56 
57  /// The constructor.
58  /// Needed e.g. by the named constructor CreateFromFile_cw() below.
59  BspTreeNodeT(float LightMapPatchSize, float SHLMapPatchSize);
60 
61  /// Named constructor.
62  static BspTreeNodeT* CreateFromFile_cw(std::istream& InFile, aux::PoolT& Pool,
63  LightMapManT& LMM, SHLMapManT& SMM, PlantDescrManT& PDM, const ArrayT<const TerrainT*>& ShTe, ModelManagerT& ModelMan);
64 
65  /// The destructor.
66  ~BspTreeNodeT();
67 
68  float GetLightMapPatchSize() const { return m_LightMapPatchSize; }
69  float GetSHLMapPatchSize() const { return m_SHLMapPatchSize; }
70 
71  void InitDrawing();
72 
73  /// Clips the line segment defined by P+U*Min and P+U*Max against the map and returns a value Hit such that
74  /// P+U*Hit yields the point where the line segment intersects the BSP tree, with Min<=Hit<=Max.
75  /// This is the classical "clip ray against BSP tree" implementation, used e.g. for PVS purposes:
76  /// it works solely with the BSP tree and does not take "other" leaf contents etc. into account.
77  /// This function MUST NOT BE CALLED ON AN EMPTY BSP TREE!
78  /// The NodeNr and NodeIsLeaf parameters are for internal recursion only.
79  /// They specify the recursion start and should always be omitted by the immediate caller.
80  double ClipLine(const VectorT& P, const VectorT& U, double Min, double Max, unsigned long NodeNr=0, bool NodeIsLeaf=false) const;
81 
82  // The NodeT interface.
83  void WriteTo(std::ostream& OutFile, aux::PoolT& Pool) const;
85 
86  // void InitDrawing();
87  bool IsOpaque() const;
88  void DrawAmbientContrib(const Vector3dT& ViewerPos) const;
89  void DrawStencilShadowVolumes(const Vector3dT& LightPos, const float LightRadius) const;
90  void DrawLightSourceContrib(const Vector3dT& ViewerPos, const Vector3dT& LightPos) const;
91  void DrawTranslucentContrib(const Vector3dT& ViewerPos) const;
92 
93 
94 #if 0
95  private:
96 
97  friend class Ca3DEWorld_EntityServiceInterfaceT;
98  friend class BspTreeBuilderT;
99 #else
100  // TODO!!! This is currently made so that CaLight and CaSHL compile...
101  public:
102 #endif
103 
104  /// In what leaf is 'Position' located? This function MUST NOT BE CALLED ON AN EMPTY MAP!
105  unsigned long WhatLeaf(const VectorT& Position) const;
106 
107  /// In what leaves is 'BoundingBox' located? This function MUST NOT BE CALLED ON AN EMPTY MAP!
108  /// The result will be APPENDED to the contents of the array 'ResultLeaves'.
109  /// The third parameter is for recursion only and should always be omitted by the immediate caller.
110  void WhatLeaves(ArrayT<unsigned long>& ResultLeaves, const BoundingBox3T<double>& BoundingBox, unsigned long NodeNr=0) const;
111 
112  /// This function traverses the BSP tree back-to-front, and stores the index numbers of the leaves it encounters in OrderedLeaves.
113  /// The result is a permutation of the numbers 0...Leaves.Size()-1, such that a back-to-front sorting is obtained for the Origin.
114  /// The cardinality of OrderedLeaves MUST MATCH THE CARDINALITY OF Leaves!
115  void GetLeavesOrderedBackToFront(ArrayT<unsigned long>& OrderedFaces, const VectorT& Origin) const;
116 
117  /// Returns 'true' if leaf 'QueryLeafNr' is in the PVS of leaf 'LeafNr', and false otherwise. Do not call on empty map.
118  bool IsInPVS(const unsigned long QueryLeafNr, unsigned long LeafNr) const;
119 
120  /// Returns 'true' if 'Position' is in the PVS of leaf 'LeafNr', and false otherwise. Do not call on empty map.
121  bool IsInPVS(const VectorT& Position, unsigned long LeafNr) const;
122 
123  /// Returns 'true' if 'BoundingBox' is in or touches the PVS of leaf 'LeafNr', and false otherwise. Do not call on empty map.
124  bool IsInPVS(const BoundingBox3T<double>& BoundingBox, unsigned long LeafNr) const;
125 
126  //void Init(); ///< Helper method for the constructors.
127  //void Clean(); ///< Helper method for the destructor. Also called at the begin of Init().
128 
129  BspTreeNodeT(const BspTreeNodeT&); ///< Use of the Copy Constructor is not allowed.
130  void operator = (const BspTreeNodeT&); ///< Use of the Assignment Operator is not allowed.
131 
132 
133  ArrayT<NodeT> Nodes;
134  ArrayT<LeafT> Leaves;
135  ArrayT<uint32_t> PVS;
137  ArrayT<cf::SceneGraph::FaceNodeT*> FaceChildren; ///< The list of all the face children of the BSP tree.
138  ArrayT<cf::SceneGraph::GenericNodeT*> OtherChildren; ///< The list of all the other children of the BSP tree.
139  ArrayT<Vector3dT> GlobalDrawVertices;
140 
141 
142  private:
143 
144  // Helper methods.
145  void GetLeavesOrderedBackToFrontHelper(unsigned long NodeNr) const;
146  void InitForNextLight() const;
147 
148  /*const*/ float m_LightMapPatchSize; ///< The edge length of the lightmap patches. The same value is used for all child nodes in this BSP tree (or else CaLight would have to be costly updated).
149  /*const*/ float m_SHLMapPatchSize; ///< The edge length of the SHL map patches. The same value is used for all child nodes in this BSP tree (or else CaLight would have to be costly updated).
150 
151  // Helper data for the Draw...() methods.
152  mutable ArrayT<bool> FaceChildIsInViewerPVS;
153  mutable ArrayT<bool> OtherChildIsInViewerPVS;
154  mutable ArrayT<cf::SceneGraph::GenericNodeT*> BackToFrontListOpaque;
155  mutable ArrayT<cf::SceneGraph::GenericNodeT*> BackToFrontListTranslucent;
156 
157  // Helper data for the Draw...() methods.
158  mutable ArrayT<bool> FaceChildIsInLightSourcePVS;
159  mutable ArrayT<bool> OtherChildIsInLightSourcePVS;
160  mutable ArrayT<cf::SceneGraph::GenericNodeT*> FrontFacingList;
161  mutable ArrayT<cf::SceneGraph::GenericNodeT*> BackFacingList;
162  mutable bool NextLightNeedsInit;
163  };
164  }
165 }
166 
167 #endif
This class manages lightmaps, e.g. by "allocating" rectangular areas in larger bitmaps.
Definition: LightMapMan.hpp:25
ArrayT< cf::SceneGraph::FaceNodeT * > FaceChildren
The list of all the face children of the BSP tree.
Definition: BspTreeNode.hpp:137
void operator=(const BspTreeNodeT &)
Use of the Assignment Operator is not allowed.
unsigned long WhatLeaf(const VectorT &Position) const
In what leaf is 'Position' located? This function MUST NOT BE CALLED ON AN EMPTY MAP! ...
Definition: BspTreeNode.cpp:499
~BspTreeNodeT()
The destructor.
Definition: BspTreeNode.cpp:138
Definition: BspTreeNode.hpp:34
BspTreeNodeT(float LightMapPatchSize, float SHLMapPatchSize)
The constructor.
Definition: BspTreeNode.cpp:22
Definition: _aux.hpp:111
This class manages SHL maps, e.g. by "allocating" rectangular areas in larger coefficient maps...
Definition: SHLMapMan.hpp:25
The plant description manager holds and manages all plant descriptions so they can be shared with mul...
Definition: PlantDescrMan.hpp:19
ArrayT< cf::SceneGraph::GenericNodeT * > OtherChildren
The list of all the other children of the BSP tree.
Definition: BspTreeNode.hpp:138
double ClipLine(const VectorT &P, const VectorT &U, double Min, double Max, unsigned long NodeNr=0, bool NodeIsLeaf=false) const
Clips the line segment defined by P+U*Min and P+U*Max against the map and returns a value Hit such th...
Definition: BspTreeNode.cpp:165
bool IsOpaque() const
TODO / FIXME: This method is a hot-fix for getting the render order with translucent Bezier Patches r...
Definition: BspTreeNode.cpp:277
Definition: BspTreeBuilder.hpp:27
void GetLeavesOrderedBackToFront(ArrayT< unsigned long > &OrderedFaces, const VectorT &Origin) const
This function traverses the BSP tree back-to-front, and stores the index numbers of the leaves it enc...
Definition: BspTreeNode.cpp:783
The class represents a BSP Tree node, implementing the Composite design pattern.
Definition: BspTreeNode.hpp:30
Definition: BspTreeNode.hpp:44
ArrayT< unsigned long > OtherChildrenSet
The contents of this leaf (bezier patches, terrains, etc. ... everything but faces).
Definition: BspTreeNode.hpp:50
This class is used for managing model instances.
Definition: ModelManager.hpp:31
static BspTreeNodeT * CreateFromFile_cw(std::istream &InFile, aux::PoolT &Pool, LightMapManT &LMM, SHLMapManT &SMM, PlantDescrManT &PDM, const ArrayT< const TerrainT * > &ShTe, ModelManagerT &ModelMan)
Named constructor.
Definition: BspTreeNode.cpp:30
void DrawAmbientContrib(const Vector3dT &ViewerPos) const
Draws the contents of this scene node.
Definition: BspTreeNode.cpp:283
bool IsInPVS(const unsigned long QueryLeafNr, unsigned long LeafNr) const
Returns 'true' if leaf 'QueryLeafNr' is in the PVS of leaf 'LeafNr', and false otherwise. Do not call on empty map.
Definition: BspTreeNode.cpp:793
void WhatLeaves(ArrayT< unsigned long > &ResultLeaves, const BoundingBox3T< double > &BoundingBox, unsigned long NodeNr=0) const
In what leaves is 'BoundingBox' located? This function MUST NOT BE CALLED ON AN EMPTY MAP! The result...
Definition: BspTreeNode.cpp:532
Definition: Node.hpp:35
const BoundingBox3T< double > & GetBoundingBox() const
Returns the bounding box of the contents of this scene node.
Definition: BspTreeNode.cpp:271