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