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