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