diff options
author | Paul Lemire <paul.lemire@kdab.com> | 2016-04-27 15:49:43 +0200 |
---|---|---|
committer | Paul Lemire <paul.lemire@kdab.com> | 2016-05-20 11:12:43 +0000 |
commit | 99c70270a1f68dfb67c707ac996fb18e94cce1c8 (patch) | |
tree | 9984737b4a8d20bcba31497584a1ccc92af7586e /src | |
parent | 56e03ad1ab1e2971d5e406608d78352ebebc2bea (diff) |
Add a QTextureSourceGenerator
This will take care of generating a texture from a source file
Change-Id: I11a9b1686d07aceea2211981576d00819020af90
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/render/texture/qtexture.cpp | 56 | ||||
-rw-r--r-- | src/render/texture/qtexture_p.h | 13 |
2 files changed, 69 insertions, 0 deletions
diff --git a/src/render/texture/qtexture.cpp b/src/render/texture/qtexture.cpp index 737f74f95..40b5c59c3 100644 --- a/src/render/texture/qtexture.cpp +++ b/src/render/texture/qtexture.cpp @@ -40,6 +40,7 @@ #include "qtextureimage.h" #include "qabstracttextureimage.h" #include "qtextureimage_p.h" +#include "qtexturedata.h" #include "qtexture.h" #include "qtexture_p.h" @@ -299,6 +300,61 @@ void QTextureLoader::setSource(const QUrl& source) } } +bool QTextureFromSourceGenerator::operator ==(const QTextureGenerator &other) const +{ + const QTextureFromSourceGenerator *otherFunctor = functor_cast<QTextureFromSourceGenerator>(&other); + return (otherFunctor != Q_NULLPTR && otherFunctor->m_url == m_url); + +} + +// Note: Maybe this should return a struct containing information such as +// the format, number of layers .... +// This would also give more flexibility for the future +QTextureDataPtr QTextureFromSourceGenerator::operator ()() +{ + QTextureDataPtr generatedData = QTextureDataPtr::create(); + // TO DO: Make it handle more formats and formats with nested images + + m_status = QAbstractTexture::Loading; + + if (m_url.isLocalFile() || m_url.scheme() == QStringLiteral("qrc")) { + QString source = Qt3DRender::QUrlHelper::urlToLocalFileOrQrc(m_url); + QVector<QTextureImageDataPtr> images; + QTextureImageDataPtr dataPtr = QTextureImageDataPtr::create(); + // TO DO: Ideally a setter shouldn't return a value + // Separate that into isCompressedFile and setCompressedFile + // Set compressed fill the QTextImageData if possible, + // returns false if it failed or is not a compressed format + + // TO DO: Make separateSetCompressed from the loading of a compressed file + // Make such loader set the property formats, targets and sizes in the generatedData + + if (!dataPtr->setCompressedFile(source)) { + // Try to load using QImage + QImage img; + if (img.load(source)) + dataPtr->setImage(img); + generatedData->setTarget(QAbstractTexture::Target2D); + generatedData->setWidth(img.width()); + generatedData->setHeight(img.height()); + generatedData->setDepth(1); + } + + if (dataPtr->data().length() > 0) { + m_status = QAbstractTexture::Ready; + generatedData->addImageData(dataPtr); + images.push_back(dataPtr); + } else { + m_status = QAbstractTexture::Error; + qWarning() << "Failed to load image : " << source; + } + } else { + m_status = QAbstractTexture::Error; + qWarning() << "implement loading from remote URLs"; + } + return generatedData; +} + } // namespace Qt3DRender QT_END_NAMESPACE diff --git a/src/render/texture/qtexture_p.h b/src/render/texture/qtexture_p.h index 754bc206e..b4faa9cc1 100644 --- a/src/render/texture/qtexture_p.h +++ b/src/render/texture/qtexture_p.h @@ -52,6 +52,7 @@ // #include <Qt3DRender/private/qabstracttexture_p.h> +#include <Qt3DRender/qtexturegenerator.h> QT_BEGIN_NAMESPACE @@ -65,6 +66,18 @@ public: QUrl m_source; }; +class QTextureFromSourceGenerator : public QTextureGenerator +{ +public: + explicit QTextureFromSourceGenerator(const QUrl &url); + QTextureDataPtr operator ()() Q_DECL_OVERRIDE; + bool operator ==(const QTextureGenerator &other) const Q_DECL_OVERRIDE; + inline QAbstractTexture::Status status() const { return m_status; } +private: + QUrl m_url; + QAbstractTexture::Status m_status; +}; + } // namespace Qt3DRender QT_END_NAMESPACE |