Panda3D
heightfieldTesselator.I
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 heightfieldTesselator.I
10  * @author jyelon
11  * @date 2006-07-17
12  */
13 
14 /**
15  *
16  */
17 INLINE HeightfieldTesselator::
18 HeightfieldTesselator(const std::string &name) : Namable(name) {
19  _poly_count = 10000;
20  _visibility_radius = 32768;
21  _focal_x = 0;
22  _focal_y = 0;
23  _horizontal_scale = 1.0;
24  _vertical_scale = 255.0;
25  _max_triangles = 512;
26  _radii_calculated = false;
27 }
28 
29 /**
30  *
31  */
32 INLINE HeightfieldTesselator::
33 ~HeightfieldTesselator() {
34 }
35 
36 /**
37  * Returns a reference to the heightfield (a PNMImage) contained inside the
38  * HeightfieldTesselator. You can use the reference to alter the heightfield.
39  */
42  return _heightfield;
43 }
44 
45 /**
46  * Loads the specified greyscale image file into the heightfield.
47  */
48 INLINE bool HeightfieldTesselator::
49 set_heightfield(const Filename &filename, PNMFileType *ftype) {
50  _radii_calculated = false;
51  return _heightfield.read(filename, ftype);
52 }
53 
54 /**
55  * Sets the polygon-count target. The tesselator usually manages to come
56  * within about 20% of the target, plus or minus.
57  */
58 INLINE void HeightfieldTesselator::
60  _radii_calculated = false;
61  _poly_count = n;
62 }
63 
64 /**
65  * Sets the visibility radius. Polygons that are completely outside the
66  * radius (relative to the focal point) are cropped away. The cropping is
67  * imperfect (all approximations are conservative), so this should be used in
68  * conjunction with a far clipping plane, fog, or some other visibility
69  * limiting mechanism. The units are in pixels.
70  */
71 INLINE void HeightfieldTesselator::
72 set_visibility_radius(int radius) {
73  _radii_calculated = false;
74  if (radius < 1) radius = 1;
75  if (radius > 32768) radius = 32768;
76  _visibility_radius = radius;
77 }
78 
79 /**
80  * Sets the focal point. The tesselator generates high-resolution terrain
81  * around the focal point, and progressively lower and lower resolution
82  * terrain as you get farther away. The units are in pixels.
83  */
84 INLINE void HeightfieldTesselator::
85 set_focal_point(int x, int y) {
86  _focal_x = x;
87  _focal_y = y;
88 }
89 
90 /**
91  * Sets the horizontal scale. The default scale is 1.0, meaning that each
92  * pixel in the heightfield is 1x1 panda units wide.
93  */
94 INLINE void HeightfieldTesselator::
96  _horizontal_scale = h;
97 }
98 
99 /**
100  * Sets the vertical scale. The default scale is 255.0, meaning that each as
101  * the gray value ranges from (0-1), the elevation ranges from (0-255) feet.
102  */
103 INLINE void HeightfieldTesselator::
105  _vertical_scale = v;
106 }
107 
108 /**
109  * Sets the max triangles per geom.
110  */
111 INLINE void HeightfieldTesselator::
113  _max_triangles = n;
114 }
115 
116 /**
117  * Returns true if the given square should be subdivided.
118  */
119 INLINE bool HeightfieldTesselator::
120 subdivide(int scale, int x, int y) {
121  if (scale == 0) {
122  return false;
123  }
124  // int size = 1<<scale; int hsize = size >> 1; int xcenter = x+hsize; int
125  // ycenter = y+hsize;
126  int deltax = x - _focal_x;
127  int deltay = y - _focal_y;
128  if (deltax < 0) deltax = -deltax;
129  if (deltay < 0) deltay = -deltay;
130  int dist = (deltax > deltay) ? deltax : deltay;
131  if (dist < _radii[scale-1]) {
132  return true;
133  }
134  return false;
135 }
bool set_heightfield(const Filename &filename, PNMFileType *type=nullptr)
Loads the specified greyscale image file into the heightfield.
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
void set_focal_point(int x, int y)
Sets the focal point.
PNMImage & heightfield()
Returns a reference to the heightfield (a PNMImage) contained inside the HeightfieldTesselator.
void set_max_triangles(int n)
Sets the max triangles per geom.
This is the base class of a family of classes that represent particular image file types that PNMImag...
Definition: pnmFileType.h:32
bool read(const Filename &filename, PNMFileType *type=nullptr, bool report_unknown_type=true)
Reads the indicated image filename.
Definition: pnmImage.cxx:278
void set_horizontal_scale(double h)
Sets the horizontal scale.
A base class for all things which can have a name.
Definition: namable.h:26
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:39
void set_visibility_radius(int r)
Sets the visibility radius.
void set_vertical_scale(double v)
Sets the vertical scale.
void set_poly_count(int n)
Sets the polygon-count target.