19 INLINE GeoMipTerrain::
20 GeoMipTerrain(
const std::string &name) {
22 _root_flattened =
false;
31 _use_near_far =
false;
32 _has_color_map =
false;
34 _auto_flatten = AFM_off;
36 _focal_is_temporary =
true;
75 if (bf ==
true && _bruteforce ==
false) {
107 INLINE
void GeoMipTerrain::
108 set_focal_point(
double x,
double y) {
109 if (!_focal_is_temporary) {
113 _focal_point.set_pos(_root, x, y, 0);
114 _focal_is_temporary =
true;
116 INLINE
void GeoMipTerrain::
117 set_focal_point(
const LPoint2d &fp) {
118 set_focal_point(fp.get_x(), fp.get_y());
120 INLINE
void GeoMipTerrain::
121 set_focal_point(
const LPoint2f &fp) {
122 set_focal_point(
double(fp.get_x()),
double(fp.get_y()));
124 INLINE
void GeoMipTerrain::
125 set_focal_point(
const LPoint3d &fp) {
126 set_focal_point(fp.get_x(), fp.get_y());
128 INLINE
void GeoMipTerrain::
129 set_focal_point(
const LPoint3f &fp) {
130 set_focal_point(
double(fp.get_x()),
double(fp.get_y()));
132 INLINE
void GeoMipTerrain::
134 if (_focal_is_temporary) {
138 _focal_is_temporary =
false;
169 _min_level = minlevel;
205 if (is_power_of_two(newbs)) {
208 if (is_power_of_two(newbs - 1)) {
209 _block_size = newbs - 1;
211 if (is_power_of_two(newbs + 1)) {
212 _block_size = newbs + 1;
214 _block_size = (
unsigned short) pow(2.0,
215 floor(log((
double) newbs) / log(2.0) + 0.5));
219 _max_level = (
unsigned short) (log((
double) _block_size) / log(2.0));
243 grutil_cat.debug() <<
"Using deprecated method set_factor, use set_near and set_far instead!\n";
244 _use_near_far =
false;
253 _use_near_far =
true;
264 _use_near_far =
true;
274 _use_near_far =
true;
300 return _auto_flatten;
314 return _blocks[mx][my];
330 if (x > _xsize - 1) x = _xsize - 1;
331 if (y > _ysize - 1) y = _ysize - 1;
332 x = floor(x / _block_size);
333 y = floor(y / _block_size);
334 return LVecBase2(x, y);
339 INLINE
unsigned short GeoMipTerrain::
340 lod_decide(
unsigned short mx,
unsigned short my) {
343 cx = (cx * _block_size + _block_size / 2) * _root.get_sx();
344 cy = (cy * _block_size + _block_size / 2) * _root.get_sy();
347 d = sqrt(pow(_focal_point.get_x(_root) - cx, 2) +
348 pow(_focal_point.get_y(_root) - cy, 2));
351 }
else if (d > _far) {
354 return (
unsigned short)((d - _near) / (_far - _near) * _max_level * (1.0 - (_min_level / _max_level)) + _min_level);
358 d = sqrt(pow(_focal_point.get_x(_root) - cx, 2) +
359 pow(_focal_point.get_y(_root) - cy, 2)) / _factor;
363 return short(floor(d));
377 <<
"Heightfield image is specified to have sRGB color space!\n" 378 "Panda applies gamma correction, which will probably cause " 379 "it to produce incorrect results.\n";
383 if (is_power_of_two(image.
get_x_size() - 1) &&
385 _heightfield = image;
392 <<
"Specified image does not have a power-of-two-plus-one size!\n";
404 if (_color_map.
read(filename, ftype)) {
406 _has_color_map =
true;
416 _has_color_map =
true;
422 tex->
store(_color_map);
437 return _has_color_map;
445 if (_has_color_map) {
447 _has_color_map =
false;
460 if (stitching && !_stitching) {
463 _stitching = stitching;
480 INLINE
double GeoMipTerrain::
481 get_pixel_value(
int x,
int y) {
482 x = std::max(std::min(x,
int(_xsize-1)),0);
483 y = std::max(std::min(y,
int(_ysize-1)),0);
487 return double(_heightfield.
get_red(x, y))
488 +
double(_heightfield.
get_green(x, y)) / 256.0
489 +
double(_heightfield.
get_blue(x, y)) / 65536.0;
492 INLINE
double GeoMipTerrain::
493 get_pixel_value(
unsigned short mx,
unsigned short my,
int x,
int y) {
494 nassertr_always(mx < (_xsize - 1) / _block_size,
false);
495 nassertr_always(my < (_ysize - 1) / _block_size,
false);
496 return get_pixel_value(mx * _block_size + x, (_ysize - 1) -
497 (my * _block_size + y));
508 get_normal(
unsigned short mx,
unsigned short my,
int x,
int y) {
509 nassertr_always(mx < (_xsize - 1) / _block_size,
false);
510 nassertr_always(my < (_ysize - 1) / _block_size,
false);
511 return get_normal(mx * _block_size + x, (_ysize - 1) -
512 (my * _block_size + y));
518 INLINE
bool GeoMipTerrain::
519 is_power_of_two(
unsigned int i) {
520 return !((i - 1) & i);
526 INLINE
float GeoMipTerrain::
530 INLINE
double GeoMipTerrain::
538 INLINE
int GeoMipTerrain::
539 sfav(
int n,
int powlevel,
int mypowlevel) {
541 t /= pow(2.0, powlevel - mypowlevel);
542 t = double(
int(t > 0.0 ? t + 0.5 : t - 0.5));
543 t *= pow(2.0, powlevel - mypowlevel);
A basic node of the scene graph or data graph.
bool is_dirty()
Returns a bool indicating whether the terrain is marked 'dirty', that means the terrain has to be reg...
void set_factor(PN_stdfloat factor)
DEPRECATED method.
The name of this class derives from the fact that we originally implemented it as a layer on top of t...
ColorSpace get_color_space() const
Returns the color space in which the image is encoded.
bool set_color_map(const Filename &filename, PNMFileType *type=nullptr)
Loads the specified image as color map.
Represents a texture object, which is typically a single 2-d image but may also represent a 1-d or 3-...
float get_blue(int x, int y) const
Returns the blue component color at the indicated pixel.
This is the base class of a family of classes that represent particular image file types that PNMImag...
const NodePath get_block_node_path(unsigned short mx, unsigned short my)
Returns the NodePath of the specified block.
PNMImage & heightfield()
Returns a reference to the heightfield (a PNMImage) contained inside GeoMipTerrain.
bool read(const Filename &filename, PNMFileType *type=nullptr, bool report_unknown_type=true)
Reads the indicated image filename.
unsigned short get_min_level()
Gets the minimum level of detail at which blocks may be generated by generate() or update().
double get_far()
Returns the far LOD distance in the terrain coordinate space.
bool store(PNMImage &pnmimage) const
Saves the texture to the indicated PNMImage, but does not write it to disk.
void set_bruteforce(bool bf)
Sets a boolean specifying whether the terrain will be rendered bruteforce.
PNMImage & color_map()
Returns a reference to the color map (a PNMImage) contained inside GeoMipTerrain.
~GeoMipTerrain()
This will not remove the terrain node itself.
unsigned short get_block_size()
Gets the block size.
float get_red(int x, int y) const
Returns the red component color at the indicated pixel.
double get_near()
Returns the near LOD distance in the terrain coordinate space.
void set_near_far(double input_near, double input_far)
Sets the near and far LOD distances in one call.
bool has_color_map() const
Returns whether a color map has been set.
static NodePath fail()
Creates a NodePath with the ET_fail error type set.
The name of a file, such as a texture file or an Egg file.
float get_green(int x, int y) const
Returns the green component color at the indicated pixel.
bool get_bruteforce()
Returns a boolean whether the terrain is rendered bruteforce or not.
void clear_color_map()
Clears the color map.
unsigned short get_max_level()
Returns the highest level possible for this block size.
LVector3 get_normal(int x, int y)
Fetches the terrain normal at (x, y), where the input coordinate is specified in pixels.
bool get_border_stitching()
Returns the current stitching setting.
void set_border_stitching(bool stitching)
If this value is true, the LOD level at the borders of the terrain will be 0.
int get_flatten_mode()
Returns the automatic-flatten mode (e.g., off, flatten_light, flatten_medium, or flatten_strong)
void set_near(double input_near)
Sets the near LOD distance, at which the terrain will be rendered at highest quality.
void set_auto_flatten(int mode)
The terrain can be automatically flattened (using flatten_light, flatten_medium, or flatten_strong) a...
void clear()
Frees all memory allocated for the image, and clears all its parameters (size, color,...
void remove_node(Thread *current_thread=Thread::get_current_thread())
Disconnects the referenced node from the scene graph.
void set_block_size(unsigned short newbs)
Sets the block size.
void set_far(double input_far)
Sets the far LOD distance, at which the terrain will be rendered at lowest quality.
void copy_from(const PNMImage ©)
Makes this image become a copy of the other image.
bool set_heightfield(const Filename &filename, PNMFileType *type=nullptr)
Loads the specified heightmap image file into the heightfield.
NodePath get_root() const
Returns the root of the terrain.
float get_bright(int x, int y) const
Returns the linear brightness of the given xel, as a linearized float in the range 0....
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
LVecBase2 get_block_from_pos(double x, double y)
Gets the coordinates of the block at the specified position.
void set_min_level(unsigned short minlevel)
Sets the minimum level of detail at which blocks may be generated by generate() or update().
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
NodePath get_focal_point() const
Returns the focal point, as a NodePath.