nux-1.16.0
IOpenGLTexture2D.cpp
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 #include "GLDeviceObjects.h"
00024 #include "IOpenGLTexture2D.h"
00025 
00026 namespace nux
00027 {
00028 
00029   NUX_IMPLEMENT_OBJECT_TYPE (IOpenGLTexture2D);
00030   IOpenGLTexture2D::IOpenGLTexture2D (unsigned int Width
00031                                       , unsigned int Height
00032                                       , unsigned int Levels
00033                                       , BitmapFormat PixelFormat, bool Dummy, NUX_FILE_LINE_DECL)
00034     :   IOpenGLBaseTexture (RTTEXTURE, Width, Height, 1, Levels, PixelFormat, NUX_FILE_LINE_PARAM)
00035   {
00036     external_id_ = Dummy;
00037     if (external_id_ == false)
00038     {
00039       glGenTextures (1, &_OpenGLID);
00040       CHECKGL ( glBindTexture (GL_TEXTURE_2D, _OpenGLID) );
00041     }
00042 
00043     //_SurfaceArray.Empty(Levels);
00044     for (unsigned int l = 0; l < Levels; l++)
00045     {
00046       IOpenGLSurface *surface = new IOpenGLSurface (this, _OpenGLID, GL_TEXTURE_2D, GL_TEXTURE_2D, l);
00047 
00048       if (Dummy == false) surface->InitializeLevel();
00049 
00050       _SurfaceArray.push_back (ObjectPtr<IOpenGLSurface> (surface));
00051       surface->UnReference ();
00052     }
00053 
00054     SetFiltering (GL_NEAREST, GL_NEAREST);
00055     SetWrap (GL_REPEAT, GL_REPEAT, GL_REPEAT);
00056     SetRenderStates();
00057 
00058     GRunTimeStats.Register (this);
00059   }
00060 
00061   IOpenGLTexture2D::~IOpenGLTexture2D()
00062   {
00063     for (int l = 0; l < _NumMipLevel; l++)
00064     {
00065       _SurfaceArray[l] = ObjectPtr<IOpenGLSurface> (0);;
00066     }
00067 
00068     _SurfaceArray.clear();
00069 
00070     if (external_id_ == false)
00071     {
00072       CHECKGL ( glDeleteTextures (1, &_OpenGLID) );
00073     }
00074     GRunTimeStats.UnRegister (this);
00075     _OpenGLID = 0;
00076 
00077   }
00078 
00079   ObjectPtr<IOpenGLSurface> IOpenGLTexture2D::GetSurfaceLevel (int Level)
00080   {
00081     if ((Level >= 0) && (Level < _NumMipLevel))
00082     {
00083       return _SurfaceArray[Level];
00084     }
00085     else
00086     {
00087       nuxAssertMsg (0, TEXT ("[IOpenGLTexture2D::GetSurfaceLevel] Invalid surface level") );
00088     }
00089 
00090     return ObjectPtr<IOpenGLSurface> (0);
00091   }
00092 
00093   void IOpenGLTexture2D::GetSurfaceLevel (int Level, ObjectPtr<IOpenGLSurface>& surface)
00094   {
00095     surface = GetSurfaceLevel (Level);
00096   }
00097 
00098   int IOpenGLTexture2D::LockRect (
00099     int Level,
00100     SURFACE_LOCKED_RECT *pLockedRect,
00101     const SURFACE_RECT *pRect)
00102   {
00103     nuxAssertMsg (pLockedRect, TEXT ("[IOpenGLTexture2D::LockRect] Invalid parameter 'pLockedRect'.") );
00104     nuxAssertMsg (Level >= 0, TEXT ("[IOpenGLTexture2D::LockRect] Invalid mipmap level.") );
00105     nuxAssertMsg (Level < _NumMipLevel, TEXT ("[IOpenGLTexture2D::LockRect] Invalid mipmap level.") );
00106 
00107     if (Level < _NumMipLevel)
00108     {
00109       ObjectPtr<IOpenGLSurface> pSurfaceLevel = _SurfaceArray[Level];
00110       return pSurfaceLevel->LockRect (pLockedRect, pRect);
00111     }
00112     else
00113     {
00114       pLockedRect->pBits = 0;
00115       pLockedRect->Pitch = 0;
00116       return OGL_INVALID_SURFACE_LEVEL;
00117     }
00118 
00119     return OGL_OK;
00120   }
00121 
00122   int IOpenGLTexture2D::UnlockRect (int Level)
00123   {
00124     nuxAssertMsg (Level >= 0, TEXT ("[IOpenGLTexture2D::LockRect] Invalid mipmap level."));
00125     nuxAssertMsg (Level < _NumMipLevel, TEXT ("[IOpenGLTexture2D::LockRect] Invalid mipmap level."));
00126 
00127     if (Level < _NumMipLevel)
00128     {
00129       ObjectPtr<IOpenGLSurface> pSurfaceLevel = _SurfaceArray[Level];
00130       return pSurfaceLevel->UnlockRect();
00131     }
00132     else
00133     {
00134       return OGL_INVALID_SURFACE_LEVEL;
00135     }
00136 
00137     return OGL_OK;
00138   }
00139 
00140   unsigned int IOpenGLTexture2D::EnableGammaCorrection (bool b)
00141   {
00142     nuxAssert (_OpenGLID);
00143     return OGL_OK;
00144   }
00145 
00146   void* IOpenGLTexture2D::GetSurfaceData (int level, int &width, int &height, int &format)
00147   {
00148     nuxAssertMsg (level >= 0, TEXT ("[IOpenGLTexture2D::LockRect] Invalid mipmap level.") );
00149     nuxAssertMsg (level < _NumMipLevel, TEXT ("[IOpenGLTexture2D::LockRect] Invalid mipmap level.") );
00150 
00151     if (level < _NumMipLevel)
00152     {
00153       ObjectPtr<IOpenGLSurface> pSurfaceLevel = _SurfaceArray [level];
00154       return pSurfaceLevel->GetSurfaceData (width, height, format);
00155     }
00156     else
00157     {
00158       width = 0;
00159       height = 0;
00160       format = BITFMT_UNKNOWN;
00161       return 0;
00162     }
00163   }
00164 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends