Panda3D
|
00001 // Filename: heightfieldTesselator.I 00002 // Created by: jyelon (17jul06) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 00016 //////////////////////////////////////////////////////////////////// 00017 // Function: HeightfieldTesselator::Constructor 00018 // Access: Published 00019 // Description: 00020 //////////////////////////////////////////////////////////////////// 00021 INLINE HeightfieldTesselator:: 00022 HeightfieldTesselator(const string &name) : Namable(name) { 00023 _poly_count = 10000; 00024 _visibility_radius = 32768; 00025 _focal_x = 0; 00026 _focal_y = 0; 00027 _horizontal_scale = 1.0; 00028 _vertical_scale = 255.0; 00029 _max_triangles = 512; 00030 _radii_calculated = false; 00031 } 00032 00033 //////////////////////////////////////////////////////////////////// 00034 // Function: HeightfieldTesselator::Destructor 00035 // Access: Published 00036 // Description: 00037 //////////////////////////////////////////////////////////////////// 00038 INLINE HeightfieldTesselator:: 00039 ~HeightfieldTesselator() { 00040 } 00041 00042 //////////////////////////////////////////////////////////////////// 00043 // Function: HeightfieldTesselator::heightfield 00044 // Access: Published 00045 // Description: Returns a reference to the heightfield (a PNMImage) 00046 // contained inside the HeightfieldTesselator. You 00047 // can use the reference to alter the heightfield. 00048 //////////////////////////////////////////////////////////////////// 00049 INLINE PNMImage &HeightfieldTesselator:: 00050 heightfield() { 00051 return _heightfield; 00052 } 00053 00054 //////////////////////////////////////////////////////////////////// 00055 // Function: HeightfieldTesselator::set_heightfield 00056 // Access: Published 00057 // Description: Loads the specified greyscale image file into 00058 // the heightfield. 00059 //////////////////////////////////////////////////////////////////// 00060 INLINE bool HeightfieldTesselator:: 00061 set_heightfield(const Filename &filename, PNMFileType *ftype) { 00062 _radii_calculated = false; 00063 return _heightfield.read(filename, ftype); 00064 } 00065 00066 //////////////////////////////////////////////////////////////////// 00067 // Function: HeightfieldTesselator::set_poly_count 00068 // Access: Public 00069 // Description: Sets the polygon-count target. The tesselator 00070 // usually manages to come within about 20% of the 00071 // target, plus or minus. 00072 //////////////////////////////////////////////////////////////////// 00073 INLINE void HeightfieldTesselator:: 00074 set_poly_count(int n) { 00075 _radii_calculated = false; 00076 _poly_count = n; 00077 } 00078 00079 //////////////////////////////////////////////////////////////////// 00080 // Function: HeightfieldTesselator::set_visibility_radius 00081 // Access: Published 00082 // Description: Sets the visibility radius. Polygons that 00083 // are completely outside the radius (relative to 00084 // the focal point) are cropped away. The cropping 00085 // is imperfect (all approximations are conservative), 00086 // so this should be used in conjunction with a far 00087 // clipping plane, fog, or some other visibility 00088 // limiting mechanism. The units are in pixels. 00089 //////////////////////////////////////////////////////////////////// 00090 INLINE void HeightfieldTesselator:: 00091 set_visibility_radius(int radius) { 00092 _radii_calculated = false; 00093 if (radius < 1) radius = 1; 00094 if (radius > 32768) radius = 32768; 00095 _visibility_radius = radius; 00096 } 00097 00098 //////////////////////////////////////////////////////////////////// 00099 // Function: HeightfieldTesselator::set_focal_point 00100 // Access: Published 00101 // Description: Sets the focal point. The tesselator generates 00102 // high-resolution terrain around the focal point, and 00103 // progressively lower and lower resolution terrain 00104 // as you get farther away. The units are in pixels. 00105 //////////////////////////////////////////////////////////////////// 00106 INLINE void HeightfieldTesselator:: 00107 set_focal_point(int x, int y) { 00108 _focal_x = x; 00109 _focal_y = y; 00110 } 00111 00112 //////////////////////////////////////////////////////////////////// 00113 // Function: HeightfieldTesselator::set_horizontal_scale 00114 // Access: Published 00115 // Description: Sets the horizontal scale. The default scale is 1.0, 00116 // meaning that each pixel in the heightfield is 00117 // 1x1 panda units wide. 00118 //////////////////////////////////////////////////////////////////// 00119 INLINE void HeightfieldTesselator:: 00120 set_horizontal_scale(double h) { 00121 _horizontal_scale = h; 00122 } 00123 00124 //////////////////////////////////////////////////////////////////// 00125 // Function: HeightfieldTesselator::set_vertical_scale 00126 // Access: Published 00127 // Description: Sets the vertical scale. The default scale is 255.0, 00128 // meaning that each as the gray value ranges from (0-1), 00129 // the elevation ranges from (0-255) feet. 00130 //////////////////////////////////////////////////////////////////// 00131 INLINE void HeightfieldTesselator:: 00132 set_vertical_scale(double v) { 00133 _vertical_scale = v; 00134 } 00135 00136 //////////////////////////////////////////////////////////////////// 00137 // Function: HeightfieldTesselator::set_max_triangles 00138 // Access: Published 00139 // Description: Sets the max triangles per geom. 00140 //////////////////////////////////////////////////////////////////// 00141 INLINE void HeightfieldTesselator:: 00142 set_max_triangles(int n) { 00143 _max_triangles = n; 00144 } 00145 00146 //////////////////////////////////////////////////////////////////// 00147 // Function: HeightfieldTesselator::subdivide 00148 // Access: Private 00149 // Description: Returns true if the given square should be subdivided. 00150 //////////////////////////////////////////////////////////////////// 00151 INLINE bool HeightfieldTesselator:: 00152 subdivide(int scale, int x, int y) { 00153 if (scale == 0) { 00154 return false; 00155 } 00156 // int size = 1<<scale; 00157 // int hsize = size >> 1; 00158 // int xcenter = x+hsize; 00159 // int ycenter = y+hsize; 00160 int deltax = x - _focal_x; 00161 int deltay = y - _focal_y; 00162 if (deltax < 0) deltax = -deltax; 00163 if (deltay < 0) deltay = -deltay; 00164 int dist = (deltax > deltay) ? deltax : deltay; 00165 if (dist < _radii[scale-1]) { 00166 return true; 00167 } 00168 return false; 00169 } 00170 00171