Cafu Engine
BezierPatchNode.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_BEZIERPATCHNODE_HPP_INCLUDED
8 #define CAFU_SCENEGRAPH_BEZIERPATCHNODE_HPP_INCLUDED
9 
10 #include "Node.hpp"
11 
12 
13 class MapBezierPatchT;
14 
15 namespace MatSys
16 {
17  class MeshT;
18  class RenderMaterialT;
19 }
20 
21 
22 namespace cf
23 {
24  namespace math
25  {
26  template<class T> class BezierPatchT;
27  }
28 
29 
30  namespace SceneGraph
31  {
33  {
34  friend class ::MapBezierPatchT; // Editor needs access to the private meshdata of the bezier patch.
35 
36 
37  public:
38 
39  /// The constructor for creating an "empty" bezier patch.
40  /// Needed e.g. by the named constructor CreateFromFile_cw() below.
41  BezierPatchNodeT(LightMapManT& LMM, float MaxError);
42 
43  /// Constructor for creating a BezierPatchNodeT from components.
44  /// This is currently only needed by the LoadWorld.cpp file of the CaBSP tool.
45  BezierPatchNodeT(LightMapManT& LMM, unsigned long SizeX_, unsigned long SizeY_, const ArrayT<float>& ControlPoints_, int SubdivsHorz_, int SubdivsVert_, MaterialT* Material_, float MaxError);
46 
47  /// Constructor for creating a BezierPatchNodeT from components.
48  /// This is currently only needed by the MapBezierPatch.cpp file for patch rendering in CaWE.
49  BezierPatchNodeT(LightMapManT& LMM, unsigned long SizeX_, unsigned long SizeY_, const ArrayT<Vector3fT>& ControlPointsXYZ_, const ArrayT<Vector3fT>& ControlPointsUV_, int SubdivsHorz_, int SubdivsVert_, MaterialT* Material_, float MaxError);
50 
51  /// Named constructor.
52  static BezierPatchNodeT* CreateFromFile_cw(std::istream& InFile, aux::PoolT& Pool, LightMapManT& LMM, SHLMapManT& SMM);
53 
54  /// The destructor.
56 
57  /// Sets color for all meshes of this bezier patch.
58  void UpdateMeshColor(const float red, const float green, const float blue, const float alpha);
59 
60  // The NodeT interface.
61  void WriteTo(std::ostream& OutFile, aux::PoolT& Pool) const;
63 
64  // void InitDrawing();
65  bool IsOpaque() const;
66  void DrawAmbientContrib(const Vector3dT& ViewerPos) const;
67  void DrawStencilShadowVolumes(const Vector3dT& LightPos, const float LightRadius) const;
68  void DrawLightSourceContrib(const Vector3dT& ViewerPos, const Vector3dT& LightPos) const;
69  void DrawTranslucentContrib(const Vector3dT& ViewerPos) const;
70 
71  void InitDefaultLightMaps(const float LightMapPatchSize);
72  void CreatePatchMeshes(ArrayT<PatchMeshT>& PatchMeshes, ArrayT< ArrayT< ArrayT<Vector3dT> > >& SampleCoords, const float LightMapPatchSize) const;
73  void BackToLightMap(const PatchMeshT& PatchMesh, const float LightMapPatchSize);
74 
75 
76  private:
77 
78  struct LightMapInfoT
79  {
80  unsigned short LightMapNr; ///< In welcher LightMap liegt unsere Teil-LightMap?
81  unsigned short PosS; ///< S-Position unserer Teil-LightMap innerhalb der LightMap.
82  unsigned short PosT; ///< T-Position unserer Teil-LightMap innerhalb der LightMap.
83  unsigned short SizeS; ///< S-Größe unserer Teil-LightMap.
84  unsigned short SizeT; ///< T-Größe unserer Teil-LightMap.
85  };
86 
87  void Init(); ///< Helper method for the constructors.
88  void Clean(); ///< Helper method for the destructor. Also called at the begin of Init().
89 
90  /// Automatically determines an appropriate number of subdivisions in horizontal direction,
91  /// that is, the number of columns per 3x3 sub-patch (all 3x3 sub-patches get tesselated equally).
92  /// Useful if SubdivsHorz is -1.
93  unsigned long GetAutoSubdivsHorz() const;
94 
95  /// Automatically determines an appropriate number of subdivisions in vertical direction,
96  /// that is, the number of rows per 3x3 sub-patch (all 3x3 sub-patches get tesselated equally).
97  /// Useful if SubdivsVert is -1.
98  unsigned long GetAutoSubdivsVert() const;
99 
100  /// Helper method, generates a mesh that is properly tesselated for lightmap purposes.
101  void GenerateLightMapMesh(cf::math::BezierPatchT<float>& LightMapMesh, const float LightMapPatchSize, const bool ComputeTS) const;
102 
103  BezierPatchNodeT(const BezierPatchNodeT&); ///< Use of the Copy Constructor is not allowed.
104  void operator = (const BezierPatchNodeT&); ///< Use of the Assignment Operator is not allowed.
105 
106  // This is the basic definition of a patch, read from and written to disk.
107  unsigned long SizeX; ///< Nr of columns.
108  unsigned long SizeY; ///< Nr of rows.
109  ArrayT<Vector3fT> ControlPointsXYZ; ///< The xyz spatial coordinates of the control points (SizeX*SizeY many).
110  ArrayT<Vector3fT> ControlPointsUV; ///< The uv texture coordinates of the control points (SizeX*SizeY many).
111 
112  int SubdivsHorz; ///< Number of subdivisions in horizontal direction, or auto-detection if -1.
113  int SubdivsVert; ///< Number of subdivisions in vertical direction, or auto-detection if -1.
114  /*const*/ float m_MaxError; ///< The maximal error distance for auto-subdividing bezier patches. Applies only if `SubdivsHorz` and `SubdivsVert` are -1.
115 
116  MaterialT* Material; ///< The material assigned to this patch.
117  LightMapInfoT LightMapInfo; ///< The lightmap information for this patch.
118 
119  // Additional information for an entire patch (not only a 3x3 sub-patch).
120  LightMapManT& LightMapMan;
121  BoundingBox3T<double> BB; ///< The BB of this bezier patch.
122  ArrayT<MatSys::MeshT*> Meshes;
123  MatSys::RenderMaterialT* RenderMaterial;
124  };
125  }
126 }
127 
128 #endif
BezierPatchNodeT(LightMapManT &LMM, float MaxError)
The constructor for creating an "empty" bezier patch.
Definition: BezierPatchNode.cpp:30
This class manages lightmaps, e.g. by "allocating" rectangular areas in larger bitmaps.
Definition: LightMapMan.hpp:25
static BezierPatchNodeT * CreateFromFile_cw(std::istream &InFile, aux::PoolT &Pool, LightMapManT &LMM, SHLMapManT &SMM)
Named constructor.
Definition: BezierPatchNode.cpp:87
This class represents a surface render material.
Definition: RenderMaterial.hpp:25
void InitDefaultLightMaps(const float LightMapPatchSize)
If this NodeT uses lightmaps, this methods initializes default (full-bright) lightmaps for it at the ...
Definition: BezierPatchNode.cpp:339
bool IsOpaque() const
TODO / FIXME: This method is a hot-fix for getting the render order with translucent Bezier Patches r...
Definition: BezierPatchNode.cpp:235
Definition: _aux.hpp:111
This class manages SHL maps, e.g. by "allocating" rectangular areas in larger coefficient maps...
Definition: SHLMapMan.hpp:25
const BoundingBox3T< double > & GetBoundingBox() const
Returns the bounding box of the contents of this scene node.
Definition: BezierPatchNode.cpp:229
Definition: PatchMesh.hpp:33
Definition: BezierPatchNode.hpp:32
This class represents a surface material ("A datastructural representation of a scripts material def...
Definition: Material.hpp:22
void BackToLightMap(const PatchMeshT &PatchMesh, const float LightMapPatchSize)
Takes the patches of the given patch mesh back into the lightmap of this node.
Definition: BezierPatchNode.cpp:586
~BezierPatchNodeT()
The destructor.
Definition: BezierPatchNode.cpp:168
This class represents a bezier patch.
Definition: MapBezierPatch.hpp:44
void CreatePatchMeshes(ArrayT< PatchMeshT > &PatchMeshes, ArrayT< ArrayT< ArrayT< Vector3dT > > > &SampleCoords, const float LightMapPatchSize) const
Creates the patch meshes for this NodeT for the purpose of radiosity computations (CaLight)...
Definition: BezierPatchNode.cpp:395
void UpdateMeshColor(const float red, const float green, const float blue, const float alpha)
Sets color for all meshes of this bezier patch.
Definition: BezierPatchNode.cpp:323
void DrawAmbientContrib(const Vector3dT &ViewerPos) const
Draws the contents of this scene node.
Definition: BezierPatchNode.cpp:241
Definition: Node.hpp:35
This class represents a mesh that approximates a Bezier patch.
Definition: BezierPatch.hpp:40