Back to index

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