Umasoft
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
Structure3d.h
1 //
2 // Structure3d.h
3 //
4 // Implements the vtStructure3d class which extends vtStructure with the
5 // ability to create 3D geometry.
6 //
7 // Copyright (c) 2001-2011 Virtual Terrain Project
8 // Free for all uses, see license.txt for details.
9 //
10 
11 #ifndef STRUCTURE3DH
12 #define STRUCTURE3DH
13 
16 
17 #include "vtdata/StructArray.h"
18 
19 #define COLOR_SPREAD 216 // 216 color variations
20 
21 class vtBuilding3d;
22 class vtFence3d;
23 class vtTerrain;
24 class vtTransform;
25 
26 extern const vtString BMAT_NAME_HIGHLIGHT;
27 
35 {
36 public:
38 
39  vtMaterial *MakeMaterial(vtMaterialDescriptor *desc, const RGBf &color);
40  int FindMatIndex(const vtString & Material, const RGBf &inputColor = RGBf(),
41  int iType = -1);
42  vtMaterialDescriptor *FindMaterialDescriptor(const vtString& MaterialName,
43  const RGBf &color = RGBf(), int iType = -1) const;
44  void InitializeMaterials();
45  void CreateMaterials();
46  vtMaterialArray *GetMatArray() const { return m_pMaterials; };
47 
48 protected:
49  // There is a single array of materials, shared by all buildings.
50  // This is done to save memory. For a list of 16000+ buildings, this can
51  // save about 200MB of RAM.
52  vtMaterialArrayPtr m_pMaterials;
53 
54  bool m_bMaterialsCreated;
55 
56  void CreateSelfColoredMaterial(vtMaterialDescriptor *descriptor);
57  void CreateColorableTextureMaterial(vtMaterialDescriptor *descriptor);
58 
59  RGBf m_Colors[COLOR_SPREAD];
60 
61  // indices of internal materials
62  int m_hightlight1, m_hightlight2, m_hightlight3, m_wire;
63 };
64 
65 
71 {
72 public:
73  vtStructure3d() { m_pContainer = NULL; m_bIsVIAContributor = false; m_bIsVIATarget = false; }
74 
75  vtTransform *GetContainer() { return m_pContainer; }
76  virtual osg::Node *GetContained() = 0;
77 
79  virtual bool CreateNode(vtTerrain *pTerr) = 0;
80  virtual bool IsCreated() = 0;
81 
83  virtual vtGeode *GetGeom() { return NULL; }
84  virtual void DeleteNode() = 0;
85 
87  virtual void ShowBounds(bool bShow) {}
88 
89  void SetCastShadow(bool b);
90  bool GetCastShadow();
91 
92  // Get the material descriptors
93  static vtMaterialDescriptorArray3d& GetMaterialDescriptors()
94  {
95  return s_MaterialDescriptors;
96  }
97  static vtMaterialDescriptor *FindMaterialDescriptor(const vtString &name,
98  const RGBf &color = RGBf(), int type = -1)
99  {
100  return s_MaterialDescriptors.FindMaterialDescriptor(name, color, type);
101  }
102  static void InitializeMaterialArrays();
103 
104  // all fences share the same set of materials
105  static void CreateSharedMaterials();
106 
107  // Visual Impact
108  const bool GetVIAContributor() const { return m_bIsVIAContributor; }
109  const bool GetVIATarget() const { return m_bIsVIATarget; }
110  void SetVIAContributor(const bool bVIAContributor) { m_bIsVIAContributor = bVIAContributor; }
111  void SetVIATarget(const bool bVIATarget) { m_bIsVIATarget = bVIATarget; }
112 
113 protected:
114  // material
115  int FindMatIndex(const vtString &Material, const RGBf &inputColor = RGBf(), int iType = -1)
116  {
117  return s_MaterialDescriptors.FindMatIndex(Material, inputColor, iType);
118  }
119  vtMaterialArray *GetSharedMaterialArray() const
120  {
121  return s_MaterialDescriptors.GetMatArray();
122  }
123  vtTransformPtr m_pContainer; // The transform which is used to position the object
124 
125 protected:
126  float ColorDiff(const RGBi &c1, const RGBi &c2);
127 
128  static vtMaterialDescriptorArray3d s_MaterialDescriptors;
129  static bool s_bMaterialsLoaded;
130 
131  // Visual Impact
132  bool m_bIsVIAContributor;
133  bool m_bIsVIATarget;
134 
135 };
136 
137 
143 {
144 public:
146 
147  // implement vtStructInstance methods
148  virtual double DistanceToPoint(const DPoint2 &p, float fMaxRadius) const;
149 
150  // implement vtStructure3d methods
152  virtual bool CreateNode(vtTerrain *pTerr);
153  virtual bool IsCreated();
154  virtual void DeleteNode();
155  virtual void ShowBounds(bool bShow);
156  virtual osg::Node *GetContained() { return m_pModel.get(); }
157 
159  void UpdateTransform(vtHeightField3d *pHeightField);
160 
162  void Reload();
163 
164 protected:
165  vtGeode *m_pHighlight; // The wireframe highlight
166  osg::ref_ptr<osg::Node> m_pModel; // the contained model
167 };
168 
169 
175 {
176 public:
178 
179  virtual vtBuilding *NewBuilding();
180  virtual vtFence *NewFence();
181  virtual vtStructInstance *NewInstance();
182 
183  vtStructure3d *GetStructure3d(int i);
184  vtBuilding3d *GetBuilding(int i) { return (vtBuilding3d *) GetAt(i)->GetBuilding(); }
185  vtFence3d *GetFence(int i) { return (vtFence3d *) GetAt(i)->GetFence(); }
186  vtStructInstance3d *GetInstance(int i) { return (vtStructInstance3d *) GetAt(i)->GetInstance(); }
187 
189  void SetTerrain(vtTerrain *pTerr) { m_pTerrain = pTerr; }
190 
193  bool ConstructStructure(int index);
194  void OffsetSelectedStructures(const DPoint2 &offset);
195  void OffsetSelectedStructuresVertical(float offset);
196 
198  void VisualDeselectAll();
199 
201  void SetEnabled(bool bTrue);
202 
204  void SetShadows(bool bTrue);
205 
206  // Be informed of edit hightlighting
207  virtual void SetEditedEdge(vtBuilding *bld, int lev, int edge);
208 
209  // Be informed when a structure is deleted
210  virtual void DestroyStructure(int i);
211 
212 protected:
213  vtTerrain *m_pTerrain;
214 };
215  // Group struct
217 
218 #endif // STRUCTURE3DH
219