File indexing completed on 2026-09-14 09:15:38
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
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
0025 class OpenGl_TextureFormat
0026 {
0027 public:
0028
0029
0030
0031 template <class theCompType, int theNbComps>
0032 static OpenGl_TextureFormat Create();
0033
0034
0035
0036
0037
0038
0039 Standard_EXPORT static OpenGl_TextureFormat FindFormat(const occ::handle<OpenGl_Context>& theCtx,
0040 Image_Format theFormat,
0041 bool theIsColorMap);
0042
0043
0044
0045
0046
0047 Standard_EXPORT static OpenGl_TextureFormat FindSizedFormat(
0048 const occ::handle<OpenGl_Context>& theCtx,
0049 GLint theSizedFormat);
0050
0051
0052
0053
0054
0055 Standard_EXPORT static OpenGl_TextureFormat FindCompressedFormat(
0056 const occ::handle<OpenGl_Context>& theCtx,
0057 Image_CompressedFormat theFormat,
0058 bool theIsColorMap);
0059
0060
0061 Standard_EXPORT static TCollection_AsciiString FormatFormat(GLint theInternalFormat);
0062
0063
0064 Standard_EXPORT static TCollection_AsciiString FormatDataType(GLint theDataType);
0065
0066 public:
0067
0068 OpenGl_TextureFormat()
0069 : myImageFormat(Image_Format_UNKNOWN),
0070 myInternalFormat(0),
0071 myPixelFormat(0),
0072 myDataType(0),
0073 myNbComponents(0)
0074 {
0075 }
0076
0077
0078 bool IsValid() const { return myInternalFormat != 0 && myPixelFormat != 0 && myDataType != 0; }
0079
0080
0081 GLint InternalFormat() const { return myInternalFormat; }
0082
0083
0084 void SetInternalFormat(GLint theInternal) { myInternalFormat = theInternal; }
0085
0086
0087 GLenum PixelFormat() const { return myPixelFormat; }
0088
0089
0090 void SetPixelFormat(GLenum theFormat) { myPixelFormat = theFormat; }
0091
0092
0093 GLint DataType() const { return myDataType; }
0094
0095
0096 void SetDataType(GLint theType) { myDataType = theType; }
0097
0098
0099 GLint NbComponents() const { return myNbComponents; }
0100
0101
0102 void SetNbComponents(GLint theNbComponents) { myNbComponents = theNbComponents; }
0103
0104
0105 bool IsSRGB() const
0106 {
0107 return myInternalFormat == GL_SRGB8 || myInternalFormat == GL_SRGB8_ALPHA8;
0108 }
0109
0110
0111 Image_Format ImageFormat() const { return myImageFormat; }
0112
0113
0114 void SetImageFormat(Image_Format theFormat) { myImageFormat = theFormat; }
0115
0116 public:
0117
0118 GLint Internal() const { return myInternalFormat; }
0119
0120
0121 GLenum Format() const { return myPixelFormat; }
0122
0123 private:
0124 Image_Format myImageFormat;
0125 GLint myInternalFormat;
0126 GLenum myPixelFormat;
0127 GLint myDataType;
0128 GLint myNbComponents;
0129 };
0130
0131
0132 template <class T>
0133 struct OpenGl_TextureFormatSelector
0134 {
0135
0136 };
0137
0138
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
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
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
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
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
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
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