1 // Copyright (c) 2017-2019 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #ifndef _OpenGl_TextureFormat_HeaderFile
15 #define _OpenGl_TextureFormat_HeaderFile
17 #include <Image_CompressedFormat.hxx>
18 #include <Image_Format.hxx>
19 #include <OpenGl_GlCore13.hxx>
20 #include <Standard_Handle.hxx>
21 #include <TCollection_AsciiString.hxx>
25 //! Stores parameters of OpenGL texture format.
26 class OpenGl_TextureFormat
30 //! Returns texture format for specified type and number of channels.
31 //! @tparam theCompType component type
32 //! @tparam theNbComps number of components
33 template<class theCompType, int theNbComps>
34 static OpenGl_TextureFormat Create();
36 //! Find texture format suitable to specified image format.
37 //! @param theCtx [in] OpenGL context defining supported texture formats
38 //! @param theFormat [in] image format
39 //! @param theIsColorMap [in] flag indicating color nature of image (to select sRGB texture)
40 //! @return found format or invalid format
41 Standard_EXPORT static OpenGl_TextureFormat FindFormat (const Handle(OpenGl_Context)& theCtx,
42 Image_Format theFormat,
45 //! Find texture format suitable to specified internal (sized) texture format.
46 //! @param theCtx [in] OpenGL context defining supported texture formats
47 //! @param theSizedFormat [in] sized (internal) texture format (example: GL_RGBA8)
48 //! @return found format or invalid format
49 Standard_EXPORT static OpenGl_TextureFormat FindSizedFormat (const Handle(OpenGl_Context)& theCtx,
50 GLint theSizedFormat);
52 //! Find texture format suitable to specified compressed texture format.
53 //! @param theCtx [in] OpenGL context defining supported texture formats
54 //! @param theFormat [in] compressed texture format
55 //! @return found format or invalid format
56 Standard_EXPORT static OpenGl_TextureFormat FindCompressedFormat (const Handle(OpenGl_Context)& theCtx,
57 Image_CompressedFormat theFormat,
60 //! Format pixel format enumeration.
61 Standard_EXPORT static TCollection_AsciiString FormatFormat (GLint theInternalFormat);
63 //! Format data type enumeration.
64 Standard_EXPORT static TCollection_AsciiString FormatDataType (GLint theDataType);
68 //! Empty constructor (invalid texture format).
69 OpenGl_TextureFormat() : myImageFormat (Image_Format_UNKNOWN), myInternalFormat (0), myPixelFormat (0), myDataType (0), myNbComponents (0) {}
71 //! Return TRUE if format is defined.
74 return myInternalFormat != 0
79 //! Returns OpenGL internal format of the pixel data (example: GL_R32F).
80 GLint InternalFormat() const { return myInternalFormat; }
82 //! Sets texture internal format.
83 void SetInternalFormat (GLint theInternal) { myInternalFormat = theInternal; }
85 //! Returns OpenGL format of the pixel data (example: GL_RED).
86 GLenum PixelFormat() const { return myPixelFormat; }
88 //! Sets OpenGL format of the pixel data.
89 void SetPixelFormat (GLenum theFormat) { myPixelFormat = theFormat; }
91 //! Returns OpenGL data type of the pixel data (example: GL_FLOAT).
92 GLint DataType() const { return myDataType; }
94 //! Sets OpenGL data type of the pixel data.
95 void SetDataType (GLint theType) { myDataType = theType; }
97 //! Returns number of components (channels). Here for debugging purposes.
98 GLint NbComponents() const { return myNbComponents; }
100 //! Sets number of components (channels).
101 void SetNbComponents (GLint theNbComponents) { myNbComponents = theNbComponents; }
103 //! Return TRUE if internal texture format is sRGB(A).
106 return myInternalFormat == GL_SRGB8
107 || myInternalFormat == GL_SRGB8_ALPHA8;
110 //! Returns image format (best match or Image_Format_UNKNOWN if no suitable fit).
111 Image_Format ImageFormat() const { return myImageFormat; }
113 //! Sets image format.
114 void SetImageFormat (Image_Format theFormat) { myImageFormat = theFormat; }
118 //! Returns OpenGL internal format of the pixel data (example: GL_R32F).
119 GLint Internal() const { return myInternalFormat; }
121 //! Returns OpenGL format of the pixel data (example: GL_RED).
122 GLenum Format() const { return myPixelFormat; }
126 Image_Format myImageFormat; //!< image format
127 GLint myInternalFormat; //!< OpenGL internal format of the pixel data
128 GLenum myPixelFormat; //!< OpenGL pixel format
129 GLint myDataType; //!< OpenGL data type of input pixel data
130 GLint myNbComponents; //!< number of channels for each pixel (from 1 to 4)
134 //! Selects preferable texture format for specified parameters.
135 template<class T> struct OpenGl_TextureFormatSelector
140 //! Specialization for unsigned byte.
141 template<> struct OpenGl_TextureFormatSelector<GLubyte>
143 static GLint DataType() { return GL_UNSIGNED_BYTE; }
144 static GLint Internal (GLuint theChannels)
148 case 1: return GL_R8;
149 case 2: return GL_RG8;
150 case 3: return GL_RGB8;
151 case 4: return GL_RGBA8;
152 default: return GL_NONE;
157 //! Specialization for unsigned short.
158 template<> struct OpenGl_TextureFormatSelector<GLushort>
160 static GLint DataType() { return GL_UNSIGNED_SHORT; }
161 static GLint Internal (GLuint theChannels)
165 case 1: return GL_R16;
166 case 2: return GL_RG16;
167 case 3: return GL_RGB16;
168 case 4: return GL_RGBA16;
169 default: return GL_NONE;
174 //! Specialization for float.
175 template<> struct OpenGl_TextureFormatSelector<GLfloat>
177 static GLint DataType() { return GL_FLOAT; }
178 static GLint Internal (GLuint theChannels)
182 case 1: return GL_R32F;
183 case 2: return GL_RG32F;
184 case 3: return GL_RGB32F;
185 case 4: return GL_RGBA32F;
186 default: return GL_NONE;
191 //! Specialization for unsigned int.
192 template<> struct OpenGl_TextureFormatSelector<GLuint>
194 static GLint DataType() { return GL_UNSIGNED_INT; }
195 static GLint Internal (GLuint theChannels)
199 case 1: return GL_RED;
200 case 2: return GL_RG;
201 case 3: return GL_RGB;
202 case 4: return GL_RGBA;
203 default: return GL_NONE;
208 //! Specialization for signed byte.
209 template<> struct OpenGl_TextureFormatSelector<GLbyte>
211 static GLint DataType() { return GL_BYTE; }
212 static GLint Internal (GLuint theChannels)
216 case 1: return GL_R8_SNORM;
217 case 2: return GL_RG8_SNORM;
218 case 3: return GL_RGB8_SNORM;
219 case 4: return GL_RGBA8_SNORM;
220 default: return GL_NONE;
225 //! Specialization for signed short.
226 template<> struct OpenGl_TextureFormatSelector<GLshort>
228 static GLint DataType() { return GL_SHORT; }
229 static GLint Internal (GLuint theChannels)
233 case 1: return GL_R16_SNORM;
234 case 2: return GL_RG16_SNORM;
235 case 3: return GL_RGB16_SNORM;
236 case 4: return GL_RGBA16_SNORM;
237 default: return GL_NONE;
242 //! Specialization for signed int.
243 template<> struct OpenGl_TextureFormatSelector<GLint>
245 static GLint DataType() { return GL_INT; }
246 static GLint Internal (GLuint theChannels)
250 case 1: return GL_RED_SNORM;
251 case 2: return GL_RG_SNORM;
252 case 3: return GL_RGB_SNORM;
253 case 4: return GL_RGBA_SNORM;
254 default: return GL_NONE;
259 // =======================================================================
262 // =======================================================================
263 template<class theCompType, int theNbComps>
264 inline OpenGl_TextureFormat OpenGl_TextureFormat::Create()
266 OpenGl_TextureFormat aFormat;
267 aFormat.SetNbComponents (theNbComps);
268 aFormat.SetInternalFormat (OpenGl_TextureFormatSelector<theCompType>::Internal (theNbComps));
269 aFormat.SetDataType (OpenGl_TextureFormatSelector<theCompType>::DataType());
270 GLenum aPixelFormat = GL_NONE;
273 case 1: aPixelFormat = GL_RED; break;
274 case 2: aPixelFormat = GL_RG; break;
275 case 3: aPixelFormat = GL_RGB; break;
276 case 4: aPixelFormat = GL_RGBA; break;
278 aFormat.SetPixelFormat (aPixelFormat);
282 #endif // _OpenGl_TextureFormat_HeaderFile