Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-14 09:15:38

0001 // Copyright (c) 2017-2019 OPEN CASCADE SAS
0002 //
0003 // This file is part of Open CASCADE Technology software library.
0004 //
0005 // This library is free software; you can redistribute it and/or modify it under
0006 // the terms of the GNU Lesser General Public License version 2.1 as published
0007 // by the Free Software Foundation, with special exception defined in the file
0008 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0009 // distribution for complete text of the license and disclaimer of any warranty.
0010 //
0011 // Alternatively, this file may be used under the terms of Open CASCADE
0012 // commercial license or contractual agreement.
0013 
0014 #ifndef _OpenGl_TextureFormat_HeaderFile
0015 #define _OpenGl_TextureFormat_HeaderFile
0016 
0017 #include <Image_CompressedFormat.hxx>
0018 #include <Image_Format.hxx>
0019 #include <OpenGl_GlCore13.hxx>
0020 #include <TCollection_AsciiString.hxx>
0021 
0022 class OpenGl_Context;
0023 
0024 //! Stores parameters of OpenGL texture format.
0025 class OpenGl_TextureFormat
0026 {
0027 public:
0028   //! Returns texture format for specified type and number of channels.
0029   //! @tparam theCompType component type
0030   //! @tparam theNbComps  number of components
0031   template <class theCompType, int theNbComps>
0032   static OpenGl_TextureFormat Create();
0033 
0034   //! Find texture format suitable to specified image format.
0035   //! @param[in] theCtx  OpenGL context defining supported texture formats
0036   //! @param[in] theFormat  image format
0037   //! @param[in] theIsColorMap  flag indicating color nature of image (to select sRGB texture)
0038   //! @return found format or invalid format
0039   Standard_EXPORT static OpenGl_TextureFormat FindFormat(const occ::handle<OpenGl_Context>& theCtx,
0040                                                          Image_Format theFormat,
0041                                                          bool         theIsColorMap);
0042 
0043   //! Find texture format suitable to specified internal (sized) texture format.
0044   //! @param[in] theCtx  OpenGL context defining supported texture formats
0045   //! @param[in] theSizedFormat  sized (internal) texture format (example: GL_RGBA8)
0046   //! @return found format or invalid format
0047   Standard_EXPORT static OpenGl_TextureFormat FindSizedFormat(
0048     const occ::handle<OpenGl_Context>& theCtx,
0049     GLint                              theSizedFormat);
0050 
0051   //! Find texture format suitable to specified compressed texture format.
0052   //! @param[in] theCtx  OpenGL context defining supported texture formats
0053   //! @param[in] theFormat  compressed texture format
0054   //! @return found format or invalid format
0055   Standard_EXPORT static OpenGl_TextureFormat FindCompressedFormat(
0056     const occ::handle<OpenGl_Context>& theCtx,
0057     Image_CompressedFormat             theFormat,
0058     bool                               theIsColorMap);
0059 
0060   //! Format pixel format enumeration.
0061   Standard_EXPORT static TCollection_AsciiString FormatFormat(GLint theInternalFormat);
0062 
0063   //! Format data type enumeration.
0064   Standard_EXPORT static TCollection_AsciiString FormatDataType(GLint theDataType);
0065 
0066 public:
0067   //! Empty constructor (invalid texture format).
0068   OpenGl_TextureFormat()
0069       : myImageFormat(Image_Format_UNKNOWN),
0070         myInternalFormat(0),
0071         myPixelFormat(0),
0072         myDataType(0),
0073         myNbComponents(0)
0074   {
0075   }
0076 
0077   //! Return TRUE if format is defined.
0078   bool IsValid() const { return myInternalFormat != 0 && myPixelFormat != 0 && myDataType != 0; }
0079 
0080   //! Returns OpenGL internal format of the pixel data (example: GL_R32F).
0081   GLint InternalFormat() const { return myInternalFormat; }
0082 
0083   //! Sets texture internal format.
0084   void SetInternalFormat(GLint theInternal) { myInternalFormat = theInternal; }
0085 
0086   //! Returns OpenGL format of the pixel data (example: GL_RED).
0087   GLenum PixelFormat() const { return myPixelFormat; }
0088 
0089   //! Sets OpenGL format of the pixel data.
0090   void SetPixelFormat(GLenum theFormat) { myPixelFormat = theFormat; }
0091 
0092   //! Returns OpenGL data type of the pixel data (example: GL_FLOAT).
0093   GLint DataType() const { return myDataType; }
0094 
0095   //! Sets OpenGL data type of the pixel data.
0096   void SetDataType(GLint theType) { myDataType = theType; }
0097 
0098   //! Returns number of components (channels). Here for debugging purposes.
0099   GLint NbComponents() const { return myNbComponents; }
0100 
0101   //! Sets number of components (channels).
0102   void SetNbComponents(GLint theNbComponents) { myNbComponents = theNbComponents; }
0103 
0104   //! Return TRUE if internal texture format is sRGB(A).
0105   bool IsSRGB() const
0106   {
0107     return myInternalFormat == GL_SRGB8 || myInternalFormat == GL_SRGB8_ALPHA8;
0108   }
0109 
0110   //! Returns image format (best match or Image_Format_UNKNOWN if no suitable fit).
0111   Image_Format ImageFormat() const { return myImageFormat; }
0112 
0113   //! Sets image format.
0114   void SetImageFormat(Image_Format theFormat) { myImageFormat = theFormat; }
0115 
0116 public:
0117   //! Returns OpenGL internal format of the pixel data (example: GL_R32F).
0118   GLint Internal() const { return myInternalFormat; }
0119 
0120   //! Returns OpenGL format of the pixel data (example: GL_RED).
0121   GLenum Format() const { return myPixelFormat; }
0122 
0123 private:
0124   Image_Format myImageFormat;    //!< image format
0125   GLint        myInternalFormat; //!< OpenGL internal format of the pixel data
0126   GLenum       myPixelFormat;    //!< OpenGL pixel format
0127   GLint        myDataType;       //!< OpenGL data type of input pixel data
0128   GLint        myNbComponents;   //!< number of channels for each pixel (from 1 to 4)
0129 };
0130 
0131 //! Selects preferable texture format for specified parameters.
0132 template <class T>
0133 struct OpenGl_TextureFormatSelector
0134 {
0135   // Not implemented
0136 };
0137 
0138 //! Specialization for unsigned byte.
0139 template <>
0140 struct OpenGl_TextureFormatSelector<GLubyte>
0141 {
0142   static GLint DataType() { return GL_UNSIGNED_BYTE; }
0143 
0144   static GLint Internal(GLuint theChannels)
0145   {
0146     switch (theChannels)
0147     {
0148       case 1:
0149         return GL_R8;
0150       case 2:
0151         return GL_RG8;
0152       case 3:
0153         return GL_RGB8;
0154       case 4:
0155         return GL_RGBA8;
0156       default:
0157         return GL_NONE;
0158     }
0159   }
0160 };
0161 
0162 //! Specialization for unsigned short.
0163 template <>
0164 struct OpenGl_TextureFormatSelector<GLushort>
0165 {
0166   static GLint DataType() { return GL_UNSIGNED_SHORT; }
0167 
0168   static GLint Internal(GLuint theChannels)
0169   {
0170     switch (theChannels)
0171     {
0172       case 1:
0173         return GL_R16;
0174       case 2:
0175         return GL_RG16;
0176       case 3:
0177         return GL_RGB16;
0178       case 4:
0179         return GL_RGBA16;
0180       default:
0181         return GL_NONE;
0182     }
0183   }
0184 };
0185 
0186 //! Specialization for float.
0187 template <>
0188 struct OpenGl_TextureFormatSelector<GLfloat>
0189 {
0190   static GLint DataType() { return GL_FLOAT; }
0191 
0192   static GLint Internal(GLuint theChannels)
0193   {
0194     switch (theChannels)
0195     {
0196       case 1:
0197         return GL_R32F;
0198       case 2:
0199         return GL_RG32F;
0200       case 3:
0201         return GL_RGB32F;
0202       case 4:
0203         return GL_RGBA32F;
0204       default:
0205         return GL_NONE;
0206     }
0207   }
0208 };
0209 
0210 //! Specialization for unsigned int.
0211 template <>
0212 struct OpenGl_TextureFormatSelector<GLuint>
0213 {
0214   static GLint DataType() { return GL_UNSIGNED_INT; }
0215 
0216   static GLint Internal(GLuint theChannels)
0217   {
0218     switch (theChannels)
0219     {
0220       case 1:
0221         return GL_RED;
0222       case 2:
0223         return GL_RG;
0224       case 3:
0225         return GL_RGB;
0226       case 4:
0227         return GL_RGBA;
0228       default:
0229         return GL_NONE;
0230     }
0231   }
0232 };
0233 
0234 //! Specialization for signed byte.
0235 template <>
0236 struct OpenGl_TextureFormatSelector<GLbyte>
0237 {
0238   static GLint DataType() { return GL_BYTE; }
0239 
0240   static GLint Internal(GLuint theChannels)
0241   {
0242     switch (theChannels)
0243     {
0244       case 1:
0245         return GL_R8_SNORM;
0246       case 2:
0247         return GL_RG8_SNORM;
0248       case 3:
0249         return GL_RGB8_SNORM;
0250       case 4:
0251         return GL_RGBA8_SNORM;
0252       default:
0253         return GL_NONE;
0254     }
0255   }
0256 };
0257 
0258 //! Specialization for signed short.
0259 template <>
0260 struct OpenGl_TextureFormatSelector<GLshort>
0261 {
0262   static GLint DataType() { return GL_SHORT; }
0263 
0264   static GLint Internal(GLuint theChannels)
0265   {
0266     switch (theChannels)
0267     {
0268       case 1:
0269         return GL_R16_SNORM;
0270       case 2:
0271         return GL_RG16_SNORM;
0272       case 3:
0273         return GL_RGB16_SNORM;
0274       case 4:
0275         return GL_RGBA16_SNORM;
0276       default:
0277         return GL_NONE;
0278     }
0279   }
0280 };
0281 
0282 //! Specialization for signed int.
0283 template <>
0284 struct OpenGl_TextureFormatSelector<GLint>
0285 {
0286   static GLint DataType() { return GL_INT; }
0287 
0288   static GLint Internal(GLuint theChannels)
0289   {
0290     switch (theChannels)
0291     {
0292       case 1:
0293         return GL_RED_SNORM;
0294       case 2:
0295         return GL_RG_SNORM;
0296       case 3:
0297         return GL_RGB_SNORM;
0298       case 4:
0299         return GL_RGBA_SNORM;
0300       default:
0301         return GL_NONE;
0302     }
0303   }
0304 };
0305 
0306 //=================================================================================================
0307 
0308 template <class theCompType, int theNbComps>
0309 inline OpenGl_TextureFormat OpenGl_TextureFormat::Create()
0310 {
0311   OpenGl_TextureFormat aFormat;
0312   aFormat.SetNbComponents(theNbComps);
0313   aFormat.SetInternalFormat(OpenGl_TextureFormatSelector<theCompType>::Internal(theNbComps));
0314   aFormat.SetDataType(OpenGl_TextureFormatSelector<theCompType>::DataType());
0315   GLenum aPixelFormat = GL_NONE;
0316   switch (theNbComps)
0317   {
0318     case 1:
0319       aPixelFormat = GL_RED;
0320       break;
0321     case 2:
0322       aPixelFormat = GL_RG;
0323       break;
0324     case 3:
0325       aPixelFormat = GL_RGB;
0326       break;
0327     case 4:
0328       aPixelFormat = GL_RGBA;
0329       break;
0330   }
0331   aFormat.SetPixelFormat(aPixelFormat);
0332   return aFormat;
0333 }
0334 
0335 #endif // _OpenGl_TextureFormat_HeaderFile