Umasoft
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
Roads.h
1 //
2 // Roads.h
3 //
4 // Copyright (c) 2001-2011 Virtual Terrain Project
5 // Free for all uses, see license.txt for details.
6 //
7 
8 #ifndef TERRAINROADSH
9 #define TERRAINROADSH
10 
18 
19 #include "vtdata/FilePath.h"
20 #include "vtdata/RoadMap.h"
21 #include "vtdata/HeightField.h"
22 #include "LodGrid.h"
23 
24 #define ROAD_CLUSTER 16
25 
30 class NodeGeom : public TNode
31 {
32 public:
33  NodeGeom();
34  virtual ~NodeGeom();
35 
36  class LinkGeom *GetLink(int n)
37  {
38  return (class LinkGeom *)m_connect[n].pLink;
39  }
40  void BuildIntersection();
41  void FindVerticesForLink(TLink *pR, bool bStart, FPoint3 &p0, FPoint3 &p1);
42  vtMesh *GenerateGeometry();
43  FPoint3 GetLinkVector(int i);
44  FPoint3 GetUnitLinkVector(int i);
45  const FPoint3 &GetAdjacentRoadpoint(int iLinkNumber);
46 
47  int m_iVerts;
48  FLine3 m_v; // vertices of the polygon
49  FPoint3 m_p3;
50 };
51 
52 //
53 // Holds information about a roadway, used internally by vtRoadMap3d during
54 // road construction.
55 //
57 {
58 public:
59  RoadBuildInfo(int iCoords);
60  FLine3 center;
61  FLine3 crossvector;
62  vtArray<float> fvLength;
63 
64  int verts;
65  int vert_index;
66 };
67 
68 enum normal_direction {
69  ND_UP,
70  ND_LEFT,
71  ND_RIGHT
72 };
73 
86 {
87 public:
88  int m_idx; // material index
89  FRECT m_rect; // region of the base texture to use
90 
91  void Adapt(const FPoint2 &in, FPoint2 &out)
92  {
93  out.x = m_rect.left + in.x * (m_rect.right - m_rect.left);
94  out.y = m_rect.bottom + in.y * (m_rect.top - m_rect.bottom);
95  }
96 };
97 
98 //
99 // virtual texture indices
100 //
101 enum RoadVTIndices
102 {
103  VTI_MARGIN,
104  VTI_SIDEWALK,
105  VTI_1LANE,
106  VTI_2LANE1WAY,
107  VTI_2LANE2WAY,
108  VTI_3LANE1WAY,
109  VTI_3LANE2WAY,
110  VTI_4LANE1WAY,
111  VTI_4LANE2WAY,
112  VTI_RAIL,
113  VTI_4WD,
114  VTI_TRAIL,
115  VTI_STONE,
116  VTI_TOTAL
117 };
118 
123 class LinkGeom : public TLink
124 {
125 public:
126  LinkGeom();
127  virtual ~LinkGeom();
128 
129  FPoint3 FindPointAlongRoad(float fDistance);
130  float Length(); // find 2D length in world units
131 
132  // link-construction methods
133  void SetupBuildInfo(RoadBuildInfo &bi);
134  void AddRoadStrip(vtMesh *pGeom, RoadBuildInfo &bi,
135  float offset_left, float offset_right,
136  float height_left, float height_right,
137  VirtualTexture &vt,
138  float u1, float u2, float uv_scale,
139  normal_direction nd);
140  void GenerateGeometry(class vtRoadMap3d *rmgeom);
141 
142  NodeGeom *GetNode(int n) { return (NodeGeom *)m_pNode[n]; }
143  LinkGeom *GetNext() { return (LinkGeom *)m_pNext; }
144 
145  int m_vti;
146  FLine3 m_centerline;
147 
148  /* Lanes lines, which define the centerline of each trafficable lane,
149  * are created by vtRoadMap3d for traffic simulation/visualization
150  * purposes.
151  */
152  std::vector<FLine3> m_Lanes;
153 };
154 
155 
159 class vtRoadMap3d : public vtRoadMap, public osg::Referenced
160 {
161 public:
162  vtRoadMap3d();
163  ~vtRoadMap3d();
164 
165  // overrides for virtual methods
166  LinkGeom *GetFirstLink() { return (LinkGeom *) m_pFirstLink; }
167  NodeGeom *GetFirstNode() { return (NodeGeom *) m_pFirstNode; }
168  TNode *NewNode() { return new NodeGeom; }
169  TLink *NewLink() { return new LinkGeom; }
170 
171  void DrapeOnTerrain(vtHeightField3d *pHeightField);
172  void BuildIntersections();
173  void AddMeshToGrid(vtMesh *pMesh, int iMatIdx);
174  vtGroup *GenerateGeometry(bool do_texture, bool progress_callback(int) = NULL);
175  void GenerateSigns(vtLodGrid *pLodGrid);
176  vtGroup *GetGroup() { return m_pGroup; }
177  void SetHeightOffGround(float fHeight) { s_fHeight = fHeight; }
178  void DetermineSurfaceAppearance();
179 
180  void SetLodDistance(float fDistance);
181  float GetLodDistance();
182 
183 public:
184  static float s_fHeight;
185 
186  // virtual textures
187  VirtualTexture m_vt[VTI_TOTAL];
188 
189  // material indices
190  int m_mi_roadside;
191  int m_mi_pavement;
192  int m_mi_roads;
193  int m_mi_4wd;
194  int m_mi_trail;
195  int m_mi_red;
196 
197 protected:
198  void _CreateMaterials(bool do_texture);
199  void _GatherExtents();
200 
201  vtGroup *m_pGroup;
202  vtMaterialArrayPtr m_pMats;
203 
204  vtLOD *m_pRoads[ROAD_CLUSTER][ROAD_CLUSTER];
205  FBox3 m_extents;
206  FPoint3 m_extent_range;
207  float m_fLodDistance; // in meters
208 };
209 typedef osg::ref_ptr<vtRoadMap3d> vtRoadMap3dPtr;
210 
211 // Useful typedefs
212 typedef NodeGeom *NodeGeomPtr;
213 typedef LinkGeom *LinkGeomPtr;
214  // transp
216 
217 #endif
218