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