diff options
author | Tomi Korpipää <tomi.korpipaa@digia.com> | 2013-05-28 08:07:18 +0300 |
---|---|---|
committer | Tomi Korpipää <tomi.korpipaa@digia.com> | 2013-05-28 08:13:54 +0300 |
commit | 79a68aaa027b57e9d845764aa692a4a59c8fa47a (patch) | |
tree | 53d3f40d9aff0bb9dd697e20b13d1d2e3e1b90dc /src/datavis3d | |
parent | 1be87eb49f3ad5bdf79b869cc659e20828ab5917 (diff) |
Android texture fixes
Adjust all texture sizes to dimensions in powers of two.
Change-Id: I6f0385a435522e83789073569d6d2cfbd84baf56
Reviewed-by: Miikka Heikkinen <miikka.heikkinen@digia.com>
Diffstat (limited to 'src/datavis3d')
-rw-r--r-- | src/datavis3d/utils/texturehelper.cpp | 23 | ||||
-rw-r--r-- | src/datavis3d/utils/utils.cpp | 69 | ||||
-rw-r--r-- | src/datavis3d/utils/utils_p.h | 1 |
3 files changed, 55 insertions, 38 deletions
diff --git a/src/datavis3d/utils/texturehelper.cpp b/src/datavis3d/utils/texturehelper.cpp index 917a3842..5f46c700 100644 --- a/src/datavis3d/utils/texturehelper.cpp +++ b/src/datavis3d/utils/texturehelper.cpp @@ -40,6 +40,7 @@ ****************************************************************************/ #include "texturehelper_p.h" +#include "utils_p.h" #include <QImage> @@ -61,16 +62,32 @@ GLuint TextureHelper::create2DTexture(const QImage &image, bool useTrilinearFilt if (image.isNull()) return 0; + QImage texImage; + +#if defined(Q_OS_ANDROID) + GLuint temp; + //qDebug() << "old size" << image.size(); + GLuint imageWidth = Utils::getNearestPowerOfTwo(image.width(), temp); + //qDebug() << "new width" << imageWidth << "padding" << temp; + GLuint imageHeight = Utils::getNearestPowerOfTwo(image.height(), temp); + //qDebug() << "new height" << imageHeight << "padding" << temp; + texImage = image.scaled(imageWidth, imageHeight, Qt::IgnoreAspectRatio, + Qt::SmoothTransformation); + //qDebug() << "new size" << texImage.size(); +#else + texImage = image; +#endif + GLuint textureId; glGenTextures(1, &textureId); glBindTexture(GL_TEXTURE_2D, textureId); if (convert) { - QImage glTexture = convertToGLFormat(image); + QImage glTexture = convertToGLFormat(texImage); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, glTexture.width(), glTexture.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, glTexture.bits()); } else { - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width(), image.height(), - 0, GL_RGBA, GL_UNSIGNED_BYTE, image.bits()); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texImage.width(), texImage.height(), + 0, GL_RGBA, GL_UNSIGNED_BYTE, texImage.bits()); } if (useTrilinearFiltering) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); diff --git a/src/datavis3d/utils/utils.cpp b/src/datavis3d/utils/utils.cpp index d4a4e089..56d1e2b1 100644 --- a/src/datavis3d/utils/utils.cpp +++ b/src/datavis3d/utils/utils.cpp @@ -53,6 +53,17 @@ QTCOMMERCIALDATAVIS3D_BEGIN_NAMESPACE +#define NUM_IN_POWER(y, x) for (;y<x;y<<=1) +#define MIN_POWER 32 + +GLuint Utils::getNearestPowerOfTwo(GLuint value, GLuint &padding) +{ + GLuint powOfTwoValue = MIN_POWER; + NUM_IN_POWER(powOfTwoValue, value); + padding = powOfTwoValue - value; + return powOfTwoValue; +} + QVector3D Utils::vectorFromColor(const QColor &color) { return QVector3D(color.redF(), color.greenF(), color.blueF()); @@ -147,29 +158,17 @@ QImage Utils::printTextToImage(const QFont &font, const QString &text, const QCo #if defined(Q_OS_ANDROID) // Android can't handle textures with dimensions not in power of 2. Resize labels accordingly. - bool widthPadded = false; - bool heightPadded = false; - - // TODO: Add some padding before power-of-2 -padding to power of 2 to avoid labels with no "breathing-room"? - labelSize = QSize(valueStrWidth, valueStrHeight); + // Add some padding before converting to power of two to avoid too tight fit + GLuint prePadding = 10; + labelSize = QSize(valueStrWidth + prePadding, valueStrHeight + prePadding); //qDebug() << "label size before padding" << labelSize; - - for (int i = 5;; i++) { - GLint newDimension = qPow(2, i); - if (!heightPadded && newDimension >= labelSize.height()) { - paddingHeight = (newDimension - labelSize.height()) / 2; - labelSize.setHeight(newDimension); - heightPadded = true; - } - if (!widthPadded && newDimension >= labelSize.width()) { - paddingWidth = (newDimension - labelSize.width()) / 2; - labelSize.setWidth(newDimension); - widthPadded = true; - } - if (widthPadded && heightPadded) - break; - } - //qDebug() << "label size after padding" << labelSize; + labelSize.setWidth(getNearestPowerOfTwo(labelSize.width(), paddingWidth)); + labelSize.setHeight(getNearestPowerOfTwo(labelSize.height(), paddingHeight)); + paddingWidth += prePadding; + paddingHeight += prePadding; + paddingWidth /= 2; + paddingHeight /= 2; + //qDebug() << "label size after padding" << labelSize << paddingWidth << paddingHeight; #else if (TransparencyNoBackground == transparency) labelSize = QSize(valueStrWidth, valueStrHeight); @@ -229,26 +228,26 @@ QVector3D Utils::getSelection(QPoint mousepos, int height) { QVector3D selectedColor; -//#if defined(QT_OPENGL_ES_2) + //#if defined(QT_OPENGL_ES_2) // This is the only one that works with ANGLE (ES 2.0) // Item count will be limited to 256*256*256 GLubyte pixel[4]; glReadPixels(mousepos.x(), height - mousepos.y(), 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, (void *)pixel); //qDebug() << "rgba" << pixel[0] << pixel[1] << pixel[2];// << pixel[3]; -//#else -// // These work with desktop OpenGL -// // They offer a lot higher possible object count and a possibility to use object ids -// GLuint pixel[3]; -// glReadPixels(mousepos.x(), height - mousepos.y(), 1, 1, -// GL_RGB, GL_UNSIGNED_INT, (void *)pixel); -// qDebug() << "rgba" << pixel[0] << pixel[1] << pixel[2];// << pixel[3]; + //#else + //// These work with desktop OpenGL + //// They offer a lot higher possible object count and a possibility to use object ids + //GLuint pixel[3]; + //glReadPixels(mousepos.x(), height - mousepos.y(), 1, 1, + // GL_RGB, GL_UNSIGNED_INT, (void *)pixel); + //qDebug() << "rgba" << pixel[0] << pixel[1] << pixel[2];// << pixel[3]; -// GLfloat pixel3[3]; -// glReadPixels(mousepos.x(), height - mousepos.y(), 1, 1, -// GL_RGB, GL_FLOAT, (void *)pixel3); -// qDebug() << "rgba" << pixel3[0] << pixel3[1] << pixel3[2];// << pixel[3]; -//#endif + //GLfloat pixel3[3]; + //glReadPixels(mousepos.x(), height - mousepos.y(), 1, 1, + // GL_RGB, GL_FLOAT, (void *)pixel3); + //qDebug() << "rgba" << pixel3[0] << pixel3[1] << pixel3[2];// << pixel[3]; + //#endif selectedColor = QVector3D(pixel[0], pixel[1], pixel[2]); //qDebug() << selectedColor; diff --git a/src/datavis3d/utils/utils_p.h b/src/datavis3d/utils/utils_p.h index 1668ac40..b3487a24 100644 --- a/src/datavis3d/utils/utils_p.h +++ b/src/datavis3d/utils/utils_p.h @@ -68,6 +68,7 @@ QTCOMMERCIALDATAVIS3D_BEGIN_NAMESPACE class Utils { public: + static GLuint getNearestPowerOfTwo(GLuint value, GLuint &padding); static QVector3D vectorFromColor(const QColor &color); static void printText(QPainter *painter, const QString &text, const QSize &position, bool absoluteCoords = true, qreal rotation = 0, qreal scale = 1.0f); |