26 _height_map = height_map;
60 INLINE PN_stdfloat STBasicTerrain::
61 interpolate(PN_stdfloat a, PN_stdfloat b, PN_stdfloat t) {
62 return (a + (b - a) * t);
70 template<
class ValueType>
71 STBasicTerrain::InterpolationData<ValueType>::
72 InterpolationData() : _width(0), _height(0)
82 template<
class ValueType>
83 void STBasicTerrain::InterpolationData<ValueType>::
84 reset(
int width,
int height) {
88 _data.insert(_data.begin(), width * height, ValueType());
96 template<
class ValueType>
97 ValueType STBasicTerrain::InterpolationData<ValueType>::
98 get_nearest_neighbor(PN_stdfloat u, PN_stdfloat v)
const {
99 int u = int(u * _width + 0.5f);
100 int v = int(v * _height + 0.5f);
101 int index = u + v * _width;
102 nassertr(index >= 0 && index < (
int)_data.size(), 0);
112 template<
class ValueType>
113 ValueType STBasicTerrain::InterpolationData<ValueType>::
114 calc_bilinear_interpolation(PN_stdfloat u, PN_stdfloat v)
const {
118 u *= (PN_stdfloat)_width;
119 v *= (PN_stdfloat)_height;
121 const int lower_x = int(u);
122 const int lower_y = int(v);
123 const int higher_x = (lower_x + 1) % _width;
124 const int higher_y = (lower_y + 1) % _height;
126 const PN_stdfloat ratio_x = u - PN_stdfloat(lower_x);
127 const PN_stdfloat ratio_y = v - PN_stdfloat(lower_y);
128 const PN_stdfloat inv_ratio_x = 1.0f - ratio_x;
129 const PN_stdfloat inv_ratio_y = 1.0f - ratio_y;
131 nassertr(lower_x + lower_y * _width >= 0 && higher_x + higher_y * _width < (
int)_data.size(), 0);
133 const ValueType &t1 = _data[lower_x + lower_y * _width];
134 const ValueType &t2 = _data[higher_x + lower_y * _width];
135 const ValueType &t3 = _data[lower_x + higher_y * _width];
136 const ValueType &t4 = _data[higher_x + higher_y * _width];
138 return (t1 * inv_ratio_x + t2 * ratio_x) * inv_ratio_y +
139 (t3 * inv_ratio_x + t4 * ratio_x) * ratio_y;
148 template<
class ValueType>
149 ValueType STBasicTerrain::InterpolationData<ValueType>::
150 calc_smooth(PN_stdfloat u, PN_stdfloat v, PN_stdfloat radius)
const {
151 ValueType retval = 0;
153 if (radius <= 0.0f) {
154 retval = calc_bilinear_interpolation(u, v);
157 const PN_stdfloat test_points[9][2] = {
158 { 0.0f * radius, 0.0f * radius },
159 { 0.8f * radius, 0.0f * radius },
160 { -0.8f * radius, 0.0f * radius },
161 { 0.0f * radius, 0.8f * radius },
162 { 0.0f * radius, -0.8f * radius },
163 { 0.25f * radius, 0.25f * radius },
164 { 0.25f * radius, -0.25f * radius },
165 { -0.25f * radius, 0.25f * radius },
166 { -0.25f * radius, -0.25f * radius }
169 PN_stdfloat total_weight = 0.0f;
170 for (
int i = 0; i < 9; ++i) {
171 const PN_stdfloat *test_point = test_points[i];
172 PN_stdfloat weight = (1.0f - sqrt((test_point[0] * test_point[0]) + (test_point[1] * test_point[1])));
173 total_weight += weight;
174 retval += weight * calc_bilinear_interpolation(u + test_point[0], v + test_point[1]);
177 retval /= total_weight;
189 template<
class ValueType>
190 bool STBasicTerrain::InterpolationData<ValueType>::
192 return !_data.empty();
const Filename & get_height_map() const
Returns the image filename that defines the height map of the terrain.
void set_height_map(const Filename &height_map)
Specifies the image filename that will define the height map of the terrain.
The name of a file, such as a texture file or an Egg file.
PN_stdfloat get_size() const
Returns the length, in scene graph units, of one edge of the heightmap as it is manifested by the ter...