Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/gobj/texture.h

Go to the documentation of this file.
00001 // Filename: texture.h
00002 // Created by:  mike (09Jan97)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 #ifndef TEXTURE_H
00019 #define TEXTURE_H
00020 
00021 #include "pandabase.h"
00022 
00023 #include "imageBuffer.h"
00024 #include "pixelBuffer.h"
00025 #include "graphicsStateGuardianBase.h"
00026 #include "pmap.h"
00027 
00028 class PNMImage;
00029 class TextureContext;
00030 class FactoryParams;
00031 
00032 ////////////////////////////////////////////////////////////////////
00033 //       Class : Texture
00034 // Description : 2D texture class
00035 ////////////////////////////////////////////////////////////////////
00036 class EXPCL_PANDA Texture : public ImageBuffer {
00037 PUBLISHED:
00038   enum FilterType {
00039     // Mag Filter and Min Filter
00040 
00041     // Point sample the pixel
00042     FT_nearest,
00043 
00044     // Bilinear filtering of four neighboring pixels
00045     FT_linear,
00046 
00047     // Min Filter Only
00048 
00049     // Point sample the pixel from the nearest mipmap level
00050     FT_nearest_mipmap_nearest,
00051 
00052     // Bilinear filter the pixel from the nearest mipmap level
00053     FT_linear_mipmap_nearest,
00054 
00055     // Point sample the pixel from two mipmap levels, and linearly blend
00056     FT_nearest_mipmap_linear,
00057 
00058     // A.k.a. trilinear filtering: Bilinear filter the pixel from
00059     // two mipmap levels, and linearly blend the results.
00060     FT_linear_mipmap_linear,
00061 
00062     // Returned by string_filter_type() for an invalid match.
00063     FT_invalid
00064   };
00065 
00066   enum WrapMode {
00067     WM_clamp,  // coords that would be outside [0-1] are clamped to 0 or 1
00068     WM_repeat,
00069     WM_mirror,
00070     WM_mirror_once,   // mirror once, then clamp
00071     WM_border_color,  // coords outside [0-1] use explict border color
00072     // Returned by string_wrap_mode() for an invalid match.
00073     WM_invalid
00074   };
00075 
00076 PUBLISHED:
00077   Texture();
00078   Texture(int xsize, int ysize, int components, int component_width, PixelBuffer::Type type, PixelBuffer::Format format,
00079           bool bAllocateRAM);
00080   ~Texture();
00081 
00082   bool read(const Filename &fullpath, int primary_file_num_channels = 0);
00083   bool read(const Filename &fullpath, const Filename &alpha_fullpath,
00084             int primary_file_num_channels = 0, int alpha_file_channel = 0);
00085   bool write(const Filename &fullpath = "") const;
00086 
00087   void set_wrapu(WrapMode wrap);
00088   void set_wrapv(WrapMode wrap);
00089   void set_minfilter(FilterType filter);
00090   void set_magfilter(FilterType filter);
00091   void set_anisotropic_degree(int anisotropic_degree);
00092   void set_border_color(const Colorf &color);
00093 
00094   INLINE WrapMode get_wrapu() const;
00095   INLINE WrapMode get_wrapv() const;
00096   INLINE FilterType get_minfilter() const;
00097   INLINE FilterType get_magfilter() const;
00098   INLINE int get_anisotropic_degree() const;
00099   INLINE bool uses_mipmaps() const;
00100 
00101 public:
00102   bool load(const PNMImage &pnmimage);
00103   bool store(PNMImage &pnmimage) const;
00104 
00105   static bool is_mipmap(FilterType type);
00106 
00107   TextureContext *prepare(GraphicsStateGuardianBase *gsg);
00108   void unprepare();
00109   void unprepare(GraphicsStateGuardianBase *gsg);
00110   void clear_gsg(GraphicsStateGuardianBase *gsg);
00111 
00112   INLINE bool has_ram_image() const;
00113   PixelBuffer *get_ram_image();
00114   INLINE void set_keep_ram_image(bool keep_ram_image);
00115   INLINE bool get_keep_ram_image() const;
00116 
00117   INLINE void apply(GraphicsStateGuardianBase *gsg);
00118 
00119   virtual void copy(GraphicsStateGuardianBase *gsg, const DisplayRegion *dr);
00120   virtual void copy(GraphicsStateGuardianBase *gsg, const DisplayRegion *dr,
00121                     const RenderBuffer &rb);
00122 
00123   // These bits are used as parameters to Texture::mark_dirty() and
00124   // also TextureContext::mark_dirty() (and related functions in
00125   // TextureContext).
00126   enum DirtyFlags {
00127     DF_image      = 0x001,  // The image pixels have changed.
00128     DF_wrap       = 0x002,  // The wrap properties have changed.
00129     DF_filter     = 0x004,  // The minfilter or magfilter have changed.
00130     DF_mipmap     = 0x008,  // The use of mipmaps or not has changed.
00131   };
00132 
00133   void mark_dirty(int flags_to_set);
00134 
00135   static WrapMode string_wrap_mode(const string &string);
00136   static FilterType string_filter_type(const string &string);
00137 
00138 private:
00139   WrapMode _wrapu;
00140   WrapMode _wrapv;
00141   FilterType _minfilter;
00142   FilterType _magfilter;
00143   int _anisotropic_degree;
00144   bool _keep_ram_image;
00145   Colorf _border_color;
00146 
00147   // A Texture keeps a list (actually, a map) of all the GSG's that it
00148   // has been prepared into.  Each GSG conversely keeps a list (a set)
00149   // of all the Textures that have been prepared there.  When either
00150   // destructs, it removes itself from the other's list.
00151   typedef pmap<GraphicsStateGuardianBase *, TextureContext *> Contexts;
00152   Contexts _contexts;
00153 
00154   // This value represents the intersection of all the dirty flags of
00155   // the various TextureContexts that might be associated with this
00156   // texture.
00157   int _all_dirty_flags;
00158 
00159 public:
00160   // These are public to allow direct manipulation of the underlying
00161   // pixel buffer when needed.  Know what you are doing!
00162   PT(PixelBuffer) _pbuffer;
00163 
00164 /*
00165   // If you request a region from the framebuffer that is not a power of 2,
00166   // we need to grab a larger region that is a power of 2 that contains the
00167   // requested region and set the pixel buffer size accordingly.  We store
00168   // the size you requested in the members below.
00169   bool _has_requested_size;
00170   int _requested_w;
00171   int _requested_h;
00172 */
00173 
00174 
00175   // Datagram stuff
00176 public:
00177   static void register_with_read_factory(void);
00178   virtual void write_datagram(BamWriter* manager, Datagram &me);
00179 
00180   static TypedWritable *make_Texture(const FactoryParams &params);
00181 
00182 protected:
00183   void fillin(DatagramIterator& scan, BamReader* manager);
00184 
00185 public:
00186   static TypeHandle get_class_type() {
00187     return _type_handle;
00188   }
00189   static void init_type() {
00190     ImageBuffer::init_type();
00191     register_type(_type_handle, "Texture",
00192                   ImageBuffer::get_class_type());
00193   }
00194   virtual TypeHandle get_type() const {
00195     return get_class_type();
00196   }
00197   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00198 
00199 private:
00200 
00201   static TypeHandle _type_handle;
00202 
00203   friend class TextureContext;
00204 };
00205 
00206 #include "texture.I"
00207 
00208 #endif
00209 

Generated on Fri May 2 00:39:44 2003 for Panda by doxygen1.3