Back to index

nux  3.0.0
IOpenGLTexture2D.cpp
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 #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, 0, NUX_FILE_LINE_PARAM);
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 
00057 #ifdef NUX_OPENGLES_20
00058     // NPOT textures in GLES2 only support GL_CLAMP_TO_EDGE unless
00059     // GL_OES_texture_npot is supported.
00060     // TODO: Check for GL_OES_texture_npot
00061     if (!_IsPOT)
00062     {
00063       SetWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
00064     }
00065 #endif
00066 
00067     SetRenderStates();
00068 
00069     GRunTimeStats.Register(this);
00070   }
00071 
00072   IOpenGLTexture2D::~IOpenGLTexture2D()
00073   {
00074     for (int l = 0; l < _NumMipLevel; l++)
00075     {
00076       _SurfaceArray[l] = ObjectPtr<IOpenGLSurface> (0);;
00077     }
00078 
00079     _SurfaceArray.clear();
00080 
00081     if (external_id_ == false)
00082     {
00083       CHECKGL(glDeleteTextures(1, &_OpenGLID));
00084     }
00085     GRunTimeStats.UnRegister(this);
00086     _OpenGLID = 0;
00087 
00088   }
00089 
00090   ObjectPtr<IOpenGLSurface> IOpenGLTexture2D::GetSurfaceLevel(int Level)
00091   {
00092     if ((Level >= 0) && (Level < _NumMipLevel))
00093     {
00094       return _SurfaceArray[Level];
00095     }
00096     else
00097     {
00098       nuxAssertMsg(0, "[IOpenGLTexture2D::GetSurfaceLevel] Invalid surface level");
00099     }
00100 
00101     return ObjectPtr<IOpenGLSurface> (0);
00102   }
00103 
00104   void IOpenGLTexture2D::GetSurfaceLevel(int Level, ObjectPtr<IOpenGLSurface>& surface)
00105   {
00106     surface = GetSurfaceLevel(Level);
00107   }
00108 
00109   int IOpenGLTexture2D::LockRect(
00110     int Level,
00111     SURFACE_LOCKED_RECT *pLockedRect,
00112     const SURFACE_RECT *pRect)
00113   {
00114     nuxAssertMsg(pLockedRect, "[IOpenGLTexture2D::LockRect] Invalid parameter 'pLockedRect'.");
00115     nuxAssertMsg(Level >= 0, "[IOpenGLTexture2D::LockRect] Invalid mipmap level.");
00116     nuxAssertMsg(Level < _NumMipLevel, "[IOpenGLTexture2D::LockRect] Invalid mipmap level.");
00117 
00118     if (Level < _NumMipLevel)
00119     {
00120       ObjectPtr<IOpenGLSurface> pSurfaceLevel = _SurfaceArray[Level];
00121       return pSurfaceLevel->LockRect(pLockedRect, pRect);
00122     }
00123     else
00124     {
00125       pLockedRect->pBits = 0;
00126       pLockedRect->Pitch = 0;
00127       return OGL_INVALID_SURFACE_LEVEL;
00128     }
00129 
00130     return OGL_OK;
00131   }
00132 
00133   int IOpenGLTexture2D::UnlockRect(int Level)
00134   {
00135     nuxAssertMsg(Level >= 0, "[IOpenGLTexture2D::LockRect] Invalid mipmap level.");
00136     nuxAssertMsg(Level < _NumMipLevel, "[IOpenGLTexture2D::LockRect] Invalid mipmap level.");
00137 
00138     if (Level < _NumMipLevel)
00139     {
00140       ObjectPtr<IOpenGLSurface> pSurfaceLevel = _SurfaceArray[Level];
00141       return pSurfaceLevel->UnlockRect();
00142     }
00143     else
00144     {
00145       return OGL_INVALID_SURFACE_LEVEL;
00146     }
00147 
00148     return OGL_OK;
00149   }
00150 
00151   unsigned int IOpenGLTexture2D::EnableGammaCorrection(bool b)
00152   {
00153     nuxAssert(_OpenGLID);
00154     return OGL_OK;
00155   }
00156 
00157   unsigned char* IOpenGLTexture2D::GetSurfaceData(int level, int &width, int &height, int &stride)
00158   {
00159     width = 0;
00160     height = 0;
00161     stride = 0;
00162 
00163     if (level < 0)
00164     {
00165       nuxAssertMsg(level >= 0, "[IOpenGLTexture2D::GetSurfaceData] Invalid mipmap level.");
00166       return NULL;
00167     }
00168 
00169     if (level >= _NumMipLevel)
00170     {
00171       nuxAssertMsg(level < _NumMipLevel, "[IOpenGLTexture2D::GetSurfaceData] Invalid mipmap level.");
00172       return NULL;
00173     }
00174 
00175     ObjectPtr<IOpenGLSurface> pSurfaceLevel = _SurfaceArray[level];
00176     return pSurfaceLevel->GetSurfaceData(width, height, stride);
00177   }
00178 }