Panda3D
stBasicTerrain.h
1 // Filename: stBasicTerrain.h
2 // Created by: drose (12Oct10)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef STBASICTERRAIN_H
16 #define STBASICTERRAIN_H
17 
18 #include "pandabase.h"
19 #include "stTerrain.h"
20 #include "luse.h"
21 #include "pvector.h"
22 
23 ////////////////////////////////////////////////////////////////////
24 // Class : STBasicTerrain
25 // Description : A specific implementation of STTerrain that supports
26 // basic heightmaps loaded from an image file, as
27 // described in a terrain.txt file similar to those
28 // provided with the SpeedTree example application.
29 ////////////////////////////////////////////////////////////////////
30 class EXPCL_PANDASPEEDTREE STBasicTerrain : public STTerrain {
31 PUBLISHED:
33  STBasicTerrain(const STBasicTerrain &copy);
34  virtual ~STBasicTerrain();
35 
36  void clear();
37 
38  bool setup_terrain(const Filename &terrain_filename);
39  bool setup_terrain(istream &in, const Filename &pathname);
40 
41  INLINE void set_height_map(const Filename &height_map);
42  INLINE const Filename &get_height_map() const;
43 
44  virtual void load_data();
45 
46  INLINE PN_stdfloat get_size() const;
47  virtual PN_stdfloat get_height(PN_stdfloat x, PN_stdfloat y) const;
48  virtual PN_stdfloat get_smooth_height(PN_stdfloat x, PN_stdfloat y, PN_stdfloat radius) const;
49  virtual PN_stdfloat get_slope(PN_stdfloat x, PN_stdfloat y) const;
50 
51  virtual void fill_vertices(GeomVertexData *data,
52  PN_stdfloat start_x, PN_stdfloat start_y,
53  PN_stdfloat size_xy, int num_xy) const;
54 
55  virtual void output(ostream &out) const;
56  virtual void write(ostream &out, int indent_level = 0) const;
57 
58 protected:
59  bool read_height_map();
60  void compute_slope(PN_stdfloat smoothing);
61 
62  INLINE PN_stdfloat interpolate(PN_stdfloat a, PN_stdfloat b, PN_stdfloat t);
63 
64 private:
65  static void read_quoted_filename(Filename &result, istream &in,
66  const Filename &dirname);
67 
68 protected:
69  template<class ValueType>
70  class InterpolationData {
71  public:
72  InterpolationData();
73  void reset(int width, int height);
74 
75  ValueType get_nearest_neighbor(PN_stdfloat u, PN_stdfloat v) const;
76  ValueType calc_bilinear_interpolation(PN_stdfloat u, PN_stdfloat v) const;
77  ValueType calc_smooth(PN_stdfloat u, PN_stdfloat v, PN_stdfloat radius) const;
78  bool is_present() const;
79 
80  int _width;
81  int _height;
82  pvector<ValueType> _data;
83  };
84 
85 protected:
86  Filename _height_map;
87  PN_stdfloat _size;
88  PN_stdfloat _height_scale;
89 
90  InterpolationData<PN_stdfloat> _height_data;
91  //InterpolationData<LVector3> _normal_data;
92  InterpolationData<PN_stdfloat> _slope_data;
93  //InterpolationData<unsigned char> _ao_data;
94 
95 public:
96  static TypeHandle get_class_type() {
97  return _type_handle;
98  }
99  static void init_type() {
100  STTerrain::init_type();
101  register_type(_type_handle, "STBasicTerrain",
102  STTerrain::get_class_type());
103  }
104  virtual TypeHandle get_type() const {
105  return get_class_type();
106  }
107  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
108 
109 private:
110  static TypeHandle _type_handle;
111 };
112 
113 #include "stBasicTerrain.I"
114 
115 #endif
A specific implementation of STTerrain that supports basic heightmaps loaded from an image file...
This is the abstract base class that defines the interface needed to describe a terrain for rendering...
Definition: stTerrain.h:39
virtual PN_stdfloat get_slope(PN_stdfloat x, PN_stdfloat y) const
After load_data() has been called, this should return the directionless slope at point (x...
Definition: stTerrain.cxx:128
virtual void fill_vertices(GeomVertexData *data, PN_stdfloat start_x, PN_stdfloat start_y, PN_stdfloat size_xy, int num_xy) const
After load_data() has been called, this will be called occasionally to populate the vertices for a te...
Definition: stTerrain.cxx:173
virtual PN_stdfloat get_smooth_height(PN_stdfloat x, PN_stdfloat y, PN_stdfloat radius) const
After load_data() has been called, this should return the approximate average height value over a cir...
Definition: stTerrain.cxx:114
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
This defines the actual numeric vertex data stored in a Geom, in the structure defined by a particula...
virtual void clear()
Resets the terrain to its initial, unloaded state.
Definition: stTerrain.cxx:65
virtual PN_stdfloat get_height(PN_stdfloat x, PN_stdfloat y) const =0
After load_data() has been called, this should return the computed height value at point (x...
Definition: stTerrain.cxx:101
virtual void load_data()=0
This will be called at some point after initialization.
Definition: stTerrain.cxx:89
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85