From 6cdceede825fd215a9addd191efd217919a3074a Mon Sep 17 00:00:00 2001 From: Paul Lemire Date: Tue, 3 Feb 2015 11:51:42 +0100 Subject: materials-cpp updated to use new Texture API Change-Id: I36d48c6eba94d20c7accc76e684f8ba36f2a53cc Reviewed-by: Sean Harmer --- examples/materials-cpp/barrel.cpp | 32 +++++++------------ examples/materials-cpp/barrel.h | 3 ++ examples/materials-cpp/houseplant.cpp | 60 ++++++++++++----------------------- examples/materials-cpp/houseplant.h | 8 +++++ examples/materials-cpp/main.cpp | 45 ++++++++++---------------- 5 files changed, 60 insertions(+), 88 deletions(-) (limited to 'examples') diff --git a/examples/materials-cpp/barrel.cpp b/examples/materials-cpp/barrel.cpp index 1f2bba90d..300764723 100644 --- a/examples/materials-cpp/barrel.cpp +++ b/examples/materials-cpp/barrel.cpp @@ -71,9 +71,17 @@ Barrel::Barrel(Qt3D::QNode *parent) , m_diffuseTexture(m_material->diffuse()) , m_normalTexture(m_material->normal()) , m_specularTexture(m_material->specular()) + , m_diffuseTextureImage(new Qt3D::QTextureImage()) + , m_normalTextureImage(new Qt3D::QTextureImage()) + , m_specularTextureImage(new Qt3D::QTextureImage()) { mesh()->setSource(QUrl(QStringLiteral("qrc:/assets/metalbarrel/metal_barrel.obj"))); scaleTransform()->setScale(0.03f); + + m_diffuseTexture->addTextureImage(m_diffuseTextureImage); + m_normalTexture->addTextureImage(m_normalTextureImage); + m_specularTexture->addTextureImage(m_specularTextureImage); + setNormalTextureSource(); setDiffuseTextureSource(); setSpecularTextureSource(); @@ -137,33 +145,15 @@ float Barrel::shininess() const void Barrel::setNormalTextureSource() { - QImage img; - if (img.load(QStringLiteral(":/assets/metalbarrel/normal_") + bumpsName[m_bumps] + QStringLiteral(".webp"))) { - m_normalTexture->setFormat(img.hasAlphaChannel() ? - Qt3D::QAbstractTextureProvider::RGBA8_UNorm : - Qt3D::QAbstractTextureProvider::RGB8_UNorm); - m_normalTexture->setFromQImage(img); - } + m_normalTextureImage->setSource(QUrl(QStringLiteral("qrc:/assets/metalbarrel/normal_") + bumpsName[m_bumps] + QStringLiteral(".webp"))); } void Barrel::setDiffuseTextureSource() { - QImage img; - if (img.load(QStringLiteral(":/assets/metalbarrel/diffus_") + diffuseColorsName[m_diffuseColor] + QStringLiteral(".webp"))) { - m_diffuseTexture->setFormat(img.hasAlphaChannel() ? - Qt3D::QAbstractTextureProvider::RGBA8_UNorm : - Qt3D::QAbstractTextureProvider::RGB8_UNorm); - m_diffuseTexture->setFromQImage(img); - } + m_diffuseTextureImage->setSource(QUrl(QStringLiteral("qrc:/assets/metalbarrel/diffus_") + diffuseColorsName[m_diffuseColor] + QStringLiteral(".webp"))); } void Barrel::setSpecularTextureSource() { - QImage img; - if (img.load(QStringLiteral(":/assets/metalbarrel/specular") + specularColorsName[m_specularColor] + QStringLiteral(".webp"))) { - m_specularTexture->setFormat(img.hasAlphaChannel() ? - Qt3D::QAbstractTextureProvider::RGBA8_UNorm : - Qt3D::QAbstractTextureProvider::RGB8_UNorm); - m_specularTexture->setFromQImage(img); - } + m_specularTextureImage->setSource(QUrl(QStringLiteral("qrc:/assets/metalbarrel/specular") + specularColorsName[m_specularColor] + QStringLiteral(".webp"))); } diff --git a/examples/materials-cpp/barrel.h b/examples/materials-cpp/barrel.h index 11704970a..ce3098d93 100644 --- a/examples/materials-cpp/barrel.h +++ b/examples/materials-cpp/barrel.h @@ -91,6 +91,9 @@ private: Qt3D::QAbstractTextureProvider *m_diffuseTexture; Qt3D::QAbstractTextureProvider *m_normalTexture; Qt3D::QAbstractTextureProvider *m_specularTexture; + Qt3D::QTextureImage *m_diffuseTextureImage; + Qt3D::QTextureImage *m_normalTextureImage; + Qt3D::QTextureImage *m_specularTextureImage; void setNormalTextureSource(); void setDiffuseTextureSource(); diff --git a/examples/materials-cpp/houseplant.cpp b/examples/materials-cpp/houseplant.cpp index 7369dd309..a89cceb40 100644 --- a/examples/materials-cpp/houseplant.cpp +++ b/examples/materials-cpp/houseplant.cpp @@ -66,6 +66,13 @@ HousePlant::HousePlant(Qt3D::QNode *parent) , m_potMaterial(new Qt3D::QNormalDiffuseMapMaterial()) , m_plantMaterial(new Qt3D::QNormalDiffuseMapAlphaMaterial()) , m_coverMaterial(new Qt3D::QNormalDiffuseMapMaterial()) + , m_potImage(new Qt3D::QTextureImage()) + + , m_potNormalImage(new Qt3D::QTextureImage()) + , m_plantImage(new Qt3D::QTextureImage()) + , m_plantNormalImage(new Qt3D::QTextureImage()) + , m_coverImage(new Qt3D::QTextureImage()) + , m_coverNormalImage(new Qt3D::QTextureImage()) , m_plantType(Bamboo) , m_potShape(Cross) { @@ -74,35 +81,20 @@ HousePlant::HousePlant(Qt3D::QNode *parent) m_plant->addComponent(m_plantMaterial); m_cover->addComponent(m_coverMaterial); + m_potMaterial->diffuse()->addTextureImage(m_potImage); + m_potMaterial->normal()->addTextureImage(m_potNormalImage); + m_plantMaterial->diffuse()->addTextureImage(m_plantImage); + m_plantMaterial->normal()->addTextureImage(m_plantNormalImage); + m_coverMaterial->diffuse()->addTextureImage(m_coverImage); + m_coverMaterial->normal()->addTextureImage(m_coverNormalImage); + updatePlantType(); updatePotShape(); - QImage img; - - if (img.load(QStringLiteral(":/assets/houseplants/cover.webp"))) { - m_coverMaterial->diffuse()->setFormat(img.hasAlphaChannel() ? - Qt3D::QAbstractTextureProvider::RGBA8_UNorm : - Qt3D::QAbstractTextureProvider::RGB8_UNorm); - m_coverMaterial->diffuse()->setFromQImage(img); - } - if (img.load(QStringLiteral(":/assets/houseplants/cover_normal.webp"))) { - m_coverMaterial->normal()->setFormat(img.hasAlphaChannel() ? - Qt3D::QAbstractTextureProvider::RGBA8_UNorm : - Qt3D::QAbstractTextureProvider::RGB8_UNorm); - m_coverMaterial->normal()->setFromQImage(img); - } - if (img.load(QStringLiteral(":/assets/houseplants/pot.webp"))) { - m_potMaterial->diffuse()->setFormat(img.hasAlphaChannel() ? - Qt3D::QAbstractTextureProvider::RGBA8_UNorm : - Qt3D::QAbstractTextureProvider::RGB8_UNorm); - m_potMaterial->diffuse()->setFromQImage(img); - } - if (img.load(QStringLiteral(":/assets/houseplants/pot_normal.webp"))) { - m_potMaterial->normal()->setFormat(img.hasAlphaChannel() ? - Qt3D::QAbstractTextureProvider::RGBA8_UNorm : - Qt3D::QAbstractTextureProvider::RGB8_UNorm); - m_potMaterial->normal()->setFromQImage(img); - } + m_coverImage->setSource(QUrl(QStringLiteral("qrc:/assets/houseplants/cover.webp"))); + m_coverNormalImage->setSource(QUrl(QStringLiteral("qrc:/assets/houseplants/cover_normal.webp"))); + m_potImage->setSource(QUrl(QStringLiteral("qrc:/assets/houseplants/pot.webp"))); + m_potNormalImage->setSource(QUrl(QStringLiteral("qrc:/assets/houseplants/pot_normal.webp"))); m_potMaterial->setShininess(10.0f); m_potMaterial->setSpecular(QColor::fromRgbF(0.75f, 0.75f, 0.75f, 1.0f)); @@ -192,18 +184,8 @@ void HousePlant::updatePotShape() void HousePlant::updatePlantType() { m_plant->mesh()->setSource(QUrl(QStringLiteral("qrc:/assets/houseplants/") + potNames[m_potShape] + QStringLiteral("-") + plantNames[m_plantType] + QStringLiteral(".obj"))); - QImage img; - if (img.load(QStringLiteral(":/assets/houseplants/") + plantNames[m_plantType] + QStringLiteral(".webp"))) { - m_plantMaterial->diffuse()->setFormat(img.hasAlphaChannel() ? - Qt3D::QAbstractTextureProvider::RGBA8_UNorm : - Qt3D::QAbstractTextureProvider::RGB8_UNorm); - m_plantMaterial->diffuse()->setFromQImage(img); - } - if (img.load(QStringLiteral(":/assets/houseplants/") + plantNames[m_plantType] + QStringLiteral("_normal.webp"))) { - m_plantMaterial->normal()->setFormat(img.hasAlphaChannel() ? - Qt3D::QAbstractTextureProvider::RGBA8_UNorm : - Qt3D::QAbstractTextureProvider::RGB8_UNorm); - m_plantMaterial->normal()->setFromQImage(img); - } + + m_plantImage->setSource(QUrl(QStringLiteral("qrc:/assets/houseplants/") + plantNames[m_plantType] + QStringLiteral(".webp"))); + m_plantNormalImage->setSource(QUrl(QStringLiteral("qrc:/assets/houseplants/") + plantNames[m_plantType] + QStringLiteral("_normal.webp"))); } diff --git a/examples/materials-cpp/houseplant.h b/examples/materials-cpp/houseplant.h index a8e821691..78b53c5ec 100644 --- a/examples/materials-cpp/houseplant.h +++ b/examples/materials-cpp/houseplant.h @@ -46,6 +46,7 @@ #include #include #include +#include class HousePlant : public Qt3D::QEntity { @@ -94,6 +95,13 @@ private: Qt3D::QNormalDiffuseMapAlphaMaterial *m_plantMaterial; Qt3D::QNormalDiffuseMapMaterial *m_coverMaterial; + Qt3D::QTextureImage *m_potImage; + Qt3D::QTextureImage *m_potNormalImage; + Qt3D::QTextureImage *m_plantImage; + Qt3D::QTextureImage *m_plantNormalImage; + Qt3D::QTextureImage *m_coverImage; + Qt3D::QTextureImage *m_coverNormalImage; + Plant m_plantType; PotShape m_potShape; diff --git a/examples/materials-cpp/main.cpp b/examples/materials-cpp/main.cpp index 06ee463f3..f1a2bed66 100644 --- a/examples/materials-cpp/main.cpp +++ b/examples/materials-cpp/main.cpp @@ -53,6 +53,7 @@ #include #include #include +#include #include "planeentity.h" #include "rotatingtrefoilknot.h" @@ -107,28 +108,18 @@ int main(int argc, char* argv[]) normalDiffuseSpecularMapMaterial->setShininess(80.0f); normalDiffuseSpecularMapMaterial->setAmbient(QColor::fromRgbF(0.2f, 0.2f, 0.2f, 1.0f)); - QImage img; - // Providing image loaders for textures would make sense - if (img.load(QStringLiteral(":/assets/textures/pattern_09/diffuse.webp"))) { - normalDiffuseSpecularMapMaterial->diffuse()->setFormat(img.hasAlphaChannel() ? - Qt3D::QAbstractTextureProvider::RGBA8_UNorm : - Qt3D::QAbstractTextureProvider::RGB8_UNorm); - normalDiffuseSpecularMapMaterial->diffuse()->setFromQImage(img); - } - - if (img.load(QStringLiteral(":/assets/textures/pattern_09/specular.webp"))) { - normalDiffuseSpecularMapMaterial->specular()->setFormat(img.hasAlphaChannel() ? - Qt3D::QAbstractTextureProvider::RGBA8_UNorm : - Qt3D::QAbstractTextureProvider::RGB8_UNorm); - normalDiffuseSpecularMapMaterial->specular()->setFromQImage(img); - } - - if (img.load(QStringLiteral(":/assets/textures/pattern_09/normal.webp"))) { - normalDiffuseSpecularMapMaterial->normal()->setFormat(img.hasAlphaChannel() ? - Qt3D::QAbstractTextureProvider::RGBA8_UNorm : - Qt3D::QAbstractTextureProvider::RGB8_UNorm); - normalDiffuseSpecularMapMaterial->normal()->setFromQImage(img); - } + Qt3D::QTextureImage *diffuseImage = new Qt3D::QTextureImage(); + diffuseImage->setSource(QUrl(QStringLiteral("qrc:/assets/textures/pattern_09/diffuse.webp"))); + normalDiffuseSpecularMapMaterial->diffuse()->addTextureImage(diffuseImage); + + Qt3D::QTextureImage *specularImage = new Qt3D::QTextureImage(); + specularImage->setSource(QUrl(QStringLiteral("qrc:/assets/textures/pattern_09/specular.webp"))); + normalDiffuseSpecularMapMaterial->specular()->addTextureImage(specularImage); + + Qt3D::QTextureImage *normalImage = new Qt3D::QTextureImage(); + normalImage->setSource(QUrl((QStringLiteral("qrc:/assets/textures/pattern_09/normal.webp")))); + normalDiffuseSpecularMapMaterial->normal()->addTextureImage(normalImage); + planeEntity->addComponent(normalDiffuseSpecularMapMaterial); // Chest @@ -139,12 +130,10 @@ int main(int argc, char* argv[]) diffuseMapMaterial->setSpecular(QColor::fromRgbF(0.2f, 0.2f, 0.2f, 1.0f)); diffuseMapMaterial->setShininess(2.0f); - if (img.load(QStringLiteral(":/assets/chest/diffuse.webp"))) { - diffuseMapMaterial->diffuse()->setFormat(img.hasAlphaChannel() ? - Qt3D::QAbstractTextureProvider::RGBA8_UNorm : - Qt3D::QAbstractTextureProvider::RGB8_UNorm); - diffuseMapMaterial->diffuse()->setFromQImage(img); - } + Qt3D::QTextureImage *chestDiffuseImage = new Qt3D::QTextureImage(); + chestDiffuseImage->setSource(QUrl(QStringLiteral("qrc:/assets/chest/diffuse.webp"))); + diffuseMapMaterial->diffuse()->addTextureImage(chestDiffuseImage); + chest->addComponent(diffuseMapMaterial); -- cgit v1.2.3