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_Format.hxx>
18 #include <OpenGl_GlCore13.hxx>
19 #include <Standard_Handle.hxx>
23 //! Stores parameters of OpenGL texture format.
24 class OpenGl_TextureFormat
28 //! Returns texture format for specified type and number of channels.
29 //! @tparam theCompType component type
30 //! @tparam theNbComps number of components
31 template<class theCompType, int theNbComps>
32 static OpenGl_TextureFormat Create();
34 //! Find texture format suitable to specified image format.
35 //! @param theCtx [in] OpenGL context defining supported texture formats
36 //! @param theFormat [in] image format
37 //! @param theIsColorMap [in] flag indicating color nature of image (to select sRGB texture)
38 //! @return found format or invalid format
39 Standard_EXPORT static OpenGl_TextureFormat FindFormat (const Handle(OpenGl_Context)& theCtx,
40 Image_Format theFormat,
43 //! Find texture format suitable to specified internal (sized) texture format.
44 //! @param theCtx [in] OpenGL context defining supported texture formats
45 //! @param theSizedFormat [in] sized (internal) texture format (example: GL_RGBA8)
46 //! @return found format or invalid format
47 Standard_EXPORT static OpenGl_TextureFormat FindSizedFormat (const Handle(OpenGl_Context)& theCtx,
48 GLint theSizedFormat);
52 //! Empty constructor (invalid texture format).
53 OpenGl_TextureFormat() : myInternalFormat (0), myPixelFormat (0), myDataType (0), myNbComponents (0) {}
55 //! Return TRUE if format is defined.
58 return myInternalFormat != 0
63 //! Returns OpenGL internal format of the pixel data (example: GL_R32F).
64 GLint InternalFormat() const { return myInternalFormat; }
66 //! Sets texture internal format.
67 void SetInternalFormat (GLint theInternal) { myInternalFormat = theInternal; }
69 //! Returns OpenGL format of the pixel data (example: GL_RED).
70 GLenum PixelFormat() const { return myPixelFormat; }
72 //! Sets OpenGL format of the pixel data.
73 void SetPixelFormat (GLenum theFormat) { myPixelFormat = theFormat; }
75 //! Returns OpenGL data type of the pixel data (example: GL_FLOAT).
76 GLint DataType() const { return myDataType; }
78 //! Sets OpenGL data type of the pixel data.
79 void SetDataType (GLint theType) { myDataType = theType; }
81 //! Returns number of components (channels). Here for debugging purposes.
82 GLint NbComponents() const { return myNbComponents; }
84 //! Sets number of components (channels).
85 void SetNbComponents (GLint theNbComponents) { myNbComponents = theNbComponents; }
87 //! Return TRUE if internal texture format is sRGB(A).
90 return myInternalFormat == GL_SRGB8
91 || myInternalFormat == GL_SRGB8_ALPHA8;
96 //! Returns OpenGL internal format of the pixel data (example: GL_R32F).
97 GLint Internal() const { return myInternalFormat; }
99 //! Returns OpenGL format of the pixel data (example: GL_RED).
100 GLenum Format() const { return myPixelFormat; }
104 GLint myInternalFormat; //!< OpenGL internal format of the pixel data
105 GLenum myPixelFormat; //!< OpenGL pixel format
106 GLint myDataType; //!< OpenGL data type of input pixel data
107 GLint myNbComponents; //!< number of channels for each pixel (from 1 to 4)
111 //! Selects preferable texture format for specified parameters.
112 template<class T> struct OpenGl_TextureFormatSelector
117 //! Specialization for unsigned byte.
118 template<> struct OpenGl_TextureFormatSelector<GLubyte>
120 static GLint DataType() { return GL_UNSIGNED_BYTE; }
121 static GLint Internal (GLuint theChannels)
125 case 1: return GL_R8;
126 case 2: return GL_RG8;
127 case 3: return GL_RGB8;
128 case 4: return GL_RGBA8;
129 default: return GL_NONE;
134 //! Specialization for unsigned short.
135 template<> struct OpenGl_TextureFormatSelector<GLushort>
137 static GLint DataType() { return GL_UNSIGNED_SHORT; }
138 static GLint Internal (GLuint theChannels)
142 case 1: return GL_R16;
143 case 2: return GL_RG16;
144 case 3: return GL_RGB16;
145 case 4: return GL_RGBA16;
146 default: return GL_NONE;
151 //! Specialization for float.
152 template<> struct OpenGl_TextureFormatSelector<GLfloat>
154 static GLint DataType() { return GL_FLOAT; }
155 static GLint Internal (GLuint theChannels)
159 case 1: return GL_R32F;
160 case 2: return GL_RG32F;
161 case 3: return GL_RGB32F;
162 case 4: return GL_RGBA32F;
163 default: return GL_NONE;
168 //! Specialization for unsigned int.
169 template<> struct OpenGl_TextureFormatSelector<GLuint>
171 static GLint DataType() { return GL_UNSIGNED_INT; }
172 static GLint Internal (GLuint theChannels)
176 case 1: return GL_RED;
177 case 2: return GL_RG;
178 case 3: return GL_RGB;
179 case 4: return GL_RGBA;
180 default: return GL_NONE;
185 //! Only unsigned formats are available in OpenGL ES 2.0
186 #if !defined(GL_ES_VERSION_2_0)
187 //! Specialization for signed byte.
188 template<> struct OpenGl_TextureFormatSelector<GLbyte>
190 static GLint DataType() { return GL_BYTE; }
191 static GLint Internal (GLuint theChannels)
195 case 1: return GL_R8_SNORM;
196 case 2: return GL_RG8_SNORM;
197 case 3: return GL_RGB8_SNORM;
198 case 4: return GL_RGBA8_SNORM;
199 default: return GL_NONE;
204 //! Specialization for signed short.
205 template<> struct OpenGl_TextureFormatSelector<GLshort>
207 static GLint DataType() { return GL_SHORT; }
208 static GLint Internal (GLuint theChannels)
212 case 1: return GL_R16_SNORM;
213 case 2: return GL_RG16_SNORM;
214 case 3: return GL_RGB16_SNORM;
215 case 4: return GL_RGBA16_SNORM;
216 default: return GL_NONE;
221 //! Specialization for signed int.
222 template<> struct OpenGl_TextureFormatSelector<GLint>
224 static GLint DataType() { return GL_INT; }
225 static GLint Internal (GLuint theChannels)
229 case 1: return GL_RED_SNORM;
230 case 2: return GL_RG_SNORM;
231 case 3: return GL_RGB_SNORM;
232 case 4: return GL_RGBA_SNORM;
233 default: return GL_NONE;
239 // =======================================================================
242 // =======================================================================
243 template<class theCompType, int theNbComps>
244 inline OpenGl_TextureFormat OpenGl_TextureFormat::Create()
246 OpenGl_TextureFormat aFormat;
247 aFormat.SetNbComponents (theNbComps);
248 aFormat.SetInternalFormat (OpenGl_TextureFormatSelector<theCompType>::Internal (theNbComps));
249 aFormat.SetDataType (OpenGl_TextureFormatSelector<theCompType>::DataType());
250 GLenum aPixelFormat = GL_NONE;
253 case 1: aPixelFormat = GL_RED; break;
254 case 2: aPixelFormat = GL_RG; break;
255 case 3: aPixelFormat = GL_RGB; break;
256 case 4: aPixelFormat = GL_RGBA; break;
258 aFormat.SetPixelFormat (aPixelFormat);
262 #endif // _OpenGl_TextureFormat_HeaderFile