Panda3D
 All Classes Functions Variables Enumerations
geoMipTerrain.h
00001 // Filename: geoMipTerrain.h
00002 // Created by:  rdb (29Jun07)
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 #ifndef GEOMIPTERRAIN_H
00016 #define GEOMIPTERRAIN_H
00017 
00018 #include "pandabase.h"
00019 
00020 #include "luse.h"
00021 #include "pandaNode.h"
00022 #include "pointerTo.h"
00023 
00024 #include "pnmImage.h"
00025 #include "nodePath.h"
00026 
00027 #include "texture.h"
00028 
00029 ////////////////////////////////////////////////////////////////////
00030 //       Class : GeoMipTerrain
00031 // Description : GeoMipTerrain, meaning Panda3D GeoMipMapping, can convert
00032 //               a heightfield image into a 3D terrain, consisting
00033 //               of several GeomNodes. It uses the GeoMipMapping
00034 //               algorithm, or Geometrical MipMapping, based on
00035 //               the LOD (Level of Detail) algorithm. For more
00036 //               information about the GeoMipMapping algoritm, see
00037 //               this paper, written by Willem H. de Boer:
00038 //               http://flipcode.com/articles/article_geomipmaps.pdf
00039 ////////////////////////////////////////////////////////////////////
00040 class EXPCL_PANDA_GRUTIL GeoMipTerrain : public TypedObject {
00041 PUBLISHED:
00042   INLINE GeoMipTerrain(const string &name);
00043   INLINE ~GeoMipTerrain();
00044 
00045   INLINE PNMImage &heightfield();
00046   bool set_heightfield(const Filename &filename, PNMFileType *type = NULL);
00047   INLINE bool set_heightfield(const PNMImage &image);
00048   INLINE bool set_heightfield(const string &path);
00049   INLINE PNMImage &color_map();
00050   INLINE bool set_color_map(const Filename &filename,
00051                                   PNMFileType *type = NULL);
00052   INLINE bool set_color_map(const PNMImage &image);
00053   INLINE bool set_color_map(const Texture *image);
00054   INLINE bool set_color_map(const string &path);
00055   INLINE bool has_color_map() const;
00056   INLINE void clear_color_map();
00057   void calc_ambient_occlusion(PN_stdfloat radius = 32, PN_stdfloat contrast = 2.0f, PN_stdfloat brightness = 0.75f);
00058   double get_elevation(double x, double y);
00059   LVector3 get_normal(int x, int y);
00060   INLINE LVector3 get_normal(unsigned short mx, unsigned short my,
00061                                                           int x,int y);
00062   INLINE void set_bruteforce(bool bf);
00063   INLINE bool get_bruteforce();
00064 
00065   // The flatten mode specifies whether the terrain nodes are flattened
00066   // together after each terrain update.
00067   enum AutoFlattenMode {
00068     // FM_off: don't ever flatten the terrain.
00069     AFM_off     = 0,
00070     // FM_light: the terrain is flattened using flatten_light.
00071     AFM_light   = 1,
00072     // FM_medium: the terrain is flattened using flatten_medium.
00073     AFM_medium  = 2,
00074     // FM_strong: the terrain is flattened using flatten_strong.
00075     AFM_strong  = 3,
00076   };
00077 
00078   INLINE void set_auto_flatten(int mode);
00079 
00080   // The focal point is the point at which the terrain will have the
00081   // highest quality (lowest level of detail). Parts farther away from
00082   // the focal point will have a lower quality (higher level of detail).
00083   // The focal point is not taken in respect if bruteforce is set true.
00084   INLINE void set_focal_point(const LPoint2d &fp);
00085   INLINE void set_focal_point(const LPoint2f &fp);
00086   INLINE void set_focal_point(const LPoint3d &fp);
00087   INLINE void set_focal_point(const LPoint3f &fp);
00088   INLINE void set_focal_point(double x, double y);
00089   INLINE void set_focal_point(NodePath fnp);
00090   INLINE NodePath get_focal_point() const;
00091   INLINE NodePath get_root() const;
00092 
00093   INLINE void set_block_size(unsigned short newbs);
00094   INLINE unsigned short get_block_size();
00095   INLINE unsigned short get_max_level();
00096   INLINE void set_min_level(unsigned short minlevel);
00097   INLINE unsigned short get_min_level();
00098   INLINE bool is_dirty();
00099   INLINE void set_factor(PN_stdfloat factor);
00100   INLINE void set_near_far(double input_near, double input_far);
00101   INLINE void set_near(double input_near);
00102   INLINE void set_far(double input_far);
00103   INLINE const NodePath get_block_node_path(unsigned short mx,
00104                                             unsigned short my);
00105   INLINE LVecBase2 get_block_from_pos(double x, double y);
00106   INLINE void set_border_stitching(bool stitching);
00107   INLINE bool get_border_stitching();
00108   INLINE double get_far();
00109   INLINE double get_near();
00110   INLINE int get_flatten_mode();
00111 
00112   PNMImage make_slope_image();
00113   void generate();
00114   bool update();
00115 
00116 private:
00117 
00118   PT(GeomNode) generate_block(unsigned short mx, unsigned short my, unsigned short level);
00119   bool update_block(unsigned short mx, unsigned short my,
00120                     signed short level = -1, bool forced = false);
00121   void calc_levels();
00122   void auto_flatten();
00123   bool root_flattened();
00124 
00125   INLINE bool is_power_of_two(unsigned int i);
00126   INLINE float f_part(float i);
00127   INLINE double f_part(double i);
00128   INLINE int sfav(int n, int powlevel, int mypowlevel);
00129   INLINE double get_pixel_value(int x, int y);
00130   INLINE double get_pixel_value(unsigned short mx, unsigned short my, int x, int y);
00131   INLINE unsigned short lod_decide(unsigned short mx, unsigned short my);
00132   unsigned short get_neighbor_level(unsigned short mx, unsigned short my, short dmx, short dmy);
00133 
00134   NodePath _root;
00135   int _auto_flatten;
00136   bool _root_flattened;
00137   PNMImage _heightfield;
00138   PNMImage _color_map;
00139   bool _is_dirty;
00140   bool _has_color_map;
00141   unsigned int _xsize;
00142   unsigned int _ysize;
00143   PN_stdfloat _factor;
00144   double _near;
00145   double _far;
00146   bool _use_near_far; // False to use the _factor, True to use the _near and _far values.
00147   unsigned short _block_size;
00148   unsigned short _max_level; // Highest level possible for this block size
00149   bool _bruteforce;
00150   NodePath _focal_point;
00151   bool _focal_is_temporary;
00152   unsigned short _min_level;
00153   bool _stitching;
00154   pvector<pvector<NodePath> > _blocks;
00155   pvector<pvector<unsigned short> > _levels;
00156   pvector<pvector<unsigned short> > _old_levels;
00157 
00158 public:
00159   static TypeHandle get_class_type() {
00160     return _type_handle;
00161   }
00162   static void init_type() {
00163     TypedObject::init_type();
00164     register_type(_type_handle, "GeoMipTerrain",
00165                   TypedObject::get_class_type());
00166   }
00167   virtual TypeHandle get_type() const {
00168     return get_class_type();
00169   }
00170   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00171 
00172 private:
00173   static TypeHandle _type_handle;
00174 
00175 };
00176 
00177 #include "geoMipTerrain.I"
00178 
00179 #endif /*GEOMIPTERRAIN_H*/
00180 
 All Classes Functions Variables Enumerations