vrecko
virtual reality framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Terrain2DSegment.h
Go to the documentation of this file.
1 
8 #ifndef VRECKO_NATURE_TERRAIN_TERRAIN2DSEGMENT_H
9 #define VRECKO_NATURE_TERRAIN_TERRAIN2DSEGMENT_H
10 
11 #include <osg/Geode>
12 #include <osg/Geometry>
13 #include <osg/Texture2D>
14 #include <osg/Array>
15 
17 
18 namespace APNature
19 {
26  {
27  private:
28  osg::Vec2f _segmentPosition; // Position of the terrain segment - located in the NW corner of the segment.
29  int _width, _height; // Exact number of vertices in the given dimensions (width = west<->east; height = north<->south)
30 
31  osg::ref_ptr<osg::Vec3Array> _pointPositions; // Positions of the points of the segment
32  osg::ref_ptr<osg::Vec3Array> _pointNormals; // Normals of the points of the segment
33  osg::ref_ptr<osg::Vec4Array> _pointColors; // Colors of the points of the segment
34  osg::ref_ptr<osg::Geode> _terrainSegmentGeode; // OSG geode containing the whole terrain segment
35 
36  // Segment neighbours
37  // Pointers to the adjacent segments // -X -Y north +X -Y
38  Terrain2DSegment *_north; // | |
39  Terrain2DSegment *_south; // west east
40  Terrain2DSegment *_west; // | |
41  Terrain2DSegment *_east; // -X +Y south +X +Y
42 
43  public:
44 
49 
54 
59  inline int Width() const { return this->_width; }
60 
65  inline int Height() const { return this->_height; }
66 
67  // Neighbours
68  inline const Terrain2DSegment* GetNeighbourNorth() const { return this->_north; }
69  inline const Terrain2DSegment* GetNeighbourSouthh() const { return this->_south; }
70  inline const Terrain2DSegment* GetNeighbourWest() const { return this->_west; }
71  inline const Terrain2DSegment* GetNeighbourEast() const { return this->_east; }
72 
73  inline bool IsNeighbourNorth() const { return this->_north != NULL; }
74  inline bool IsNeighbourSouth() const { return this->_south != NULL; }
75  inline bool IsNeighbourWest() const { return this->_west != NULL; }
76  inline bool IsNeighbourEast() const { return this->_east != NULL; }
77 
82  inline osg::ref_ptr<osg::Geode> GetTerrainSegmentGeode() const { return this->_terrainSegmentGeode; }
83 
90  inline const osg::Vec3f& GetPoint(int x, int y) const { return (*_pointPositions)[PointIndex(x, y)]; }
91 
98  inline void SetPoint(int x, int y, const osg::Vec3f& point) { (*_pointPositions)[PointIndex(x, y)] = point; }
99 
105  void SetSegmentPosition(const osg::Vec2f& position);
106 
110  void InitializeGeode();
111 
116  void Flatten(float height);
117 
118  private:
119 
120  // Declare Terrain2D friendly class so that it can handle memory allocation of the segment
121  // and some other private matters of the class
122  friend class Terrain2D;
123 
130  inline unsigned int PointIndex(int x, int y) const { return y * this->_width + x;}
131 
132  inline void SetNeighbourNorth(Terrain2DSegment *north) { this->_north = north; }
133  inline void SetNeighbourSouth(Terrain2DSegment *south) { this->_south = south; }
134  inline void SetNeighbourWest(Terrain2DSegment *west) { this->_west = west; }
135  inline void SetNeighbourEast(Terrain2DSegment *east) { this->_east = east; }
136 
140  void AllocatePoints();
141 
145  void AllocatePointPositions();
146 
150  void AllocatePointNormals();
151 
156  void ComputePointPositions();
157 
161  void ComputePointNormals();
162  };
163 }
164 
165 #endif