Cafu Engine
Tree.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_TREE_HPP_INCLUDED
8 #define CAFU_TREE_HPP_INCLUDED
9 
10 #include "Templates/Array.hpp"
11 #include "Math3D/Vector3.hpp"
12 #include "MaterialSystem/Mesh.hpp"
13 #include "Math3D/Matrix.hpp"
14 #include "Math3D/BoundingBox.hpp"
15 
16 #include <string>
17 
18 
19 struct PlantDescriptionT;
20 namespace MatSys { class RenderMaterialT; }
21 class MaterialT;
22 
23 
24 /// Describes a renderable tree.
25 /// The tree is created from a PlantDescriptionT using a random seed so it will be randomly created
26 /// based on this plant description.
27 class TreeT
28 {
29  private:
30 
31  /// The branch of a tree. A tree always consists of one or more branches, whereas the first branch is
32  /// the trees trunk.
33  struct BranchT
34  {
35  /// The segment of a branch. A branch consits of one or more segments.
36  struct SegmentT
37  {
38  MatrixT Matrix; ///< Transformationsmatrix fuer dieses Segment.
39  float RadiusAtRoot; ///< Radius des Querschnitts dieses Segments an seiner Wurzel (am "Anfang").
40  unsigned long FirstCoordIndex; ///< Index in the global tree vertices and texture coordinates array of the first vertice of this segment.
41  unsigned long NrOfCoords; ///< Number of vertices/texture coordinates of this segment.
42  };
43 
44  /// The leaf of a tree.
45  struct LeafT
46  {
47  Vector3fT Origin ; ///< Der Leaf-Ursprung/Mittelpunkt/Aufhaengepunkt (in World-Coords).
48  float Coords[8]; ///< Rel. X/Y-Koord.paare bzgl. Origin fuer li. oben, re. oben, re. unten und li. unten (in World-Coords).
49  float Color [3]; ///< Die Farbe (Farbe+Dimming+Varianz), mit der die Texture dieses Leafs moduliert werden soll.
50  MatSys::RenderMaterialT* RenderMat; ///< The render material used to render this leaf.
51  };
52 
53 
54  float Length; ///< Laenge dieses Branches in Metern.
55  float StartAngle; ///< Winkel der Abweichung von der Achse des Parents.
56  float Radius; ///< Radius an der Wurzel.
57 
58  ArrayT<SegmentT> Segments; ///< Die Segmente dieses Branches.
59  ArrayT<BranchT> ChildBranches; ///< Unsere Sub-Branches.
60  ArrayT<unsigned long> LeavesIndices; ///< Indizes unserer Blaetter ins 'AllLeaves'-Array (nuetzlich fuer LoD-Zwecke).
61  };
62 
63 
64  float Size; ///< Height of the tree in world space units (millimeters).
65  BranchT Trunk; ///< The trees trunk with all of its branches.
66  MatSys::RenderMaterialT* BarkRenderMat; ///< Render material used to render the trunk and all branches.
67  ArrayT<MatSys::RenderMaterialT*> LeafRenderMats; ///< The render materials used for the leaves.
68  ArrayT<float> GlobalAbsCoordTable; ///< Array of all vertices (coordinates) of the trunk and all branches.
69  ArrayT<float> GlobalTexCoordTable; ///< Array of the related texture coordinates.
70  ArrayT<BranchT::LeafT> AllLeaves; ///< List of all leaves.
71  BoundingBox3fT TreeBounds;
72 
73  BranchT ComputeBranch(float RelPosOnParent, char BranchLevel, const PlantDescriptionT* TD, const MatrixT& BranchMatrix, unsigned long& NumOfAllBranchTriangles);
74  void DrawBranch(const BranchT& Branch) const;
75  void GetLeavesAfterLoD(const BranchT& Branch, ArrayT<unsigned long>& LeavesAfterLoD, unsigned long& NrOfLeavesAfterLoD) const;
76  void DrawLeaf(const BranchT::LeafT& Leaf) const;
77 
78 
79  public:
80 
81  bool DrawLeaves; ///< When 'true', also the leaves are drawn. When 'false', the leaves are omitted from drawing.
82  bool UseRealBlendingForLeaves; ///< Determines if the leaves are drawn using real alpha blending, or only a simple alpha test.
83 
84 
85  /// Default-Constructor.
86  /// Not really useful, but we need it in order to be able to store 'TreeT' objects in an 'ArrayT'.
87  TreeT() : BarkRenderMat(NULL) {}
88 
89  /// Creates a tree.
90  /// @param TD The plant description used as a template for this tree.
91  /// @param RandomSeed The random seed used to create this tree. Using a different random seed for every tree object will result
92  /// in different trees even if the same plant description is used.
93  TreeT(const PlantDescriptionT* TD, unsigned long RandomSeed);
94 
95  /// Draws the tree.
96  /// Note that the tree is drawn at (0, 0, 0) in modelspace so you have
97  /// to translate the matrix to the right position before drawing the tree.
98  void Draw() const;
99 
100  /// Returns the trees bounding box.
101  const BoundingBox3fT& GetTreeBounds() const { return TreeBounds; }
102 };
103 
104 #endif
The segment of a branch. A branch consits of one or more segments.
Definition: Tree.hpp:36
This class represents a surface render material.
Definition: RenderMaterial.hpp:25
TreeT()
Default-Constructor.
Definition: Tree.hpp:87
MatrixT Matrix
Transformationsmatrix fuer dieses Segment.
Definition: Tree.hpp:38
The leaf of a tree.
Definition: Tree.hpp:45
Contains a plant description that has all the information needed to create a plant.
Definition: PlantDescription.hpp:23
const BoundingBox3fT & GetTreeBounds() const
Returns the trees bounding box.
Definition: Tree.hpp:101
Describes a renderable tree.
Definition: Tree.hpp:27
This class represents a surface material ("A datastructural representation of a scripts material def...
Definition: Material.hpp:22
float RadiusAtRoot
Radius des Querschnitts dieses Segments an seiner Wurzel (am "Anfang").
Definition: Tree.hpp:39
bool UseRealBlendingForLeaves
Determines if the leaves are drawn using real alpha blending, or only a simple alpha test...
Definition: Tree.hpp:82
MatSys::RenderMaterialT * RenderMat
The render material used to render this leaf.
Definition: Tree.hpp:50
Vector3fT Origin
Der Leaf-Ursprung/Mittelpunkt/Aufhaengepunkt (in World-Coords).
Definition: Tree.hpp:47
unsigned long NrOfCoords
Number of vertices/texture coordinates of this segment.
Definition: Tree.hpp:41
unsigned long FirstCoordIndex
Index in the global tree vertices and texture coordinates array of the first vertice of this segment...
Definition: Tree.hpp:40
float Coords[8]
Rel. X/Y-Koord.paare bzgl. Origin fuer li. oben, re. oben, re. unten und li. unten (in World-Coords)...
Definition: Tree.hpp:48
bool DrawLeaves
When 'true', also the leaves are drawn. When 'false', the leaves are omitted from drawing...
Definition: Tree.hpp:81
void Draw() const
Draws the tree.
Definition: Tree.cpp:477
Definition: Renderer.hpp:16
float Color[3]
Die Farbe (Farbe+Dimming+Varianz), mit der die Texture dieses Leafs moduliert werden soll...
Definition: Tree.hpp:49