Panda3D
 All Classes Functions Variables Enumerations
geoMipTerrain.h
1 // Filename: geoMipTerrain.h
2 // Created by: rdb (29Jun07)
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 GEOMIPTERRAIN_H
16 #define GEOMIPTERRAIN_H
17 
18 #include "pandabase.h"
19 
20 #include "luse.h"
21 #include "pandaNode.h"
22 #include "pointerTo.h"
23 
24 #include "pnmImage.h"
25 #include "nodePath.h"
26 
27 #include "texture.h"
28 
29 ////////////////////////////////////////////////////////////////////
30 // Class : GeoMipTerrain
31 // Description : GeoMipTerrain, meaning Panda3D GeoMipMapping, can convert
32 // a heightfield image into a 3D terrain, consisting
33 // of several GeomNodes. It uses the GeoMipMapping
34 // algorithm, or Geometrical MipMapping, based on
35 // the LOD (Level of Detail) algorithm. For more
36 // information about the GeoMipMapping algoritm, see
37 // this paper, written by Willem H. de Boer:
38 // http://flipcode.com/articles/article_geomipmaps.pdf
39 ////////////////////////////////////////////////////////////////////
40 class EXPCL_PANDA_GRUTIL GeoMipTerrain : public TypedObject {
41 PUBLISHED:
42  INLINE GeoMipTerrain(const string &name);
43  INLINE ~GeoMipTerrain();
44 
45  INLINE PNMImage &heightfield();
46  bool set_heightfield(const Filename &filename, PNMFileType *type = NULL);
47  INLINE bool set_heightfield(const PNMImage &image);
48  INLINE PNMImage &color_map();
49  INLINE bool set_color_map(const Filename &filename,
50  PNMFileType *type = NULL);
51  INLINE bool set_color_map(const PNMImage &image);
52  INLINE bool set_color_map(const Texture *image);
53  INLINE bool set_color_map(const string &path);
54  INLINE bool has_color_map() const;
55  INLINE void clear_color_map();
56  void calc_ambient_occlusion(PN_stdfloat radius = 32, PN_stdfloat contrast = 2.0f, PN_stdfloat brightness = 0.75f);
57  double get_elevation(double x, double y);
58  LVector3 get_normal(int x, int y);
59  INLINE LVector3 get_normal(unsigned short mx, unsigned short my,
60  int x,int y);
61  INLINE void set_bruteforce(bool bf);
62  INLINE bool get_bruteforce();
63 
64  // The flatten mode specifies whether the terrain nodes are flattened
65  // together after each terrain update.
66  enum AutoFlattenMode {
67  // FM_off: don't ever flatten the terrain.
68  AFM_off = 0,
69  // FM_light: the terrain is flattened using flatten_light.
70  AFM_light = 1,
71  // FM_medium: the terrain is flattened using flatten_medium.
72  AFM_medium = 2,
73  // FM_strong: the terrain is flattened using flatten_strong.
74  AFM_strong = 3,
75  };
76 
77  INLINE void set_auto_flatten(int mode);
78 
79  // The focal point is the point at which the terrain will have the
80  // highest quality (lowest level of detail). Parts farther away from
81  // the focal point will have a lower quality (higher level of detail).
82  // The focal point is not taken in respect if bruteforce is set true.
83  INLINE void set_focal_point(const LPoint2d &fp);
84  INLINE void set_focal_point(const LPoint2f &fp);
85  INLINE void set_focal_point(const LPoint3d &fp);
86  INLINE void set_focal_point(const LPoint3f &fp);
87  INLINE void set_focal_point(double x, double y);
88  INLINE void set_focal_point(NodePath fnp);
89  INLINE NodePath get_focal_point() const;
90  INLINE NodePath get_root() const;
91 
92  INLINE void set_block_size(unsigned short newbs);
93  INLINE unsigned short get_block_size();
94  INLINE unsigned short get_max_level();
95  INLINE void set_min_level(unsigned short minlevel);
96  INLINE unsigned short get_min_level();
97  INLINE bool is_dirty();
98  INLINE void set_factor(PN_stdfloat factor);
99  INLINE void set_near_far(double input_near, double input_far);
100  INLINE void set_near(double input_near);
101  INLINE void set_far(double input_far);
102  INLINE const NodePath get_block_node_path(unsigned short mx,
103  unsigned short my);
104  INLINE LVecBase2 get_block_from_pos(double x, double y);
105  INLINE void set_border_stitching(bool stitching);
106  INLINE bool get_border_stitching();
107  INLINE double get_far();
108  INLINE double get_near();
109  INLINE int get_flatten_mode();
110 
111  PNMImage make_slope_image();
112  void generate();
113  bool update();
114 
115 private:
116 
117  PT(GeomNode) generate_block(unsigned short mx, unsigned short my, unsigned short level);
118  bool update_block(unsigned short mx, unsigned short my,
119  signed short level = -1, bool forced = false);
120  void calc_levels();
121  void auto_flatten();
122  bool root_flattened();
123 
124  INLINE bool is_power_of_two(unsigned int i);
125  INLINE float f_part(float i);
126  INLINE double f_part(double i);
127  INLINE int sfav(int n, int powlevel, int mypowlevel);
128  INLINE double get_pixel_value(int x, int y);
129  INLINE double get_pixel_value(unsigned short mx, unsigned short my, int x, int y);
130  INLINE unsigned short lod_decide(unsigned short mx, unsigned short my);
131  unsigned short get_neighbor_level(unsigned short mx, unsigned short my, short dmx, short dmy);
132 
133  NodePath _root;
134  int _auto_flatten;
135  bool _root_flattened;
136  PNMImage _heightfield;
137  PNMImage _color_map;
138  bool _is_dirty;
139  bool _has_color_map;
140  unsigned int _xsize;
141  unsigned int _ysize;
142  PN_stdfloat _factor;
143  double _near;
144  double _far;
145  bool _use_near_far; // False to use the _factor, True to use the _near and _far values.
146  unsigned short _block_size;
147  unsigned short _max_level; // Highest level possible for this block size
148  bool _bruteforce;
149  NodePath _focal_point;
150  bool _focal_is_temporary;
151  unsigned short _min_level;
152  bool _stitching;
153  pvector<pvector<NodePath> > _blocks;
155  pvector<pvector<unsigned short> > _old_levels;
156 
157 public:
158  static TypeHandle get_class_type() {
159  return _type_handle;
160  }
161  static void init_type() {
163  register_type(_type_handle, "GeoMipTerrain",
164  TypedObject::get_class_type());
165  }
166  virtual TypeHandle get_type() const {
167  return get_class_type();
168  }
169  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
170 
171 private:
172  static TypeHandle _type_handle;
173 
174 };
175 
176 #include "geoMipTerrain.I"
177 
178 #endif /*GEOMIPTERRAIN_H*/
179 
static void init_type()
This function is declared non-inline to work around a compiler bug in g++ 2.96.
Definition: typedObject.cxx:52
The name of this class derives from the fact that we originally implemented it as a layer on top of t...
Definition: pnmImage.h:68
GeoMipTerrain, meaning Panda3D GeoMipMapping, can convert a heightfield image into a 3D terrain...
Definition: geoMipTerrain.h:40
Represents a texture object, which is typically a single 2-d image but may also represent a 1-d or 3-...
Definition: texture.h:75
This is the base class of a family of classes that represent particular image file types that PNMImag...
Definition: pnmFileType.h:35
This is an abstract class that all classes which use TypeHandle, and also provide virtual functions t...
Definition: typedObject.h:98
This is a three-component vector distance (as opposed to a three-component point, which represents a ...
Definition: lvector3.h:100
This is a two-component point in space.
Definition: lpoint2.h:411
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
Definition: lpoint3.h:99
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:39
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
This is the base class for all two-component vectors and points.
Definition: lvecBase2.h:105
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
Definition: lpoint3.h:531
This is a two-component point in space.
Definition: lpoint2.h:92
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:165
A node that holds Geom objects, renderable pieces of geometry.
Definition: geomNode.h:37