Back to index

nux  3.0.0
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 char* TextureFilename);
00071   BaseTexture* CreateTextureFromBitmapData(const NBitmapData* BitmapData);
00072 
00073   BaseTexture* LoadTextureFromFile(const std::string& filename);
00074 
00076   class BaseTexture: public ResourceData
00077   {
00078     NUX_DECLARE_OBJECT_TYPE(BaseTexture, ResourceData);
00079 
00080     BaseTexture(NUX_FILE_LINE_PROTO);
00081     virtual ~BaseTexture();
00082 
00093     virtual bool Update(const NBitmapData* BitmapData, bool UpdateAndCacheResource = true) = 0;
00094 
00105     virtual bool Update(const char* filename, bool UpdateAndCacheResource = true) = 0;
00106 
00107     virtual void GetData(void* Buffer, int MipIndex, int StrideY, int face = 0) = 0;
00108 
00112     virtual int GetWidth() const = 0;
00113 
00117     virtual int GetHeight() const = 0;
00118 
00123     virtual int GetDepth() const
00124     {
00125       return 1;
00126     }
00127 
00131     virtual bool IsPowerOfTwo() const = 0;
00132 
00136     virtual BitmapFormat GetFormat() const = 0;
00137 
00141     virtual int GetNumMipLevel() const = 0;
00142 
00146     virtual bool IsNull() const = 0;
00147 
00152     virtual BaseTexture* Clone() const = 0;
00153 
00160     ObjectPtr<IOpenGLBaseTexture> GetDeviceTexture();
00161 
00168     ObjectPtr<CachedBaseTexture> GetCachedTexture();
00169   };
00170 
00172 
00175   class Texture2D: public BaseTexture
00176   {
00177     NUX_DECLARE_OBJECT_TYPE(Texture2D, BaseTexture);
00178 
00179   public:
00180     Texture2D(NUX_FILE_LINE_PROTO);
00181     Texture2D(const Texture2D& texture, NUX_FILE_LINE_PROTO);
00182     Texture2D(const NTextureData& BaseTexture, NUX_FILE_LINE_PROTO);
00183     Texture2D& operator = (const Texture2D& texture);
00184     ~Texture2D();
00185 
00194     virtual bool Update(const NBitmapData* BitmapData, bool UpdateAndCacheResource = true);
00195 
00204     virtual bool Update(const char* Filename, bool UpdateAndCacheResource = true);
00205 
00209     virtual bool IsNull() const
00210     {
00211       return _image.IsNull();
00212     }
00213 
00214     void GetData(void* Buffer, int MipIndex, int StrideY, int face = 0);
00215 
00219     int GetNumMipLevel() const
00220     {
00221       return _image.GetNumMipmap();
00222     }
00226     int GetWidth() const
00227     {
00228       return _image.GetWidth();
00229     }
00230 
00234     int GetHeight() const
00235     {
00236       return _image.GetHeight();
00237     }
00238 
00242     BitmapFormat GetFormat() const
00243     {
00244       return _image.GetFormat();
00245     }
00246 
00250     bool IsPowerOfTwo() const
00251     {
00252       return IsPowerOf2(_image.GetWidth()) && IsPowerOf2(_image.GetHeight());
00253     }
00254 
00259     virtual BaseTexture* Clone() const;
00260 
00261   private:
00262     NTextureData _image; 
00263   };
00264 
00265   class TextureRectangle: public BaseTexture
00266   {
00267     NUX_DECLARE_OBJECT_TYPE(TextureRectangle, BaseTexture);
00268 
00269   public:
00270     TextureRectangle(NUX_FILE_LINE_PROTO);
00271     TextureRectangle(const TextureRectangle& texture);
00272     TextureRectangle(const NTextureData& Image);
00273     TextureRectangle& operator = (const TextureRectangle& texture);
00274     ~TextureRectangle();
00275 
00276     virtual bool Update(const NBitmapData* BitmapData, bool UpdateAndCacheResource = true);
00277     virtual bool Update(const char* filename, bool UpdateAndCacheResource = true);
00278 
00282     virtual bool IsNull() const
00283     {
00284       return _image.IsNull();
00285     }
00286 
00287     void GetData(void* Buffer, int MipIndex, int StrideY, int face = 0);
00288 
00292     int GetNumMipLevel() const
00293     {
00294       return _image.GetNumMipmap();
00295     }
00296 
00300     int GetWidth() const
00301     {
00302       return _image.GetWidth();
00303     }
00304 
00308     int GetHeight() const
00309     {
00310       return _image.GetHeight();
00311     }
00312 
00316     BitmapFormat GetFormat() const
00317     {
00318       return _image.GetFormat();
00319     }
00320 
00324     bool IsPowerOfTwo() const
00325     {
00326       return IsPowerOf2(_image.GetWidth()) && IsPowerOf2(_image.GetHeight());
00327     }
00328 
00333     virtual BaseTexture* Clone() const;
00334 
00335   private:
00336     NTextureData _image;
00337   };
00338 
00339   class TextureCube: public BaseTexture
00340   {
00341     NUX_DECLARE_OBJECT_TYPE(TextureCube, BaseTexture);
00342 
00343   public:
00344     TextureCube(NUX_FILE_LINE_PROTO);
00345     //Texture2D(const NTextureData& Image);
00346     TextureCube(const TextureCube& texture);
00347     TextureCube& operator = (const TextureCube& texture);
00348     ~TextureCube();
00349 
00350     virtual bool Update(const NBitmapData* BitmapData, bool UpdateAndCacheResource = true);
00351     virtual bool Update(const char* filename, bool UpdateAndCacheResource = true);
00352 
00356     virtual bool IsNull() const
00357     {
00358       return _image.IsNull();
00359     }
00360     void GetData(void* Buffer, int MipIndex, int StrideY, int face = 0);
00361 
00365     int GetNumMipLevel() const
00366     {
00367       return _image.GetNumMipmap();
00368     }
00369 
00373     int GetWidth() const
00374     {
00375       return _image.GetWidth();
00376     }
00377 
00381     int GetHeight() const
00382     {
00383       return _image.GetHeight();
00384     }
00385 
00389     BitmapFormat GetFormat() const
00390     {
00391       return _image.GetFormat();
00392     }
00393 
00397     bool IsPowerOfTwo() const
00398     {
00399       return IsPowerOf2(_image.GetWidth()) && IsPowerOf2(_image.GetHeight());
00400     }
00401 
00406     virtual BaseTexture* Clone() const;
00407 
00408   private:
00409     NCubemapData _image;
00410   };
00411 
00412   class TextureVolume: public BaseTexture
00413   {
00414     NUX_DECLARE_OBJECT_TYPE(TextureVolume, BaseTexture);
00415 
00416   public:
00417     TextureVolume(NUX_FILE_LINE_PROTO);
00418     //Texture2D(const NTextureData& Image);
00419     TextureVolume(const TextureVolume& texture);
00420     TextureVolume& operator = (const TextureVolume& texture);
00421     ~TextureVolume();
00422 
00423     virtual bool Update(const NBitmapData* BitmapData, bool UpdateAndCacheResource = true);
00424     virtual bool Update(const char* filename, bool UpdateAndCacheResource = true);
00425 
00429     virtual bool IsNull() const
00430     {
00431       return _image.IsNull();
00432     }
00433     void GetData(void* Buffer, int MipIndex, int StrideY, int slice = 0);
00434     
00438     int GetNumMipLevel() const
00439     {
00440       return _image.GetNumMipmap();
00441     }
00442 
00446     int GetWidth() const
00447     {
00448       return _image.GetWidth();
00449     }
00450 
00454     int GetHeight() const
00455     {
00456       return _image.GetHeight();
00457     }
00458 
00463     int GetDepth() const
00464     {
00465       return _image.GetDepth();
00466     }
00467 
00471     BitmapFormat GetFormat() const
00472     {
00473       return _image.GetFormat();
00474     }
00475 
00479     bool IsPowerOfTwo() const
00480     {
00481       return IsPowerOf2(_image.GetWidth()) && IsPowerOf2(_image.GetHeight());
00482     }
00483 
00488     virtual BaseTexture* Clone() const;
00489 
00490   private:
00491     NVolumeData _image;
00492   };
00493 
00494   class TextureFrameAnimation: public BaseTexture
00495   {
00496     NUX_DECLARE_OBJECT_TYPE(TextureFrameAnimation, BaseTexture);
00497 
00498   public:
00499     TextureFrameAnimation(NUX_FILE_LINE_PROTO);
00500     TextureFrameAnimation(const TextureFrameAnimation& texture);
00501     TextureFrameAnimation& operator = (const TextureFrameAnimation& texture);
00502     ~TextureFrameAnimation();
00503 
00504     virtual bool Update(const NBitmapData* BitmapData, bool UpdateAndCacheResource = true);
00505     virtual bool Update(const char* filename, bool UpdateAndCacheResource = true);
00506 
00510     virtual bool IsNull() const
00511     {
00512       return _image.IsNull();
00513     }
00514     void GetData(void* Buffer, int MipIndex, int StrideY, int slice = 0);
00515     int GetFrameTime(int Frame);
00516 
00520     int GetNumMipLevel() const
00521     {
00522       return _image.GetNumMipmap();
00523     }
00524 
00528     int GetWidth() const
00529     {
00530       return _image.GetWidth();
00531     }
00532 
00536     int GetHeight() const
00537     {
00538       return _image.GetHeight();
00539     }
00540 
00544     int GetDepth() const
00545     {
00546       return _image.GetDepth();
00547     }
00548 
00552     BitmapFormat GetFormat() const
00553     {
00554       return _image.GetFormat();
00555     }
00556 
00560     bool IsPowerOfTwo() const
00561     {
00562       return IsPowerOf2(_image.GetWidth()) && IsPowerOf2(_image.GetHeight());
00563     }
00564 
00569     virtual BaseTexture* Clone() const;
00570 
00571   private:
00572     NAnimatedTextureData _image;
00573   };
00574 
00575   class CachedBaseTexture: public CachedResourceData
00576   {
00577     NUX_DECLARE_OBJECT_TYPE(CachedBaseTexture, CachedResourceData);
00578   public:
00579     ObjectPtr < IOpenGLBaseTexture > m_Texture;
00580 
00581     CachedBaseTexture(NResourceSet* ResourceManager);
00582     ~CachedBaseTexture();
00583 
00584     virtual void LoadMipLevel(BaseTexture* SourceTexture, int MipLevel) = 0;
00585 
00586     virtual bool UpdateResource(ResourceData* Resource);
00587 
00588     bool RecreateTexture(BaseTexture* Source);
00589 
00590     virtual void UpdateTexture(BaseTexture* Source) = 0;
00591 
00592     unsigned int SourceWidth;
00593     unsigned int SourceHeight;
00594     unsigned int SourceDepth;
00595     BitmapFormat SourceFormat;
00596   };
00597 
00598   class CachedTexture2D: public CachedBaseTexture
00599   {
00600     NUX_DECLARE_OBJECT_TYPE(CachedTexture2D, CachedBaseTexture);
00601   public:
00602     CachedTexture2D(NResourceSet* ResourceManager, Texture2D* SourceTexture);
00603     ~CachedTexture2D();
00604 
00605     virtual void UpdateTexture(BaseTexture* Source);
00606     virtual void LoadMipLevel(BaseTexture* SourceTexture, int MipLevel);
00607   };
00608 
00609   class CachedTextureRectangle: public CachedBaseTexture
00610   {
00611     NUX_DECLARE_OBJECT_TYPE(CachedTextureRectangle, CachedBaseTexture);
00612   public:
00613     CachedTextureRectangle(NResourceSet* ResourceManager, TextureRectangle* SourceTexture);
00614     ~CachedTextureRectangle();
00615 
00616     virtual void UpdateTexture(BaseTexture* Source);
00617     virtual void LoadMipLevel(BaseTexture* SourceTexture, int MipLevel);
00618   };
00619 
00620   class CachedTextureCube: public CachedBaseTexture
00621   {
00622     NUX_DECLARE_OBJECT_TYPE(CachedTextureCube, CachedBaseTexture);
00623   public:
00624     CachedTextureCube(NResourceSet* ResourceManager, TextureCube* SourceTexture);
00625     ~CachedTextureCube();
00626 
00627     virtual void UpdateTexture(BaseTexture* Source);
00628     virtual void LoadMipLevel(BaseTexture* SourceTexture, int MipLevel);
00629   };
00630 
00631   class CachedTextureVolume: public CachedBaseTexture
00632   {
00633     NUX_DECLARE_OBJECT_TYPE(CachedTextureVolume, CachedBaseTexture);
00634   public:
00635     CachedTextureVolume(NResourceSet* ResourceManager, TextureVolume* SourceTexture);
00636     ~CachedTextureVolume();
00637 
00638     virtual void UpdateTexture(BaseTexture* Source);
00639     virtual void LoadMipLevel(BaseTexture* SourceTexture, int MipLevel);
00640   };
00641 
00642   class CachedTextureFrameAnimation: public CachedBaseTexture
00643   {
00644     NUX_DECLARE_OBJECT_TYPE(CachedTextureFrameAnimation, CachedBaseTexture);
00645   public:
00646     CachedTextureFrameAnimation(NResourceSet* ResourceManager, TextureFrameAnimation* SourceTexture);
00647     ~CachedTextureFrameAnimation();
00648 
00649     virtual void UpdateTexture(BaseTexture* Source);
00650     virtual void LoadMipLevel(BaseTexture* SourceTexture, int MipLevel);
00651   };
00652 
00653 }
00654 
00655 #endif // GLTEXTURERESOURCEMANAGER_H