Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:04:35

0001 // Created by: Kirill GAVRILOV
0002 // Copyright (c) 2013-2014 OPEN CASCADE SAS
0003 //
0004 // This file is part of Open CASCADE Technology software library.
0005 //
0006 // This library is free software; you can redistribute it and/or modify it under
0007 // the terms of the GNU Lesser General Public License version 2.1 as published
0008 // by the Free Software Foundation, with special exception defined in the file
0009 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0010 // distribution for complete text of the license and disclaimer of any warranty.
0011 //
0012 // Alternatively, this file may be used under the terms of Open CASCADE
0013 // commercial license or contractual agreement.
0014 
0015 #ifndef OpenGl_Texture_HeaderFile
0016 #define OpenGl_Texture_HeaderFile
0017 
0018 #include <Graphic3d_CubeMap.hxx>
0019 #include <Graphic3d_Vec3.hxx>
0020 #include <OpenGl_TextureFormat.hxx>
0021 #include <OpenGl_NamedResource.hxx>
0022 #include <OpenGl_Sampler.hxx>
0023 #include <Graphic3d_TextureUnit.hxx>
0024 #include <Graphic3d_TypeOfTexture.hxx>
0025 
0026 
0027 //! Texture resource.
0028 class OpenGl_Texture : public OpenGl_NamedResource
0029 {
0030   DEFINE_STANDARD_RTTIEXT(OpenGl_Texture, OpenGl_NamedResource)
0031 public:
0032 
0033   //! Helpful constants
0034   static const unsigned int NO_TEXTURE = 0;
0035 
0036   //! Return pixel size of pixel format in bytes.
0037   //! Note that this method considers that OpenGL natively supports this pixel format,
0038   //! which might be not the case - in the latter case, actual pixel size might differ!
0039   Standard_EXPORT static Standard_Size PixelSizeOfPixelFormat (Standard_Integer theInternalFormat);
0040 
0041 public:
0042 
0043   //! Create uninitialized texture.
0044   Standard_EXPORT OpenGl_Texture (const TCollection_AsciiString& theResourceId = TCollection_AsciiString(),
0045                                   const Handle(Graphic3d_TextureParams)& theParams = Handle(Graphic3d_TextureParams)());
0046 
0047   //! Destroy object.
0048   Standard_EXPORT virtual ~OpenGl_Texture();
0049 
0050   //! @return true if current object was initialized
0051   virtual bool IsValid() const { return myTextureId != NO_TEXTURE; }
0052 
0053   //! @return target to which the texture is bound (GL_TEXTURE_1D, GL_TEXTURE_2D)
0054   unsigned int GetTarget() const { return myTarget; }
0055 
0056   //! Return texture dimensions (0 LOD)
0057   const Graphic3d_Vec3i& Size() const { return mySize; }
0058 
0059   //! Return texture width (0 LOD)
0060   Standard_Integer SizeX() const { return mySize.x(); }
0061 
0062   //! Return texture height (0 LOD)
0063   Standard_Integer SizeY() const { return mySize.y(); }
0064 
0065   //! Return texture depth (0 LOD)
0066   Standard_Integer SizeZ() const { return mySize.z(); }
0067 
0068   //! @return texture ID
0069   unsigned int TextureId() const { return myTextureId; }
0070 
0071   //! @return texture format (not sized)
0072   unsigned int GetFormat() const { return myTextFormat; }
0073   
0074   //! @return texture format (sized)
0075   Standard_Integer SizedFormat() const { return mySizedFormat; }
0076 
0077   //! Return true for GL_RED and GL_ALPHA formats.
0078   bool IsAlpha() const { return myIsAlpha; }
0079 
0080   //! Setup to interpret the format as Alpha by Shader Manager
0081   //! (should be GL_ALPHA within compatible context or GL_RED otherwise).
0082   void SetAlpha (const bool theValue) { myIsAlpha = theValue; }
0083 
0084   //! Return if 2D surface is defined top-down (TRUE) or bottom-up (FALSE).
0085   //! Normally set from Image_PixMap::IsTopDown() within texture initialization.
0086   bool IsTopDown() const { return myIsTopDown; }
0087 
0088   //! Set if 2D surface is defined top-down (TRUE) or bottom-up (FALSE).
0089   void SetTopDown (bool theIsTopDown) { myIsTopDown = theIsTopDown; }
0090 
0091   //! Creates Texture id if not yet generated.
0092   //! Data should be initialized by another method.
0093   Standard_EXPORT bool Create (const Handle(OpenGl_Context)& theCtx);
0094 
0095   //! Destroy object - will release GPU memory if any.
0096   Standard_EXPORT virtual void Release (OpenGl_Context* theCtx) Standard_OVERRIDE;
0097 
0098   //! Return texture sampler.
0099   const Handle(OpenGl_Sampler)& Sampler() const { return mySampler; }
0100 
0101   //! Set texture sampler.
0102   void SetSampler (const Handle(OpenGl_Sampler)& theSampler) { mySampler = theSampler; }
0103 
0104   //! Initialize the Sampler Object (as OpenGL object).
0105   //! @param theCtx currently bound OpenGL context
0106   Standard_EXPORT bool InitSamplerObject (const Handle(OpenGl_Context)& theCtx);
0107 
0108   //! Bind this Texture to the unit specified in sampler parameters.
0109   //! Also binds Sampler Object if it is allocated.
0110   void Bind (const Handle(OpenGl_Context)& theCtx) const
0111   {
0112     Bind (theCtx, mySampler->Parameters()->TextureUnit());
0113   }
0114 
0115   //! Unbind texture from the unit specified in sampler parameters.
0116   //! Also unbinds Sampler Object if it is allocated.
0117   void Unbind (const Handle(OpenGl_Context)& theCtx) const
0118   {
0119     Unbind (theCtx, mySampler->Parameters()->TextureUnit());
0120   }
0121 
0122   //! Bind this Texture to specified unit.
0123   //! Also binds Sampler Object if it is allocated.
0124   Standard_EXPORT void Bind (const Handle(OpenGl_Context)& theCtx,
0125                              const Graphic3d_TextureUnit   theTextureUnit) const;
0126 
0127   //! Unbind texture from specified unit.
0128   //! Also unbinds Sampler Object if it is allocated.
0129   Standard_EXPORT void Unbind (const Handle(OpenGl_Context)& theCtx,
0130                                const Graphic3d_TextureUnit   theTextureUnit) const;
0131 
0132   //! Revision of associated data source.
0133   Standard_Size Revision() const { return myRevision; }
0134 
0135   //! Set revision of associated data source.
0136   void SetRevision (const Standard_Size theRevision) { myRevision = theRevision; }
0137 
0138   //! Notice that texture will be unbound after this call.
0139   Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theCtx,
0140                              const Image_PixMap&           theImage,
0141                              const Graphic3d_TypeOfTexture theType,
0142                              const Standard_Boolean        theIsColorMap);
0143 
0144   //! Initialize the texture with specified format, size and texture type.
0145   //! If theImage is empty the texture data will contain trash.
0146   //! Notice that texture will be unbound after this call.
0147   Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theCtx,
0148                              const OpenGl_TextureFormat&   theFormat,
0149                              const Graphic3d_Vec3i&        theSizeXYZ,
0150                              const Graphic3d_TypeOfTexture theType,
0151                              const Image_PixMap*           theImage = NULL);
0152 
0153   //! Initialize the 2D texture with specified format, size and texture type.
0154   //! If theImage is empty the texture data will contain trash.
0155   //! Notice that texture will be unbound after this call.
0156   bool Init (const Handle(OpenGl_Context)& theCtx,
0157              const OpenGl_TextureFormat&   theFormat,
0158              const Graphic3d_Vec2i&        theSizeXY,
0159              const Graphic3d_TypeOfTexture theType,
0160              const Image_PixMap*           theImage = NULL)
0161   {
0162     return Init (theCtx, theFormat, Graphic3d_Vec3i (theSizeXY, 1), theType, theImage);
0163   }
0164 
0165   //! Initialize the texture with Graphic3d_TextureMap.
0166   //! It is an universal way to initialize.
0167   //! Suitable initialization method will be chosen.
0168   Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theCtx,
0169                              const Handle(Graphic3d_TextureRoot)& theTextureMap);
0170 
0171   //! Generate mipmaps.
0172   Standard_EXPORT bool GenerateMipmaps (const Handle(OpenGl_Context)& theCtx);
0173 
0174   //! Initialize the texture with Image_CompressedPixMap.
0175   Standard_EXPORT bool InitCompressed (const Handle(OpenGl_Context)& theCtx,
0176                                        const Image_CompressedPixMap& theImage,
0177                                        const Standard_Boolean        theIsColorMap);
0178 
0179   //! Initialize the 2D multisampling texture using glTexImage2DMultisample().
0180   Standard_EXPORT bool Init2DMultisample (const Handle(OpenGl_Context)& theCtx,
0181                                           const Standard_Integer theNbSamples,
0182                                           const Standard_Integer theTextFormat,
0183                                           const Standard_Integer theSizeX,
0184                                           const Standard_Integer theSizeY);
0185 
0186   //! Allocates texture rectangle with specified format and size.
0187   //! \note Texture data is not initialized (will contain trash).
0188   Standard_EXPORT bool InitRectangle (const Handle(OpenGl_Context)& theCtx,
0189                                       const Standard_Integer        theSizeX,
0190                                       const Standard_Integer        theSizeY,
0191                                       const OpenGl_TextureFormat&   theFormat);
0192 
0193   //! Initializes 3D texture rectangle with specified format and size.
0194   Standard_EXPORT bool Init3D (const Handle(OpenGl_Context)& theCtx,
0195                                const OpenGl_TextureFormat&   theFormat,
0196                                const Graphic3d_Vec3i&        theSizeXYZ,
0197                                const void*                   thePixels);
0198 
0199   //! @return true if texture was generated within mipmaps
0200   Standard_Boolean HasMipmaps() const { return myMaxMipLevel > 0; }
0201 
0202   //! Return upper mipmap level index (0 means no mipmaps).
0203   Standard_Integer MaxMipmapLevel() const { return myMaxMipLevel; }
0204 
0205   //! Return number of MSAA samples.
0206   Standard_Integer NbSamples() const { return myNbSamples; }
0207 
0208   //! Returns estimated GPU memory usage for holding data without considering overheads and allocation alignment rules.
0209   Standard_EXPORT virtual Standard_Size EstimatedDataSize() const Standard_OVERRIDE;
0210 
0211   //! Returns TRUE for point sprite texture.
0212   virtual bool IsPointSprite() const { return false; }
0213 
0214   //! Auxiliary method for making an image dump from texture data.
0215   //! @param theImage   [out] result image data (will be overridden)
0216   //! @param theCtx      [in] active GL context
0217   //! @param theTexUnit  [in] texture slot to use
0218   //! @param theLevel    [in] mipmap level to dump
0219   //! @param theCubeSide [in] cubemap side to dump within [0, 5] range
0220   //! @return FALSE on error
0221   Standard_EXPORT bool ImageDump (Image_PixMap& theImage,
0222                                   const Handle(OpenGl_Context)& theCtx,
0223                                   Graphic3d_TextureUnit theTexUnit,
0224                                   Standard_Integer theLevel = 0,
0225                                   Standard_Integer theCubeSide = 0) const;
0226 
0227 public:
0228 
0229   Standard_DEPRECATED("Deprecated method, OpenGl_TextureFormat::FindFormat() should be used instead")
0230   static bool GetDataFormat (const Handle(OpenGl_Context)& theCtx,
0231                              const Image_Format theFormat,
0232                              Standard_Integer&  theTextFormat,
0233                              unsigned int&      thePixelFormat,
0234                              unsigned int&      theDataType)
0235   {
0236     OpenGl_TextureFormat aFormat = OpenGl_TextureFormat::FindFormat (theCtx, theFormat, false);
0237     theTextFormat  = aFormat.InternalFormat();
0238     thePixelFormat = aFormat.PixelFormat();
0239     theDataType    = aFormat.DataType();
0240     return aFormat.IsValid();
0241   }
0242 
0243   Standard_DEPRECATED("Deprecated method, OpenGl_TextureFormat::FindFormat() should be used instead")
0244   static bool GetDataFormat (const Handle(OpenGl_Context)& theCtx,
0245                              const Image_PixMap& theData,
0246                              Standard_Integer&   theTextFormat,
0247                              unsigned int&       thePixelFormat,
0248                              unsigned int&       theDataType)
0249   {
0250     OpenGl_TextureFormat aFormat = OpenGl_TextureFormat::FindFormat (theCtx, theData.Format(), false);
0251     theTextFormat  = aFormat.InternalFormat();
0252     thePixelFormat = aFormat.PixelFormat();
0253     theDataType    = aFormat.DataType();
0254     return aFormat.IsValid();
0255   }
0256 
0257   Standard_DEPRECATED("Deprecated method, OpenGl_TextureFormat should be passed instead of separate parameters")
0258   bool Init (const Handle(OpenGl_Context)& theCtx,
0259              const Standard_Integer        theTextFormat,
0260              const unsigned int            thePixelFormat,
0261              const unsigned int            theDataType,
0262              const Standard_Integer        theSizeX,
0263              const Standard_Integer        theSizeY,
0264              const Graphic3d_TypeOfTexture theType,
0265              const Image_PixMap*           theImage = NULL)
0266   {
0267     OpenGl_TextureFormat aFormat;
0268     aFormat.SetInternalFormat (theTextFormat);
0269     aFormat.SetPixelFormat (thePixelFormat);
0270     aFormat.SetDataType (theDataType);
0271     return Init (theCtx, aFormat, Graphic3d_Vec2i (theSizeX, theSizeY), theType, theImage);
0272   }
0273 
0274   Standard_DEPRECATED("Deprecated method, theIsColorMap parameter should be explicitly specified")
0275   bool Init (const Handle(OpenGl_Context)& theCtx,
0276              const Image_PixMap& theImage,
0277              const Graphic3d_TypeOfTexture theType)
0278   {
0279     return Init (theCtx, theImage, theType, true);
0280   }
0281 
0282   Standard_DEPRECATED("Deprecated method, OpenGl_TextureFormat should be passed instead of separate parameters")
0283   bool Init3D (const Handle(OpenGl_Context)& theCtx,
0284                const Standard_Integer theTextFormat,
0285                const unsigned int     thePixelFormat,
0286                const unsigned int     theDataType,
0287                const Standard_Integer theSizeX,
0288                const Standard_Integer theSizeY,
0289                const Standard_Integer theSizeZ,
0290                const void* thePixels)
0291   {
0292     OpenGl_TextureFormat aFormat;
0293     aFormat.SetInternalFormat (theTextFormat);
0294     aFormat.SetPixelFormat (thePixelFormat);
0295     aFormat.SetDataType (theDataType);
0296     return Init3D (theCtx, aFormat, Graphic3d_Vec3i (theSizeX, theSizeY, theSizeZ), thePixels);
0297   }
0298 
0299   //! Initializes 6 sides of cubemap.
0300   //! If theCubeMap is not NULL then size and format will be taken from it and corresponding arguments will be ignored.
0301   //! Otherwise this parameters will be taken from arguments.
0302   //! @param theCtx         [in] active OpenGL context
0303   //! @param theCubeMap     [in] cubemap definition, can be NULL
0304   //! @param theSize        [in] cubemap dimensions
0305   //! @param theFormat      [in] image format
0306   //! @param theToGenMipmap [in] flag to generate mipmaped cubemap
0307   //! @param theIsColorMap  [in] flag indicating cubemap storing color values
0308   Standard_EXPORT bool InitCubeMap (const Handle(OpenGl_Context)&    theCtx,
0309                                     const Handle(Graphic3d_CubeMap)& theCubeMap,
0310                                     Standard_Size    theSize,
0311                                     Image_Format     theFormat,
0312                                     Standard_Boolean theToGenMipmap,
0313                                     Standard_Boolean theIsColorMap);
0314 
0315 protected:
0316 
0317   //! Apply default sampler parameters after texture creation.
0318   Standard_EXPORT void applyDefaultSamplerParams (const Handle(OpenGl_Context)& theCtx);
0319 
0320 protected:
0321 
0322   Handle(OpenGl_Sampler) mySampler; //!< texture sampler
0323   Standard_Size    myRevision;   //!< revision of associated data source
0324   unsigned int     myTextureId;  //!< GL resource ID
0325   unsigned int     myTarget;     //!< GL_TEXTURE_1D/GL_TEXTURE_2D/GL_TEXTURE_3D
0326   Graphic3d_Vec3i  mySize;       //!< texture width x height x depth
0327   unsigned int     myTextFormat; //!< texture format - GL_RGB, GL_RGBA,...
0328   Standard_Integer mySizedFormat;//!< internal (sized) texture format
0329   Standard_Integer myNbSamples;  //!< number of MSAA samples
0330   Standard_Integer myMaxMipLevel;//!< upper mipmap level index (0 means no mipmaps)
0331   bool             myIsAlpha;    //!< indicates alpha format
0332   bool             myIsTopDown;  //!< indicates if 2D surface is defined top-down (TRUE) or bottom-up (FALSE)
0333 
0334 };
0335 
0336 DEFINE_STANDARD_HANDLE(OpenGl_Texture, OpenGl_NamedResource)
0337 
0338 #endif // _OpenGl_Texture_H__