Back to home page

EIC code displayed by LXR

 
 

    


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

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 
0029   //! Returns texture format for specified type and number of channels.
0030   //! @tparam theCompType component type
0031   //! @tparam theNbComps  number of components
0032   template<class theCompType, int theNbComps>
0033   static OpenGl_TextureFormat Create();
0034 
0035   //! Find texture format suitable to specified image format.
0036   //! @param theCtx [in] OpenGL context defining supported texture formats
0037   //! @param theFormat [in] image format
0038   //! @param theIsColorMap [in] flag indicating color nature of image (to select sRGB texture)
0039   //! @return found format or invalid format
0040   Standard_EXPORT static OpenGl_TextureFormat FindFormat (const Handle(OpenGl_Context)& theCtx,
0041                                                           Image_Format theFormat,
0042                                                           bool theIsColorMap);
0043 
0044   //! Find texture format suitable to specified internal (sized) texture format.
0045   //! @param theCtx [in] OpenGL context defining supported texture formats
0046   //! @param theSizedFormat [in] sized (internal) texture format (example: GL_RGBA8)
0047   //! @return found format or invalid format
0048   Standard_EXPORT static OpenGl_TextureFormat FindSizedFormat (const Handle(OpenGl_Context)& theCtx,
0049                                                                GLint theSizedFormat);
0050 
0051   //! Find texture format suitable to specified compressed texture format.
0052   //! @param theCtx [in] OpenGL context defining supported texture formats
0053   //! @param theFormat [in] compressed texture format
0054   //! @return found format or invalid format
0055   Standard_EXPORT static OpenGl_TextureFormat FindCompressedFormat (const Handle(OpenGl_Context)& theCtx,
0056                                                                     Image_CompressedFormat theFormat,
0057                                                                     bool theIsColorMap);
0058 
0059   //! Format pixel format enumeration.
0060   Standard_EXPORT static TCollection_AsciiString FormatFormat (GLint theInternalFormat);
0061 
0062   //! Format data type enumeration.
0063   Standard_EXPORT static TCollection_AsciiString FormatDataType (GLint theDataType);
0064 
0065 public:
0066 
0067   //! Empty constructor (invalid texture format).
0068   OpenGl_TextureFormat() : myImageFormat (Image_Format_UNKNOWN), myInternalFormat (0), myPixelFormat (0), myDataType (0), myNbComponents (0) {}
0069 
0070   //! Return TRUE if format is defined.
0071   bool IsValid() const
0072   {
0073     return myInternalFormat != 0
0074         && myPixelFormat != 0
0075         && myDataType != 0;
0076   }
0077 
0078   //! Returns OpenGL internal format of the pixel data (example: GL_R32F).
0079   GLint InternalFormat() const { return myInternalFormat; }
0080 
0081   //! Sets texture internal format.
0082   void SetInternalFormat (GLint theInternal) { myInternalFormat = theInternal; }
0083 
0084   //! Returns OpenGL format of the pixel data (example: GL_RED).
0085   GLenum PixelFormat() const { return myPixelFormat; }
0086 
0087   //! Sets OpenGL format of the pixel data.
0088   void SetPixelFormat (GLenum theFormat) { myPixelFormat = theFormat; }
0089 
0090   //! Returns OpenGL data type of the pixel data (example: GL_FLOAT).
0091   GLint DataType() const { return myDataType; }
0092 
0093   //! Sets OpenGL data type of the pixel data.
0094   void SetDataType (GLint theType) { myDataType = theType; }
0095 
0096   //! Returns number of components (channels). Here for debugging purposes.
0097   GLint NbComponents() const { return myNbComponents; }
0098 
0099   //! Sets number of components (channels).
0100   void SetNbComponents (GLint theNbComponents) { myNbComponents = theNbComponents; }
0101 
0102   //! Return TRUE if internal texture format is sRGB(A).
0103   bool IsSRGB() const
0104   {
0105     return myInternalFormat == GL_SRGB8
0106         || myInternalFormat == GL_SRGB8_ALPHA8;
0107   }
0108 
0109   //! Returns image format (best match or Image_Format_UNKNOWN if no suitable fit).
0110   Image_Format ImageFormat() const { return myImageFormat; }
0111 
0112   //! Sets image format.
0113   void SetImageFormat (Image_Format theFormat) { myImageFormat = theFormat; }
0114 
0115 public:
0116 
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 
0125   Image_Format myImageFormat; //!< image format
0126   GLint  myInternalFormat; //!< OpenGL internal format of the pixel data
0127   GLenum myPixelFormat;    //!< OpenGL pixel format
0128   GLint  myDataType;       //!< OpenGL data type of input pixel data
0129   GLint  myNbComponents;   //!< number of channels for each pixel (from 1 to 4)
0130 
0131 };
0132 
0133 //! Selects preferable texture format for specified parameters.
0134 template<class T> struct OpenGl_TextureFormatSelector
0135 {
0136   // Not implemented
0137 };
0138 
0139 //! Specialization for unsigned byte.
0140 template<> struct OpenGl_TextureFormatSelector<GLubyte>
0141 {
0142   static GLint DataType() { return GL_UNSIGNED_BYTE; }
0143   static GLint Internal (GLuint theChannels)
0144   {
0145     switch (theChannels)
0146     {
0147       case 1:  return GL_R8;
0148       case 2:  return GL_RG8;
0149       case 3:  return GL_RGB8;
0150       case 4:  return GL_RGBA8;
0151       default: return GL_NONE;
0152     }
0153   }
0154 };
0155 
0156 //! Specialization for unsigned short.
0157 template<> struct OpenGl_TextureFormatSelector<GLushort>
0158 {
0159   static GLint DataType() { return GL_UNSIGNED_SHORT; }
0160   static GLint Internal (GLuint theChannels)
0161   {
0162     switch (theChannels)
0163     {
0164       case 1:  return GL_R16;
0165       case 2:  return GL_RG16;
0166       case 3:  return GL_RGB16;
0167       case 4:  return GL_RGBA16;
0168       default: return GL_NONE;
0169     }
0170   }
0171 };
0172 
0173 //! Specialization for float.
0174 template<> struct OpenGl_TextureFormatSelector<GLfloat>
0175 {
0176   static GLint DataType() { return GL_FLOAT; }
0177   static GLint Internal (GLuint theChannels)
0178   {
0179     switch (theChannels)
0180     {
0181       case 1:  return GL_R32F;
0182       case 2:  return GL_RG32F;
0183       case 3:  return GL_RGB32F;
0184       case 4:  return GL_RGBA32F;
0185       default: return GL_NONE;
0186     }
0187   }
0188 };
0189 
0190 //! Specialization for unsigned int.
0191 template<> struct OpenGl_TextureFormatSelector<GLuint>
0192 {
0193   static GLint DataType() { return GL_UNSIGNED_INT; }
0194   static GLint Internal (GLuint theChannels)
0195   {
0196     switch (theChannels)
0197     {
0198       case 1:  return GL_RED;
0199       case 2:  return GL_RG;
0200       case 3:  return GL_RGB;
0201       case 4:  return GL_RGBA;
0202       default: return GL_NONE;
0203     }
0204   }
0205 };
0206 
0207 //! Specialization for signed byte.
0208 template<> struct OpenGl_TextureFormatSelector<GLbyte>
0209 {
0210   static GLint DataType() { return GL_BYTE; }
0211   static GLint Internal (GLuint theChannels)
0212   {
0213     switch (theChannels)
0214     {
0215       case 1:  return GL_R8_SNORM;
0216       case 2:  return GL_RG8_SNORM;
0217       case 3:  return GL_RGB8_SNORM;
0218       case 4:  return GL_RGBA8_SNORM;
0219       default: return GL_NONE;
0220     }
0221   }
0222 };
0223 
0224 //! Specialization for signed short.
0225 template<> struct OpenGl_TextureFormatSelector<GLshort>
0226 {
0227   static GLint DataType() { return GL_SHORT; }
0228   static GLint Internal (GLuint theChannels)
0229   {
0230     switch (theChannels)
0231     {
0232       case 1:  return GL_R16_SNORM;
0233       case 2:  return GL_RG16_SNORM;
0234       case 3:  return GL_RGB16_SNORM;
0235       case 4:  return GL_RGBA16_SNORM;
0236       default: return GL_NONE;
0237     }
0238   }
0239 };
0240 
0241 //! Specialization for signed int.
0242 template<> struct OpenGl_TextureFormatSelector<GLint>
0243 {
0244   static GLint DataType() { return GL_INT; }
0245   static GLint Internal (GLuint theChannels)
0246   {
0247     switch (theChannels)
0248     {
0249       case 1:  return GL_RED_SNORM;
0250       case 2:  return GL_RG_SNORM;
0251       case 3:  return GL_RGB_SNORM;
0252       case 4:  return GL_RGBA_SNORM;
0253       default: return GL_NONE;
0254     }
0255   }
0256 };
0257 
0258 // =======================================================================
0259 // function : Create
0260 // purpose  :
0261 // =======================================================================
0262 template<class theCompType, int theNbComps>
0263 inline OpenGl_TextureFormat OpenGl_TextureFormat::Create()
0264 {
0265   OpenGl_TextureFormat aFormat;
0266   aFormat.SetNbComponents (theNbComps);
0267   aFormat.SetInternalFormat (OpenGl_TextureFormatSelector<theCompType>::Internal (theNbComps));
0268   aFormat.SetDataType (OpenGl_TextureFormatSelector<theCompType>::DataType());
0269   GLenum aPixelFormat = GL_NONE;
0270   switch (theNbComps)
0271   {
0272     case 1: aPixelFormat = GL_RED;  break;
0273     case 2: aPixelFormat = GL_RG;   break;
0274     case 3: aPixelFormat = GL_RGB;  break;
0275     case 4: aPixelFormat = GL_RGBA; break;
0276   }
0277   aFormat.SetPixelFormat (aPixelFormat);
0278   return aFormat;
0279 }
0280 
0281 #endif // _OpenGl_TextureFormat_HeaderFile