20 #ifndef _STELGLUTILITYFUNCTIONS_HPP_
21 #define _STELGLUTILITYFUNCTIONS_HPP_
26 #include "StelGLCompatibility.hpp"
27 #include "StelRenderer.hpp"
28 #include "StelIndexBuffer.hpp"
29 #include "StelTextureParams.hpp"
30 #include "StelVertexAttribute.hpp"
31 #include "StelVertexBuffer.hpp"
42 inline GLint glAttributeType(
const AttributeType type)
46 case AttributeType_Vec2f:
47 case AttributeType_Vec3f:
48 case AttributeType_Vec4f:
53 Q_ASSERT_X(
false, Q_FUNC_INFO,
"Unknown vertex attribute type");
68 inline const char* glslAttributeName(
const AttributeInterpretation interpretation)
70 switch(interpretation)
72 case AttributeInterpretation_Position:
return "vertex";
73 case AttributeInterpretation_TexCoord:
return "texCoord";
74 case AttributeInterpretation_Normal:
return "normal";
75 case AttributeInterpretation_Color:
return "color";
76 default: Q_ASSERT_X(
false, Q_FUNC_INFO,
"Unknown vertex attribute interpretation");
95 inline GLenum gl1AttributeEnum(
const AttributeInterpretation interpretation)
97 switch(interpretation)
99 case AttributeInterpretation_Position:
return GL_VERTEX_ARRAY;
100 case AttributeInterpretation_TexCoord:
return GL_TEXTURE_COORD_ARRAY;
101 case AttributeInterpretation_Normal:
return GL_NORMAL_ARRAY;
102 case AttributeInterpretation_Color:
return GL_COLOR_ARRAY;
103 default: Q_ASSERT_X(
false, Q_FUNC_INFO,
"Unknown vertex attribute interpretation");
107 return GL_VERTEX_ARRAY;
113 inline GLint glPrimitiveType(
const PrimitiveType type)
117 case PrimitiveType_Points:
return GL_POINTS;
118 case PrimitiveType_Triangles:
return GL_TRIANGLES;
119 case PrimitiveType_TriangleStrip:
return GL_TRIANGLE_STRIP;
120 case PrimitiveType_TriangleFan:
return GL_TRIANGLE_FAN;
121 case PrimitiveType_Lines:
return GL_LINES;
122 case PrimitiveType_LineStrip:
return GL_LINE_STRIP;
123 case PrimitiveType_LineLoop:
return GL_LINE_LOOP;
125 Q_ASSERT_X(
false, Q_FUNC_INFO,
"Unknown graphics primitive type");
134 inline GLenum glIndexType(
const IndexType indexType)
136 if(indexType == IndexType_U16) {
return GL_UNSIGNED_SHORT;}
137 else if(indexType == IndexType_U32) {
return GL_UNSIGNED_INT;}
138 Q_ASSERT_X(
false, Q_FUNC_INFO,
"Unknown index type");
146 inline GLint glTextureWrap(
const TextureWrap wrap)
150 case TextureWrap_Repeat:
return GL_REPEAT;
151 case TextureWrap_ClampToEdge:
return GL_CLAMP_TO_EDGE;
153 Q_ASSERT_X(
false, Q_FUNC_INFO,
"Unknown texture wrap mode");
162 inline QString glErrorToString(
const GLenum error)
166 case GL_NO_ERROR:
return "GL_NO_ERROR";
167 case GL_INVALID_ENUM:
return "GL_INVALID_ENUM";
168 case GL_INVALID_VALUE:
return "GL_INVALID_VALUE";
169 case GL_INVALID_OPERATION:
return "GL_INVALID_OPERATION";
170 case GL_INVALID_FRAMEBUFFER_OPERATION:
return "GL_INVALID_FRAMEBUFFER_OPERATION";
171 case GL_OUT_OF_MEMORY:
return "GL_OUT_OF_MEMORY";
173 Q_ASSERT_X(
false, Q_FUNC_INFO,
"Unknown GL error");
185 inline GLint glGetTextureInternalFormat(
const QImage& image)
187 const bool gray = image.isGrayscale();
188 const bool alpha = image.hasAlphaChannel();
189 if(gray) {
return alpha ? GL_LUMINANCE8_ALPHA8 : GL_LUMINANCE8;}
190 else {
return alpha ? GL_RGBA8 : GL_RGB8;}
197 inline GLint glGetTextureInternalFormat(
const TextureDataFormat format)
201 case TextureDataFormat_RGBA_F32:
return GL_RGBA32F;
break;
202 default: Q_ASSERT_X(
false, Q_FUNC_INFO,
"Unknown texture data format");
215 inline GLenum glGetTextureLoadFormat(
const QImage& image)
217 const bool gray = image.isGrayscale();
218 const bool alpha = image.hasAlphaChannel();
219 if(gray) {
return alpha ? GL_LUMINANCE_ALPHA : GL_LUMINANCE;}
220 else {
return alpha ? GL_RGBA : GL_RGB;}
226 inline GLint glGetTextureLoadFormat(
const TextureDataFormat format)
230 case TextureDataFormat_RGBA_F32:
return GL_RGBA;
break;
231 default: Q_ASSERT_X(
false, Q_FUNC_INFO,
"Unknown texture data format");
244 inline GLenum glGetTextureType(
const QImage& image)
247 return GL_UNSIGNED_BYTE;
253 inline GLint glGetTextureType(
const TextureDataFormat format)
257 case TextureDataFormat_RGBA_F32:
return GL_FLOAT;
break;
258 default: Q_ASSERT_X(
false, Q_FUNC_INFO,
"Unknown texture data format");
267 inline bool glTextureSizeWithinLimits(
const QSize size,
const TextureDataFormat format)
269 const GLint internalFormat = glGetTextureInternalFormat(format);
270 const GLenum loadFormat = glGetTextureLoadFormat(format);
271 const GLenum type = glGetTextureType(format);
273 glTexImage2D(GL_PROXY_TEXTURE_2D, 0, internalFormat, size.width(), size.height(), 0,
274 loadFormat, type, NULL);
276 GLint width = size.width();
277 GLint height = size.height();
278 glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
279 glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &height);
281 return width != 0 && height != 0;
289 inline bool glTextureSizeWithinLimits(
const QImage& image)
291 const GLint internalFormat = glGetTextureInternalFormat(image);
292 const GLenum loadFormat = glGetTextureLoadFormat(image);
293 const GLenum type = glGetTextureType(image);
295 glTexImage2D(GL_PROXY_TEXTURE_2D, 0, internalFormat, image.width(), image.height(), 0,
296 loadFormat, type, NULL);
298 GLint width = image.width();
299 GLint height = image.height();
300 glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
301 glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &height);
303 return width != 0 && height != 0;
314 inline void glEnsureTextureSizeWithinLimits(QImage& image)
316 while(!glTextureSizeWithinLimits(image))
318 if(image.width() <= 1)
320 Q_ASSERT_X(
false, Q_FUNC_INFO,
"Even a texture with width <= 1 is \"too large\": "
321 "maybe image format is invalid/not GL supported?");
323 image = image.scaledToWidth(image.width() / 2, Qt::FastTransformation);
341 QString glFileSystemTexturePath(
const QString& filename,
const bool pvrSupported);
349 inline void checkGLErrors(
const QString& context)
351 const GLenum glError = glGetError();
352 if(glError == GL_NO_ERROR) {
return;}
354 qWarning() <<
"OpenGL error detected at " << context <<
" : "
355 << glErrorToString(glError);
358 #endif // _STELGLUTILITYFUNCTIONS_HPP_