nux-0.9.46

NuxGraphics/GLTextureResourceManager.h

Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #ifndef GLTEXTURERESOURCEMANAGER_H
00024 #define GLTEXTURERESOURCEMANAGER_H
00025 
00026 #include "GLResourceManager.h"
00027 #include "IOpenGLBaseTexture.h"
00028 
00029 #include <gdk-pixbuf/gdk-pixbuf.h>
00030 
00031 namespace nux
00032 {
00033 
00034   class NTextureData;
00035   class BaseTexture;
00036   class CachedBaseTexture;
00037 
00053   BaseTexture *CreateTexture2DFromFile (const char *filename, int max_size,
00054                                         bool premultiply);
00055 
00065   BaseTexture *CreateTexture2DFromPixbuf (GdkPixbuf *pixbuf, bool premultiply);
00066 
00067   // FIXME(loicm) Should be deprecated.
00068   BaseTexture *CreateTextureFromPixbuf (GdkPixbuf *pixbuf);
00069 
00070   BaseTexture *CreateTextureFromFile (const TCHAR *TextureFilename);
00071   BaseTexture *CreateTextureFromBitmapData (const NBitmapData *BitmapData);
00072 
00074   class BaseTexture: public ResourceData
00075   {
00076     NUX_DECLARE_OBJECT_TYPE (BaseTexture, ResourceData);
00077 
00078     BaseTexture(NUX_FILE_LINE_PROTO);
00079     virtual ~BaseTexture();
00080 
00091     virtual bool Update (const NBitmapData *BitmapData, bool UpdateAndCacheResource = true) = 0;
00092 
00103     virtual bool Update (const TCHAR *filename, bool UpdateAndCacheResource = true) = 0;
00104 
00105     virtual void GetData (void *Buffer, int MipIndex, int StrideY, int face = 0) = 0;
00106 
00110     virtual int GetWidth () const = 0;
00111 
00115     virtual int GetHeight () const = 0;
00116 
00121     virtual int GetDepth() const
00122     {
00123       return 1;
00124     }
00125 
00129     virtual bool IsPowerOfTwo () const = 0;
00130 
00134     virtual BitmapFormat GetFormat() const = 0;
00135 
00139     virtual int GetNumMipLevel () const = 0;
00140 
00144     virtual bool IsNull () const = 0;
00145 
00150     virtual BaseTexture* Clone () const = 0;
00151 
00157     ObjectPtr < IOpenGLBaseTexture > GetDeviceTexture ();
00158 
00164     ObjectPtr < CachedBaseTexture > GetCachedTexture ();
00165   };
00166 
00168 
00171   class Texture2D: public BaseTexture
00172   {
00173     NUX_DECLARE_OBJECT_TYPE (Texture2D, BaseTexture);
00174 
00175   public:
00176     Texture2D (NUX_FILE_LINE_PROTO);
00177     Texture2D (const Texture2D &texture, NUX_FILE_LINE_PROTO);
00178     Texture2D (const NTextureData &BaseTexture, NUX_FILE_LINE_PROTO);
00179     Texture2D &operator = (const Texture2D &texture);
00180     ~Texture2D ();
00181 
00190     virtual bool Update (const NBitmapData *BitmapData, bool UpdateAndCacheResource = true);
00191 
00200     virtual bool Update (const TCHAR *Filename, bool UpdateAndCacheResource = true);
00201 
00205     virtual bool IsNull () const
00206     {
00207       return _image.IsNull();
00208     }
00209 
00210     void GetData (void *Buffer, int MipIndex, int StrideY, int face = 0);
00211 
00215     int GetNumMipLevel () const
00216     {
00217       return _image.GetNumMipmap ();
00218     }
00222     int GetWidth () const
00223     {
00224       return _image.GetWidth ();
00225     }
00226 
00230     int GetHeight () const
00231     {
00232       return _image.GetHeight ();
00233     }
00234 
00238     BitmapFormat GetFormat () const
00239     {
00240       return _image.GetFormat ();
00241     }
00242 
00246     bool IsPowerOfTwo () const
00247     {
00248       return IsPowerOf2 (_image.GetWidth () ) && IsPowerOf2 (_image.GetHeight () );
00249     }
00250 
00255     virtual BaseTexture* Clone() const;
00256 
00257   private:
00258     NTextureData _image; 
00259   };
00260 
00261   class TextureRectangle: public BaseTexture
00262   {
00263     NUX_DECLARE_OBJECT_TYPE (TextureRectangle, BaseTexture);
00264 
00265   public:
00266     TextureRectangle (NUX_FILE_LINE_PROTO);
00267     TextureRectangle (const TextureRectangle &texture);
00268     TextureRectangle (const NTextureData& Image);
00269     TextureRectangle &operator = (const TextureRectangle &texture);
00270     ~TextureRectangle ();
00271 
00272     virtual bool Update (const NBitmapData *BitmapData, bool UpdateAndCacheResource = true);
00273     virtual bool Update (const TCHAR *filename, bool UpdateAndCacheResource = true);
00274 
00278     virtual bool IsNull () const
00279     {
00280       return _image.IsNull ();
00281     }
00282 
00283     void GetData (void *Buffer, int MipIndex, int StrideY, int face = 0);
00284 
00288     int GetNumMipLevel () const
00289     {
00290       return _image.GetNumMipmap ();
00291     }
00292 
00296     int GetWidth () const
00297     {
00298       return _image.GetWidth ();
00299     }
00300 
00304     int GetHeight() const
00305     {
00306       return _image.GetHeight();
00307     }
00308 
00312     BitmapFormat GetFormat() const
00313     {
00314       return _image.GetFormat();
00315     }
00316 
00320     bool IsPowerOfTwo() const
00321     {
00322       return IsPowerOf2 (_image.GetWidth() ) && IsPowerOf2 (_image.GetHeight() );
00323     }
00324 
00329     virtual BaseTexture* Clone() const;
00330 
00331   private:
00332     NTextureData _image;
00333   };
00334 
00335   class TextureCube: public BaseTexture
00336   {
00337     NUX_DECLARE_OBJECT_TYPE (TextureCube, BaseTexture);
00338 
00339   public:
00340     TextureCube (NUX_FILE_LINE_PROTO);
00341     //Texture2D(const NTextureData& Image);
00342     TextureCube (const TextureCube &texture);
00343     TextureCube &operator = (const TextureCube &texture);
00344     ~TextureCube();
00345 
00346     virtual bool Update (const NBitmapData *BitmapData, bool UpdateAndCacheResource = true);
00347     virtual bool Update (const TCHAR *filename, bool UpdateAndCacheResource = true);
00348 
00352     virtual bool IsNull () const
00353     {
00354       return _image.IsNull ();
00355     }
00356     void GetData (void *Buffer, int MipIndex, int StrideY, int face = 0);
00357 
00361     int GetNumMipLevel () const
00362     {
00363       return _image.GetNumMipmap ();
00364     }
00365 
00369     int GetWidth () const
00370     {
00371       return _image.GetWidth ();
00372     }
00373 
00377     int GetHeight () const
00378     {
00379       return _image.GetHeight ();
00380     }
00381 
00385     BitmapFormat GetFormat () const
00386     {
00387       return _image.GetFormat ();
00388     }
00389 
00393     bool IsPowerOfTwo () const
00394     {
00395       return IsPowerOf2 (_image.GetWidth() ) && IsPowerOf2 (_image.GetHeight() );
00396     }
00397 
00402     virtual BaseTexture* Clone () const;
00403 
00404   private:
00405     NCubemapData _image;
00406   };
00407 
00408   class TextureVolume: public BaseTexture
00409   {
00410     NUX_DECLARE_OBJECT_TYPE (TextureVolume, BaseTexture);
00411 
00412   public:
00413     TextureVolume (NUX_FILE_LINE_PROTO);
00414     //Texture2D(const NTextureData& Image);
00415     TextureVolume (const TextureVolume &texture);
00416     TextureVolume &operator = (const TextureVolume &texture);
00417     ~TextureVolume();
00418 
00419     virtual bool Update (const NBitmapData *BitmapData, bool UpdateAndCacheResource = true);
00420     virtual bool Update (const TCHAR *filename, bool UpdateAndCacheResource = true);
00421 
00425     virtual bool IsNull () const
00426     {
00427       return _image.IsNull ();
00428     }
00429     void GetData (void *Buffer, int MipIndex, int StrideY, int slice = 0);
00430     
00434     int GetNumMipLevel () const
00435     {
00436       return _image.GetNumMipmap ();
00437     }
00438 
00442     int GetWidth () const
00443     {
00444       return _image.GetWidth ();
00445     }
00446 
00450     int GetHeight () const
00451     {
00452       return _image.GetHeight ();
00453     }
00454 
00459     int GetDepth () const
00460     {
00461       return _image.GetDepth ();
00462     }
00463 
00467     BitmapFormat GetFormat () const
00468     {
00469       return _image.GetFormat ();
00470     }
00471 
00475     bool IsPowerOfTwo () const
00476     {
00477       return IsPowerOf2 (_image.GetWidth () ) && IsPowerOf2 (_image.GetHeight () );
00478     }
00479 
00484     virtual BaseTexture* Clone () const;
00485 
00486   private:
00487     NVolumeData _image;
00488   };
00489 
00490   class TextureFrameAnimation: public BaseTexture
00491   {
00492     NUX_DECLARE_OBJECT_TYPE (TextureFrameAnimation, BaseTexture);
00493 
00494   public:
00495     TextureFrameAnimation (NUX_FILE_LINE_PROTO);
00496     TextureFrameAnimation (const TextureFrameAnimation &texture);
00497     TextureFrameAnimation &operator = (const TextureFrameAnimation &texture);
00498     ~TextureFrameAnimation ();
00499 
00500     virtual bool Update (const NBitmapData *BitmapData, bool UpdateAndCacheResource = true);
00501     virtual bool Update (const TCHAR *filename, bool UpdateAndCacheResource = true);
00502 
00506     virtual bool IsNull () const
00507     {
00508       return _image.IsNull ();
00509     }
00510     void GetData (void *Buffer, int MipIndex, int StrideY, int slice = 0);
00511     int GetFrameTime (int Frame);
00512 
00516     int GetNumMipLevel () const
00517     {
00518       return _image.GetNumMipmap ();
00519     }
00520 
00524     int GetWidth () const
00525     {
00526       return _image.GetWidth ();
00527     }
00528 
00532     int GetHeight () const
00533     {
00534       return _image.GetHeight ();
00535     }
00536 
00540     int GetDepth () const
00541     {
00542       return _image.GetDepth ();
00543     }
00544 
00548     BitmapFormat GetFormat () const
00549     {
00550       return _image.GetFormat ();
00551     }
00552 
00556     bool IsPowerOfTwo () const
00557     {
00558       return IsPowerOf2 (_image.GetWidth () ) && IsPowerOf2 (_image.GetHeight () );
00559     }
00560 
00565     virtual BaseTexture* Clone () const;
00566 
00567   private:
00568     NAnimatedTextureData _image;
00569   };
00570 
00571   class CachedBaseTexture: public CachedResourceData
00572   {
00573     NUX_DECLARE_OBJECT_TYPE (CachedBaseTexture, CachedResourceData);
00574   public:
00575     ObjectPtr < IOpenGLBaseTexture > m_Texture;
00576 
00577     CachedBaseTexture (NResourceSet *ResourceManager);
00578     ~CachedBaseTexture();
00579 
00580     virtual void LoadMipLevel (BaseTexture *SourceTexture, int MipLevel) = 0;
00581 
00582     virtual bool UpdateResource (ResourceData *Resource);
00583 
00584     bool RecreateTexture (BaseTexture *Source);
00585 
00586     virtual void UpdateTexture (BaseTexture *Source) = 0;
00587 
00588     unsigned int SourceWidth;
00589     unsigned int SourceHeight;
00590     unsigned int SourceDepth;
00591     BitmapFormat SourceFormat;
00592   };
00593 
00594   class CachedTexture2D: public CachedBaseTexture
00595   {
00596     NUX_DECLARE_OBJECT_TYPE (CachedTexture2D, CachedBaseTexture);
00597   public:
00598     CachedTexture2D (NResourceSet *ResourceManager, Texture2D *SourceTexture);
00599     ~CachedTexture2D();
00600 
00601     virtual void UpdateTexture (BaseTexture *Source);
00602     virtual void LoadMipLevel (BaseTexture *SourceTexture, int MipLevel);
00603   };
00604 
00605   class CachedTextureRectangle: public CachedBaseTexture
00606   {
00607     NUX_DECLARE_OBJECT_TYPE (CachedTextureRectangle, CachedBaseTexture);
00608   public:
00609     CachedTextureRectangle (NResourceSet *ResourceManager, TextureRectangle *SourceTexture);
00610     ~CachedTextureRectangle ();
00611 
00612     virtual void UpdateTexture (BaseTexture *Source);
00613     virtual void LoadMipLevel (BaseTexture *SourceTexture, int MipLevel);
00614   };
00615 
00616   class CachedTextureCube: public CachedBaseTexture
00617   {
00618     NUX_DECLARE_OBJECT_TYPE (CachedTextureCube, CachedBaseTexture);
00619   public:
00620     CachedTextureCube (NResourceSet *ResourceManager, TextureCube *SourceTexture);
00621     ~CachedTextureCube ();
00622 
00623     virtual void UpdateTexture (BaseTexture *Source);
00624     virtual void LoadMipLevel (BaseTexture *SourceTexture, int MipLevel);
00625   };
00626 
00627   class CachedTextureVolume: public CachedBaseTexture
00628   {
00629     NUX_DECLARE_OBJECT_TYPE (CachedTextureVolume, CachedBaseTexture);
00630   public:
00631     CachedTextureVolume (NResourceSet *ResourceManager, TextureVolume *SourceTexture);
00632     ~CachedTextureVolume ();
00633 
00634     virtual void UpdateTexture (BaseTexture *Source);
00635     virtual void LoadMipLevel (BaseTexture *SourceTexture, int MipLevel);
00636   };
00637 
00638   class CachedTextureFrameAnimation: public CachedBaseTexture
00639   {
00640     NUX_DECLARE_OBJECT_TYPE (CachedTextureFrameAnimation, CachedBaseTexture);
00641   public:
00642     CachedTextureFrameAnimation (NResourceSet *ResourceManager, TextureFrameAnimation *SourceTexture);
00643     ~CachedTextureFrameAnimation ();
00644 
00645     virtual void UpdateTexture (BaseTexture *Source);
00646     virtual void LoadMipLevel (BaseTexture *SourceTexture, int MipLevel);
00647   };
00648 
00649 }
00650 
00651 #endif // GLTEXTURERESOURCEMANAGER_H