nux-1.14.0
IOpenGLSurface.h
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 IOPENGLSURFACE_H
00024 #define IOPENGLSURFACE_H
00025 
00026 namespace nux
00027 {
00028 
00029   class IOpenGLResource;
00030   class IOpenGLSurface: public IOpenGLResource
00031   {
00032     NUX_DECLARE_OBJECT_TYPE (IOpenGLSurface, IOpenGLResource);
00033   public:
00034     virtual int RefCount() const;
00035 
00036     int LockRect (
00037       SURFACE_LOCKED_RECT *pLockedRect,
00038       const SURFACE_RECT *pRect);
00039     int UnlockRect();
00040 
00041     BitmapFormat GetPixelFormat() const
00042     {
00043       if (_BaseTexture == 0)
00044       {
00045         nuxAssert (0);  // should not happen
00046         return BITFMT_UNKNOWN;
00047       }
00048 
00049       return _BaseTexture->GetPixelFormat();
00050     }
00051 
00052     int GetWidth() const
00053     {
00054       if (_BaseTexture == 0)
00055       {
00056         nuxAssert (0);  // should not happen
00057         return 0;
00058       }
00059 
00060       return ImageSurface::GetLevelDim (_BaseTexture->_PixelFormat, _BaseTexture->_Width, _SMipLevel);
00061     }
00062 
00063     int GetHeight() const
00064     {
00065       if (_BaseTexture == 0)
00066       {
00067         nuxAssert (0);  // should not happen
00068         return 0;
00069       }
00070 
00071       return ImageSurface::GetLevelDim (_BaseTexture->_PixelFormat, _BaseTexture->_Height, _SMipLevel);
00072     }
00073 
00074     int GetMipLevel() const
00075     {
00076       return _SMipLevel;
00077     }
00078 
00079     int GetSurfaceTarget() const
00080     {
00081       return _SSurfaceTarget;
00082     }
00083 
00084     int GetDesc (SURFACE_DESC *pDesc)
00085     {
00086       pDesc->Width    = GetWidth();
00087       pDesc->Height   = GetHeight();
00088       pDesc->PixelFormat   = GetPixelFormat();
00089       pDesc->Type     = _ResourceType;
00090       return OGL_OK;
00091     }
00092 
00094     void CopyRenderTarget (int x, int y, int width, int height);
00095 
00097     void* GetSurfaceData (int &width, int &height, int &format);
00098 
00099   private:
00100     virtual ~IOpenGLSurface();
00101 
00102     int InitializeLevel();
00103 
00104     // By Default, the surface refers to the face 0 of the texture and to the mip level 0 of that face.
00105     //    IOpenGLSurface(IOpenGLBaseTexture* DeviceBaseTexture, GLenum OpenGLID)
00106     //        : _STextureTarget(GL_TEXTURE_2D)
00107     //        , _SSurfaceTarget(GL_TEXTURE_2D)
00108     //        , _SMipLevel(0)
00109     //        , _BaseTexture(DeviceBaseTexture)
00110     //        , IOpenGLResource(RTSURFACE)
00111     //    {
00112     //        // IOpenGLSurface surfaces are created inside a IOpenGLTexture2D, IOpenGLCubeTexture and IOpenGLVolumeTexture.
00113     //        // They reside within those classes. The reference counting starts once a call to GetSurfaceLevel,
00114     //        // GetCubeMapSurface or GetVolumeLevel is made to the container object.
00115     //        _RefCount = 0;
00116     //        _OpenGLID = OpenGLID;
00117     //    }
00118     IOpenGLSurface (IOpenGLBaseTexture *DeviceBaseTexture, GLenum OpenGLID, GLenum TextureTarget, GLenum SurfaceTarget, int MipLevel, int Slice = 0 /*for volume textures*/, NUX_FILE_LINE_PROTO)
00119       : IOpenGLResource (RTSURFACE, NUX_FILE_LINE_PARAM)
00120       , _STextureTarget (TextureTarget)
00121       , _SSurfaceTarget (SurfaceTarget)
00122       , _SMipLevel (MipLevel)
00123       , _SSlice (Slice)
00124       , _BaseTexture (DeviceBaseTexture)
00125       , _AllocatedUnpackBuffer (0xFFFFFFFF)
00126     {
00127       // IOpenGLSurface surfaces are created inside a IOpenGLTexture2D, IOpenGLCubeTexture and IOpenGLVolumeTexture.
00128       // They reside within those classes. The reference counting starts once a call to GetSurfaceLevel,
00129       // GetCubeMapSurface or GetVolumeLevel is made to the container object.
00130       _RefCount = 0;
00131       _OpenGLID = OpenGLID;
00132       _LockedRect.pBits = 0;
00133       _LockedRect.Pitch = 0;
00134       _CompressedDataSize = 0;
00135       _Initialized = 0;
00136     }
00137 
00138     // _STextureTarget may be
00139     //      GL_TEXTURE_2D
00140     //      GL_TEXTURE_RECTANGLE_ARB
00141     //      GL_TEXTURE_CUBE_MAP
00142     //      GL_TEXTURE_3D
00143     GLenum      _STextureTarget;
00144 
00145     // _SSurfaceTarget may be
00146     //      GL_TEXTURE_2D
00147     //      GL_TEXTURE_3D
00148     //      GL_TEXTURE_RECTANGLE_ARB
00149     //      GL_TEXTURE_CUBE_MAP_POSITIVE_X
00150     //      GL_TEXTURE_CUBE_MAP_NEGATIVE_X
00151     //      GL_TEXTURE_CUBE_MAP_POSITIVE_Y
00152     //      GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
00153     //      GL_TEXTURE_CUBE_MAP_POSITIVE_Z
00154     //      GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
00155     GLenum      _SSurfaceTarget;
00156     int        _SMipLevel;
00157     int        _SSlice;                     // for volume texture
00158 
00159     SURFACE_LOCKED_RECT  _LockedRect;
00160     SURFACE_RECT        _Rect;
00161     int            _CompressedDataSize;
00162 
00163     IOpenGLBaseTexture *_BaseTexture;
00164     bool            _Initialized;
00165 
00166     int _AllocatedUnpackBuffer;
00167     friend class IOpenGLTexture2D;
00168     friend class IOpenGLRectangleTexture;
00169     friend class IOpenGLCubeTexture;
00170     friend class IOpenGLVolumeTexture;
00171     friend class IOpenGLAnimatedTexture;
00172     friend class ObjectPtr<IOpenGLSurface>;
00173   };
00174 
00175 }
00176 
00177 #endif // IOPENGLSURFACE_H
00178 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends