diff options
61 files changed, 4687 insertions, 38 deletions
diff --git a/examples/examples.pro b/examples/examples.pro index b94e16331..359b38eab 100644 --- a/examples/examples.pro +++ b/examples/examples.pro @@ -23,7 +23,8 @@ SUBDIRS += \ materials \ keyboardinput-qml \ loader-qml \ - wave + wave \ + materials-cpp # TODO Port the old examples to new APIs #SUBDIRS += qt3d diff --git a/examples/materials-cpp/barrel.cpp b/examples/materials-cpp/barrel.cpp new file mode 100644 index 000000000..3af8bac89 --- /dev/null +++ b/examples/materials-cpp/barrel.cpp @@ -0,0 +1,169 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "barrel.h" + +const char *diffuseColorsName[] = { + "red", + "blue", + "green", + "rust", + "stainless_steel" +}; + +const char *specularColorsName[] = { + "_rust", + "_stainless_steel", + "" +}; + +const char *bumpsName[] = { + "no_bumps", + "soft_bumps", + "middle_bumps", + "hard_bumps" +}; + +Barrel::Barrel(Qt3D::QNode *parent) + : RenderableEntity(parent) + , m_bumps(NoBumps) + , m_diffuseColor(Red) + , m_specularColor(None) + , m_material(new Qt3D::QNormalDiffuseSpecularMapMaterial()) + , m_diffuseTexture(m_material->diffuse()) + , m_normalTexture(m_material->normal()) + , m_specularTexture(m_material->specular()) +{ + mesh()->setSource(QUrl(QStringLiteral("qrc:/assets/metalbarrel/metal_barrel.obj"))); + scaleTransform()->setScale(0.03f); + setNormalTextureSource(); + setDiffuseTextureSource(); + setSpecularTextureSource(); + m_material->setShininess(10.0f); + addComponent(m_material); +} + +Barrel::~Barrel() +{ +} + +void Barrel::setDiffuse(Barrel::DiffuseColor diffuse) +{ + if (diffuse != m_diffuseColor) { + m_diffuseColor = diffuse; + setDiffuseTextureSource(); + } +} + +void Barrel::setSpecular(Barrel::SpecularColor specular) +{ + if (specular != m_specularColor) { + m_specularColor = specular; + setSpecularTextureSource(); + } +} + +void Barrel::setBumps(Barrel::Bumps bumps) +{ + if (bumps != m_bumps) { + m_bumps = bumps; + setNormalTextureSource(); + } +} + +void Barrel::setShininess(float shininess) +{ + if (shininess != m_material->shininess()) + m_material->setShininess(shininess); +} + +Barrel::DiffuseColor Barrel::diffuse() const +{ + return m_diffuseColor; +} + +Barrel::SpecularColor Barrel::specular() const +{ + return m_specularColor; +} + +Barrel::Bumps Barrel::bumps() const +{ + return m_bumps; +} + +float Barrel::shininess() const +{ + return m_material->shininess(); +} + +void Barrel::setNormalTextureSource() +{ + QImage img; + if (img.load(QStringLiteral(":/assets/metalbarrel/normal_") + bumpsName[m_bumps] + QStringLiteral(".webp"))) { + m_normalTexture->setFormat(img.hasAlphaChannel() ? + Qt3D::QTexture::RGBA8_UNorm : + Qt3D::QTexture::RGB8_UNorm); + m_normalTexture->setFromQImage(img); + } +} + +void Barrel::setDiffuseTextureSource() +{ + QImage img; + if (img.load(QStringLiteral(":/assets/metalbarrel/diffus_") + diffuseColorsName[m_diffuseColor] + QStringLiteral(".webp"))) { + m_diffuseTexture->setFormat(img.hasAlphaChannel() ? + Qt3D::QTexture::RGBA8_UNorm : + Qt3D::QTexture::RGB8_UNorm); + m_diffuseTexture->setFromQImage(img); + } +} + +void Barrel::setSpecularTextureSource() +{ + QImage img; + if (img.load(QStringLiteral(":/assets/metalbarrel/specular") + specularColorsName[m_specularColor] + QStringLiteral(".webp"))) { + m_specularTexture->setFormat(img.hasAlphaChannel() ? + Qt3D::QTexture::RGBA8_UNorm : + Qt3D::QTexture::RGB8_UNorm); + m_specularTexture->setFromQImage(img); + } +} diff --git a/examples/materials-cpp/barrel.h b/examples/materials-cpp/barrel.h new file mode 100644 index 000000000..6fcdb434b --- /dev/null +++ b/examples/materials-cpp/barrel.h @@ -0,0 +1,101 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef BARREL_H +#define BARREL_H + +#include <Qt3DRenderer/QNormalDiffuseSpecularMapMaterial> +#include <Qt3DRenderer/QTexture> +#include "renderableentity.h" + +class Barrel : public RenderableEntity +{ +public: + Barrel(Qt3D::QNode *parent = 0); + ~Barrel(); + + enum DiffuseColor { + Red = 0, + Blue, + Green, + RustDiffuse, + StainlessSteelDiffuse + }; + + enum SpecularColor { + RustSpecular = 0, + StainlessSteelSpecular, + None + }; + + enum Bumps { + NoBumps = 0, + SoftBumps, + MiddleBumps, + HardBumps + }; + + void setDiffuse(DiffuseColor diffuse); + void setSpecular(SpecularColor specular); + void setBumps(Bumps bumps); + void setShininess(float shininess); + + DiffuseColor diffuse() const; + SpecularColor specular() const; + Bumps bumps() const; + float shininess() const; + +private: + Bumps m_bumps; + DiffuseColor m_diffuseColor; + SpecularColor m_specularColor; + Qt3D::QNormalDiffuseSpecularMapMaterial *m_material; + Qt3D::QTexture *m_diffuseTexture; + Qt3D::QTexture *m_normalTexture; + Qt3D::QTexture *m_specularTexture; + + void setNormalTextureSource(); + void setDiffuseTextureSource(); + void setSpecularTextureSource(); + +}; + +#endif // BARREL_H diff --git a/examples/materials-cpp/forwardrenderer.cpp b/examples/materials-cpp/forwardrenderer.cpp new file mode 100644 index 000000000..0731f2904 --- /dev/null +++ b/examples/materials-cpp/forwardrenderer.cpp @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "forwardrenderer.h" + +ForwardRenderer::ForwardRenderer(Qt3D::QNode *parent) + : Qt3D::QFrameGraph(parent) + , m_viewport(new Qt3D::QViewport()) + , m_cameraSelector(new Qt3D::QCameraSelector()) + , m_clearBuffer(new Qt3D::QClearBuffer()) +{ + m_viewport->setRect(QRectF(0.0f, 0.0f, 1.0f, 1.0f)); + m_viewport->setClearColor(Qt::white); + m_clearBuffer->setBuffers(Qt3D::QClearBuffer::ColorDepthBuffer); + m_cameraSelector->setParent(m_viewport); + m_clearBuffer->setParent(m_cameraSelector); + setActiveFrameGraph(m_viewport); +} + +ForwardRenderer::~ForwardRenderer() +{ +} + +void ForwardRenderer::setCamera(Qt3D::QEntity *camera) +{ + m_cameraSelector->setCamera(camera); +} diff --git a/examples/materials-cpp/forwardrenderer.h b/examples/materials-cpp/forwardrenderer.h new file mode 100644 index 000000000..b45070416 --- /dev/null +++ b/examples/materials-cpp/forwardrenderer.h @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef FORWARDRENDERER_H +#define FORWARDRENDERER_H + +#include <Qt3DRenderer/QFrameGraph> +#include <Qt3DRenderer/QViewport> +#include <Qt3DRenderer/QCameraSelector> +#include <Qt3DRenderer/QClearBuffer> + +class ForwardRenderer : public Qt3D::QFrameGraph +{ +public: + ForwardRenderer(Qt3D::QNode *parent); + ~ForwardRenderer(); + + void setCamera(Qt3D::QEntity *camera); + +private: + Qt3D::QViewport *m_viewport; + Qt3D::QCameraSelector *m_cameraSelector; + Qt3D::QClearBuffer *m_clearBuffer; +}; + +#endif // FORWARDRENDERER_H diff --git a/examples/materials-cpp/houseplant.cpp b/examples/materials-cpp/houseplant.cpp new file mode 100644 index 000000000..5b6b66857 --- /dev/null +++ b/examples/materials-cpp/houseplant.cpp @@ -0,0 +1,209 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "houseplant.h" +#include <Qt3DRenderer/QTexture> + +const char *potNames[] = { + "cross", + "square", + "triangle", + "sphere" +}; + +const char *plantNames[] = { + "bamboo", + "palm", + "pine", + "spikes", + "shrub" +}; + + +HousePlant::HousePlant(Qt3D::QNode *parent) + : Qt3D::QEntity(parent) + , m_pot(new RenderableEntity(this)) + , m_plant(new RenderableEntity(m_pot)) + , m_cover(new RenderableEntity(m_pot)) + , m_potMaterial(new Qt3D::QNormalDiffuseMapMaterial()) + , m_plantMaterial(new Qt3D::QNormalDiffuseMapAlphaMaterial()) + , m_coverMaterial(new Qt3D::QNormalDiffuseMapMaterial()) + , m_plantType(Bamboo) + , m_potShape(Cross) +{ + m_pot->scaleTransform()->setScale(0.03f); + m_pot->addComponent(m_potMaterial); + m_plant->addComponent(m_plantMaterial); + m_cover->addComponent(m_coverMaterial); + + updatePlantType(); + updatePotShape(); + + QImage img; + + if (img.load(QStringLiteral(":/assets/houseplants/cover.webp"))) { + m_coverMaterial->diffuse()->setFormat(img.hasAlphaChannel() ? + Qt3D::QTexture::RGBA8_UNorm : + Qt3D::QTexture::RGB8_UNorm); + m_coverMaterial->diffuse()->setFromQImage(img); + } + if (img.load(QStringLiteral(":/assets/houseplants/cover_normal.webp"))) { + m_coverMaterial->normal()->setFormat(img.hasAlphaChannel() ? + Qt3D::QTexture::RGBA8_UNorm : + Qt3D::QTexture::RGB8_UNorm); + m_coverMaterial->normal()->setFromQImage(img); + } + if (img.load(QStringLiteral(":/assets/houseplants/pot.webp"))) { + m_potMaterial->diffuse()->setFormat(img.hasAlphaChannel() ? + Qt3D::QTexture::RGBA8_UNorm : + Qt3D::QTexture::RGB8_UNorm); + m_potMaterial->diffuse()->setFromQImage(img); + } + if (img.load(QStringLiteral(":/assets/houseplants/pot_normal.webp"))) { + m_potMaterial->normal()->setFormat(img.hasAlphaChannel() ? + Qt3D::QTexture::RGBA8_UNorm : + Qt3D::QTexture::RGB8_UNorm); + m_potMaterial->normal()->setFromQImage(img); + } + + m_potMaterial->setShininess(10.0f); + m_potMaterial->setSpecular(QColor::fromRgbF(0.75f, 0.75f, 0.75f, 1.0f)); + + m_plantMaterial->setShininess(10.0f); + + m_coverMaterial->setSpecular(QColor::fromRgbF(0.05f, 0.05f, 0.05f, 1.0f)); + m_coverMaterial->setShininess(5.0f); +} + +HousePlant::~HousePlant() +{ +} + +void HousePlant::setPotShape(HousePlant::PotShape shape) +{ + if (shape != m_potShape) { + m_potShape = shape; + updatePotShape(); + } +} + +void HousePlant::setPlantType(HousePlant::Plant plant) +{ + if (plant != m_plantType) { + m_plantType = plant; + updatePlantType(); + } +} + +HousePlant::PotShape HousePlant::potShape() const +{ + return m_potShape; +} + +HousePlant::Plant HousePlant::plantType() const +{ + return m_plantType; +} + +void HousePlant::setX(float x) +{ + m_pot->translateTransform()->setDx(x); +} + +void HousePlant::setY(float y) +{ + m_pot->translateTransform()->setDy(y); +} + +void HousePlant::setZ(float z) +{ + m_pot->translateTransform()->setDz(z); +} + +void HousePlant::setScale(float scale) +{ + m_pot->scaleTransform()->setScale(scale); +} + +float HousePlant::x() const +{ + return m_pot->translateTransform()->dx(); +} + +float HousePlant::y() const +{ + return m_pot->translateTransform()->dy(); +} + +float HousePlant::z() const +{ + return m_pot->translateTransform()->dz(); +} + +float HousePlant::scale() const +{ + return m_pot->scaleTransform()->scale(); +} + +void HousePlant::updatePotShape() +{ + m_pot->mesh()->setSource(QUrl(QStringLiteral("qrc:/assets/houseplants/") + potNames[m_potShape] + QStringLiteral("-pot.obj"))); + m_plant->mesh()->setSource(QUrl(QStringLiteral("qrc:/assets/houseplants/") + potNames[m_potShape] + QStringLiteral("-") + plantNames[m_plantType] + QStringLiteral(".obj"))); +} + +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::QTexture::RGBA8_UNorm : + Qt3D::QTexture::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::QTexture::RGBA8_UNorm : + Qt3D::QTexture::RGB8_UNorm); + m_plantMaterial->normal()->setFromQImage(img); + } +} + diff --git a/examples/materials-cpp/houseplant.h b/examples/materials-cpp/houseplant.h new file mode 100644 index 000000000..a8e821691 --- /dev/null +++ b/examples/materials-cpp/houseplant.h @@ -0,0 +1,104 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef HOUSEPLANT_H +#define HOUSEPLANT_H + +#include "renderableentity.h" +#include <QEntity> +#include <QNormalDiffuseMapAlphaMaterial> +#include <QNormalDiffuseMapMaterial> + +class HousePlant : public Qt3D::QEntity +{ + Q_OBJECT +public: + explicit HousePlant(Qt3D::QNode *parent = 0); + ~HousePlant(); + + enum PotShape { + Cross = 0, + Square, + Triangle, + Sphere + }; + + enum Plant { + Bamboo = 0, + Palm, + Pine, + Spikes, + Shrub + }; + + void setPotShape(PotShape shape); + void setPlantType(Plant plant); + + PotShape potShape() const; + Plant plantType() const; + + void setX(float x); + void setY(float y); + void setZ(float z); + void setScale(float scale); + + float x() const; + float y() const; + float z() const; + float scale() const; + +private: + RenderableEntity *m_pot; + RenderableEntity *m_plant; + RenderableEntity *m_cover; + + Qt3D::QNormalDiffuseMapMaterial *m_potMaterial; + Qt3D::QNormalDiffuseMapAlphaMaterial *m_plantMaterial; + Qt3D::QNormalDiffuseMapMaterial *m_coverMaterial; + + Plant m_plantType; + PotShape m_potShape; + + void updatePotShape(); + void updatePlantType(); +}; + +#endif // HOUSEPLANT_H diff --git a/examples/materials-cpp/main.cpp b/examples/materials-cpp/main.cpp new file mode 100644 index 000000000..fe3ee426d --- /dev/null +++ b/examples/materials-cpp/main.cpp @@ -0,0 +1,213 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QGuiApplication> + +#include <Qt3DCore/QEntity> +#include <Qt3DCore/Window> +#include <Qt3DCore/QAspectEngine> +#include <Qt3DCore/QCamera> + +#include <Qt3DRenderer/QRenderAspect> +#include <Qt3DRenderer/QPhongMaterial> +#include <Qt3DRenderer/QDiffuseMapMaterial> + +#include "planeentity.h" +#include "forwardrenderer.h" +#include "rotatingtrefoilknot.h" +#include "barrel.h" +#include "exampleresources.h" +#include "houseplant.h" + +int main(int argc, char* argv[]) +{ + QGuiApplication app(argc, argv); + initializeAssetResources("../exampleresources/example-assets.qrb"); + + Qt3D::Window view; + Qt3D::QAspectEngine engine; + engine.registerAspect(new Qt3D::QRenderAspect()); + engine.initialize(); + QVariantMap data; + data.insert(QStringLiteral("surface"), QVariant::fromValue(static_cast<QSurface *>(&view))); + data.insert(QStringLiteral("window"), QVariant::fromValue(&view)); + engine.setData(data); + + // Scene Root + Qt3D::QEntity *sceneRoot = new Qt3D::QEntity(); + + // Scene Camera + Qt3D::QCamera *basicCamera = new Qt3D::QCamera(sceneRoot); + basicCamera->setProjectionType(Qt3D::QCameraLens::PerspectiveProjection); + basicCamera->setAspectRatio(view.width() / view.height()); + basicCamera->setUpVector(QVector3D(0.0f, 1.0f, 0.0f)); + basicCamera->setViewCenter(QVector3D(0.0f, 3.5f, 0.0f)); + basicCamera->setPosition(QVector3D(0.0f, 3.5f, 25.0f)); + // For camera controls + view.setCamera(basicCamera); + + // Forward Renderer FrameGraph + ForwardRenderer *forwardRenderer = new ForwardRenderer(sceneRoot); + forwardRenderer->setCamera(basicCamera); + sceneRoot->addComponent(forwardRenderer); + + // Scene floor + PlaneEntity *planeEntity = new PlaneEntity(sceneRoot); + planeEntity->mesh()->setHeight(100.0f); + planeEntity->mesh()->setWidth(100.0f); + planeEntity->mesh()->setMeshResolution(QSize(20, 20)); + + Qt3D::QNormalDiffuseSpecularMapMaterial *normalDiffuseSpecularMapMaterial = new Qt3D::QNormalDiffuseSpecularMapMaterial(); + normalDiffuseSpecularMapMaterial->setTextureScale(10.0f); + 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::QTexture::RGBA8_UNorm : + Qt3D::QTexture::RGB8_UNorm); + normalDiffuseSpecularMapMaterial->diffuse()->setFromQImage(img); + } + + if (img.load(QStringLiteral(":/assets/textures/pattern_09/specular.webp"))) { + normalDiffuseSpecularMapMaterial->specular()->setFormat(img.hasAlphaChannel() ? + Qt3D::QTexture::RGBA8_UNorm : + Qt3D::QTexture::RGB8_UNorm); + normalDiffuseSpecularMapMaterial->specular()->setFromQImage(img); + } + + if (img.load(QStringLiteral(":/assets/textures/pattern_09/normal.webp"))) { + normalDiffuseSpecularMapMaterial->normal()->setFormat(img.hasAlphaChannel() ? + Qt3D::QTexture::RGBA8_UNorm : + Qt3D::QTexture::RGB8_UNorm); + normalDiffuseSpecularMapMaterial->normal()->setFromQImage(img); + } + planeEntity->addComponent(normalDiffuseSpecularMapMaterial); + + // Chest + RenderableEntity *chest = new RenderableEntity(sceneRoot); + chest->scaleTransform()->setScale(0.03f); + chest->mesh()->setSource(QUrl(QStringLiteral("qrc:/assets/chest/Chest.obj"))); + Qt3D::QDiffuseMapMaterial *diffuseMapMaterial = new Qt3D::QDiffuseMapMaterial(); + 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::QTexture::RGBA8_UNorm : + Qt3D::QTexture::RGB8_UNorm); + diffuseMapMaterial->diffuse()->setFromQImage(img); + } + chest->addComponent(diffuseMapMaterial); + + + // TrefoilKnot + RotatingTrefoilKnot *trefoil = new RotatingTrefoilKnot(sceneRoot); + trefoil->translateTransform()->setDy(3.5); + trefoil->scaleTransform()->setScale(0.5f); + Qt3D::QPhongMaterial *phongMaterial = new Qt3D::QPhongMaterial(); + phongMaterial->setDiffuse(QColor(204, 205, 75)); // Safari Yellow #cccd4b + phongMaterial->setSpecular(Qt::white); + trefoil->addComponent(phongMaterial); + + // Barrels + Barrel *basicBarrel = new Barrel(sceneRoot); + basicBarrel->translateTransform()->setDx(8.0f); + + Barrel *rustyBarrel = new Barrel(sceneRoot); + rustyBarrel->setDiffuse(Barrel::RustDiffuse); + rustyBarrel->setSpecular(Barrel::RustSpecular); + rustyBarrel->setBumps(Barrel::HardBumps); + rustyBarrel->translateTransform()->setDx(10.0f); + + Barrel *blueBarrel = new Barrel(sceneRoot); + blueBarrel->setDiffuse(Barrel::Blue); + blueBarrel->setBumps(Barrel::MiddleBumps); + blueBarrel->translateTransform()->setDx(12.0f); + + Barrel *greenBarrel = new Barrel(sceneRoot); + greenBarrel->setDiffuse(Barrel::Green); + greenBarrel->setBumps(Barrel::SoftBumps); + greenBarrel->translateTransform()->setDx(14.0f); + + Barrel *stainlessBarrel = new Barrel(sceneRoot); + stainlessBarrel->setDiffuse(Barrel::StainlessSteelDiffuse); + stainlessBarrel->setBumps(Barrel::NoBumps); + stainlessBarrel->setSpecular(Barrel::StainlessSteelSpecular); + stainlessBarrel->setShininess(150.0f); + stainlessBarrel->translateTransform()->setDx(16); + + // Plants + HousePlant *squareBamboo = new HousePlant(sceneRoot); + squareBamboo->setPotShape(HousePlant::Square); + squareBamboo->setX(4.0f); + + HousePlant *trianglePalm = new HousePlant(sceneRoot); + trianglePalm->setPlantType(HousePlant::Palm); + trianglePalm->setPotShape(HousePlant::Triangle); + trianglePalm->setZ(4.0f); + + HousePlant *spherePine = new HousePlant(sceneRoot); + spherePine->setPlantType(HousePlant::Pine); + spherePine->setPotShape(HousePlant::Sphere); + spherePine->setX(-4.0f); + + HousePlant *crossSpikes = new HousePlant(sceneRoot); + crossSpikes->setPlantType(HousePlant::Spikes); + crossSpikes->setZ(-4.0f); + + HousePlant *crossPalm = new HousePlant(sceneRoot); + crossPalm->setPlantType(HousePlant::Palm); + crossPalm->setZ(-8.0f); + crossPalm->setScale(0.05f); + + HousePlant *crossShrub = new HousePlant(sceneRoot); + crossShrub->setPlantType(HousePlant::Shrub); + crossShrub->setZ(8.0f); + crossShrub->setScale(0.05f); + + engine.setRootEntity(sceneRoot); + view.show(); + + return app.exec(); +} diff --git a/examples/materials-cpp/materials-cpp.pro b/examples/materials-cpp/materials-cpp.pro new file mode 100644 index 000000000..e4d5939bc --- /dev/null +++ b/examples/materials-cpp/materials-cpp.pro @@ -0,0 +1,24 @@ +TEMPLATE = app + +QT += 3dcore 3drenderer + +include("../exampleresources/exampleresources.pri") + +HEADERS += \ + planeentity.h \ + forwardrenderer.h \ + renderableentity.h \ + trefoilknot.h \ + barrel.h \ + rotatingtrefoilknot.h \ + houseplant.h + +SOURCES += \ + main.cpp \ + planeentity.cpp \ + forwardrenderer.cpp \ + renderableentity.cpp \ + trefoilknot.cpp \ + barrel.cpp \ + rotatingtrefoilknot.cpp \ + houseplant.cpp diff --git a/examples/materials-cpp/planeentity.cpp b/examples/materials-cpp/planeentity.cpp new file mode 100644 index 000000000..0996fdd96 --- /dev/null +++ b/examples/materials-cpp/planeentity.cpp @@ -0,0 +1,80 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "planeentity.h" + +#include <Qt3DRenderer/QPlaneMesh> +#include <Qt3DCore/QTransform> +#include <Qt3DCore/QScaleTransform> +#include <Qt3DCore/QTranslateTransform> + +PlaneEntity::PlaneEntity(Qt3D::QNode *parent) + : Qt3D::QEntity(new Qt3D::QEntity(parent)) + , m_mesh(new Qt3D::QPlaneMesh()) + , m_transform(new Qt3D::QTransform()) + , m_scaleTransform(new Qt3D::QScaleTransform()) + , m_translateTransform(new Qt3D::QTranslateTransform()) +{ + m_transform->addTransform(m_translateTransform); + m_transform->addTransform(m_scaleTransform); + addComponent(m_mesh); + addComponent(m_transform); +} + +PlaneEntity::~PlaneEntity() +{ +} + +Qt3D::QScaleTransform *PlaneEntity::scaleTransform() const +{ + return m_scaleTransform; +} + +Qt3D::QTranslateTransform *PlaneEntity::translateTransform() const +{ + return m_translateTransform; +} + +Qt3D::QPlaneMesh *PlaneEntity::mesh() const +{ + return m_mesh; +} + diff --git a/examples/materials-cpp/planeentity.h b/examples/materials-cpp/planeentity.h new file mode 100644 index 000000000..b221b7edc --- /dev/null +++ b/examples/materials-cpp/planeentity.h @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef PLANEENTITY_H +#define PLANEENTITY_H + +#include <Qt3DCore/QEntity> +#include <Qt3DCore/QTransform> +#include <Qt3DCore/QScaleTransform> +#include <Qt3DCore/QTranslateTransform> +#include <Qt3DRenderer/QPlaneMesh> + +class PlaneEntity : public Qt3D::QEntity +{ +public: + PlaneEntity(Qt3D::QNode *parent = 0); + ~PlaneEntity(); + + Qt3D::QScaleTransform *scaleTransform() const; + Qt3D::QTranslateTransform *translateTransform() const; + Qt3D::QPlaneMesh *mesh() const; + +private: + Qt3D::QPlaneMesh *m_mesh; + Qt3D::QTransform *m_transform; + Qt3D::QScaleTransform *m_scaleTransform; + Qt3D::QTranslateTransform *m_translateTransform; +}; + +#endif // PLANEENTITY_H diff --git a/examples/materials-cpp/renderableentity.cpp b/examples/materials-cpp/renderableentity.cpp new file mode 100644 index 000000000..266d9a7cc --- /dev/null +++ b/examples/materials-cpp/renderableentity.cpp @@ -0,0 +1,82 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "renderableentity.h" + +RenderableEntity::RenderableEntity(Qt3D::QNode *parent) + : Qt3D::QEntity(parent) + , m_mesh(new Qt3D::QMesh()) + , m_transform(new Qt3D::QTransform()) + , m_rotateTransform(new Qt3D::QRotateTransform()) + , m_scaleTransform(new Qt3D::QScaleTransform()) + , m_translateTransform(new Qt3D::QTranslateTransform()) +{ + m_transform->addTransform(m_rotateTransform); + m_transform->addTransform(m_scaleTransform); + m_transform->addTransform(m_translateTransform); + addComponent(m_mesh); + addComponent(m_transform); +} + +RenderableEntity::~RenderableEntity() +{ + +} + +Qt3D::QMesh *RenderableEntity::mesh() const +{ + return m_mesh; +} + +Qt3D::QScaleTransform *RenderableEntity::scaleTransform() const +{ + return m_scaleTransform; +} + +Qt3D::QTranslateTransform *RenderableEntity::translateTransform() const +{ + return m_translateTransform; +} + +Qt3D::QRotateTransform *RenderableEntity::rotateTransform() const +{ + return m_rotateTransform; +} diff --git a/examples/materials-cpp/renderableentity.h b/examples/materials-cpp/renderableentity.h new file mode 100644 index 000000000..e95c6b131 --- /dev/null +++ b/examples/materials-cpp/renderableentity.h @@ -0,0 +1,71 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef RENDERABLEENTITY_H +#define RENDERABLEENTITY_H + +#include <Qt3DCore/QEntity> +#include <Qt3DCore/QTransform> +#include <Qt3DCore/QScaleTransform> +#include <Qt3DCore/QRotateTransform> +#include <Qt3DCore/QTranslateTransform> +#include <Qt3DRenderer/QMesh> + +class RenderableEntity : public Qt3D::QEntity +{ +public: + RenderableEntity(Qt3D::QNode *parent = 0); + ~RenderableEntity(); + + Qt3D::QMesh *mesh() const; + Qt3D::QScaleTransform *scaleTransform() const; + Qt3D::QTranslateTransform *translateTransform() const; + Qt3D::QRotateTransform *rotateTransform() const; + +private: + Qt3D::QMesh *m_mesh; + Qt3D::QTransform *m_transform; + Qt3D::QRotateTransform *m_rotateTransform; + Qt3D::QScaleTransform *m_scaleTransform; + Qt3D::QTranslateTransform *m_translateTransform; +}; + +#endif // RENDERABLEENTITY_H diff --git a/examples/materials-cpp/rotatingtrefoilknot.cpp b/examples/materials-cpp/rotatingtrefoilknot.cpp new file mode 100644 index 000000000..4ddf10c12 --- /dev/null +++ b/examples/materials-cpp/rotatingtrefoilknot.cpp @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "rotatingtrefoilknot.h" + +RotatingTrefoilKnot::RotatingTrefoilKnot(Qt3D::QNode *parent) + : TrefoilKnot(parent) + , m_thetaAnimation(new QPropertyAnimation(this)) + , m_phiAnimation(new QPropertyAnimation(this)) +{ + m_thetaAnimation->setDuration(2000); + m_thetaAnimation->setStartValue(0.0f); + m_thetaAnimation->setEndValue(360.0f); + m_thetaAnimation->setLoopCount(-1); + m_thetaAnimation->setTargetObject(xaxisRotateTransform()); + m_thetaAnimation->setPropertyName(QByteArrayLiteral("angle")); + m_thetaAnimation->start(); + + m_phiAnimation->setDuration(2000); + m_phiAnimation->setStartValue(0.0f); + m_phiAnimation->setEndValue(360.0f); + m_phiAnimation->setLoopCount(-1); + m_phiAnimation->setTargetObject(yaxisRotateTransform()); + m_phiAnimation->setPropertyName(QByteArrayLiteral("angle")); + m_phiAnimation->start(); +} + +RotatingTrefoilKnot::~RotatingTrefoilKnot() +{ +} diff --git a/examples/materials-cpp/rotatingtrefoilknot.h b/examples/materials-cpp/rotatingtrefoilknot.h new file mode 100644 index 000000000..c3fea0c64 --- /dev/null +++ b/examples/materials-cpp/rotatingtrefoilknot.h @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef ROTATINGTREFOILKNOT_H +#define ROTATINGTREFOILKNOT_H + +#include "trefoilknot.h" +#include <QPropertyAnimation> + +class RotatingTrefoilKnot : public TrefoilKnot +{ +public: + explicit RotatingTrefoilKnot(Qt3D::QNode *parent = 0); + ~RotatingTrefoilKnot(); + +private: + QPropertyAnimation *m_thetaAnimation; + QPropertyAnimation *m_phiAnimation; + +}; + +#endif // ROTATINGTREFOILKNOT_H diff --git a/examples/materials-cpp/trefoilknot.cpp b/examples/materials-cpp/trefoilknot.cpp new file mode 100644 index 000000000..be028d26f --- /dev/null +++ b/examples/materials-cpp/trefoilknot.cpp @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "trefoilknot.h" + +TrefoilKnot::TrefoilKnot(Qt3D::QNode *parent) + : Qt3D::QEntity(parent) + , m_mesh(new Qt3D::QMesh()) + , m_transform(new Qt3D::QTransform()) + , m_scaleTransform(new Qt3D::QScaleTransform()) + , m_xaxisRotation(new Qt3D::QRotateTransform()) + , m_yaxisRotation(new Qt3D::QRotateTransform()) + , m_translateTransform(new Qt3D::QTranslateTransform()) +{ + m_mesh->setSource(QUrl("qrc:/assets/obj/trefoil.obj")); + m_xaxisRotation->setAxis(QVector3D(1.0f, 0.0f, 0.0f)); + m_yaxisRotation->setAxis(QVector3D(0.0f, 1.0f, 0.0f)); + + m_transform->addTransform(m_scaleTransform); + m_transform->addTransform(m_xaxisRotation); + m_transform->addTransform(m_yaxisRotation); + m_transform->addTransform(m_translateTransform); + + addComponent(m_mesh); + addComponent(m_transform); +} + +TrefoilKnot::~TrefoilKnot() +{ +} + +Qt3D::QScaleTransform *TrefoilKnot::scaleTransform() const +{ + return m_scaleTransform; +} + +Qt3D::QRotateTransform *TrefoilKnot::xaxisRotateTransform() const +{ + return m_xaxisRotation; +} + +Qt3D::QRotateTransform *TrefoilKnot::yaxisRotateTransform() const +{ + return m_yaxisRotation; +} + +Qt3D::QTranslateTransform *TrefoilKnot::translateTransform() const +{ + return m_translateTransform; +} diff --git a/examples/materials-cpp/trefoilknot.h b/examples/materials-cpp/trefoilknot.h new file mode 100644 index 000000000..d55939a91 --- /dev/null +++ b/examples/materials-cpp/trefoilknot.h @@ -0,0 +1,72 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef TREFOILKNOT_H +#define TREFOILKNOT_H + +#include <Qt3DCore/QEntity> +#include <Qt3DCore/QTransform> +#include <Qt3DCore/QScaleTransform> +#include <Qt3DCore/QRotateTransform> +#include <Qt3DCore/QTranslateTransform> +#include <Qt3DRenderer/QMesh> + +class TrefoilKnot : public Qt3D::QEntity +{ +public: + explicit TrefoilKnot(Qt3D::QNode *parent = 0); + ~TrefoilKnot(); + + Qt3D::QScaleTransform *scaleTransform() const; + Qt3D::QRotateTransform *xaxisRotateTransform() const; + Qt3D::QRotateTransform *yaxisRotateTransform() const; + Qt3D::QTranslateTransform *translateTransform() const; + +private: + Qt3D::QMesh *m_mesh; + Qt3D::QTransform *m_transform; + Qt3D::QScaleTransform *m_scaleTransform; + Qt3D::QRotateTransform *m_xaxisRotation; + Qt3D::QRotateTransform *m_yaxisRotation; + Qt3D::QTranslateTransform *m_translateTransform; +}; + +#endif // TREFOILKNOT_H diff --git a/examples/materials/Barrel.qml b/examples/materials/Barrel.qml index 8545f1f07..a31f6f595 100644 --- a/examples/materials/Barrel.qml +++ b/examples/materials/Barrel.qml @@ -59,7 +59,7 @@ Entity { RenderableEntity { id: barrel - source: ":/assets/metalbarrel/metal_barrel.obj" + source: "assets/metalbarrel/metal_barrel.obj" scale: 0.03 material: NormalDiffuseSpecularMapMaterial { diff --git a/examples/materials/Chest.qml b/examples/materials/Chest.qml index b01754be6..0d2813fd4 100644 --- a/examples/materials/Chest.qml +++ b/examples/materials/Chest.qml @@ -54,7 +54,7 @@ Entity { RenderableEntity { id: chest - source: ":/assets/chest/Chest.obj" + source: "assets/chest/Chest.obj" scale: 0.03 material: DiffuseMapMaterial { diff --git a/examples/materials/HousePlant.qml b/examples/materials/HousePlant.qml index 704749820..6e5d39c7e 100644 --- a/examples/materials/HousePlant.qml +++ b/examples/materials/HousePlant.qml @@ -58,7 +58,7 @@ Entity { RenderableEntity { id: pot - source: ":/assets/houseplants/" + root.potShape + "-pot.obj" + source: "assets/houseplants/" + root.potShape + "-pot.obj" scale: 0.03 material: NormalDiffuseMapMaterial { @@ -70,7 +70,7 @@ Entity { } RenderableEntity { - source: ":/assets/houseplants/" + root.potShape + "-" + root.plantType + ".obj" + source: "assets/houseplants/" + root.potShape + "-" + root.plantType + ".obj" material: NormalDiffuseMapMaterial { effect: root.normalDiffuseMapAlphaEffect diffuse: "assets/houseplants/" + root.plantType + ".webp" @@ -80,7 +80,7 @@ Entity { } RenderableEntity { - source: ":/assets/houseplants/" + root.potShape + "-pot-cover.obj" + source: "assets/houseplants/" + root.potShape + "-pot-cover.obj" material: NormalDiffuseMapMaterial { effect: root.normalDiffuseMapEffect diffuse: "assets/houseplants/cover.webp" diff --git a/examples/materials/TrefoilKnot.qml b/examples/materials/TrefoilKnot.qml index 42367099b..71e9ec11f 100644 --- a/examples/materials/TrefoilKnot.qml +++ b/examples/materials/TrefoilKnot.qml @@ -65,6 +65,6 @@ Entity { Mesh { id: mesh - source: ":/assets/obj/trefoil.obj" + source: "assets/obj/trefoil.obj" } } diff --git a/src/render/defaults/defaults.pri b/src/render/defaults/defaults.pri index c2f77f887..2ec17c3b8 100644 --- a/src/render/defaults/defaults.pri +++ b/src/render/defaults/defaults.pri @@ -2,7 +2,22 @@ INCLUDEPATH += $$PWD HEADERS += \ $$PWD/qphongmaterial.h \ - $$PWD/qphongmaterial_p.h + $$PWD/qphongmaterial_p.h \ + $$PWD/qdiffusemapmaterial_p.h \ + $$PWD/qdiffusemapmaterial.h \ + $$PWD/qnormaldiffusespecularmapmaterial.h \ + $$PWD/qnormaldiffusespecularmapmaterial_p.h \ + $$PWD/qnormaldiffusemapmaterial.h \ + $$PWD/qnormaldiffusemapmaterial_p.h \ + $$PWD/qnormaldiffusemapalphamaterial.h \ + $$PWD/qnormaldiffusemapalphamaterial_p.h \ + $$PWD/qdiffusespecularmapmaterial.h \ + $$PWD/qdiffusespecularmapmaterial_p.h SOURCES += \ - $$PWD/qphongmaterial.cpp + $$PWD/qphongmaterial.cpp \ + $$PWD/qdiffusemapmaterial.cpp \ + $$PWD/qnormaldiffusespecularmapmaterial.cpp \ + $$PWD/qnormaldiffusemapmaterial.cpp \ + $$PWD/qnormaldiffusemapalphamaterial.cpp \ + $$PWD/qdiffusespecularmapmaterial.cpp diff --git a/src/render/defaults/qdiffusemapmaterial.cpp b/src/render/defaults/qdiffusemapmaterial.cpp new file mode 100644 index 000000000..2cf30e981 --- /dev/null +++ b/src/render/defaults/qdiffusemapmaterial.cpp @@ -0,0 +1,274 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qdiffusemapmaterial.h" +#include "qdiffusemapmaterial_p.h" +#include <Qt3DRenderer/qmaterial.h> +#include <Qt3DRenderer/qeffect.h> +#include <Qt3DRenderer/qtexture.h> +#include <Qt3DRenderer/qtechnique.h> +#include <Qt3DRenderer/qshaderprogram.h> +#include <Qt3DRenderer/qparameter.h> +#include <Qt3DRenderer/qrenderpass.h> +#include <Qt3DRenderer/qopenglfilter.h> +#include <QUrl> + +QT_BEGIN_NAMESPACE + +namespace Qt3D { + +QDiffuseMapMaterialPrivate::QDiffuseMapMaterialPrivate(QDiffuseMapMaterial *qq) + : QMaterialPrivate(qq) + , m_diffuseMapEffect(new QEffect()) + , m_diffuseTexture(new QTexture(QTexture::Target2D)) + , m_ambientParameter(new QParameter(QStringLiteral("ka"), QColor::fromRgbF(0.05f, 0.05f, 0.05f, 1.0f))) + , m_diffuseParameter(new QParameter(QStringLiteral("diffuseTexture"), m_diffuseTexture)) + , m_specularParameter(new QParameter(QStringLiteral("ks"), QColor::fromRgbF(0.95f, 0.95f, 0.95f, 1.0f))) + , m_shininessParameter(new QParameter(QStringLiteral("shininess"), 150.0f)) + , m_textureScaleParameter(new QParameter(QStringLiteral("texCoordScale"), 1.0f)) + , m_diffuseMapGL3Technique(new QTechnique()) + , m_diffuseMapGL2Technique(new QTechnique()) + , m_diffuseMapES2Technique(new QTechnique()) + , m_diffuseMapGL3RenderPass(new QRenderPass()) + , m_diffuseMapGL2RenderPass(new QRenderPass()) + , m_diffuseMapES2RenderPass(new QRenderPass()) + , m_diffuseMapGL3Shader(new QShaderProgram()) + , m_diffuseMapGL2ES2Shader(new QShaderProgram()) + { + m_diffuseTexture->setMagnificationFilter(QTexture::Linear); + m_diffuseTexture->setMinificationFilter(QTexture::LinearMipMapLinear); + m_diffuseTexture->setWrapMode(QTextureWrapMode(QTextureWrapMode::Repeat)); + m_diffuseTexture->setGenerateMipMaps(true); + m_diffuseTexture->setMaximumAnisotropy(16.0f); +} + +void QDiffuseMapMaterialPrivate::init() +{ + m_diffuseMapGL3Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/diffusemap.vert")))); + m_diffuseMapGL3Shader->setFragmentShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/diffusemap.frag")))); + m_diffuseMapGL2ES2Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/es2/diffusemap.vert")))); + m_diffuseMapGL2ES2Shader->setFragmentShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/es2/diffusemap.frag")))); + + m_diffuseMapGL3Technique->openGLFilter()->setApi(QOpenGLFilter::Desktop); + m_diffuseMapGL3Technique->openGLFilter()->setMajorVersion(3); + m_diffuseMapGL3Technique->openGLFilter()->setMinorVersion(1); + m_diffuseMapGL3Technique->openGLFilter()->setProfile(QOpenGLFilter::Core); + + m_diffuseMapGL2Technique->openGLFilter()->setApi(QOpenGLFilter::Desktop); + m_diffuseMapGL2Technique->openGLFilter()->setMajorVersion(2); + m_diffuseMapGL2Technique->openGLFilter()->setMinorVersion(0); + m_diffuseMapGL2Technique->openGLFilter()->setProfile(QOpenGLFilter::None); + + m_diffuseMapES2Technique->openGLFilter()->setApi(QOpenGLFilter::ES); + m_diffuseMapES2Technique->openGLFilter()->setMajorVersion(2); + m_diffuseMapES2Technique->openGLFilter()->setMinorVersion(0); + m_diffuseMapES2Technique->openGLFilter()->setProfile(QOpenGLFilter::None); + + m_diffuseMapGL3RenderPass->setShaderProgram(m_diffuseMapGL3Shader); + m_diffuseMapGL2RenderPass->setShaderProgram(m_diffuseMapGL2ES2Shader); + m_diffuseMapES2RenderPass->setShaderProgram(m_diffuseMapGL2ES2Shader); + + m_diffuseMapGL3Technique->addPass(m_diffuseMapGL3RenderPass); + m_diffuseMapGL2Technique->addPass(m_diffuseMapGL2RenderPass); + m_diffuseMapES2Technique->addPass(m_diffuseMapES2RenderPass); + + m_diffuseMapEffect->addTechnique(m_diffuseMapGL3Technique); + m_diffuseMapEffect->addTechnique(m_diffuseMapGL2Technique); + m_diffuseMapEffect->addTechnique(m_diffuseMapES2Technique); + + m_diffuseMapEffect->addParameter(m_ambientParameter); + m_diffuseMapEffect->addParameter(m_diffuseParameter); + m_diffuseMapEffect->addParameter(m_specularParameter); + m_diffuseMapEffect->addParameter(m_shininessParameter); + m_diffuseMapEffect->addParameter(m_textureScaleParameter); + + static_cast<QMaterial *>(q_ptr)->setEffect(m_diffuseMapEffect); +} + +/*! + \class Qt3D::QDiffuseMapMaterial + \brief The QDiffuseMapMaterial provides a default implementation of the phong lighting effect where the diffuse light component + is read from a texture map. + \inmodule Qt3DRender + \since 5.5 + + The specular lighting effect is based on the combination of 3 lighting components ambient, diffuse and specular. + The relative strengths of these components is controlled by means of their reflectivity coefficients which are modelled as RGB triplets: + + \list + \li Ambient is the color that is emitted by an object without any other light source. + \li Diffuse is the color that is emitted for rought surface reflections with the lights. + \li Specular is the color emitted for shiny surface reflections with the lights. + \li The shininess of a surface is controlled by a float property. + \endlist + + This material uses an effect with a single render pass approach and performs per fragment lighting. + Techniques are provided for OpenGL 2, OpenGL 3 or above as well as OpenGL ES 2. +*/ + +/*! + \fn Qt3D::QDiffuseMapMaterial::QDiffuseMapMaterial() + Constructs a new Qt3D::QDiffuseMapMaterial instance with parent object \a parent. + */ + +QDiffuseMapMaterial::QDiffuseMapMaterial(QNode *parent) + : QMaterial(*new QDiffuseMapMaterialPrivate(this), parent) +{ + Q_D(QDiffuseMapMaterial); + QObject::connect(d->m_ambientParameter, SIGNAL(valueChanged()), this, SIGNAL(ambientChanged())); + QObject::connect(d->m_diffuseParameter, SIGNAL(valueChanged()), this, SIGNAL(diffuseChanged())); + QObject::connect(d->m_specularParameter, SIGNAL(valueChanged()), this, SIGNAL(specularChanged())); + QObject::connect(d->m_shininessParameter, SIGNAL(valueChanged()), this, SIGNAL(shininessChanged())); + QObject::connect(d->m_textureScaleParameter, SIGNAL(valueChanged()), this, SIGNAL(textureScaleChanged())); + d->init(); +} + +QDiffuseMapMaterial::~QDiffuseMapMaterial() +{ +} + +/*! + \fn Qt3D::QDiffuseMapMaterial::ambient() const + \return the current QColor ambient value. + */ +QColor QDiffuseMapMaterial::ambient() const +{ + Q_D(const QDiffuseMapMaterial); + return d->m_ambientParameter->value().value<QColor>(); +} + +/*! + \fn Qt3D::QDiffuseMapMaterial::specular() const + \return the current QColor specular value. + */ +QColor QDiffuseMapMaterial::specular() const +{ + Q_D(const QDiffuseMapMaterial); + return d->m_specularParameter->value().value<QColor>(); +} + +/*! + \fn Qt3D::QDiffuseMapMaterial::shininess() const + \return the current shininess as a float value. + */ +float QDiffuseMapMaterial::shininess() const +{ + Q_D(const QDiffuseMapMaterial); + return d->m_diffuseParameter->value().toFloat(); +} + +/*! + \fn Qt3D::QDiffuseMapMaterial::diffuse() const + \return the current QTexture used as the diffuse map. + \b {Note:} By default the diffuse texture has a linear + magnification filter, a linear mipmap linear minification filter, + the wrap mode is repeat, the maximum anisotropy is set to 16.0 + and mipmapping is enabled. + */ +QTexture *QDiffuseMapMaterial::diffuse() const +{ + Q_D(const QDiffuseMapMaterial); + return d->m_diffuseParameter->value().value<QTexture *>(); +} + +/*! + \fn Qt3D::QDiffuseMapMaterial::textureScale() const + \return the current texture scale. + */ +float QDiffuseMapMaterial::textureScale() const +{ + Q_D(const QDiffuseMapMaterial); + return d->m_textureScaleParameter->value().toFloat(); +} + +/*! + \fn Qt3D::QDiffuseMapMaterial::setAmbient(const QColor &ambient) + Sets the current ambient color to \a ambient. + */ +void QDiffuseMapMaterial::setAmbient(const QColor &ambient) +{ + Q_D(const QDiffuseMapMaterial); + d->m_ambientParameter->setValue(ambient); +} + +/*! + \fn Qt3D::QDiffuseMapMaterial::setSpecular(const QColor &specular) + Sets the current specular color to \a specular. + */ +void QDiffuseMapMaterial::setSpecular(const QColor &specular) +{ + Q_D(QDiffuseMapMaterial); + d->m_specularParameter->setValue(specular); +} + +/*! + \fn Qt3D::QDiffuseMapMaterial::setShininess(float shininess) + Sets the current shininess value to \a shininess. + */ +void QDiffuseMapMaterial::setShininess(float shininess) +{ + Q_D(QDiffuseMapMaterial); + d->m_shininessParameter->setValue(shininess); +} + +/*! + \fn Qt3D::QDiffuseMapMaterial::setDiffuse(QTexture *diffuseMap) + Sets the current diffuse map to \a diffuseMap. + */ +void QDiffuseMapMaterial::setDiffuse(QTexture *diffuseMap) +{ + Q_D(QDiffuseMapMaterial); + d->m_diffuseParameter->setValue(QVariant::fromValue(diffuseMap)); +} + +/*! + \fn Qt3D::QDiffuseMapMaterial::setTextureScale(float textureScale) + Sets the current texture scale to \a textureScale. + */ +void QDiffuseMapMaterial::setTextureScale(float textureScale) +{ + Q_D(QDiffuseMapMaterial); + d->m_textureScaleParameter->setValue(textureScale); +} + +} // Qt3D + +QT_END_NAMESPACE diff --git a/src/render/defaults/qdiffusemapmaterial.h b/src/render/defaults/qdiffusemapmaterial.h new file mode 100644 index 000000000..00c1f4f2d --- /dev/null +++ b/src/render/defaults/qdiffusemapmaterial.h @@ -0,0 +1,95 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QT3D_RENDER_QDIFFUSEGMAPMATERIAL_H +#define QT3D_RENDER_QDIFFUSEMAPMATERIAL_H + +#include <Qt3DRenderer/qmaterial.h> +#include <QColor> + +QT_BEGIN_NAMESPACE + +namespace Qt3D { + +class QDiffuseMapMaterialPrivate; +class QTexture; + +class QT3DRENDERERSHARED_EXPORT QDiffuseMapMaterial : public QMaterial +{ + Q_OBJECT + Q_PROPERTY(QColor ambient READ ambient WRITE setAmbient NOTIFY ambientChanged) + Q_PROPERTY(QColor specular READ specular WRITE setSpecular NOTIFY specularChanged) + Q_PROPERTY(float shininess READ shininess WRITE setShininess NOTIFY shininessChanged) + Q_PROPERTY(QTexture *diffuse READ diffuse WRITE setDiffuse NOTIFY diffuseChanged) + Q_PROPERTY(float textureScale READ textureScale WRITE setTextureScale NOTIFY textureScaleChanged) + +public: + explicit QDiffuseMapMaterial(QNode *parent = 0); + ~QDiffuseMapMaterial(); + + QColor ambient() const; + QColor specular() const; + float shininess() const; + QTexture *diffuse() const; + float textureScale() const; + + void setAmbient(const QColor &color); + void setSpecular(const QColor &specular); + void setShininess(float shininess); + void setDiffuse(QTexture *diffuse); + void setTextureScale(float textureScale); + +Q_SIGNALS: + void ambientChanged(); + void diffuseChanged(); + void specularChanged(); + void shininessChanged(); + void textureScaleChanged(); + +private: + Q_DECLARE_PRIVATE(QDiffuseMapMaterial) +}; + +} // Qt3D + +QT_END_NAMESPACE + +#endif // QT3D_RENDER_QDIFFUSEMAPMATERIAL_H diff --git a/src/render/defaults/qdiffusemapmaterial_p.h b/src/render/defaults/qdiffusemapmaterial_p.h new file mode 100644 index 000000000..9fffcfa4b --- /dev/null +++ b/src/render/defaults/qdiffusemapmaterial_p.h @@ -0,0 +1,91 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QT3D_RENDER_QDIFFUSEMAPMATERIAL_P_H +#define QT3D_RENDER_QDIFFUSEMAPMATERIAL_P_H + +#include <Qt3DRenderer/private/qmaterial_p.h> + +QT_BEGIN_NAMESPACE + +namespace Qt3D { + +class QEffect; +class QTexture; +class QTechnique; +class QParameter; +class QShaderProgram; +class QRenderPass; +class QParameterMapping; + +class QDiffuseMapMaterial; + +class QDiffuseMapMaterialPrivate : public QMaterialPrivate +{ + QDiffuseMapMaterialPrivate(QDiffuseMapMaterial *qq); + + void init(); + + QEffect *m_diffuseMapEffect; + QTexture *m_diffuseTexture; + QParameter *m_ambientParameter; + QParameter *m_diffuseParameter; + QParameter *m_specularParameter; + QParameter *m_shininessParameter; + QParameter *m_textureScaleParameter; + QTechnique *m_diffuseMapGL3Technique; + QTechnique *m_diffuseMapGL2Technique; + QTechnique *m_diffuseMapES2Technique; + QRenderPass *m_diffuseMapGL3RenderPass; + QRenderPass *m_diffuseMapGL2RenderPass; + QRenderPass *m_diffuseMapES2RenderPass; + QShaderProgram *m_diffuseMapGL3Shader; + QShaderProgram *m_diffuseMapGL2ES2Shader; + + Q_DECLARE_PUBLIC(QDiffuseMapMaterial) +}; + +} // Qt3D + +QT_END_NAMESPACE + +#endif // QT3D_RENDER_QDIFFUSEAPMATERIAL_P_H + diff --git a/src/render/defaults/qdiffusespecularmapmaterial.cpp b/src/render/defaults/qdiffusespecularmapmaterial.cpp new file mode 100644 index 000000000..1784250b4 --- /dev/null +++ b/src/render/defaults/qdiffusespecularmapmaterial.cpp @@ -0,0 +1,285 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qdiffusespecularmapmaterial.h" +#include "qdiffusespecularmapmaterial_p.h" + +#include <Qt3DRenderer/qmaterial.h> +#include <Qt3DRenderer/qeffect.h> +#include <Qt3DRenderer/qtexture.h> +#include <Qt3DRenderer/qtechnique.h> +#include <Qt3DRenderer/qshaderprogram.h> +#include <Qt3DRenderer/qparameter.h> +#include <Qt3DRenderer/qrenderpass.h> +#include <Qt3DRenderer/qopenglfilter.h> +#include <QUrl> + +QT_BEGIN_NAMESPACE + +namespace Qt3D { + +QDiffuseSpecularMapMaterialPrivate::QDiffuseSpecularMapMaterialPrivate(QDiffuseSpecularMapMaterial *qq) + : QMaterialPrivate(qq) + , m_diffuseSpecularMapEffect(new QEffect()) + , m_diffuseTexture(new QTexture(QTexture::Target2D)) + , m_specularTexture(new QTexture(QTexture::Target2D)) + , m_ambientParameter(new QParameter(QStringLiteral("ka"), QColor::fromRgbF(0.05f, 0.05f, 0.05f, 1.0f))) + , m_diffuseParameter(new QParameter(QStringLiteral("diffuseTexture"), m_diffuseTexture)) + , m_specularParameter(new QParameter(QStringLiteral("specularTexture"), m_specularTexture)) + , m_shininessParameter(new QParameter(QStringLiteral("shininess"), 150.0f)) + , m_textureScaleParameter(new QParameter(QStringLiteral("texCoordScale"), 1.0f)) + , m_diffuseSpecularMapGL3Technique(new QTechnique()) + , m_diffuseSpecularMapGL2Technique(new QTechnique()) + , m_diffuseSpecularMapES2Technique(new QTechnique()) + , m_diffuseSpecularMapGL3RenderPass(new QRenderPass()) + , m_diffuseSpecularMapGL2RenderPass(new QRenderPass()) + , m_diffuseSpecularMapES2RenderPass(new QRenderPass()) + , m_diffuseSpecularMapGL3Shader(new QShaderProgram()) + , m_diffuseSpecularMapGL2ES2Shader(new QShaderProgram()) +{ + m_diffuseTexture->setMagnificationFilter(QTexture::Linear); + m_diffuseTexture->setMinificationFilter(QTexture::LinearMipMapLinear); + m_diffuseTexture->setWrapMode(QTextureWrapMode(QTextureWrapMode::Repeat)); + m_diffuseTexture->setGenerateMipMaps(true); + m_diffuseTexture->setMaximumAnisotropy(16.0f); + + m_specularTexture->setMagnificationFilter(QTexture::Linear); + m_specularTexture->setMinificationFilter(QTexture::LinearMipMapLinear); + m_specularTexture->setWrapMode(QTextureWrapMode(QTextureWrapMode::Repeat)); + m_specularTexture->setGenerateMipMaps(true); + m_specularTexture->setMaximumAnisotropy(16.0f); +} + +void QDiffuseSpecularMapMaterialPrivate::init() +{ + m_diffuseSpecularMapGL3Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/diffusespecularmap.vert")))); + m_diffuseSpecularMapGL3Shader->setFragmentShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/diffusespecularmap.frag")))); + m_diffuseSpecularMapGL2ES2Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/es2/diffusespecularmap.vert")))); + m_diffuseSpecularMapGL2ES2Shader->setFragmentShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/es2/diffusespecularmap.frag")))); + + m_diffuseSpecularMapGL3Technique->openGLFilter()->setApi(QOpenGLFilter::Desktop); + m_diffuseSpecularMapGL3Technique->openGLFilter()->setMajorVersion(3); + m_diffuseSpecularMapGL3Technique->openGLFilter()->setMinorVersion(1); + m_diffuseSpecularMapGL3Technique->openGLFilter()->setProfile(QOpenGLFilter::Core); + + m_diffuseSpecularMapGL2Technique->openGLFilter()->setApi(QOpenGLFilter::Desktop); + m_diffuseSpecularMapGL2Technique->openGLFilter()->setMajorVersion(2); + m_diffuseSpecularMapGL2Technique->openGLFilter()->setMinorVersion(0); + m_diffuseSpecularMapGL2Technique->openGLFilter()->setProfile(QOpenGLFilter::None); + + m_diffuseSpecularMapES2Technique->openGLFilter()->setApi(QOpenGLFilter::ES); + m_diffuseSpecularMapES2Technique->openGLFilter()->setMajorVersion(2); + m_diffuseSpecularMapES2Technique->openGLFilter()->setMinorVersion(0); + m_diffuseSpecularMapES2Technique->openGLFilter()->setProfile(QOpenGLFilter::None); + + m_diffuseSpecularMapGL3RenderPass->setShaderProgram(m_diffuseSpecularMapGL3Shader); + m_diffuseSpecularMapGL2RenderPass->setShaderProgram(m_diffuseSpecularMapGL2ES2Shader); + m_diffuseSpecularMapES2RenderPass->setShaderProgram(m_diffuseSpecularMapGL2ES2Shader); + + m_diffuseSpecularMapGL3Technique->addPass(m_diffuseSpecularMapGL3RenderPass); + m_diffuseSpecularMapGL2Technique->addPass(m_diffuseSpecularMapGL2RenderPass); + m_diffuseSpecularMapES2Technique->addPass(m_diffuseSpecularMapES2RenderPass); + + m_diffuseSpecularMapEffect->addTechnique(m_diffuseSpecularMapGL3Technique); + m_diffuseSpecularMapEffect->addTechnique(m_diffuseSpecularMapGL2Technique); + m_diffuseSpecularMapEffect->addTechnique(m_diffuseSpecularMapES2Technique); + + m_diffuseSpecularMapEffect->addParameter(m_ambientParameter); + m_diffuseSpecularMapEffect->addParameter(m_diffuseParameter); + m_diffuseSpecularMapEffect->addParameter(m_specularParameter); + m_diffuseSpecularMapEffect->addParameter(m_shininessParameter); + m_diffuseSpecularMapEffect->addParameter(m_textureScaleParameter); + + static_cast<QMaterial *>(q_ptr)->setEffect(m_diffuseSpecularMapEffect); +} + +/*! + \class Qt3D::QDiffuseSpecularMapMaterial + \brief The QDiffuseSpecularMapMaterial provides a default implementation of the phong lighting and bump effect where the diffuse and specular light components + are read from texture maps. + \inmodule Qt3DRender + \since 5.5 + + The specular lighting effect is based on the combination of 3 lighting components ambient, diffuse and specular. + The relative strengths of these components is controlled by means of their reflectivity coefficients which are modelled as RGB triplets: + + \list + \li Ambient is the color that is emitted by an object without any other light source. + \li Diffuse is the color that is emitted for rought surface reflections with the lights. + \li Specular is the color emitted for shiny surface reflections with the lights. + \li The shininess of a surface is controlled by a float property. + \endlist + + This material uses an effect with a single render pass approach and performs per fragment lighting. + Techniques are provided for OpenGL 2, OpenGL 3 or above as well as OpenGL ES 2. +*/ + +/*! + \fn Qt3D::QDiffuseSpecularMapMaterial::QDiffuseSpecularMapMaterial() + Constructs a new Qt3D::QDiffuseSpecularMapMaterial instance with parent object \a parent. +*/ +QDiffuseSpecularMapMaterial::QDiffuseSpecularMapMaterial(QNode *parent) + : QMaterial(*new QDiffuseSpecularMapMaterialPrivate(this), parent) +{ + Q_D(QDiffuseSpecularMapMaterial); + QObject::connect(d->m_ambientParameter, SIGNAL(valueChanged()), this, SIGNAL(ambientChanged())); + QObject::connect(d->m_diffuseParameter, SIGNAL(valueChanged()), this, SIGNAL(diffuseChanged())); + QObject::connect(d->m_specularParameter, SIGNAL(valueChanged()), this, SIGNAL(specularChanged())); + QObject::connect(d->m_shininessParameter, SIGNAL(valueChanged()), this, SIGNAL(shininessChanged())); + QObject::connect(d->m_textureScaleParameter, SIGNAL(valueChanged()), this, SIGNAL(textureScaleChanged())); + d->init(); +} + +QDiffuseSpecularMapMaterial::~QDiffuseSpecularMapMaterial() +{ +} + +/*! + \fn Qt3D::QDiffuseSpecularMapMaterial::ambient() const + \return the current QColor ambient value. + */ +QColor QDiffuseSpecularMapMaterial::ambient() const +{ + Q_D(const QDiffuseSpecularMapMaterial); + return d->m_ambientParameter->value().value<QColor>(); +} + +/*! + \fn Qt3D::QDiffuseSpecularMapMaterial::diffuse() const + \return the current diffuse map texture. + \b {Note:} By default the diffuse texture has a linear + magnification filter, a linear mipmap linear minification filter, + the wrap mode is repeat, the maximum anisotropy is set to 16.0 + and mipmapping is enabled. + */ +QTexture *QDiffuseSpecularMapMaterial::diffuse() const +{ + Q_D(const QDiffuseSpecularMapMaterial); + return d->m_diffuseParameter->value().value<QTexture *>(); +} + +/*! + \fn Qt3D::QDiffuseSpecularMapMaterial::specular() const + \return the current specular map texture. + \b {Note:} By default the specular texture has a linear + magnification filter, a linear mipmap linear minification filter, + the wrap mode is repeat, the maximum anisotropy is set to 16.0 + and mipmapping is enabled. + */ +QTexture *QDiffuseSpecularMapMaterial::specular() const +{ + Q_D(const QDiffuseSpecularMapMaterial); + return d->m_specularParameter->value().value<QTexture *>(); +} + +/*! + \fn Qt3D::QDiffuseSpecularMapMaterial::shininess() const + \return the current shininess as a float value. + */ +float QDiffuseSpecularMapMaterial::shininess() const +{ + Q_D(const QDiffuseSpecularMapMaterial); + return d->m_shininessParameter->value().toFloat(); +} + +/*! + \fn Qt3D::QDiffuseSpecularMapMaterial::textureScale() const + \return the current texture scale. + */ +float QDiffuseSpecularMapMaterial::textureScale() const +{ + Q_D(const QDiffuseSpecularMapMaterial); + return d->m_textureScaleParameter->value().toFloat(); +} + +/*! + \fn Qt3D::QDiffuseSpecularMapMaterial::setAmbient(const QColor &ambient) + Sets the current ambient color to \a ambient + */ +void QDiffuseSpecularMapMaterial::setAmbient(const QColor &ambient) +{ + Q_D(QDiffuseSpecularMapMaterial); + d->m_ambientParameter->setValue(ambient); +} + +/*! + \fn Qt3D::QDiffuseSpecularMapMaterial::setDiffuse(QTexture *diffuse) + Sets the current diffuse map to \a diffuse. + */ +void QDiffuseSpecularMapMaterial::setDiffuse(QTexture *diffuse) +{ + Q_D(QDiffuseSpecularMapMaterial); + d->m_diffuseParameter->setValue(QVariant::fromValue(diffuse)); +} + +/*! + \fn Qt3D::QDiffuseSpecularMapMaterial::setSpecular(QTexture *specular) + Sets the current specular map to \a specular. + */ +void QDiffuseSpecularMapMaterial::setSpecular(QTexture *specular) +{ + Q_D(QDiffuseSpecularMapMaterial); + d->m_specularParameter->setValue(QVariant::fromValue(specular)); +} + +/*! + \fn Qt3D::QDiffuseSpecularMapMaterial::setShininess(float shininess) + Sets the current shininess value to \a shininess. + */ +void QDiffuseSpecularMapMaterial::setShininess(float shininess) +{ + Q_D(QDiffuseSpecularMapMaterial); + d->m_shininessParameter->setValue(shininess); +} + +/*! + \fn Qt3D::QDiffuseSpecularMapMaterial::setTextureScale(float textureScale) + Sets the current texture scale to \a textureScale. + */ +void QDiffuseSpecularMapMaterial::setTextureScale(float textureScale) +{ + Q_D(QDiffuseSpecularMapMaterial); + d->m_textureScaleParameter->setValue(textureScale); +} + +} // Qt3D + +QT_END_NAMESPACE diff --git a/src/render/defaults/qdiffusespecularmapmaterial.h b/src/render/defaults/qdiffusespecularmapmaterial.h new file mode 100644 index 000000000..bcaa5b37e --- /dev/null +++ b/src/render/defaults/qdiffusespecularmapmaterial.h @@ -0,0 +1,95 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QT3D_RENDER_QDIFFUSESPECULARMAPMATERIAL_H +#define QT3D_RENDER_QDIFFUSESPECULARMAPMATERIAL_H + +#include <Qt3DRenderer/qmaterial.h> +#include <QColor> + +QT_BEGIN_NAMESPACE + +namespace Qt3D { + +class QTexture; +class QDiffuseSpecularMapMaterialPrivate; + +class QT3DRENDERERSHARED_EXPORT QDiffuseSpecularMapMaterial : public QMaterial +{ + Q_OBJECT + Q_PROPERTY(QColor ambient READ ambient WRITE setAmbient NOTIFY ambientChanged) + Q_PROPERTY(float shininess READ shininess WRITE setShininess NOTIFY shininessChanged) + Q_PROPERTY(QTexture *specular READ specular WRITE setSpecular NOTIFY specularChanged) + Q_PROPERTY(QTexture *diffuse READ diffuse WRITE setDiffuse NOTIFY diffuseChanged) + Q_PROPERTY(float textureScale READ textureScale WRITE setTextureScale NOTIFY textureScaleChanged) + +public: + explicit QDiffuseSpecularMapMaterial(QNode *parent = 0); + ~QDiffuseSpecularMapMaterial(); + + QColor ambient() const; + QTexture *diffuse() const; + QTexture *specular() const; + float shininess() const; + float textureScale() const; + + void setAmbient(const QColor &ambient); + void setDiffuse(QTexture *diffuse); + void setSpecular(QTexture *specular); + void setShininess(float shininess); + void setTextureScale(float textureScale); + +Q_SIGNALS: + void ambientChanged(); + void diffuseChanged(); + void specularChanged(); + void shininessChanged(); + void textureScaleChanged(); + +private: + Q_DECLARE_PRIVATE(QDiffuseSpecularMapMaterial) +}; + +} // Qt3D + +QT_END_NAMESPACE + +#endif // QT3D_RENDER_QDIFFUSESPECULARMAPMATERIAL_H diff --git a/src/render/defaults/qdiffusespecularmapmaterial_p.h b/src/render/defaults/qdiffusespecularmapmaterial_p.h new file mode 100644 index 000000000..bed6b62ae --- /dev/null +++ b/src/render/defaults/qdiffusespecularmapmaterial_p.h @@ -0,0 +1,93 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QT3D_RENDER_QDIFFUSESPECULARMAPMATERIAL_P_H +#define QT3D_RENDER_QDIFFUSESPECULARMAPMATERIAL_P_H + +#include <Qt3DRenderer/private/qmaterial_p.h> + +QT_BEGIN_NAMESPACE + +namespace Qt3D { + +class QEffect; +class QTexture; +class QTechnique; +class QParameter; +class QShaderProgram; +class QRenderPass; +class QParameterMapping; + +class QDiffuseSpecularMapMaterial; + +class QDiffuseSpecularMapMaterialPrivate : public QMaterialPrivate +{ +public: + QDiffuseSpecularMapMaterialPrivate(QDiffuseSpecularMapMaterial *qq); + + void init(); + + QEffect *m_diffuseSpecularMapEffect; + QTexture *m_diffuseTexture; + QTexture *m_specularTexture; + QParameter *m_ambientParameter; + QParameter *m_diffuseParameter; + QParameter *m_specularParameter; + QParameter *m_shininessParameter; + QParameter *m_textureScaleParameter; + QTechnique *m_diffuseSpecularMapGL3Technique; + QTechnique *m_diffuseSpecularMapGL2Technique; + QTechnique *m_diffuseSpecularMapES2Technique; + QRenderPass *m_diffuseSpecularMapGL3RenderPass; + QRenderPass *m_diffuseSpecularMapGL2RenderPass; + QRenderPass *m_diffuseSpecularMapES2RenderPass; + QShaderProgram *m_diffuseSpecularMapGL3Shader; + QShaderProgram *m_diffuseSpecularMapGL2ES2Shader; + + Q_DECLARE_PUBLIC(QDiffuseSpecularMapMaterial) +}; + +} // Qt3D + +QT_END_NAMESPACE + +#endif // QT3D_RENDER_QDIFFUSESPECULARMAPMATERIAL_P_H + diff --git a/src/render/defaults/qnormaldiffusemapalphamaterial.cpp b/src/render/defaults/qnormaldiffusemapalphamaterial.cpp new file mode 100644 index 000000000..fe381b6db --- /dev/null +++ b/src/render/defaults/qnormaldiffusemapalphamaterial.cpp @@ -0,0 +1,161 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qnormaldiffusemapalphamaterial.h" +#include "qnormaldiffusemapalphamaterial_p.h" + +#include <Qt3DRenderer/qeffect.h> +#include <Qt3DRenderer/qtexture.h> +#include <Qt3DRenderer/qtechnique.h> +#include <Qt3DRenderer/qparameter.h> +#include <Qt3DRenderer/qshaderprogram.h> +#include <Qt3DRenderer/qrenderpass.h> +#include <Qt3DRenderer/qopenglfilter.h> +#include <Qt3DRenderer/qalphacoverage.h> +#include <Qt3DRenderer/qdepthtest.h> + +#include <QUrl> + +QT_BEGIN_NAMESPACE + +namespace Qt3D { + + +QNormalDiffuseMapAlphaMaterialPrivate::QNormalDiffuseMapAlphaMaterialPrivate(QNormalDiffuseMapAlphaMaterial *qq) + : QNormalDiffuseMapMaterialPrivate(qq) + , m_alphaCoverage(new QAlphaCoverage()) + , m_depthTest(new QDepthTest()) +{ +} + +void QNormalDiffuseMapAlphaMaterialPrivate::init() +{ + m_normalDiffuseGL3Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/normaldiffusemap.vert")))); + m_normalDiffuseGL3Shader->setFragmentShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/normaldiffusemapalpha.frag")))); + m_normalDiffuseGL2ES2Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/es2/normaldiffusemap.vert")))); + m_normalDiffuseGL2ES2Shader->setFragmentShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/es2/normaldiffusemapalpha.frag")))); + + m_normalDiffuseGL3Technique->openGLFilter()->setApi(QOpenGLFilter::Desktop); + m_normalDiffuseGL3Technique->openGLFilter()->setMajorVersion(3); + m_normalDiffuseGL3Technique->openGLFilter()->setMinorVersion(1); + m_normalDiffuseGL3Technique->openGLFilter()->setProfile(QOpenGLFilter::Core); + + m_normalDiffuseGL2Technique->openGLFilter()->setApi(QOpenGLFilter::Desktop); + m_normalDiffuseGL2Technique->openGLFilter()->setMajorVersion(2); + m_normalDiffuseGL2Technique->openGLFilter()->setMinorVersion(0); + m_normalDiffuseGL2Technique->openGLFilter()->setProfile(QOpenGLFilter::None); + + m_normalDiffuseES2Technique->openGLFilter()->setApi(QOpenGLFilter::ES); + m_normalDiffuseES2Technique->openGLFilter()->setMajorVersion(2); + m_normalDiffuseES2Technique->openGLFilter()->setMinorVersion(0); + m_normalDiffuseES2Technique->openGLFilter()->setProfile(QOpenGLFilter::None); + + m_depthTest->setFunc(QDepthTest::Less); + + m_normalDiffuseGL3RenderPass->setShaderProgram(m_normalDiffuseGL3Shader); + m_normalDiffuseGL3RenderPass->addRenderState(m_alphaCoverage); + m_normalDiffuseGL3RenderPass->addRenderState(m_depthTest); + + m_normalDiffuseGL2RenderPass->setShaderProgram(m_normalDiffuseGL2ES2Shader); + m_normalDiffuseGL2RenderPass->addRenderState(m_alphaCoverage); + m_normalDiffuseGL2RenderPass->addRenderState(m_depthTest); + + m_normalDiffuseES2RenderPass->setShaderProgram(m_normalDiffuseGL2ES2Shader); + m_normalDiffuseES2RenderPass->addRenderState(m_alphaCoverage); + m_normalDiffuseES2RenderPass->addRenderState(m_depthTest); + + m_normalDiffuseGL3Technique->addPass(m_normalDiffuseGL3RenderPass); + m_normalDiffuseGL2Technique->addPass(m_normalDiffuseGL2RenderPass); + m_normalDiffuseES2Technique->addPass(m_normalDiffuseES2RenderPass); + + m_normalDiffuseEffect->addTechnique(m_normalDiffuseGL3Technique); + m_normalDiffuseEffect->addTechnique(m_normalDiffuseGL2Technique); + m_normalDiffuseEffect->addTechnique(m_normalDiffuseES2Technique); + + m_normalDiffuseEffect->addParameter(m_ambientParameter); + m_normalDiffuseEffect->addParameter(m_diffuseParameter); + m_normalDiffuseEffect->addParameter(m_normalParameter); + m_normalDiffuseEffect->addParameter(m_specularParameter); + m_normalDiffuseEffect->addParameter(m_shininessParameter); + m_normalDiffuseEffect->addParameter(m_textureScaleParameter); + + static_cast<QMaterial *>(q_ptr)->setEffect(m_normalDiffuseEffect); +} + + +/*! + \class Qt3D::QNormalDiffuseMapAlphaMaterial + \brief The QNormalDiffuseMapAlphaMaterial provides a default implementation of the phong lighting and bump effect where the diffuse light component + is read from a texture map and the normals of the mesh being rendered from a normal texture map. In addition, it defines an alpha to coverage and + a depth test to be performed in the rendering pass. + \inmodule Qt3DRender + \since 5.5 + + The specular lighting effect is based on the combination of 3 lighting components ambient, diffuse and specular. + The relative strengths of these components is controlled by means of their reflectivity coefficients which are modelled as RGB triplets: + + \list + \li Ambient is the color that is emitted by an object without any other light source. + \li Diffuse is the color that is emitted for rought surface reflections with the lights. + \li Specular is the color emitted for shiny surface reflections with the lights. + \li The shininess of a surface is controlled by a float property. + \endlist + + This material uses an effect with a single render pass approach and performs per fragment lighting. + Techniques are provided for OpenGL 2, OpenGL 3 or above as well as OpenGL ES 2. +*/ + +/*! + \fn Qt3D::QNormalDiffuseMapAlphaMaterial::QNormalDiffuseMapAlphaMaterial() + Constructs a new Qt3D::QNormalDiffuseMapAlphaMaterial instance with parent object \a parent. +*/ +QNormalDiffuseMapAlphaMaterial::QNormalDiffuseMapAlphaMaterial(QNode *parent) + : QNormalDiffuseMapMaterial(*new QNormalDiffuseMapAlphaMaterialPrivate(this), parent) +{ +} + +QNormalDiffuseMapAlphaMaterial::~QNormalDiffuseMapAlphaMaterial() +{ +} + +} // Qt3D + +QT_END_NAMESPACE diff --git a/src/render/defaults/qnormaldiffusemapalphamaterial.h b/src/render/defaults/qnormaldiffusemapalphamaterial.h new file mode 100644 index 000000000..20682dfa2 --- /dev/null +++ b/src/render/defaults/qnormaldiffusemapalphamaterial.h @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QT3D_RENDER_QNORMALDIFFUSEMAPALPHAMATERIAL_H +#define QT3D_RENDER_QNORMALDIFFUSEMAPALPHAMATERIAL_H + +#include <Qt3DRenderer/qnormaldiffusemapmaterial.h> + +QT_BEGIN_NAMESPACE + +namespace Qt3D { + +class QNormalDiffuseMapAlphaMaterialPrivate; + +class QT3DRENDERERSHARED_EXPORT QNormalDiffuseMapAlphaMaterial : public QNormalDiffuseMapMaterial +{ + Q_OBJECT +public: + explicit QNormalDiffuseMapAlphaMaterial(QNode *parent = 0); + ~QNormalDiffuseMapAlphaMaterial(); + +private: + Q_DECLARE_PRIVATE(QNormalDiffuseMapAlphaMaterial) +}; + +} // Qt3D + +QT_END_NAMESPACE + +#endif // QT3D_RENDER_QNORMALDIFFUSEMAPALPHAMATERIAL_H diff --git a/src/render/defaults/qnormaldiffusemapalphamaterial_p.h b/src/render/defaults/qnormaldiffusemapalphamaterial_p.h new file mode 100644 index 000000000..003d97bb1 --- /dev/null +++ b/src/render/defaults/qnormaldiffusemapalphamaterial_p.h @@ -0,0 +1,74 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QT3D_RENDER_QNORMALDIFFUSEMAPALPHAMATERIAL_P_H +#define QT3D_RENDER_QNORMALDIFFUSEMAPALPHAMATERIAL_P_H + +#include <Qt3DRenderer/private/qnormaldiffusemapmaterial_p.h> + +QT_BEGIN_NAMESPACE + +namespace Qt3D { + +class QNormalDiffuseMapAlphaMaterial; + +class QAlphaCoverage; +class QDepthTest; + +class QNormalDiffuseMapAlphaMaterialPrivate: public QNormalDiffuseMapMaterialPrivate +{ +public: + QNormalDiffuseMapAlphaMaterialPrivate(QNormalDiffuseMapAlphaMaterial *qq); + + void init() Q_DECL_OVERRIDE; + + QAlphaCoverage *m_alphaCoverage; + QDepthTest *m_depthTest; + + Q_DECLARE_PUBLIC(QNormalDiffuseMapAlphaMaterial) +}; + +} // Qt3D + +QT_END_NAMESPACE + +#endif // QNORMALDIFFUSEMAPALPHAMATERIAL_P_H + diff --git a/src/render/defaults/qnormaldiffusemapmaterial.cpp b/src/render/defaults/qnormaldiffusemapmaterial.cpp new file mode 100644 index 000000000..5a8458e56 --- /dev/null +++ b/src/render/defaults/qnormaldiffusemapmaterial.cpp @@ -0,0 +1,319 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qnormaldiffusemapmaterial.h" +#include "qnormaldiffusemapmaterial_p.h" + +#include <Qt3DRenderer/qeffect.h> +#include <Qt3DRenderer/qtexture.h> +#include <Qt3DRenderer/qtechnique.h> +#include <Qt3DRenderer/qparameter.h> +#include <Qt3DRenderer/qshaderprogram.h> +#include <Qt3DRenderer/qrenderpass.h> +#include <Qt3DRenderer/qopenglfilter.h> + +#include <QUrl> + +QT_BEGIN_NAMESPACE + +namespace Qt3D { + +QNormalDiffuseMapMaterialPrivate::QNormalDiffuseMapMaterialPrivate(QNormalDiffuseMapMaterial *qq) + : QMaterialPrivate(qq) + , m_normalDiffuseEffect(new QEffect()) + , m_diffuseTexture(new QTexture(QTexture::Target2D)) + , m_normalTexture(new QTexture(QTexture::Target2D)) + , m_ambientParameter(new QParameter(QStringLiteral("ka"), QColor::fromRgbF(0.1f, 0.1f, 0.1f, 1.0f))) + , m_diffuseParameter(new QParameter(QStringLiteral("diffuseTexture"), m_diffuseTexture)) + , m_normalParameter(new QParameter(QStringLiteral("normalTexture"), m_normalTexture)) + , m_specularParameter(new QParameter(QStringLiteral("ks"), QColor::fromRgbF(0.95f, 0.95f, 0.95f, 1.0f))) + , m_shininessParameter(new QParameter(QStringLiteral("shininess"), 150.0f)) + , m_textureScaleParameter(new QParameter(QStringLiteral("texCoordScale"), 1.0f)) + , m_normalDiffuseGL3Technique(new QTechnique()) + , m_normalDiffuseGL2Technique(new QTechnique()) + , m_normalDiffuseES2Technique(new QTechnique()) + , m_normalDiffuseGL3RenderPass(new QRenderPass()) + , m_normalDiffuseGL2RenderPass(new QRenderPass()) + , m_normalDiffuseES2RenderPass(new QRenderPass()) + , m_normalDiffuseGL3Shader(new QShaderProgram()) + , m_normalDiffuseGL2ES2Shader(new QShaderProgram()) +{ + m_diffuseTexture->setMagnificationFilter(QTexture::Linear); + m_diffuseTexture->setMinificationFilter(QTexture::LinearMipMapLinear); + m_diffuseTexture->setWrapMode(QTextureWrapMode(QTextureWrapMode::Repeat)); + m_diffuseTexture->setGenerateMipMaps(true); + m_diffuseTexture->setMaximumAnisotropy(16.0f); + + m_normalTexture->setMagnificationFilter(QTexture::Linear); + m_normalTexture->setMinificationFilter(QTexture::Linear); + m_normalTexture->setWrapMode(QTextureWrapMode(QTextureWrapMode::Repeat)); + m_normalTexture->setMaximumAnisotropy(16.0f); +} + +void QNormalDiffuseMapMaterialPrivate::init() +{ + m_normalDiffuseGL3Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/normaldiffusemap.vert")))); + m_normalDiffuseGL3Shader->setFragmentShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/normaldiffusemap.frag")))); + m_normalDiffuseGL2ES2Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/es2/normaldiffusemap.vert")))); + m_normalDiffuseGL2ES2Shader->setFragmentShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/es2/normaldiffusemap.frag")))); + + m_normalDiffuseGL3Technique->openGLFilter()->setApi(QOpenGLFilter::Desktop); + m_normalDiffuseGL3Technique->openGLFilter()->setMajorVersion(3); + m_normalDiffuseGL3Technique->openGLFilter()->setMinorVersion(1); + m_normalDiffuseGL3Technique->openGLFilter()->setProfile(QOpenGLFilter::Core); + + m_normalDiffuseGL2Technique->openGLFilter()->setApi(QOpenGLFilter::Desktop); + m_normalDiffuseGL2Technique->openGLFilter()->setMajorVersion(2); + m_normalDiffuseGL2Technique->openGLFilter()->setMinorVersion(0); + m_normalDiffuseGL2Technique->openGLFilter()->setProfile(QOpenGLFilter::None); + + m_normalDiffuseES2Technique->openGLFilter()->setApi(QOpenGLFilter::ES); + m_normalDiffuseES2Technique->openGLFilter()->setMajorVersion(2); + m_normalDiffuseES2Technique->openGLFilter()->setMinorVersion(0); + m_normalDiffuseES2Technique->openGLFilter()->setProfile(QOpenGLFilter::None); + + m_normalDiffuseGL3RenderPass->setShaderProgram(m_normalDiffuseGL3Shader); + m_normalDiffuseGL2RenderPass->setShaderProgram(m_normalDiffuseGL2ES2Shader); + m_normalDiffuseES2RenderPass->setShaderProgram(m_normalDiffuseGL2ES2Shader); + + m_normalDiffuseGL3Technique->addPass(m_normalDiffuseGL3RenderPass); + m_normalDiffuseGL2Technique->addPass(m_normalDiffuseGL2RenderPass); + m_normalDiffuseES2Technique->addPass(m_normalDiffuseES2RenderPass); + + m_normalDiffuseEffect->addTechnique(m_normalDiffuseGL3Technique); + m_normalDiffuseEffect->addTechnique(m_normalDiffuseGL2Technique); + m_normalDiffuseEffect->addTechnique(m_normalDiffuseES2Technique); + + m_normalDiffuseEffect->addParameter(m_ambientParameter); + m_normalDiffuseEffect->addParameter(m_diffuseParameter); + m_normalDiffuseEffect->addParameter(m_normalParameter); + m_normalDiffuseEffect->addParameter(m_specularParameter); + m_normalDiffuseEffect->addParameter(m_shininessParameter); + m_normalDiffuseEffect->addParameter(m_textureScaleParameter); + + static_cast<QMaterial *>(q_ptr)->setEffect(m_normalDiffuseEffect); +} + +/*! + \class Qt3D::QNormalDiffuseMapMaterial + \brief The QNormalDiffuseMapMaterial provides a default implementation of the phong lighting and bump effect where the diffuse light component + is read from a texture map and the normals of the mesh being rendered from a normal texture map. + \inmodule Qt3DRender + \since 5.5 + + The specular lighting effect is based on the combination of 3 lighting components ambient, diffuse and specular. + The relative strengths of these components is controlled by means of their reflectivity coefficients which are modelled as RGB triplets: + + \list + \li Ambient is the color that is emitted by an object without any other light source. + \li Diffuse is the color that is emitted for rought surface reflections with the lights. + \li Specular is the color emitted for shiny surface reflections with the lights. + \li The shininess of a surface is controlled by a float property. + \endlist + + This material uses an effect with a single render pass approach and performs per fragment lighting. + Techniques are provided for OpenGL 2, OpenGL 3 or above as well as OpenGL ES 2. +*/ + +/*! + \fn Qt3D::QNormalDiffuseMapMaterial::QNormalDiffuseMapMaterial() + Constructs a new Qt3D::QNormalDiffuseMapMaterial instance with parent object \a parent. +*/ +QNormalDiffuseMapMaterial::QNormalDiffuseMapMaterial(QNode *parent) + : QMaterial(*new QNormalDiffuseMapMaterialPrivate(this), parent) +{ + Q_D(QNormalDiffuseMapMaterial); + QObject::connect(d->m_ambientParameter, SIGNAL(valueChanged()), this, SIGNAL(ambientChanged())); + QObject::connect(d->m_diffuseParameter, SIGNAL(valueChanged()), this, SIGNAL(diffuseChanged())); + QObject::connect(d->m_normalParameter, SIGNAL(valueChanged()), this, SIGNAL(normalChanged())); + QObject::connect(d->m_specularParameter, SIGNAL(valueChanged()), this, SIGNAL(specularChanged())); + QObject::connect(d->m_shininessParameter, SIGNAL(valueChanged()), this, SIGNAL(shininessChanged())); + QObject::connect(d->m_textureScaleParameter, SIGNAL(valueChanged()), this, SIGNAL(textureScaleChanged())); + d->init(); +} + +QNormalDiffuseMapMaterial::QNormalDiffuseMapMaterial(QNormalDiffuseMapMaterialPrivate &dd, QNode *parent) + : QMaterial(dd, parent) +{ + Q_D(QNormalDiffuseMapMaterial); + QObject::connect(d->m_ambientParameter, SIGNAL(valueChanged()), this, SIGNAL(ambientChanged())); + QObject::connect(d->m_diffuseParameter, SIGNAL(valueChanged()), this, SIGNAL(diffuseChanged())); + QObject::connect(d->m_normalParameter, SIGNAL(valueChanged()), this, SIGNAL(normalChanged())); + QObject::connect(d->m_specularParameter, SIGNAL(valueChanged()), this, SIGNAL(specularChanged())); + QObject::connect(d->m_shininessParameter, SIGNAL(valueChanged()), this, SIGNAL(shininessChanged())); + QObject::connect(d->m_textureScaleParameter, SIGNAL(valueChanged()), this, SIGNAL(textureScaleChanged())); + d->init(); +} + +QNormalDiffuseMapMaterial::~QNormalDiffuseMapMaterial() +{ +} + +/*! + \fn Qt3D::QNormalDiffuseMapMaterial::ambient() const + \return the current QColor ambient value. + */ +QColor QNormalDiffuseMapMaterial::ambient() const +{ + Q_D(const QNormalDiffuseMapMaterial); + return d->m_ambientParameter->value().value<QColor>(); +} + +/*! + \fn Qt3D::QNormalDiffuseMapMaterial::specular() const + \return the current QColor specular value. + */ +QColor QNormalDiffuseMapMaterial::specular() const +{ + Q_D(const QNormalDiffuseMapMaterial); + return d->m_specularParameter->value().value<QColor>(); +} + +/*! + \fn Qt3D::QNormalDiffuseMapMaterial::diffuse() const + \return the current diffuse map texture. + \b {Note:} By default the diffuse texture has a linear + magnification filter, a linear mipmap linear minification filter, + the wrap mode is repeat, the maximum anisotropy is set to 16.0 + and mipmapping is enabled. + */ +QTexture *QNormalDiffuseMapMaterial::diffuse() const +{ + Q_D(const QNormalDiffuseMapMaterial); + return d->m_diffuseParameter->value().value<QTexture *>(); +} + +/*! + \fn Qt3D::QNormalDiffuseMapMaterial::normal() const + \return the current normal map texture. + \b {Note:} By default the normal texture has linear minification + and magnification filters, the wrap mode is repeat and maximum anisotropy + is set to 16.0. + */ +QTexture *QNormalDiffuseMapMaterial::normal() const +{ + Q_D(const QNormalDiffuseMapMaterial); + return d->m_normalParameter->value().value<QTexture *>(); +} + +/*! + \fn Qt3D::QNormalDiffuseMapMaterial::shininess() const + \return the current shininess as a float value. + */ +float QNormalDiffuseMapMaterial::shininess() const +{ + Q_D(const QNormalDiffuseMapMaterial); + return d->m_shininessParameter->value().toFloat(); +} + +/*! + \fn Qt3D::QNormalDiffuseMapMaterial::textureScale() const + \return the current texture scale. + */ +float QNormalDiffuseMapMaterial::textureScale() const +{ + Q_D(const QNormalDiffuseMapMaterial); + return d->m_textureScaleParameter->value().toFloat(); +} + +/*! + \fn Qt3D::QNormalDiffuseMapMaterial::setAmbient(const QColor &ambient) + Sets the current ambient color to \a ambient + */ +void QNormalDiffuseMapMaterial::setAmbient(const QColor &ambient) +{ + Q_D(QNormalDiffuseMapMaterial); + d->m_ambientParameter->setValue(ambient); +} + +/*! + \fn Qt3D::QNormalDiffuseMapMaterial::setSpecular(const QColor &specular) + Sets the current specular color to \a specular. + */ +void QNormalDiffuseMapMaterial::setSpecular(const QColor &specular) +{ + Q_D(QNormalDiffuseMapMaterial); + d->m_specularParameter->setValue(specular); +} + +/*! + \fn Qt3D::QNormalDiffuseMapMaterial::setDiffuse(QTexture *diffuse) + Sets the current diffuse map to \a diffuse. + */ +void QNormalDiffuseMapMaterial::setDiffuse(QTexture *diffuse) +{ + Q_D(QNormalDiffuseMapMaterial); + d->m_diffuseParameter->setValue(QVariant::fromValue(diffuse)); +} + +/*! + \fn Qt3D::QNormalDiffuseMapMaterial::setNormal(QTexture *normal) + Sets the current normal map to \a normal. + */ +void QNormalDiffuseMapMaterial::setNormal(QTexture *normal) +{ + Q_D(QNormalDiffuseMapMaterial); + d->m_normalParameter->setValue(QVariant::fromValue(normal)); +} + +/*! + \fn Qt3D::QNormalDiffuseMapMaterial::setShininess(float shininess) + Sets the current shininess value to \a shininess. + */ +void QNormalDiffuseMapMaterial::setShininess(float shininess) +{ + Q_D(QNormalDiffuseMapMaterial); + d->m_shininessParameter->setValue(shininess); +} + +/*! + \fn Qt3D::QNormalDiffuseMapMaterial::setTextureScale(float textureScale) + Sets the current texture scale to \a textureScale. + */ +void QNormalDiffuseMapMaterial::setTextureScale(float textureScale) +{ + Q_D(QNormalDiffuseMapMaterial); + d->m_textureScaleParameter->setValue(textureScale); +} + +} // Qt3D + +QT_END_NAMESPACE diff --git a/src/render/defaults/qnormaldiffusemapmaterial.h b/src/render/defaults/qnormaldiffusemapmaterial.h new file mode 100644 index 000000000..34c1cf7c3 --- /dev/null +++ b/src/render/defaults/qnormaldiffusemapmaterial.h @@ -0,0 +1,102 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QT3D_RENDER_QNORMALDIFFUSEMAPMATERIAL_H +#define QT3D_RENDER_QNORMALDIFFUSEMAPMATERIAL_H + +#include <Qt3DRenderer/qmaterial.h> +#include <QColor> + +QT_BEGIN_NAMESPACE + +namespace Qt3D { + +class QTexture; +class QNormalDiffuseMapMaterialPrivate; + +class QT3DRENDERERSHARED_EXPORT QNormalDiffuseMapMaterial : public QMaterial +{ + Q_OBJECT + Q_PROPERTY(QColor ambient READ ambient WRITE setAmbient NOTIFY ambientChanged) + Q_PROPERTY(QColor specular READ specular WRITE setSpecular NOTIFY specularChanged) + Q_PROPERTY(QTexture *diffuse READ diffuse WRITE setDiffuse NOTIFY diffuseChanged) + Q_PROPERTY(QTexture *normal READ normal WRITE setNormal NOTIFY normalChanged) + Q_PROPERTY(float shininess READ shininess WRITE setShininess NOTIFY shininessChanged) + Q_PROPERTY(float textureScale READ textureScale WRITE setTextureScale NOTIFY textureScaleChanged) + +public: + explicit QNormalDiffuseMapMaterial(QNode *parent = 0); + ~QNormalDiffuseMapMaterial(); + + QColor ambient() const; + QColor specular() const; + QTexture *diffuse() const; + QTexture *normal() const; + float shininess() const; + float textureScale() const; + + void setAmbient(const QColor &ambient); + void setSpecular(const QColor &specular); + void setDiffuse(QTexture *diffuse); + void setNormal(QTexture *normal); + void setShininess(float shininess); + void setTextureScale(float textureScale); + +Q_SIGNALS: + void ambientChanged(); + void diffuseChanged(); + void normalChanged(); + void specularChanged(); + void shininessChanged(); + void textureScaleChanged(); + +protected: + QNormalDiffuseMapMaterial(QNormalDiffuseMapMaterialPrivate &dd, QNode *parent = 0); + +private: + Q_DECLARE_PRIVATE(QNormalDiffuseMapMaterial) +}; + +} // Qt3D + +QT_END_NAMESPACE + +#endif // QNORMALDIFFUSEMAPMATERIAL_H diff --git a/src/render/defaults/qnormaldiffusemapmaterial_p.h b/src/render/defaults/qnormaldiffusemapmaterial_p.h new file mode 100644 index 000000000..0c11d3214 --- /dev/null +++ b/src/render/defaults/qnormaldiffusemapmaterial_p.h @@ -0,0 +1,94 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QT3D_RENDER_QNORMALDIFFUSEMAPMATERIAL_P_H +#define QT3D_RENDER_QNORMALDIFFUSEMAPMATERIAL_P_H + +#include <Qt3DRenderer/private/qmaterial_p.h> + +QT_BEGIN_NAMESPACE + +namespace Qt3D { + +class QEffect; +class QTexture; +class QTechnique; +class QParameter; +class QShaderProgram; +class QRenderPass; +class QParameterMapping; + +class QNormalDiffuseMapMaterial; + +class QNormalDiffuseMapMaterialPrivate: public QMaterialPrivate +{ +public: + QNormalDiffuseMapMaterialPrivate(QNormalDiffuseMapMaterial *qq); + + virtual void init(); + + QEffect *m_normalDiffuseEffect; + QTexture *m_diffuseTexture; + QTexture *m_normalTexture; + QParameter *m_ambientParameter; + QParameter *m_diffuseParameter; + QParameter *m_normalParameter; + QParameter *m_specularParameter; + QParameter *m_shininessParameter; + QParameter *m_textureScaleParameter; + QTechnique *m_normalDiffuseGL3Technique; + QTechnique *m_normalDiffuseGL2Technique; + QTechnique *m_normalDiffuseES2Technique; + QRenderPass *m_normalDiffuseGL3RenderPass; + QRenderPass *m_normalDiffuseGL2RenderPass; + QRenderPass *m_normalDiffuseES2RenderPass; + QShaderProgram *m_normalDiffuseGL3Shader; + QShaderProgram *m_normalDiffuseGL2ES2Shader; + + Q_DECLARE_PUBLIC(QNormalDiffuseMapMaterial) +}; + +} // Qt3D + +QT_END_NAMESPACE + +#endif // QT3D_RENDER_QNORMALDIFFUSEMAPMATERIAL_P_H + diff --git a/src/render/defaults/qnormaldiffusespecularmapmaterial.cpp b/src/render/defaults/qnormaldiffusespecularmapmaterial.cpp new file mode 100644 index 000000000..d15d1efae --- /dev/null +++ b/src/render/defaults/qnormaldiffusespecularmapmaterial.cpp @@ -0,0 +1,330 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qnormaldiffusespecularmapmaterial.h" +#include "qnormaldiffusespecularmapmaterial_p.h" +#include <Qt3DRenderer/qmaterial.h> +#include <Qt3DRenderer/qeffect.h> +#include <Qt3DRenderer/qtexture.h> +#include <Qt3DRenderer/qtechnique.h> +#include <Qt3DRenderer/qshaderprogram.h> +#include <Qt3DRenderer/qparameter.h> +#include <Qt3DRenderer/qrenderpass.h> +#include <Qt3DRenderer/qopenglfilter.h> +#include <QUrl> + +QT_BEGIN_NAMESPACE + +namespace Qt3D { + +QNormalDiffuseSpecularMapMaterialPrivate::QNormalDiffuseSpecularMapMaterialPrivate(QNormalDiffuseSpecularMapMaterial *qq) + : QMaterialPrivate(qq) + , m_normalDiffuseSpecularEffect(new QEffect()) + , m_diffuseTexture(new QTexture(QTexture::Target2D)) + , m_normalTexture(new QTexture(QTexture::Target2D)) + , m_specularTexture(new QTexture(QTexture::Target2D)) + , m_ambientParameter(new QParameter(QStringLiteral("ka"), QColor::fromRgbF(0.05f, 0.05f, 0.05f, 1.0f))) + , m_diffuseParameter(new QParameter(QStringLiteral("diffuseTexture"), m_diffuseTexture)) + , m_normalParameter(new QParameter(QStringLiteral("normalTexture"), m_normalTexture)) + , m_specularParameter(new QParameter(QStringLiteral("specularTexture"), m_specularTexture)) + , m_shininessParameter(new QParameter(QStringLiteral("shininess"), 150.0f)) + , m_textureScaleParameter(new QParameter(QStringLiteral("texCoordScale"), 1.0f)) + , m_normalDiffuseSpecularGL3Technique(new QTechnique()) + , m_normalDiffuseSpecularGL2Technique(new QTechnique()) + , m_normalDiffuseSpecularES2Technique(new QTechnique()) + , m_normalDiffuseSpecularGL3RenderPass(new QRenderPass()) + , m_normalDiffuseSpecularGL2RenderPass(new QRenderPass()) + , m_normalDiffuseSpecularES2RenderPass(new QRenderPass()) + , m_normalDiffuseSpecularGL3Shader(new QShaderProgram()) + , m_normalDiffuseSpecularGL2ES2Shader(new QShaderProgram()) +{ + m_diffuseTexture->setMagnificationFilter(QTexture::Linear); + m_diffuseTexture->setMinificationFilter(QTexture::LinearMipMapLinear); + m_diffuseTexture->setWrapMode(QTextureWrapMode(QTextureWrapMode::Repeat)); + m_diffuseTexture->setGenerateMipMaps(true); + m_diffuseTexture->setMaximumAnisotropy(16.0f); + + m_normalTexture->setMagnificationFilter(QTexture::Linear); + m_normalTexture->setMinificationFilter(QTexture::Linear); + m_normalTexture->setWrapMode(QTextureWrapMode(QTextureWrapMode::Repeat)); + m_normalTexture->setMaximumAnisotropy(16.0f); + + m_specularTexture->setMagnificationFilter(QTexture::Linear); + m_specularTexture->setMinificationFilter(QTexture::LinearMipMapLinear); + m_specularTexture->setWrapMode(QTextureWrapMode(QTextureWrapMode::Repeat)); + m_specularTexture->setGenerateMipMaps(true); + m_specularTexture->setMaximumAnisotropy(16.0f); +} + +void QNormalDiffuseSpecularMapMaterialPrivate::init() +{ + m_normalDiffuseSpecularGL3Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/normaldiffusespecularmap.vert")))); + m_normalDiffuseSpecularGL3Shader->setFragmentShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/normaldiffusespecularmap.frag")))); + m_normalDiffuseSpecularGL2ES2Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/es2/normaldiffusespecularmap.vert")))); + m_normalDiffuseSpecularGL2ES2Shader->setFragmentShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/es2/normaldiffusespecularmap.frag")))); + + m_normalDiffuseSpecularGL3Technique->openGLFilter()->setApi(QOpenGLFilter::Desktop); + m_normalDiffuseSpecularGL3Technique->openGLFilter()->setMajorVersion(3); + m_normalDiffuseSpecularGL3Technique->openGLFilter()->setMinorVersion(1); + m_normalDiffuseSpecularGL3Technique->openGLFilter()->setProfile(QOpenGLFilter::Core); + + m_normalDiffuseSpecularGL2Technique->openGLFilter()->setApi(QOpenGLFilter::Desktop); + m_normalDiffuseSpecularGL2Technique->openGLFilter()->setMajorVersion(2); + m_normalDiffuseSpecularGL2Technique->openGLFilter()->setMinorVersion(0); + m_normalDiffuseSpecularGL2Technique->openGLFilter()->setProfile(QOpenGLFilter::None); + + m_normalDiffuseSpecularES2Technique->openGLFilter()->setApi(QOpenGLFilter::ES); + m_normalDiffuseSpecularES2Technique->openGLFilter()->setMajorVersion(2); + m_normalDiffuseSpecularES2Technique->openGLFilter()->setMinorVersion(0); + m_normalDiffuseSpecularES2Technique->openGLFilter()->setProfile(QOpenGLFilter::None); + + m_normalDiffuseSpecularGL3RenderPass->setShaderProgram(m_normalDiffuseSpecularGL3Shader); + m_normalDiffuseSpecularGL2RenderPass->setShaderProgram(m_normalDiffuseSpecularGL2ES2Shader); + m_normalDiffuseSpecularES2RenderPass->setShaderProgram(m_normalDiffuseSpecularGL2ES2Shader); + + m_normalDiffuseSpecularGL3Technique->addPass(m_normalDiffuseSpecularGL3RenderPass); + m_normalDiffuseSpecularGL2Technique->addPass(m_normalDiffuseSpecularGL2RenderPass); + m_normalDiffuseSpecularES2Technique->addPass(m_normalDiffuseSpecularES2RenderPass); + + m_normalDiffuseSpecularEffect->addTechnique(m_normalDiffuseSpecularGL3Technique); + m_normalDiffuseSpecularEffect->addTechnique(m_normalDiffuseSpecularGL2Technique); + m_normalDiffuseSpecularEffect->addTechnique(m_normalDiffuseSpecularES2Technique); + + m_normalDiffuseSpecularEffect->addParameter(m_ambientParameter); + m_normalDiffuseSpecularEffect->addParameter(m_diffuseParameter); + m_normalDiffuseSpecularEffect->addParameter(m_normalParameter); + m_normalDiffuseSpecularEffect->addParameter(m_specularParameter); + m_normalDiffuseSpecularEffect->addParameter(m_shininessParameter); + m_normalDiffuseSpecularEffect->addParameter(m_textureScaleParameter); + + static_cast<QMaterial *>(q_ptr)->setEffect(m_normalDiffuseSpecularEffect); +} + +/*! + \class Qt3D::QNormalDiffuseSpecularMapMaterial + \brief The QNormalDiffuseSpecularMapMaterial provides a default implementation of the phong lighting and bump effect where the diffuse and specular light components + are read from texture maps and the normals of the mesh being rendered from a normal texture map. + \inmodule Qt3DRender + \since 5.5 + + The specular lighting effect is based on the combination of 3 lighting components ambient, diffuse and specular. + The relative strengths of these components is controlled by means of their reflectivity coefficients which are modelled as RGB triplets: + + \list + \li Ambient is the color that is emitted by an object without any other light source. + \li Diffuse is the color that is emitted for rought surface reflections with the lights. + \li Specular is the color emitted for shiny surface reflections with the lights. + \li The shininess of a surface is controlled by a float property. + \endlist + + This material uses an effect with a single render pass approach and performs per fragment lighting. + Techniques are provided for OpenGL 2, OpenGL 3 or above as well as OpenGL ES 2. +*/ + +/*! + \fn Qt3D::QNormalDiffuseSpecularMapMaterial::QNormalDiffuseSpecularMapMaterial() + Constructs a new Qt3D::QNormalDiffuseSpecularMapMaterial instance with parent object \a parent. +*/ +QNormalDiffuseSpecularMapMaterial::QNormalDiffuseSpecularMapMaterial(QNode *parent) + : QMaterial(*new QNormalDiffuseSpecularMapMaterialPrivate(this), parent) +{ + Q_D(QNormalDiffuseSpecularMapMaterial); + QObject::connect(d->m_ambientParameter, SIGNAL(valueChanged()), this, SIGNAL(ambientChanged())); + QObject::connect(d->m_diffuseParameter, SIGNAL(valueChanged()), this, SIGNAL(diffuseChanged())); + QObject::connect(d->m_normalParameter, SIGNAL(valueChanged()), this, SIGNAL(normalChanged())); + QObject::connect(d->m_specularParameter, SIGNAL(valueChanged()), this, SIGNAL(specularChanged())); + QObject::connect(d->m_shininessParameter, SIGNAL(valueChanged()), this, SIGNAL(shininessChanged())); + QObject::connect(d->m_textureScaleParameter, SIGNAL(valueChanged()), this, SIGNAL(textureScaleChanged())); + d->init(); +} + + +QNormalDiffuseSpecularMapMaterial::QNormalDiffuseSpecularMapMaterial(QNormalDiffuseSpecularMapMaterialPrivate &dd, QNode *parent) + : QMaterial(dd, parent) +{ + Q_D(QNormalDiffuseSpecularMapMaterial); + QObject::connect(d->m_ambientParameter, SIGNAL(valueChanged()), this, SIGNAL(ambientChanged())); + QObject::connect(d->m_diffuseParameter, SIGNAL(valueChanged()), this, SIGNAL(diffuseChanged())); + QObject::connect(d->m_normalParameter, SIGNAL(valueChanged()), this, SIGNAL(normalChanged())); + QObject::connect(d->m_specularParameter, SIGNAL(valueChanged()), this, SIGNAL(specularChanged())); + QObject::connect(d->m_shininessParameter, SIGNAL(valueChanged()), this, SIGNAL(shininessChanged())); + QObject::connect(d->m_textureScaleParameter, SIGNAL(valueChanged()), this, SIGNAL(textureScaleChanged())); + d->init(); +} + +QNormalDiffuseSpecularMapMaterial::~QNormalDiffuseSpecularMapMaterial() +{ +} + +/*! + \fn Qt3D::QNormalDiffuseSpecularMapMaterial::ambient() const + \return the current QColor ambient value. + */ +QColor QNormalDiffuseSpecularMapMaterial::ambient() const +{ + Q_D(const QNormalDiffuseSpecularMapMaterial); + return d->m_ambientParameter->value().value<QColor>(); +} + +/*! + \fn Qt3D::QNormalDiffuseSpecularMapMaterial::diffuse() const + \return the current diffuse map texture. + \b {Note:} By default the diffuse texture has a linear + magnification filter, a linear mipmap linear minification filter, + the wrap mode is repeat, the maximum anisotropy is set to 16.0 + and mipmapping is enabled. + */ +QTexture *QNormalDiffuseSpecularMapMaterial::diffuse() const +{ + Q_D(const QNormalDiffuseSpecularMapMaterial); + return d->m_diffuseParameter->value().value<QTexture *>(); +} + +/*! + \fn Qt3D::QNormalDiffuseSpecularMapMaterial::normal() const + \return the current normal map texture. + \b {Note:} By default the normal texture has linear minification + and magnification filters, the wrap mode is repeat and maximum anisotropy + is set to 16.0. + */ +QTexture *QNormalDiffuseSpecularMapMaterial::normal() const +{ + Q_D(const QNormalDiffuseSpecularMapMaterial); + return d->m_normalParameter->value().value<QTexture *>(); +} + +/*! + \fn Qt3D::QNormalDiffuseSpecularMapMaterial::specular() const + \return the current specular map texture. + \b {Note:} By default the specular texture has a linear + magnification filter, a linear mipmap linear minification filter, + the wrap mode is repeat, the maximum anisotropy is set to 16.0 + and mipmapping is enabled. + */ +QTexture *QNormalDiffuseSpecularMapMaterial::specular() const +{ + Q_D(const QNormalDiffuseSpecularMapMaterial); + return d->m_specularParameter->value().value<QTexture *>(); +} + +/*! + \fn Qt3D::QNormalDiffuseSpecularMapMaterial::shininess() const + \return the current shininess as a float value. + */ +float QNormalDiffuseSpecularMapMaterial::shininess() const +{ + Q_D(const QNormalDiffuseSpecularMapMaterial); + return d->m_shininessParameter->value().toFloat(); +} + +/*! + \fn Qt3D::QNormalDiffuseSpecularMapMaterial::textureScale() const + \return the current texture scale. + */ +float QNormalDiffuseSpecularMapMaterial::textureScale() const +{ + Q_D(const QNormalDiffuseSpecularMapMaterial); + return d->m_textureScaleParameter->value().toFloat(); +} + +/*! + \fn Qt3D::QNormalDiffuseSpecularMapMaterial::setAmbient(const QColor &ambient) + Sets the current ambient color to \a ambient + */ +void QNormalDiffuseSpecularMapMaterial::setAmbient(const QColor &ambient) +{ + Q_D(QNormalDiffuseSpecularMapMaterial); + d->m_ambientParameter->setValue(ambient); +} + +/*! + \fn Qt3D::QNormalDiffuseSpecularMapMaterial::setDiffuse(QTexture *diffuse) + Sets the current diffuse map to \a diffuse. + */ +void QNormalDiffuseSpecularMapMaterial::setDiffuse(QTexture *diffuse) +{ + Q_D(QNormalDiffuseSpecularMapMaterial); + d->m_diffuseParameter->setValue(QVariant::fromValue(diffuse)); +} + +/*! + \fn Qt3D::QNormalDiffuseSpecularMapMaterial::setNormal(QTexture *normal) + Sets the current normal map to \a normal. + */ +void QNormalDiffuseSpecularMapMaterial::setNormal(QTexture *normal) +{ + Q_D(QNormalDiffuseSpecularMapMaterial); + d->m_normalParameter->setValue(QVariant::fromValue(normal)); +} + +/*! + \fn Qt3D::QNormalDiffuseSpecularMapMaterial::setSpecular(QTexture *specular) + Sets the current specular map to \a specular. + */ +void QNormalDiffuseSpecularMapMaterial::setSpecular(QTexture *specular) +{ + Q_D(QNormalDiffuseSpecularMapMaterial); + d->m_specularParameter->setValue(QVariant::fromValue(specular)); +} + +/*! + \fn Qt3D::QNormalDiffuseSpecularMapMaterial::setShininess(float shininess) + Sets the current shininess value to \a shininess. + */ +void QNormalDiffuseSpecularMapMaterial::setShininess(float shininess) +{ + Q_D(QNormalDiffuseSpecularMapMaterial); + d->m_shininessParameter->setValue(shininess); +} + +/*! + \fn Qt3D::QNormalDiffuseSpecularMapMaterial::setTextureScale(float textureScale) + Sets the current texture scale to \a textureScale. + */ +void QNormalDiffuseSpecularMapMaterial::setTextureScale(float textureScale) +{ + Q_D(QNormalDiffuseSpecularMapMaterial); + d->m_textureScaleParameter->setValue(textureScale); +} + +} // Qt3D + +QT_END_NAMESPACE diff --git a/src/render/defaults/qnormaldiffusespecularmapmaterial.h b/src/render/defaults/qnormaldiffusespecularmapmaterial.h new file mode 100644 index 000000000..1694fe290 --- /dev/null +++ b/src/render/defaults/qnormaldiffusespecularmapmaterial.h @@ -0,0 +1,101 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QT3D_RENDER_QNORMALDIFFUSESPECULARMAPMATERIAL_H +#define QT3D_RENDER_QNORMALDIFFUSESPECULARMAPMATERIAL_H + +#include <Qt3DRenderer/qmaterial.h> +#include <QColor> + +QT_BEGIN_NAMESPACE + +namespace Qt3D { + +class QNormalDiffuseSpecularMapMaterialPrivate; + +class QT3DRENDERERSHARED_EXPORT QNormalDiffuseSpecularMapMaterial : public QMaterial +{ + Q_OBJECT + Q_PROPERTY(QColor ambient READ ambient WRITE setAmbient NOTIFY ambientChanged) + Q_PROPERTY(QTexture *diffuse READ diffuse WRITE setDiffuse NOTIFY diffuseChanged) + Q_PROPERTY(QTexture *normal READ normal WRITE setNormal NOTIFY normalChanged) + Q_PROPERTY(QTexture *specular READ specular WRITE setSpecular NOTIFY specularChanged) + Q_PROPERTY(float shininess READ shininess WRITE setShininess NOTIFY shininessChanged) + Q_PROPERTY(float textureScale READ textureScale WRITE setTextureScale NOTIFY textureScaleChanged) + +public: + explicit QNormalDiffuseSpecularMapMaterial(QNode *parent = 0); + ~QNormalDiffuseSpecularMapMaterial(); + + QColor ambient() const; + QTexture *diffuse() const; + QTexture *normal() const; + QTexture *specular() const; + float shininess() const; + float textureScale() const; + + void setAmbient(const QColor &ambient); + void setDiffuse(QTexture *diffuse); + void setNormal(QTexture *normal); + void setSpecular(QTexture *specular); + void setShininess(float shininess); + void setTextureScale(float textureScale); + +Q_SIGNALS: + void ambientChanged(); + void diffuseChanged(); + void normalChanged(); + void specularChanged(); + void shininessChanged(); + void textureScaleChanged(); + +protected: + QNormalDiffuseSpecularMapMaterial(QNormalDiffuseSpecularMapMaterialPrivate &dd, QNode *parent = 0); + +private: + Q_DECLARE_PRIVATE(QNormalDiffuseSpecularMapMaterial) +}; + +} // Qt3D + +QT_END_NAMESPACE + +#endif // QT3D_RENDER_QNORMALDIFFUSESPECULARMAPMATERIAL_H diff --git a/src/render/defaults/qnormaldiffusespecularmapmaterial_p.h b/src/render/defaults/qnormaldiffusespecularmapmaterial_p.h new file mode 100644 index 000000000..5ea89140c --- /dev/null +++ b/src/render/defaults/qnormaldiffusespecularmapmaterial_p.h @@ -0,0 +1,95 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QT3D_RENDER_QNORMALDIFFUSESPECULARMAPMATERIAL_P_H +#define QT3D_RENDER_QNORMALDIFFUSESPECULARMAPMATERIAL_P_H + +#include <Qt3DRenderer/private/qmaterial_p.h> + +QT_BEGIN_NAMESPACE + +namespace Qt3D { + +class QEffect; +class QTexture; +class QTechnique; +class QParameter; +class QShaderProgram; +class QRenderPass; +class QParameterMapping; + +class QNormalDiffuseSpecularMapMaterial; + +class QNormalDiffuseSpecularMapMaterialPrivate : public QMaterialPrivate +{ +public: + QNormalDiffuseSpecularMapMaterialPrivate(QNormalDiffuseSpecularMapMaterial *qq); + + void init(); + + QEffect *m_normalDiffuseSpecularEffect; + QTexture *m_diffuseTexture; + QTexture *m_normalTexture; + QTexture *m_specularTexture; + QParameter *m_ambientParameter; + QParameter *m_diffuseParameter; + QParameter *m_normalParameter; + QParameter *m_specularParameter; + QParameter *m_shininessParameter; + QParameter *m_textureScaleParameter; + QTechnique *m_normalDiffuseSpecularGL3Technique; + QTechnique *m_normalDiffuseSpecularGL2Technique; + QTechnique *m_normalDiffuseSpecularES2Technique; + QRenderPass *m_normalDiffuseSpecularGL3RenderPass; + QRenderPass *m_normalDiffuseSpecularGL2RenderPass; + QRenderPass *m_normalDiffuseSpecularES2RenderPass; + QShaderProgram *m_normalDiffuseSpecularGL3Shader; + QShaderProgram *m_normalDiffuseSpecularGL2ES2Shader; + + Q_DECLARE_PUBLIC(QNormalDiffuseSpecularMapMaterial) +}; + +} // Qt3D + +QT_END_NAMESPACE + +#endif // QT3D_RENDER_QNORMALDIFFUSESPECULARMAPMATERIAL_P_H + diff --git a/src/render/defaults/qphongmaterial.cpp b/src/render/defaults/qphongmaterial.cpp index 87c88ef54..269de6a79 100644 --- a/src/render/defaults/qphongmaterial.cpp +++ b/src/render/defaults/qphongmaterial.cpp @@ -48,7 +48,6 @@ #include <Qt3DRenderer/qparameter.h> #include <Qt3DRenderer/qrenderpass.h> #include <Qt3DRenderer/qopenglfilter.h> -#include <Qt3DRenderer/qparametermapping.h> #include <QUrl> QT_BEGIN_NAMESPACE @@ -58,9 +57,9 @@ namespace Qt3D { QPhongMaterialPrivate::QPhongMaterialPrivate(QPhongMaterial *qq) : QMaterialPrivate(qq) , m_phongEffect(new QEffect()) - , m_ambientParameter(new QParameter(QStringLiteral("ambient"), QColor::fromRgbF(0.05f, 0.05f, 0.05f, 1.0f))) - , m_diffuseParameter(new QParameter(QStringLiteral("diffuse"), QColor::fromRgbF(0.7f, 0.7f, 0.7f, 1.0f))) - , m_specularParameter(new QParameter(QStringLiteral("specular"), QColor::fromRgbF(0.95f, 0.95f, 0.95f, 1.0f))) + , m_ambientParameter(new QParameter(QStringLiteral("ka"), QColor::fromRgbF(0.05f, 0.05f, 0.05f, 1.0f))) + , m_diffuseParameter(new QParameter(QStringLiteral("kd"), QColor::fromRgbF(0.7f, 0.7f, 0.7f, 1.0f))) + , m_specularParameter(new QParameter(QStringLiteral("ks"), QColor::fromRgbF(0.95f, 0.95f, 0.95f, 1.0f))) , m_shininessParameter(new QParameter(QStringLiteral("shininess"), 150.0f)) , m_phongGL3Technique(new QTechnique()) , m_phongGL2Technique(new QTechnique()) @@ -70,9 +69,6 @@ QPhongMaterialPrivate::QPhongMaterialPrivate(QPhongMaterial *qq) , m_phongES2RenderPass(new QRenderPass()) , m_phongGL3Shader(new QShaderProgram()) , m_phongGL2ES2Shader(new QShaderProgram()) - , m_ambientParameterMapping(new QParameterMapping(QStringLiteral("ambient"), QStringLiteral("ka"), QParameterMapping::Uniform)) - , m_diffuseParameterMapping(new QParameterMapping(QStringLiteral("diffuse"), QStringLiteral("kd"), QParameterMapping::Uniform)) - , m_specularParameterMapping(new QParameterMapping(QStringLiteral("specular"), QStringLiteral("ks"), QParameterMapping::Uniform)) { } @@ -95,7 +91,7 @@ QPhongMaterialPrivate::QPhongMaterialPrivate(QPhongMaterial *qq) /*! \fn QPhongMaterial::QPhongMaterial - Contrusts a new QPhongMaterial instance. + Constructs a new QPhongMaterial instance with parent object \a parent. */ QPhongMaterial::QPhongMaterial(QNode *parent) : QMaterial(*new QPhongMaterialPrivate(this), parent) @@ -154,12 +150,12 @@ float QPhongMaterial::shininess() const /*! \fn QPhongMaterial::setAmbient - Sets the current ambient color to \a color. + Sets the current ambient color to \a ambient. */ -void QPhongMaterial::setAmbient(const QColor &color) +void QPhongMaterial::setAmbient(const QColor &ambient) { Q_D(QPhongMaterial); - d->m_shininessParameter->setValue(color); + d->m_shininessParameter->setValue(ambient); } /*! @@ -174,7 +170,7 @@ void QPhongMaterial::setDiffuse(const QColor &diffuse) /*! \fn QPhongMaterial::setSpecular - Sets the current specular color to \a color. + Sets the current specular color to \a specular. */ void QPhongMaterial::setSpecular(const QColor &specular) { @@ -217,19 +213,8 @@ void QPhongMaterialPrivate::init() m_phongES2Technique->openGLFilter()->setProfile(QOpenGLFilter::None); m_phongGL3RenderPass->setShaderProgram(m_phongGL3Shader); - m_phongGL3RenderPass->addBinding(m_ambientParameterMapping); - m_phongGL3RenderPass->addBinding(m_diffuseParameterMapping); - m_phongGL3RenderPass->addBinding(m_specularParameterMapping); - m_phongGL2RenderPass->setShaderProgram(m_phongGL2ES2Shader); - m_phongGL2RenderPass->addBinding(m_ambientParameterMapping); - m_phongGL2RenderPass->addBinding(m_diffuseParameterMapping); - m_phongGL2RenderPass->addBinding(m_specularParameterMapping); - m_phongES2RenderPass->setShaderProgram(m_phongGL2ES2Shader); - m_phongES2RenderPass->addBinding(m_ambientParameterMapping); - m_phongES2RenderPass->addBinding(m_diffuseParameterMapping); - m_phongES2RenderPass->addBinding(m_specularParameterMapping); m_phongGL3Technique->addPass(m_phongGL3RenderPass); m_phongGL2Technique->addPass(m_phongGL2RenderPass); diff --git a/src/render/defaults/qphongmaterial.h b/src/render/defaults/qphongmaterial.h index 49f677567..ce2f4dc07 100644 --- a/src/render/defaults/qphongmaterial.h +++ b/src/render/defaults/qphongmaterial.h @@ -60,7 +60,7 @@ class QT3DRENDERERSHARED_EXPORT QPhongMaterial : public QMaterial Q_PROPERTY(float shininess READ shininess WRITE setShininess NOTIFY shininessChanged) public: - QPhongMaterial(QNode *parent = 0); + explicit QPhongMaterial(QNode *parent = 0); ~QPhongMaterial(); QColor ambient() const; diff --git a/src/render/defaults/qphongmaterial_p.h b/src/render/defaults/qphongmaterial_p.h index 421a4c475..ab1a83a3b 100644 --- a/src/render/defaults/qphongmaterial_p.h +++ b/src/render/defaults/qphongmaterial_p.h @@ -59,6 +59,7 @@ class QPhongMaterial; class QPhongMaterialPrivate : public QMaterialPrivate { +public: QPhongMaterialPrivate(QPhongMaterial *qq); void init(); @@ -76,9 +77,6 @@ class QPhongMaterialPrivate : public QMaterialPrivate QRenderPass *m_phongES2RenderPass; QShaderProgram *m_phongGL3Shader; QShaderProgram *m_phongGL2ES2Shader; - QParameterMapping *m_ambientParameterMapping; - QParameterMapping *m_diffuseParameterMapping; - QParameterMapping *m_specularParameterMapping; Q_DECLARE_PUBLIC(QPhongMaterial) }; diff --git a/src/render/frontend/qmesh.cpp b/src/render/frontend/qmesh.cpp index 48103eb7e..a69458091 100644 --- a/src/render/frontend/qmesh.cpp +++ b/src/render/frontend/qmesh.cpp @@ -128,8 +128,10 @@ QMeshDataPtr MeshFunctor::operator()() loader.setTangentGenerationEnabled(true); qCDebug(Render::Jobs) << Q_FUNC_INFO << "Loading mesh from" << m_sourcePath; + // TO DO: Handle file download if remote url - QString filePath = m_sourcePath.toString().replace(QStringLiteral("qrc:/"), QStringLiteral("")); + // If the file contained :/ directly, is it mapped to "" when toString is called + QString filePath = m_sourcePath.toString().replace(QStringLiteral("qrc:/"), QStringLiteral(":/")); if (loader.load(filePath)) return QMeshDataPtr(loader.mesh()); diff --git a/src/render/render.qrc b/src/render/render.qrc index 99d332339..68852b445 100644 --- a/src/render/render.qrc +++ b/src/render/render.qrc @@ -19,5 +19,23 @@ <file>shaders/gl3/phong.frag</file> <file>shaders/es2/phong.vert</file> <file>shaders/es2/phong.frag</file> + <file>shaders/gl3/normaldiffusespecularmap.vert</file> + <file>shaders/gl3/normaldiffusespecularmap.frag</file> + <file>shaders/gl3/diffusemap.vert</file> + <file>shaders/gl3/diffusemap.frag</file> + <file>shaders/es2/normaldiffusespecularmap.vert</file> + <file>shaders/es2/normaldiffusespecularmap.frag</file> + <file>shaders/es2/diffusemap.vert</file> + <file>shaders/es2/diffusemap.frag</file> + <file>shaders/es2/normaldiffusemap.vert</file> + <file>shaders/es2/normaldiffusemap.frag</file> + <file>shaders/es2/normaldiffusemapalpha.frag</file> + <file>shaders/gl3/normaldiffusemap.frag</file> + <file>shaders/gl3/normaldiffusemap.vert</file> + <file>shaders/gl3/normaldiffusemapalpha.frag</file> + <file>shaders/es2/diffusespecularmap.vert</file> + <file>shaders/es2/diffusespecularmap.frag</file> + <file>shaders/gl3/diffusespecularmap.vert</file> + <file>shaders/gl3/diffusespecularmap.frag</file> </qresource> </RCC> diff --git a/src/render/shaders/es2/diffusemap.frag b/src/render/shaders/es2/diffusemap.frag new file mode 100644 index 000000000..070c99a3f --- /dev/null +++ b/src/render/shaders/es2/diffusemap.frag @@ -0,0 +1,48 @@ +#version 120 + +// TODO: Replace with a uniform block +uniform vec4 lightPosition = vec4(0.0, 0.0, 0.0, 1.0); +uniform vec3 lightIntensity = vec3(1.0, 1.0, 1.0); + +// TODO: Replace with a struct +uniform vec3 ka; // Ambient reflectivity +uniform vec3 ks; // Specular reflectivity +uniform float shininess; // Specular shininess factor + +uniform sampler2D diffuseTexture; + +varying vec3 position; +varying vec3 normal; +varying vec2 texCoord; + +vec3 adsModel( const vec3 pos, const vec3 n ) +{ + // Calculate the vector from the light to the fragment + vec3 s = normalize( vec3( lightPosition ) - pos ); + + // Calculate the vector from the fragment to the eye position + // (origin since this is in "eye" or "camera" space) + vec3 v = normalize( -pos ); + + // Reflect the light beam using the normal at this fragment + vec3 r = reflect( -s, n ); + + // Calculate the diffuse component + float diffuse = max( dot( s, n ), 0.0 ); + + // Calculate the specular component + float specular = 0.0; + if ( dot( s, n ) > 0.0 ) + specular = pow( max( dot( r, v ), 0.0 ), shininess ); + + // Lookup diffuse color + vec3 diffuseColor = texture2D( diffuseTexture, texCoord ).rgb; + + // Combine the ambient, diffuse and specular contributions + return lightIntensity * ( ( ka + diffuse ) * diffuseColor + ks * specular ); +} + +void main() +{ + gl_FragColor = vec4( adsModel( position, normalize( normal ) ), 1.0 ); +} diff --git a/src/render/shaders/es2/diffusemap.vert b/src/render/shaders/es2/diffusemap.vert new file mode 100644 index 000000000..abf23d6b7 --- /dev/null +++ b/src/render/shaders/es2/diffusemap.vert @@ -0,0 +1,24 @@ +#version 120 + +attribute vec3 vertexPosition; +attribute vec3 vertexNormal; +attribute vec2 vertexTexCoord; + +varying vec3 position; +varying vec3 normal; +varying vec2 texCoord; + +uniform mat4 modelView; +uniform mat3 modelViewNormal; +uniform mat4 mvp; + +uniform float texCoordScale; + +void main() +{ + texCoord = vertexTexCoord * texCoordScale; + normal = normalize( modelViewNormal * vertexNormal ); + position = vec3( modelView * vec4( vertexPosition, 1.0 ) ); + + gl_Position = mvp * vec4( vertexPosition, 1.0 ); +} diff --git a/src/render/shaders/es2/diffusespecularmap.frag b/src/render/shaders/es2/diffusespecularmap.frag new file mode 100644 index 000000000..c7582545e --- /dev/null +++ b/src/render/shaders/es2/diffusespecularmap.frag @@ -0,0 +1,49 @@ +#version 120 + +// TODO: Replace with a uniform block +uniform vec4 lightPosition = vec4(1.0, 1.0, 0.0, 1.0); +uniform vec3 lightIntensity = vec3(1.0, 1.0, 1.0); + +// TODO: Replace with a struct +uniform vec3 ka; // Ambient reflectivity +uniform float shininess; // Specular shininess factor + +uniform sampler2D diffuseTexture; +uniform sampler2D specularTexture; + +varying vec3 position; +varying vec3 normal; +varying vec2 texCoord; + +vec3 adsModel( const in vec3 pos, const in vec3 n ) +{ + // Calculate the vector from the light to the fragment + vec3 s = normalize( vec3( lightPosition ) - pos ); + + // Calculate the vector from the fragment to the eye position + // (origin since this is in "eye" or "camera" space) + vec3 v = normalize( -pos ); + + // Reflect the light beam using the normal at this fragment + vec3 r = reflect( -s, n ); + + // Calculate the diffuse component + float diffuse = max( dot( s, n ), 0.0 ); + + // Calculate the specular component + float specular = 0.0; + if ( dot( s, n ) > 0.0 ) + specular = ( shininess / ( 8.0 * 3.14 ) ) * pow( max( dot( r, v ), 0.0 ), shininess ); + + // Lookup diffuse and specular factors + vec3 diffuseColor = texture2D( diffuseTexture, texCoord ).rgb; + vec3 specularColor = texture2D( specularTexture, texCoord ).rgb; + + // Combine the ambient, diffuse and specular contributions + return lightIntensity * ( ( ka + diffuse ) * diffuseColor + specular * specularColor ); +} + +void main() +{ + gl_FragColor = vec4( adsModel( position, normalize( normal ) ), 1.0 ); +} diff --git a/src/render/shaders/es2/diffusespecularmap.vert b/src/render/shaders/es2/diffusespecularmap.vert new file mode 100644 index 000000000..abf23d6b7 --- /dev/null +++ b/src/render/shaders/es2/diffusespecularmap.vert @@ -0,0 +1,24 @@ +#version 120 + +attribute vec3 vertexPosition; +attribute vec3 vertexNormal; +attribute vec2 vertexTexCoord; + +varying vec3 position; +varying vec3 normal; +varying vec2 texCoord; + +uniform mat4 modelView; +uniform mat3 modelViewNormal; +uniform mat4 mvp; + +uniform float texCoordScale; + +void main() +{ + texCoord = vertexTexCoord * texCoordScale; + normal = normalize( modelViewNormal * vertexNormal ); + position = vec3( modelView * vec4( vertexPosition, 1.0 ) ); + + gl_Position = mvp * vec4( vertexPosition, 1.0 ); +} diff --git a/src/render/shaders/es2/normaldiffusemap.frag b/src/render/shaders/es2/normaldiffusemap.frag new file mode 100644 index 000000000..93037e772 --- /dev/null +++ b/src/render/shaders/es2/normaldiffusemap.frag @@ -0,0 +1,49 @@ +#version 120 + +varying vec3 lightDir; +varying vec3 viewDir; +varying vec2 texCoord; + +uniform sampler2D diffuseTexture; +uniform sampler2D normalTexture; + +// TODO: Replace with a uniform block +uniform vec4 lightPosition = vec4(0.0, 0.0, 0.0, 1.0); +uniform vec3 lightIntensity = vec3(1.0, 1.0, 1.0); + +// TODO: Replace with a struct +uniform vec3 ka; // Ambient reflectivity +uniform vec3 ks; // Specular reflectivity +uniform float shininess; // Specular shininess factor + +vec3 adsModel( const vec3 norm, const vec3 diffuseReflect) +{ + // Reflection of light direction about normal + vec3 r = reflect( -lightDir, norm ); + + // Calculate the ambient contribution + vec3 ambient = lightIntensity * ka; + + // Calculate the diffuse contribution + float sDotN = max( dot( lightDir, norm ), 0.0 ); + vec3 diffuse = lightIntensity * diffuseReflect * sDotN; + + // Sum the ambient and diffuse contributions + vec3 ambientAndDiff = ambient + diffuse; + + // Calculate the specular highlight contribution + vec3 spec = vec3( 0.0 ); + if ( sDotN > 0.0 ) + spec = lightIntensity * ks * pow( max( dot( r, viewDir ), 0.0 ), shininess ); + return ambientAndDiff + spec; +} + +void main() +{ + // Sample the textures at the interpolated texCoords + vec4 diffuseTextureColor = texture2D( diffuseTexture, texCoord ); + vec4 normal = 2.0 * texture2D( normalTexture, texCoord ) - vec4( 1.0 ); + + // Combine spec with ambient+diffuse for final fragment color + gl_FragColor = vec4( adsModel( normalize( normal.xyz ), diffuseTextureColor.xyz), 1.0 ); +} diff --git a/src/render/shaders/es2/normaldiffusemap.vert b/src/render/shaders/es2/normaldiffusemap.vert new file mode 100644 index 000000000..aab5dec14 --- /dev/null +++ b/src/render/shaders/es2/normaldiffusemap.vert @@ -0,0 +1,51 @@ +#version 120 + +attribute vec3 vertexPosition; +attribute vec3 vertexNormal; +attribute vec2 vertexTexCoord; +attribute vec4 vertexTangent; + +varying vec3 lightDir; +varying vec3 viewDir; +varying vec2 texCoord; + +uniform mat4 modelView; +uniform mat3 modelViewNormal; +uniform mat4 projectionMatrix; +uniform mat4 mvp; + +uniform float texCoordScale; + +// TODO: Replace with a uniform block +uniform vec4 lightPosition = vec4(0.0, 0.0, 0.0, 1.0); +uniform vec3 lightIntensity = vec3(1.0, 1.0, 1.0); + +void main() +{ + // Pass through texture coordinates + texCoord = vertexTexCoord * texCoordScale; + + // Transform position, normal, and tangent to eye coords + vec3 normal = normalize( modelViewNormal * vertexNormal ); + vec3 tangent = normalize( modelViewNormal * vertexTangent.xyz ); + vec3 position = vec3( modelView * vec4( vertexPosition, 1.0 ) ); + + // Calculate binormal vector + vec3 binormal = normalize( cross( normal, tangent ) ); + + // Construct matrix to transform from eye coords to tangent space + mat3 tangentMatrix = mat3 ( + tangent.x, binormal.x, normal.x, + tangent.y, binormal.y, normal.y, + tangent.z, binormal.z, normal.z ); + + // Transform light direction and view direction to tangent space + vec3 s = vec3( lightPosition ) - position; + lightDir = normalize( tangentMatrix * s ); + + vec3 v = -position; + viewDir = normalize( tangentMatrix * v ); + + // Calculate vertex position in clip coordinates + gl_Position = mvp * vec4( vertexPosition, 1.0 ); +} diff --git a/src/render/shaders/es2/normaldiffusemapalpha.frag b/src/render/shaders/es2/normaldiffusemapalpha.frag new file mode 100644 index 000000000..0385152c5 --- /dev/null +++ b/src/render/shaders/es2/normaldiffusemapalpha.frag @@ -0,0 +1,50 @@ +#version 120 + +varying vec3 lightDir; +varying vec3 viewDir; +varying vec2 texCoord; + +uniform sampler2D diffuseTexture; +uniform sampler2D normalTexture; + +// TODO: Replace with a uniform block +uniform vec4 lightPosition = vec4(0.0, 0.0, 0.0, 1.0); +uniform vec3 lightIntensity = vec3(1.0, 1.0, 1.0); + +// TODO: Replace with a struct +uniform vec3 ka; // Ambient reflectivity +uniform vec3 ks; // Specular reflectivity +uniform float shininess; // Specular shininess factor + +vec3 adsModel( const vec3 norm, const vec3 diffuseReflect) +{ + // Reflection of light direction about normal + vec3 r = reflect( -lightDir, norm ); + + // Calculate the ambient contribution + vec3 ambient = lightIntensity * ka; + + // Calculate the diffuse contribution + float sDotN = max( dot( lightDir, norm ), 0.0 ); + vec3 diffuse = lightIntensity * diffuseReflect * sDotN; + + // Sum the ambient and diffuse contributions + vec3 ambientAndDiff = ambient + diffuse; + + // Calculate the specular highlight contribution + vec3 spec = vec3( 0.0 ); + if ( sDotN > 0.0 ) + spec = lightIntensity * ks * pow( max( dot( r, viewDir ), 0.0 ), shininess ); + return ambientAndDiff + spec; +} + +void main() +{ + // Sample the textures at the interpolated texCoords + vec4 diffuseTextureColor = texture2D( diffuseTexture, texCoord ); + vec4 normal = 2.0 * texture2D( normalTexture, texCoord ) - vec4( 1.0 ); + + // Combine spec with ambient+diffuse for final fragment color + // Use the alpha from the diffuse texture (for alpha to coverage) + gl_FragColor = vec4( adsModel( normalize( normal.xyz ), diffuseTextureColor.xyz), diffuseTextureColor.a ); +} diff --git a/src/render/shaders/es2/normaldiffusespecularmap.frag b/src/render/shaders/es2/normaldiffusespecularmap.frag new file mode 100644 index 000000000..9db1ef21a --- /dev/null +++ b/src/render/shaders/es2/normaldiffusespecularmap.frag @@ -0,0 +1,50 @@ +#version 120 + +varying vec3 lightDir; +varying vec3 viewDir; +varying vec2 texCoord; + +uniform sampler2D diffuseTexture; +uniform sampler2D specularTexture; +uniform sampler2D normalTexture; + +// TODO: Replace with a uniform block +uniform vec4 lightPosition = vec4(0.0, 0.0, 0.0, 1.0); +uniform vec3 lightIntensity = vec3(1.0, 1.0, 1.0); + +// TODO: Replace with a struct +uniform vec3 ka; // Ambient reflectivity +uniform float shininess; // Specular shininess factor + +vec3 adsModel( const vec3 norm, const vec3 diffuseReflect, const vec3 specular ) +{ + // Reflection of light direction about normal + vec3 r = reflect( -lightDir, norm ); + + // Calculate the ambient contribution + vec3 ambient = lightIntensity * ka; + + // Calculate the diffuse contribution + float sDotN = max( dot( lightDir, norm ), 0.0 ); + vec3 diffuse = lightIntensity * diffuseReflect * sDotN; + + // Sum the ambient and diffuse contributions + vec3 ambientAndDiff = ambient + diffuse; + + // Calculate the specular highlight contribution + vec3 spec = vec3( 0.0 ); + if ( sDotN > 0.0 ) + spec = lightIntensity * ( shininess / ( 8.0 * 3.14 ) ) * pow( max( dot( r, viewDir ), 0.0 ), shininess ); + return (ambientAndDiff + spec * specular.rgb); +} + +void main() +{ + // Sample the textures at the interpolated texCoords + vec4 diffuseTextureColor = texture2D( diffuseTexture, texCoord ); + vec4 specularTextureColor = texture2D( specularTexture, texCoord ); + vec4 normal = 2.0 * texture2D( normalTexture, texCoord ) - vec4( 1.0 ); + + // Calculate the lighting model + gl_FragColor = vec4( adsModel( normalize( normal.xyz ), diffuseTextureColor.xyz, specularTextureColor.xyz ), 1.0 ); +} diff --git a/src/render/shaders/es2/normaldiffusespecularmap.vert b/src/render/shaders/es2/normaldiffusespecularmap.vert new file mode 100644 index 000000000..aab5dec14 --- /dev/null +++ b/src/render/shaders/es2/normaldiffusespecularmap.vert @@ -0,0 +1,51 @@ +#version 120 + +attribute vec3 vertexPosition; +attribute vec3 vertexNormal; +attribute vec2 vertexTexCoord; +attribute vec4 vertexTangent; + +varying vec3 lightDir; +varying vec3 viewDir; +varying vec2 texCoord; + +uniform mat4 modelView; +uniform mat3 modelViewNormal; +uniform mat4 projectionMatrix; +uniform mat4 mvp; + +uniform float texCoordScale; + +// TODO: Replace with a uniform block +uniform vec4 lightPosition = vec4(0.0, 0.0, 0.0, 1.0); +uniform vec3 lightIntensity = vec3(1.0, 1.0, 1.0); + +void main() +{ + // Pass through texture coordinates + texCoord = vertexTexCoord * texCoordScale; + + // Transform position, normal, and tangent to eye coords + vec3 normal = normalize( modelViewNormal * vertexNormal ); + vec3 tangent = normalize( modelViewNormal * vertexTangent.xyz ); + vec3 position = vec3( modelView * vec4( vertexPosition, 1.0 ) ); + + // Calculate binormal vector + vec3 binormal = normalize( cross( normal, tangent ) ); + + // Construct matrix to transform from eye coords to tangent space + mat3 tangentMatrix = mat3 ( + tangent.x, binormal.x, normal.x, + tangent.y, binormal.y, normal.y, + tangent.z, binormal.z, normal.z ); + + // Transform light direction and view direction to tangent space + vec3 s = vec3( lightPosition ) - position; + lightDir = normalize( tangentMatrix * s ); + + vec3 v = -position; + viewDir = normalize( tangentMatrix * v ); + + // Calculate vertex position in clip coordinates + gl_Position = mvp * vec4( vertexPosition, 1.0 ); +} diff --git a/src/render/shaders/gl3/diffusemap.frag b/src/render/shaders/gl3/diffusemap.frag new file mode 100644 index 000000000..acff3d884 --- /dev/null +++ b/src/render/shaders/gl3/diffusemap.frag @@ -0,0 +1,51 @@ +#version 150 core + +// TODO: Replace with a uniform block +uniform vec4 lightPosition = vec4(0.0, 0.0, 0.0, 1.0); +uniform vec3 lightIntensity = vec3(1.0, 1.0, 1.0); + +// TODO: Replace with a struct +uniform vec3 ka; // Ambient reflectivity +uniform vec3 ks; // Specular reflectivity +uniform float shininess; // Specular shininess factor + +uniform sampler2D diffuseTexture; + +in vec3 position; +in vec3 normal; +in vec2 texCoord; + +out vec4 fragColor; + +vec3 adsModel( const in vec3 pos, const in vec3 n ) +{ + // Calculate the vector from the light to the fragment + vec3 s = normalize( vec3( lightPosition ) - pos ); + + // Calculate the vector from the fragment to the eye position + // (origin since this is in "eye" or "camera" space) + vec3 v = normalize( -pos ); + + // Reflect the light beam using the normal at this fragment + vec3 r = reflect( -s, n ); + + // Calculate the diffuse component + float diffuse = max( dot( s, n ), 0.0 ); + + // Calculate the specular component + float specular = 0.0; + if ( dot( s, n ) > 0.0 ) + specular = pow( max( dot( r, v ), 0.0 ), shininess ); + + // Lookup diffuse color + vec3 diffuseColor = texture( diffuseTexture, texCoord ).rgb; + + // Combine the ambient, diffuse and specular contributions + return lightIntensity * ( ( ka + diffuse ) * diffuseColor + ks * specular ); +} + +void main() +{ + fragColor = vec4( adsModel( position, normalize( normal ) ), 1.0 ); + //fragColor = vec4( texCoord.s, texCoord.t, 0.0, 1.0 ); +} diff --git a/src/render/shaders/gl3/diffusemap.vert b/src/render/shaders/gl3/diffusemap.vert new file mode 100644 index 000000000..2b27413a5 --- /dev/null +++ b/src/render/shaders/gl3/diffusemap.vert @@ -0,0 +1,24 @@ +#version 150 core + +in vec3 vertexPosition; +in vec3 vertexNormal; +in vec2 vertexTexCoord; + +out vec3 position; +out vec3 normal; +out vec2 texCoord; + +uniform mat4 modelView; +uniform mat3 modelViewNormal; +uniform mat4 mvp; + +uniform float texCoordScale; + +void main() +{ + texCoord = vertexTexCoord * texCoordScale; + normal = normalize( modelViewNormal * vertexNormal ); + position = vec3( modelView * vec4( vertexPosition, 1.0 ) ); + + gl_Position = mvp * vec4( vertexPosition, 1.0 ); +} diff --git a/src/render/shaders/gl3/diffusespecularmap.frag b/src/render/shaders/gl3/diffusespecularmap.frag new file mode 100644 index 000000000..c52977ed1 --- /dev/null +++ b/src/render/shaders/gl3/diffusespecularmap.frag @@ -0,0 +1,51 @@ +#version 150 core + +// TODO: Replace with a uniform block +uniform vec4 lightPosition = vec4(1.0, 1.0, 0.0, 1.0); +uniform vec3 lightIntensity = vec3(1.0, 1.0, 1.0); + +// TODO: Replace with a struct +uniform vec3 ka; // Ambient reflectivity +uniform float shininess; // Specular shininess factor + +uniform sampler2D diffuseTexture; +uniform sampler2D specularTexture; + +in vec3 position; +in vec3 normal; +in vec2 texCoord; + +out vec4 fragColor; + +vec3 adsModel( const in vec3 pos, const in vec3 n ) +{ + // Calculate the vector from the light to the fragment + vec3 s = normalize( vec3( lightPosition ) - pos ); + + // Calculate the vector from the fragment to the eye position + // (origin since this is in "eye" or "camera" space) + vec3 v = normalize( -pos ); + + // Reflect the light beam using the normal at this fragment + vec3 r = reflect( -s, n ); + + // Calculate the diffuse component + float diffuse = max( dot( s, n ), 0.0 ); + + // Calculate the specular component + float specular = 0.0; + if ( dot( s, n ) > 0.0 ) + specular = ( shininess / ( 8.0 * 3.14 ) ) * pow( max( dot( r, v ), 0.0 ), shininess ); + + // Lookup diffuse and specular factors + vec3 diffuseColor = texture( diffuseTexture, texCoord ).rgb; + vec3 specularColor = texture( specularTexture, texCoord ).rgb; + + // Combine the ambient, diffuse and specular contributions + return lightIntensity * ( ( ka + diffuse ) * diffuseColor + specular * specularColor ); +} + +void main() +{ + fragColor = vec4( adsModel( position, normalize( normal ) ), 1.0 ); +} diff --git a/src/render/shaders/gl3/diffusespecularmap.vert b/src/render/shaders/gl3/diffusespecularmap.vert new file mode 100644 index 000000000..2b27413a5 --- /dev/null +++ b/src/render/shaders/gl3/diffusespecularmap.vert @@ -0,0 +1,24 @@ +#version 150 core + +in vec3 vertexPosition; +in vec3 vertexNormal; +in vec2 vertexTexCoord; + +out vec3 position; +out vec3 normal; +out vec2 texCoord; + +uniform mat4 modelView; +uniform mat3 modelViewNormal; +uniform mat4 mvp; + +uniform float texCoordScale; + +void main() +{ + texCoord = vertexTexCoord * texCoordScale; + normal = normalize( modelViewNormal * vertexNormal ); + position = vec3( modelView * vec4( vertexPosition, 1.0 ) ); + + gl_Position = mvp * vec4( vertexPosition, 1.0 ); +} diff --git a/src/render/shaders/gl3/normaldiffusemap.frag b/src/render/shaders/gl3/normaldiffusemap.frag new file mode 100644 index 000000000..e8cf91014 --- /dev/null +++ b/src/render/shaders/gl3/normaldiffusemap.frag @@ -0,0 +1,54 @@ +#version 150 core + +in vec3 lightDir; +in vec3 viewDir; +in vec2 texCoord; + +uniform sampler2D diffuseTexture; +uniform sampler2D normalTexture; + +// TODO: Replace with a uniform block +uniform vec4 lightPosition = vec4(0.0, 0.0, 0.0, 1.0); +uniform vec3 lightIntensity = vec3(1.0, 1.0, 1.0); + +// TODO: Replace with a struct +uniform vec3 ka; // Ambient reflectivity +uniform vec3 ks; // Specular reflectivity +uniform float shininess; // Specular shininess factor + +out vec4 fragColor; + +void adsModel( const in vec3 norm, const in vec3 diffuseReflect, out vec3 ambientAndDiff, out vec3 spec ) +{ + // Reflection of light direction about normal + vec3 r = reflect( -lightDir, norm ); + + // Calculate the ambient contribution + vec3 ambient = lightIntensity * ka; + + // Calculate the diffuse contribution + float sDotN = max( dot( lightDir, norm ), 0.0 ); + vec3 diffuse = lightIntensity * diffuseReflect * sDotN; + + // Sum the ambient and diffuse contributions + ambientAndDiff = ambient + diffuse; + + // Calculate the specular highlight contribution + spec = vec3( 0.0 ); + if ( sDotN > 0.0 ) + spec = lightIntensity * ks * pow( max( dot( r, viewDir ), 0.0 ), shininess ); +} + +void main() +{ + // Sample the textures at the interpolated texCoords + vec4 diffuseTextureColor = texture( diffuseTexture, texCoord ); + vec4 normal = 2.0 * texture( normalTexture, texCoord ) - vec4( 1.0 ); + + // Calculate the lighting model, keeping the specular component separate + vec3 ambientAndDiff, spec; + adsModel( normalize( normal.xyz ), diffuseTextureColor.xyz, ambientAndDiff, spec ); + + // Combine spec with ambient+diffuse for final fragment color + fragColor = vec4( ambientAndDiff + spec, 1.0 ); +} diff --git a/src/render/shaders/gl3/normaldiffusemap.vert b/src/render/shaders/gl3/normaldiffusemap.vert new file mode 100644 index 000000000..009187134 --- /dev/null +++ b/src/render/shaders/gl3/normaldiffusemap.vert @@ -0,0 +1,51 @@ +#version 150 core + +in vec3 vertexPosition; +in vec3 vertexNormal; +in vec2 vertexTexCoord; +in vec4 vertexTangent; + +out vec3 lightDir; +out vec3 viewDir; +out vec2 texCoord; + +uniform mat4 modelView; +uniform mat3 modelViewNormal; +uniform mat4 projectionMatrix; +uniform mat4 mvp; + +uniform float texCoordScale; + +// TODO: Replace with a uniform block +uniform vec4 lightPosition = vec4(0.0, 0.0, 0.0, 1.0); +uniform vec3 lightIntensity = vec3(1.0, 1.0, 1.0); + +void main() +{ + // Pass through texture coordinates + texCoord = vertexTexCoord * texCoordScale; + + // Transform position, normal, and tangent to eye coords + vec3 normal = normalize( modelViewNormal * vertexNormal ); + vec3 tangent = normalize( modelViewNormal * vertexTangent.xyz ); + vec3 position = vec3( modelView * vec4( vertexPosition, 1.0 ) ); + + // Calculate binormal vector + vec3 binormal = normalize( cross( normal, tangent ) ); + + // Construct matrix to transform from eye coords to tangent space + mat3 tangentMatrix = mat3 ( + tangent.x, binormal.x, normal.x, + tangent.y, binormal.y, normal.y, + tangent.z, binormal.z, normal.z ); + + // Transform light direction and view direction to tangent space + vec3 s = vec3( lightPosition ) - position; + lightDir = normalize( tangentMatrix * s ); + + vec3 v = -position; + viewDir = normalize( tangentMatrix * v ); + + // Calculate vertex position in clip coordinates + gl_Position = mvp * vec4( vertexPosition, 1.0 ); +} diff --git a/src/render/shaders/gl3/normaldiffusemapalpha.frag b/src/render/shaders/gl3/normaldiffusemapalpha.frag new file mode 100644 index 000000000..f56a890d0 --- /dev/null +++ b/src/render/shaders/gl3/normaldiffusemapalpha.frag @@ -0,0 +1,55 @@ +#version 150 core + +in vec3 lightDir; +in vec3 viewDir; +in vec2 texCoord; + +uniform sampler2D diffuseTexture; +uniform sampler2D normalTexture; + +// TODO: Replace with a uniform block +uniform vec4 lightPosition = vec4(0.0, 0.0, 0.0, 1.0); +uniform vec3 lightIntensity = vec3(1.0, 1.0, 1.0); + +// TODO: Replace with a struct +uniform vec3 ka; // Ambient reflectivity +uniform vec3 ks; // Specular reflectivity +uniform float shininess; // Specular shininess factor + +out vec4 fragColor; + +void adsModel( const in vec3 norm, const in vec3 diffuseReflect, out vec3 ambientAndDiff, out vec3 spec ) +{ + // Reflection of light direction about normal + vec3 r = reflect( -lightDir, norm ); + + // Calculate the ambient contribution + vec3 ambient = lightIntensity * ka; + + // Calculate the diffuse contribution + float sDotN = max( dot( lightDir, norm ), 0.0 ); + vec3 diffuse = lightIntensity * diffuseReflect * sDotN; + + // Sum the ambient and diffuse contributions + ambientAndDiff = ambient + diffuse; + + // Calculate the specular highlight contribution + spec = vec3( 0.0 ); + if ( sDotN > 0.0 ) + spec = lightIntensity * ks * pow( max( dot( r, viewDir ), 0.0 ), shininess ); +} + +void main() +{ + // Sample the textures at the interpolated texCoords + vec4 diffuseTextureColor = texture( diffuseTexture, texCoord ); + vec4 normal = 2.0 * texture( normalTexture, texCoord ) - vec4( 1.0 ); + + // Calculate the lighting model, keeping the specular component separate + vec3 ambientAndDiff, spec; + adsModel( normalize( normal.xyz ), diffuseTextureColor.xyz, ambientAndDiff, spec ); + + // Combine spec with ambient+diffuse for final fragment color + // Use the alpha from the diffuse texture (for alpha to coverage) + fragColor = vec4( ambientAndDiff + spec, diffuseTextureColor.a ); +} diff --git a/src/render/shaders/gl3/normaldiffusespecularmap.frag b/src/render/shaders/gl3/normaldiffusespecularmap.frag new file mode 100644 index 000000000..aa09bb661 --- /dev/null +++ b/src/render/shaders/gl3/normaldiffusespecularmap.frag @@ -0,0 +1,58 @@ +#version 150 core + +in vec3 lightDir; +in vec3 viewDir; +in vec2 texCoord; + +uniform sampler2D diffuseTexture; +uniform sampler2D specularTexture; +uniform sampler2D normalTexture; + +// TODO: Replace with a uniform block +uniform vec4 lightPosition = vec4(0.0, 0.0, 0.0, 1.0); +uniform vec3 lightIntensity = vec3(1.0, 1.0, 1.0); + +// TODO: Replace with a struct +uniform vec3 ka; // Ambient reflectivity +uniform float shininess; // Specular shininess factor + +out vec4 fragColor; + +void adsModel( const in vec3 norm, const in vec3 diffuseReflect, out vec3 ambientAndDiff, out vec3 spec ) +{ + // Reflection of light direction about normal + vec3 r = reflect( -lightDir, norm ); + + // Calculate the ambient contribution + vec3 ambient = lightIntensity * ka; + + // Calculate the diffuse contribution + float sDotN = max( dot( lightDir, norm ), 0.0 ); + vec3 diffuse = lightIntensity * diffuseReflect * sDotN; + + // Sum the ambient and diffuse contributions + ambientAndDiff = ambient + diffuse; + + // Calculate the specular highlight contribution + spec = vec3( 0.0 ); + if ( sDotN > 0.0 ) + spec = lightIntensity * ( shininess / ( 8.0 * 3.14 ) ) * pow( max( dot( r, viewDir ), 0.0 ), shininess ); +} + +void main() +{ + // Sample the textures at the interpolated texCoords + vec4 diffuseTextureColor = texture( diffuseTexture, texCoord ); + vec4 specularTextureColor = texture( specularTexture, texCoord ); + vec4 normal = 2.0 * texture( normalTexture, texCoord ) - vec4( 1.0 ); + + // Calculate the lighting model, keeping the specular component separate + vec3 ambientAndDiff, spec; + adsModel( normalize( normal.xyz ), diffuseTextureColor.xyz, ambientAndDiff, spec ); + + // Multiply specular factor by specular texture sample + vec3 specularColor = spec * specularTextureColor.rgb; + + // Combine spec with ambient+diffuse for final fragment color + fragColor = vec4( ambientAndDiff + specularColor, 1.0 ); +} diff --git a/src/render/shaders/gl3/normaldiffusespecularmap.vert b/src/render/shaders/gl3/normaldiffusespecularmap.vert new file mode 100644 index 000000000..6f02e3882 --- /dev/null +++ b/src/render/shaders/gl3/normaldiffusespecularmap.vert @@ -0,0 +1,51 @@ +#version 150 core + +in vec3 vertexPosition; +in vec3 vertexNormal; +in vec2 vertexTexCoord; +in vec4 vertexTangent; + +out vec3 lightDir; +out vec3 viewDir; +out vec2 texCoord; + +uniform mat4 modelView; +uniform mat3 modelViewNormal; +uniform mat4 projectionMatrix; +uniform mat4 mvp; + +uniform float texCoordScale = 1.0; + +// TODO: Replace with a uniform block +uniform vec4 lightPosition = vec4(0.0, 0.0, 0.0, 1.0); +uniform vec3 lightIntensity = vec3(1.0, 1.0, 1.0); + +void main() +{ + // Pass through texture coordinates + texCoord = vertexTexCoord * texCoordScale; + + // Transform position, normal, and tangent to eye coords + vec3 normal = normalize( modelViewNormal * vertexNormal ); + vec3 tangent = normalize( modelViewNormal * vertexTangent.xyz ); + vec3 position = vec3( modelView * vec4( vertexPosition, 1.0 ) ); + + // Calculate binormal vector + vec3 binormal = normalize( cross( normal, tangent ) ); + + // Construct matrix to transform from eye coords to tangent space + mat3 tangentMatrix = mat3 ( + tangent.x, binormal.x, normal.x, + tangent.y, binormal.y, normal.y, + tangent.z, binormal.z, normal.z ); + + // Transform light direction and view direction to tangent space + vec3 s = vec3( lightPosition ) - position; + lightDir = normalize( tangentMatrix * s ); + + vec3 v = -position; + viewDir = normalize( tangentMatrix * v ); + + // Calculate vertex position in clip coordinates + gl_Position = mvp * vec4( vertexPosition, 1.0 ); +} diff --git a/src/render/shaders/gl3/phong.frag b/src/render/shaders/gl3/phong.frag index 741f4da9f..f281316b6 100644 --- a/src/render/shaders/gl3/phong.frag +++ b/src/render/shaders/gl3/phong.frag @@ -2,7 +2,7 @@ // TODO: Replace with a uniform block uniform vec4 lightPosition = vec4(1.0, 1.0, 0.0, 1.0); -uniform vec3 lightIntensity = vec3(1.0, 0.0, 1.0); +uniform vec3 lightIntensity = vec3(1.0, 1.0, 1.0); // TODO: Replace with a struct uniform vec3 ka; // Ambient reflectivity |