Panda3D

geoMipTerrain.h

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