From bf716cfdf0afecccdb1f2eabb2e6a172c620fbff Mon Sep 17 00:00:00 2001 From: Miikka Heikkinen Date: Tue, 28 Oct 2014 12:48:17 +0200 Subject: Support for dynamic opengl builds. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Task-number: QTRD-3392 Change-Id: I786bbf5ee9252b92d43d02d6b88ed9b986b4a5eb Reviewed-by: Tomi Korpipää --- src/datavisualization/utils/texturehelper.cpp | 146 ++++++++++++++------------ 1 file changed, 76 insertions(+), 70 deletions(-) (limited to 'src/datavisualization/utils/texturehelper.cpp') diff --git a/src/datavisualization/utils/texturehelper.cpp b/src/datavisualization/utils/texturehelper.cpp index 939ce901..3944fb0c 100644 --- a/src/datavisualization/utils/texturehelper.cpp +++ b/src/datavisualization/utils/texturehelper.cpp @@ -30,24 +30,24 @@ extern void discardDebugMsgs(QtMsgType type, const QMessageLogContext &context, TextureHelper::TextureHelper() { + initializeOpenGLFunctions(); #if !defined(QT_OPENGL_ES_2) - // Discard warnings about deprecated functions - QtMessageHandler handler = qInstallMessageHandler(discardDebugMsgs); + if (!Utils::isOpenGLES()) { + // Discard warnings about deprecated functions + QtMessageHandler handler = qInstallMessageHandler(discardDebugMsgs); - m_openGlFunctions_2_1 = new QOpenGLFunctions_2_1; - m_openGlFunctions_2_1->initializeOpenGLFunctions(); + m_openGlFunctions_2_1 = + QOpenGLContext::currentContext()->versionFunctions(); + m_openGlFunctions_2_1->initializeOpenGLFunctions(); - // Restore original message handler - qInstallMessageHandler(handler); + // Restore original message handler + qInstallMessageHandler(handler); + } #endif - initializeOpenGLFunctions(); } TextureHelper::~TextureHelper() { -#if !defined(QT_OPENGL_ES_2) - delete m_openGlFunctions_2_1; -#endif } GLuint TextureHelper::create2DTexture(const QImage &image, bool useTrilinearFiltering, @@ -58,16 +58,16 @@ GLuint TextureHelper::create2DTexture(const QImage &image, bool useTrilinearFilt QImage texImage = image; -#if defined(QT_OPENGL_ES_2) - GLuint imageWidth = Utils::getNearestPowerOfTwo(image.width()); - GLuint imageHeight = Utils::getNearestPowerOfTwo(image.height()); - if (smoothScale) { - texImage = image.scaled(imageWidth, imageHeight, Qt::IgnoreAspectRatio, - Qt::SmoothTransformation); - } else { - texImage = image.scaled(imageWidth, imageHeight, Qt::IgnoreAspectRatio); + if (!Utils::isOpenGLES()) { + GLuint imageWidth = Utils::getNearestPowerOfTwo(image.width()); + GLuint imageHeight = Utils::getNearestPowerOfTwo(image.height()); + if (smoothScale) { + texImage = image.scaled(imageWidth, imageHeight, Qt::IgnoreAspectRatio, + Qt::SmoothTransformation); + } else { + texImage = image.scaled(imageWidth, imageHeight, Qt::IgnoreAspectRatio); + } } -#endif GLuint textureId; glGenTextures(1, &textureId); @@ -93,16 +93,19 @@ GLuint TextureHelper::create2DTexture(const QImage &image, bool useTrilinearFilt return textureId; } -#if !defined(QT_OPENGL_ES_2) GLuint TextureHelper::create3DTexture(const QVector *data, int width, int height, int depth, QImage::Format dataFormat) { - if (!width || !height || !depth) + if (Utils::isOpenGLES() || !width || !height || !depth) return 0; + GLuint textureId = 0; +#if defined(QT_OPENGL_ES_2) + Q_UNUSED(dataFormat) + Q_UNUSED(data) +#else glEnable(GL_TEXTURE_3D); - GLuint textureId; glGenTextures(1, &textureId); glBindTexture(GL_TEXTURE_3D, textureId); glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); @@ -126,17 +129,15 @@ GLuint TextureHelper::create3DTexture(const QVector *data, int width, int } m_openGlFunctions_2_1->glTexImage3D(GL_TEXTURE_3D, 0, internalFormat, width, height, depth, 0, format, GL_UNSIGNED_BYTE, data->constData()); - status = glGetError(); if (status) qWarning() << __FUNCTION__ << "3D texture creation failed:" << status; glBindTexture(GL_TEXTURE_3D, 0); glDisable(GL_TEXTURE_3D); - +#endif return textureId; } -#endif GLuint TextureHelper::createCubeMapTexture(const QImage &image, bool useTrilinearFiltering) { @@ -184,11 +185,11 @@ GLuint TextureHelper::createSelectionTexture(const QSize &size, GLuint &frameBuf // glGetError docs advise to call glGetError in loop to clear all error flags while (status) status = glGetError(); -#if !defined(QT_OPENGL_ES_2) - glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, size.width(), size.height()); -#else - glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, size.width(), size.height()); -#endif + if (Utils::isOpenGLES()) + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, size.width(), size.height()); + else + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, size.width(), size.height()); + status = glGetError(); if (status) { qCritical() << "Selection texture render buffer creation failed:" << status; @@ -271,59 +272,64 @@ GLuint TextureHelper::createGradientTexture(const QLinearGradient &gradient) return create2DTexture(image, false, true, false, true); } -#if !defined(QT_OPENGL_ES_2) GLuint TextureHelper::createDepthTexture(const QSize &size, GLuint textureSize) { - GLuint depthtextureid; - - // Create depth texture for the shadow mapping - glGenTextures(1, &depthtextureid); - glBindTexture(GL_TEXTURE_2D, depthtextureid); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB); - glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, size.width() * textureSize, - size.height() * textureSize, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL); - glBindTexture(GL_TEXTURE_2D, 0); - + GLuint depthtextureid = 0; +#if defined(QT_OPENGL_ES_2) + Q_UNUSED(size) + Q_UNUSED(textureSize) +#else + if (!Utils::isOpenGLES()) { + // Create depth texture for the shadow mapping + glGenTextures(1, &depthtextureid); + glBindTexture(GL_TEXTURE_2D, depthtextureid); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB); + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, size.width() * textureSize, + size.height() * textureSize, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL); + glBindTexture(GL_TEXTURE_2D, 0); + } +#endif return depthtextureid; } -#endif -#if !defined(QT_OPENGL_ES_2) GLuint TextureHelper::createDepthTextureFrameBuffer(const QSize &size, GLuint &frameBuffer, GLuint textureSize) { GLuint depthtextureid = createDepthTexture(size, textureSize); +#if defined(QT_OPENGL_ES_2) + Q_UNUSED(frameBuffer) +#else + if (!Utils::isOpenGLES()) { + // Create frame buffer + if (!frameBuffer) + glGenFramebuffers(1, &frameBuffer); + glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer); + + // Attach texture to depth attachment + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthtextureid, 0); + + m_openGlFunctions_2_1->glDrawBuffer(GL_NONE); + m_openGlFunctions_2_1->glReadBuffer(GL_NONE); + + // Verify that the frame buffer is complete + GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); + if (status != GL_FRAMEBUFFER_COMPLETE) { + qCritical() << "Depth texture frame buffer creation failed" << status; + glDeleteTextures(1, &depthtextureid); + depthtextureid = 0; + } - // Create frame buffer - if (!frameBuffer) - glGenFramebuffers(1, &frameBuffer); - glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer); - - // Attach texture to depth attachment - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthtextureid, 0); - - glDrawBuffer(GL_NONE); - glReadBuffer(GL_NONE); - - // Verify that the frame buffer is complete - GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); - if (status != GL_FRAMEBUFFER_COMPLETE) { - qCritical() << "Depth texture frame buffer creation failed" << status; - glDeleteTextures(1, &depthtextureid); - depthtextureid = 0; + // Restore the default framebuffer + glBindFramebuffer(GL_FRAMEBUFFER, 0); } - - // Restore the default framebuffer - glBindFramebuffer(GL_FRAMEBUFFER, 0); - +#endif return depthtextureid; } -#endif void TextureHelper::deleteTexture(GLuint *texture) { -- cgit v1.2.3