diff options
-rw-r--r-- | examples/qt3d/gooch-qml/MyEntity.qml | 57 | ||||
-rw-r--r-- | examples/qt3d/gooch-qml/gooch-qml.pro | 16 | ||||
-rw-r--r-- | examples/qt3d/gooch-qml/gooch-qml.qrc | 6 | ||||
-rw-r--r-- | examples/qt3d/gooch-qml/main.cpp | 64 | ||||
-rw-r--r-- | examples/qt3d/gooch-qml/main.qml | 72 | ||||
-rw-r--r-- | examples/qt3d/qt3d.pro | 3 | ||||
-rw-r--r-- | src/quick3d/imports/render/defaults/defaults.pri | 1 | ||||
-rw-r--r-- | src/quick3d/imports/render/defaults/qml/GoochMaterial.qml | 116 | ||||
-rw-r--r-- | src/quick3d/imports/render/qt3dquick3drendererplugin.cpp | 1 | ||||
-rw-r--r-- | src/render/defaults/defaults.pri | 7 | ||||
-rw-r--r-- | src/render/defaults/qgoochmaterial.cpp | 300 | ||||
-rw-r--r-- | src/render/defaults/qgoochmaterial.h | 99 | ||||
-rw-r--r-- | src/render/defaults/qgoochmaterial_p.h | 84 | ||||
-rw-r--r-- | src/render/doc/src/externalresources.qdoc | 5 | ||||
-rw-r--r-- | src/render/render.qrc | 4 | ||||
-rw-r--r-- | src/render/shaders/es2/gooch.frag | 57 | ||||
-rw-r--r-- | src/render/shaders/es2/gooch.vert | 17 | ||||
-rw-r--r-- | src/render/shaders/gl3/gooch.frag | 59 | ||||
-rw-r--r-- | src/render/shaders/gl3/gooch.vert | 19 |
19 files changed, 984 insertions, 3 deletions
diff --git a/examples/qt3d/gooch-qml/MyEntity.qml b/examples/qt3d/gooch-qml/MyEntity.qml new file mode 100644 index 000000000..0bd449f1a --- /dev/null +++ b/examples/qt3d/gooch-qml/MyEntity.qml @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2015 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:LGPL3$ +** 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 The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/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 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later 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 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import Qt3D 2.0 +import Qt3D.Renderer 2.0 + +Entity { + id: root + property alias x: translation.dx + property alias y: translation.dy + property alias z: translation.dz + property alias scale: scaleTransform.scale + property alias material: gooch + property alias source: mesh.source + + components: [ + Transform { + Translate { id: translation } + Scale{ id: scaleTransform } + }, + Mesh { id: mesh }, + GoochMaterial { id: gooch } + ] +} diff --git a/examples/qt3d/gooch-qml/gooch-qml.pro b/examples/qt3d/gooch-qml/gooch-qml.pro new file mode 100644 index 000000000..73c947c72 --- /dev/null +++ b/examples/qt3d/gooch-qml/gooch-qml.pro @@ -0,0 +1,16 @@ +!include( ../examples.pri ) { + error( "Couldn't find the examples.pri file!" ) +} + +QT += 3dcore 3drenderer 3dquick qml quick + +SOURCES += \ + main.cpp + +OTHER_FILES += \ + main.qml \ + MyEntity.qml + +RESOURCES += \ + gooch-qml.qrc \ + ../exampleresources/obj.qrc diff --git a/examples/qt3d/gooch-qml/gooch-qml.qrc b/examples/qt3d/gooch-qml/gooch-qml.qrc new file mode 100644 index 000000000..6cc293f70 --- /dev/null +++ b/examples/qt3d/gooch-qml/gooch-qml.qrc @@ -0,0 +1,6 @@ +<RCC> + <qresource prefix="/"> + <file>main.qml</file> + <file>MyEntity.qml</file> + </qresource> +</RCC> diff --git a/examples/qt3d/gooch-qml/main.cpp b/examples/qt3d/gooch-qml/main.cpp new file mode 100644 index 000000000..ad2a11e62 --- /dev/null +++ b/examples/qt3d/gooch-qml/main.cpp @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2015 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:LGPL3$ +** 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 The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/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 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later 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 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <window.h> +#include <Qt3DRenderer/QRenderAspect> +#include <Qt3DQuick/QQmlAspectEngine> + +#include <QGuiApplication> +#include <QtQml> + +int main(int argc, char* argv[]) +{ + QGuiApplication app(argc, argv); + + Window view; + Qt3D::Quick::QQmlAspectEngine engine; + + engine.aspectEngine()->registerAspect(new Qt3D::QRenderAspect()); + + // Expose the window as a context property so we can set the aspect ratio + engine.qmlEngine()->rootContext()->setContextProperty("_window", &view); + QVariantMap data; + data.insert(QStringLiteral("surface"), QVariant::fromValue(static_cast<QSurface *>(&view))); + data.insert(QStringLiteral("eventSource"), QVariant::fromValue(&view)); + engine.aspectEngine()->setData(data); + engine.aspectEngine()->initialize(); + engine.setSource(QUrl("qrc:/main.qml")); + view.show(); + + return app.exec(); +} diff --git a/examples/qt3d/gooch-qml/main.qml b/examples/qt3d/gooch-qml/main.qml new file mode 100644 index 000000000..7fb070153 --- /dev/null +++ b/examples/qt3d/gooch-qml/main.qml @@ -0,0 +1,72 @@ +/**************************************************************************** +** +** Copyright (C) 2015 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:LGPL3$ +** 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 The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/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 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later 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 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import Qt3D 2.0 +import Qt3D.Renderer 2.0 +import QtQuick 2.2 as QQ2 + +Entity { + id: sceneRoot + + Configuration { controlledCamera: mainCamera } + + components: [ + FrameGraph { ForwardRenderer { camera: mainCamera } } + ] + + Camera { + id: mainCamera + projectionType: CameraLens.PerspectiveProjection + fieldOfView: 22.5 + aspectRatio: _window.width / _window.height + nearPlane: 0.01 + farPlane: 1000.0 + position: Qt.vector3d( 0.0, 0.0, 15.0 ) + viewCenter: Qt.vector3d( 0.0, 0.0, 0.0 ) + upVector: Qt.vector3d( 0.0, 1.0, 0.0 ) + } + + MyEntity { + id: myEntity + + source: "assets/obj/trefoil.obj" + scale: 1.0 + material.diffuse: Qt.rgba( 1.0, 0.75, 1.0, 1.0 ) + material.specular: Qt.rgba( 0.2, 0.2, 0.2, 1.0 ) + material.alpha: 0.2 + material.beta: 0.6 + } +} diff --git a/examples/qt3d/qt3d.pro b/examples/qt3d/qt3d.pro index 72d1b234f..e2fa0b6b8 100644 --- a/examples/qt3d/qt3d.pro +++ b/examples/qt3d/qt3d.pro @@ -31,7 +31,8 @@ SUBDIRS += \ anaglyph-rendering \ simple-shaders-qml \ planets-qml \ - plasma + plasma \ + gooch-qml # TODO Port the old examples to new APIs #SUBDIRS += qt3d diff --git a/src/quick3d/imports/render/defaults/defaults.pri b/src/quick3d/imports/render/defaults/defaults.pri index 2d219b646..edaa567cd 100644 --- a/src/quick3d/imports/render/defaults/defaults.pri +++ b/src/quick3d/imports/render/defaults/defaults.pri @@ -14,3 +14,4 @@ QML_FILES = \ $$PWD/qml/ForwardRenderer.qml \ $$PWD/qml/PerVertexColorMaterial.qml \ $$PWD/qml/SkyboxEntity.qml \ + $$PWD/qml/GoochMaterial.qml diff --git a/src/quick3d/imports/render/defaults/qml/GoochMaterial.qml b/src/quick3d/imports/render/defaults/qml/GoochMaterial.qml new file mode 100644 index 000000000..dac326514 --- /dev/null +++ b/src/quick3d/imports/render/defaults/qml/GoochMaterial.qml @@ -0,0 +1,116 @@ +/**************************************************************************** +** +** Copyright (C) 2015 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:LGPL3$ +** 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 The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/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 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later 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 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import Qt3D 2.0 +import Qt3D.Renderer 2.0 + +Material { + id:root + property color diffuse: Qt.rgba( 0.0, 0.0, 0.0, 1.0 ) + property color specular: Qt.rgba( 0.0, 0.0, 0.0, 1.0 ) + property color coolColor: Qt.rgba( 0.0, 0.0, 0.4, 1.0 ) + property color warmColor: Qt.rgba( 0.4, 0.4, 0.0, 1.0 ) + property real alpha: 0.25 + property real beta: 0.5 + property real shininess: 100.0 + + ShaderProgram { + id: gl3GoochShader + vertexShaderCode: loadSource("qrc:/shaders/gl3/gooch.vert") + fragmentShaderCode: loadSource("qrc:/shaders/gl3/gooch.frag") + } + + ShaderProgram { + id: gl2es2GoochShader + vertexShaderCode: loadSource("qrc:/shaders/es2/gooch.vert") + fragmentShaderCode: loadSource("qrc:/shaders/es2/gooch.frag") + } + + effect: Effect { + parameters: [ + Parameter { name: "kd"; value: root.diffuse }, + Parameter { name: "ks"; value: root.specular }, + Parameter { name: "kblue"; value: root.coolColor }, + Parameter { name: "kyellow"; value: root.warmColor }, + Parameter { name: "alpha"; value: root.alpha }, + Parameter { name: "beta"; value: root.beta }, + Parameter { name: "shininess"; value: root.shininess }, + Parameter { name: "lightPosition"; value: Qt.vector4d(10.0, 10.0, -10.0, 1.0) } + ] + + techniques: [ + // GL 3 Technique + Technique { + openGLFilter { + api: OpenGLFilter.Desktop + profile: OpenGLFilter.Core + majorVersion: 3 + minorVersion: 2 + } + renderPasses: RenderPass { + shaderProgram: gl3GoochShader + } + }, + + // GL 2 Technique + Technique { + openGLFilter { + api: OpenGLFilter.Desktop + profile: OpenGLFilter.None + majorVersion: 2 + minorVersion: 0 + } + renderPasses: RenderPass { + shaderProgram: gl2es2GoochShader + } + }, + + // ES 2 Technique + Technique { + openGLFilter { + api: OpenGLFilter.ES + profile: OpenGLFilter.None + majorVersion: 2 + minorVersion: 0 + } + renderPasses: RenderPass { + shaderProgram: gl2es2GoochShader + } + } + ] + } +} + diff --git a/src/quick3d/imports/render/qt3dquick3drendererplugin.cpp b/src/quick3d/imports/render/qt3dquick3drendererplugin.cpp index 39e6a9d51..41413a691 100644 --- a/src/quick3d/imports/render/qt3dquick3drendererplugin.cpp +++ b/src/quick3d/imports/render/qt3dquick3drendererplugin.cpp @@ -123,6 +123,7 @@ static const struct { { "NormalDiffuseMapMaterial", 2, 0 }, { "NormalDiffuseSpecularMapMaterial", 2, 0 }, { "PerVertexColorMaterial", 2, 0 }, + { "GoochMaterial", 2, 0 }, // FrameGraphs { "ForwardRenderer", 2, 0 }, // Entities diff --git a/src/render/defaults/defaults.pri b/src/render/defaults/defaults.pri index fb2c04a08..190f8e4b5 100644 --- a/src/render/defaults/defaults.pri +++ b/src/render/defaults/defaults.pri @@ -18,7 +18,9 @@ HEADERS += \ $$PWD/qpervertexcolormaterial.h \ $$PWD/qpervertexcolormaterial_p.h \ $$PWD/qskyboxentity.h \ - $$PWD/qskyboxentity_p.h + $$PWD/qskyboxentity_p.h \ + $$PWD/qgoochmaterial.h \ + $$PWD/qgoochmaterial_p.h SOURCES += \ $$PWD/qphongmaterial.cpp \ @@ -29,4 +31,5 @@ SOURCES += \ $$PWD/qdiffusespecularmapmaterial.cpp \ $$PWD/qforwardrenderer.cpp \ $$PWD/qpervertexcolormaterial.cpp \ - $$PWD/qskyboxentity.cpp + $$PWD/qskyboxentity.cpp \ + $$PWD/qgoochmaterial.cpp diff --git a/src/render/defaults/qgoochmaterial.cpp b/src/render/defaults/qgoochmaterial.cpp new file mode 100644 index 000000000..e8f89b899 --- /dev/null +++ b/src/render/defaults/qgoochmaterial.cpp @@ -0,0 +1,300 @@ +/**************************************************************************** +** +** Copyright (C) 2015 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:LGPL3$ +** 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 The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/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 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later 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 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qgoochmaterial.h" +#include "qgoochmaterial_p.h" + +#include <Qt3DRenderer/qeffect.h> +#include <Qt3DRenderer/qopenglfilter.h> +#include <Qt3DRenderer/qparameter.h> +#include <Qt3DRenderer/qrenderpass.h> +#include <Qt3DRenderer/qtechnique.h> + +#include <QtCore/qurl.h> + +QT_BEGIN_NAMESPACE + +namespace Qt3D { + +/*! + \class Qt3D::QGoochMaterialPrivate + \internal +*/ +QGoochMaterialPrivate::QGoochMaterialPrivate() + : QMaterialPrivate() + , m_effect(new QEffect) + , m_diffuseParameter(new QParameter(QStringLiteral("kd"), QColor::fromRgbF(0.0f, 0.0f, 0.0f))) + , m_specularParameter(new QParameter(QStringLiteral("ks"), QColor::fromRgbF(0.0f, 0.0f, 0.0f))) + , m_coolParameter(new QParameter(QStringLiteral("kblue"), QColor::fromRgbF(0.0f, 0.0f, 0.4f))) + , m_warmParameter(new QParameter(QStringLiteral("kyellow"), QColor::fromRgbF(0.4f, 0.4f, 0.0f))) + , m_alphaParameter(new QParameter(QStringLiteral("alpha"), 0.25f)) + , m_betaParameter(new QParameter(QStringLiteral("alpha"), 0.5f)) + , m_shininessParameter(new QParameter(QStringLiteral("shininess"), 100.0f)) + , m_gl3Technique(new QTechnique) + , m_gl2Technique(new QTechnique) + , m_es2Technique(new QTechnique) + , m_gl3RenderPass(new QRenderPass) + , m_gl2RenderPass(new QRenderPass) + , m_es2RenderPass(new QRenderPass) + , m_gl3Shader(new QShaderProgram) + , m_gl2ES2Shader(new QShaderProgram) +{ +} + +void QGoochMaterialPrivate::init() +{ + Q_Q(QGoochMaterial); + QObject::connect(m_diffuseParameter, SIGNAL(valueChanged()), q, SIGNAL(diffuseChanged())); + QObject::connect(m_specularParameter, SIGNAL(valueChanged()), q, SIGNAL(specularChanged())); + QObject::connect(m_coolParameter, SIGNAL(valueChanged()), q, SIGNAL(coolChanged())); + QObject::connect(m_warmParameter, SIGNAL(valueChanged()), q, SIGNAL(warmChanged())); + QObject::connect(m_alphaParameter, SIGNAL(valueChanged()), q, SIGNAL(alphaChanged())); + QObject::connect(m_betaParameter, SIGNAL(valueChanged()), q, SIGNAL(betaChanged())); + QObject::connect(m_shininessParameter, SIGNAL(valueChanged()), q, SIGNAL(shininessChanged())); + + m_gl3Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/gooch.vert")))); + m_gl3Shader->setFragmentShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/gooch.frag")))); + m_gl2ES2Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/es2/gooch.vert")))); + m_gl2ES2Shader->setFragmentShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/es2/gooch.frag")))); + + m_gl3Technique->openGLFilter()->setApi(QOpenGLFilter::Desktop); + m_gl3Technique->openGLFilter()->setMajorVersion(3); + m_gl3Technique->openGLFilter()->setMinorVersion(1); + m_gl3Technique->openGLFilter()->setProfile(QOpenGLFilter::Core); + + m_gl2Technique->openGLFilter()->setApi(QOpenGLFilter::Desktop); + m_gl2Technique->openGLFilter()->setMajorVersion(2); + m_gl2Technique->openGLFilter()->setMinorVersion(0); + m_gl2Technique->openGLFilter()->setProfile(QOpenGLFilter::None); + + m_es2Technique->openGLFilter()->setApi(QOpenGLFilter::ES); + m_es2Technique->openGLFilter()->setMajorVersion(2); + m_es2Technique->openGLFilter()->setMinorVersion(0); + m_es2Technique->openGLFilter()->setProfile(QOpenGLFilter::None); + + m_gl3RenderPass->setShaderProgram(m_gl3Shader); + m_gl2RenderPass->setShaderProgram(m_gl2ES2Shader); + m_es2RenderPass->setShaderProgram(m_gl2ES2Shader); + + m_gl3Technique->addPass(m_gl3RenderPass); + m_gl2Technique->addPass(m_gl2RenderPass); + m_es2Technique->addPass(m_es2RenderPass); + + m_effect->addTechnique(m_gl3Technique); + m_effect->addTechnique(m_gl2Technique); + m_effect->addTechnique(m_es2Technique); + + m_effect->addParameter(m_diffuseParameter); + m_effect->addParameter(m_specularParameter); + m_effect->addParameter(m_coolParameter); + m_effect->addParameter(m_warmParameter); + m_effect->addParameter(m_alphaParameter); + m_effect->addParameter(m_betaParameter); + m_effect->addParameter(m_shininessParameter); + + q->setEffect(m_effect); +} + +/*! + \class Qt3D::QGoochMaterial + \brief The QGoochMaterial provides a material that implements the Gooch + shading model, popular in CAD and CAM applications. + \inmodule Qt3DRenderer + \since 5.5 + + The Gooch lighting model uses both color and brightness to help show the + curvature of 3D surfaces. This is often better than models such as Phong + that rely purely upon changes in brightness. In situations such as in CAD + and CAM applications where photorealism is not a goal, the Gooch shading + model in conjunction with some kind of silhouette edge inking is a popular + solution. + + The Gooch lighting model is explained fully in the \l{original Gooch + paper}. The Gooch model mixes a diffuse objetc color with a user-provided + cool color and warm color to produce the end points of a color ramp that is + used to shade the object based upon the cosine of the angle between the + vector from the fragment to the light source and the fragment's normal + vector. Optionally, a specular highlight can be added on top. The relative + contributions to the cool and warm colors by the diffuse color are + controlled by the alpha and beta properties respecitvely. + + 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. +*/ + +/*! + Constructs a new Qt3D::QGoochMaterial instance with parent object \a parent. +*/ +QGoochMaterial::QGoochMaterial(QNode *parent) + : QMaterial(*new QGoochMaterialPrivate, parent) +{ + Q_D(QGoochMaterial); + d->init(); +} + +QGoochMaterial::QGoochMaterial(QGoochMaterialPrivate &dd, QNode *parent) + : QMaterial(dd, parent) +{ + Q_D(QGoochMaterial); + d->init(); +} + +/*! + \property Qt3D::QGoochMaterial::diffuse + + Holds the current diffuse color. +*/ +QColor QGoochMaterial::diffuse() const +{ + Q_D(const QGoochMaterial); + return d->m_diffuseParameter->value().value<QColor>(); +} + +/*! + \property Qt3D::QGoochMaterial::specular + + Holds the current specular color. +*/ +QColor QGoochMaterial::specular() const +{ + Q_D(const QGoochMaterial); + return d->m_specularParameter->value().value<QColor>(); +} + +/*! + \property Qt3D::QGoochMaterial::cool + + Holds the current cool color. +*/ +QColor QGoochMaterial::cool() const +{ + Q_D(const QGoochMaterial); + return d->m_coolParameter->value().value<QColor>(); +} + +/*! + \property Qt3D::QGoochMaterial::warm + + Holds the current warm color. +*/ +QColor QGoochMaterial::warm() const +{ + Q_D(const QGoochMaterial); + return d->m_warmParameter->value().value<QColor>(); +} + +/*! + \property Qt3D::QGoochMaterial::alpha + + Holds the current alpha value. The start point of the color ramp + used by the Gooch shader is calculated as {c = cool + alpha * diffuse}. +*/ +float QGoochMaterial::alpha() const +{ + Q_D(const QGoochMaterial); + return d->m_alphaParameter->value().toFloat(); +} + +/*! + \property Qt3D::QGoochMaterial::beta + + Holds the current beta value. The start point of the color ramp + used by the Gooch shader is calculated as {c = warm + beta * diffuse}. +*/ +float QGoochMaterial::beta() const +{ + Q_D(const QGoochMaterial); + return d->m_betaParameter->value().toFloat(); +} + +/*! + \property Qt3D::QGoochMaterial::shininess + + Holds the current shininess value. Higher values of shininess result in + a smaller and brighter highlight. +*/ +float QGoochMaterial::shininess() const +{ + Q_D(const QGoochMaterial); + return d->m_shininessParameter->value().toFloat(); +} + +void QGoochMaterial::setDiffuse(const QColor &diffuse) +{ + Q_D(QGoochMaterial); + return d->m_diffuseParameter->setValue(diffuse); +} + +void QGoochMaterial::setSpecular(const QColor &specular) +{ + Q_D(QGoochMaterial); + return d->m_specularParameter->setValue(specular); +} + +void QGoochMaterial::setCool(const QColor &cool) +{ + Q_D(QGoochMaterial); + return d->m_coolParameter->setValue(cool); +} + +void QGoochMaterial::setWarm(const QColor &warm) +{ + Q_D(QGoochMaterial); + return d->m_warmParameter->setValue(warm); +} + +void QGoochMaterial::setAlpha(float alpha) +{ + Q_D(QGoochMaterial); + return d->m_alphaParameter->setValue(alpha); +} + +void QGoochMaterial::setBeta(float beta) +{ + Q_D(QGoochMaterial); + return d->m_betaParameter->setValue(beta); +} + +void QGoochMaterial::setShininess(float shininess) +{ + Q_D(QGoochMaterial); + return d->m_shininessParameter->setValue(shininess); +} + +} + +QT_END_NAMESPACE diff --git a/src/render/defaults/qgoochmaterial.h b/src/render/defaults/qgoochmaterial.h new file mode 100644 index 000000000..5c8be4a13 --- /dev/null +++ b/src/render/defaults/qgoochmaterial.h @@ -0,0 +1,99 @@ +/**************************************************************************** +** +** Copyright (C) 2015 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:LGPL3$ +** 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 The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/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 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later 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 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QT3D_RENDER_QGOOCHMATERIAL_H +#define QT3D_RENDER_QGOOCHMATERIAL_H + +#include <Qt3DRenderer/qmaterial.h> +#include <QColor> + +QT_BEGIN_NAMESPACE + +namespace Qt3D { + +class QGoochMaterialPrivate; + +class QGoochMaterial : public QMaterial +{ + Q_OBJECT + Q_PROPERTY(QColor diffuse READ diffuse WRITE setDiffuse NOTIFY diffuseChanged) + Q_PROPERTY(QColor specular READ specular WRITE setSpecular NOTIFY specularChanged) + Q_PROPERTY(QColor cool READ cool WRITE setCool NOTIFY coolChanged) + Q_PROPERTY(QColor warm READ warm WRITE setWarm NOTIFY warmChanged) + Q_PROPERTY(float alpha READ alpha WRITE setAlpha NOTIFY alphaChanged) + Q_PROPERTY(float beta READ beta WRITE setBeta NOTIFY betaChanged) + Q_PROPERTY(float shininess READ shininess WRITE setShininess NOTIFY shininessChanged) + +public: + explicit QGoochMaterial(QNode *parent = 0); + QColor diffuse() const; + QColor specular() const; + QColor cool() const; + QColor warm() const; + float alpha() const; + float beta() const; + float shininess() const; + +public Q_SLOTS: + void setDiffuse(const QColor &diffuse); + void setSpecular(const QColor &specular); + void setCool(const QColor &cool); + void setWarm(const QColor &warm); + void setAlpha(float alpha); + void setBeta(float beta); + void setShininess(float shininess); + +Q_SIGNALS: + void diffuseChanged(); + void specularChanged(); + void coolChanged(); + void warmChanged(); + void alphaChanged(); + void betaChanged(); + void shininessChanged(); + +protected: + QGoochMaterial(QGoochMaterialPrivate &dd, QNode *parent = 0); + +private: + Q_DECLARE_PRIVATE(QGoochMaterial) +}; + +} // Qt3D + +QT_END_NAMESPACE + +#endif // QT3D_RENDER_QGOOCHMATERIAL_H diff --git a/src/render/defaults/qgoochmaterial_p.h b/src/render/defaults/qgoochmaterial_p.h new file mode 100644 index 000000000..7f07536e1 --- /dev/null +++ b/src/render/defaults/qgoochmaterial_p.h @@ -0,0 +1,84 @@ +/**************************************************************************** +** +** 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:LGPL3$ +** 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 The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/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 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later 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 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QT3D_RENDER_QGOOCHMATERIAL_P_H +#define QT3D_RENDER_QGOOCHMATERIAL_P_H + +#include <Qt3DRenderer/private/qmaterial_p.h> + +QT_BEGIN_NAMESPACE + +namespace Qt3D { + +class QEffect; +class QRenderPass; +class QShaderProgram; +class QTechnique; + +class QGoochMaterialPrivate : public QMaterialPrivate +{ +public: + QGoochMaterialPrivate(); + void init(); + + QEffect *m_effect; + QParameter *m_diffuseParameter; + QParameter *m_specularParameter; + QParameter *m_coolParameter; + QParameter *m_warmParameter; + QParameter *m_alphaParameter; + QParameter *m_betaParameter; + QParameter *m_shininessParameter; + QParameter *m_lightPositionParameter; + QParameter *m_lightIntensityParameter; + QTechnique *m_gl3Technique; + QTechnique *m_gl2Technique; + QTechnique *m_es2Technique; + QRenderPass *m_gl3RenderPass; + QRenderPass *m_gl2RenderPass; + QRenderPass *m_es2RenderPass; + QShaderProgram *m_gl3Shader; + QShaderProgram *m_gl2ES2Shader; + + Q_DECLARE_PUBLIC(QGoochMaterial) +}; + +} // Qt3D + +QT_END_NAMESPACE + +#endif // QT3D_RENDER_QGOOCHMATERIAL_P_H + diff --git a/src/render/doc/src/externalresources.qdoc b/src/render/doc/src/externalresources.qdoc index 158ca1ed2..8ddce9d47 100644 --- a/src/render/doc/src/externalresources.qdoc +++ b/src/render/doc/src/externalresources.qdoc @@ -38,3 +38,8 @@ \externalpage http://en.wikipedia.org/wiki/Z-buffering \title early z-fill pass */ + +/*! + \externalpage http://www.cs.northwestern.edu/~ago820/SIG98/abstract.html + \title original Gooch paper +*/ diff --git a/src/render/render.qrc b/src/render/render.qrc index 9878bec69..21db94917 100644 --- a/src/render/render.qrc +++ b/src/render/render.qrc @@ -49,5 +49,9 @@ <file>shaders/es2/skybox.vert</file> <file>shaders/gl3/skybox.frag</file> <file>shaders/gl3/skybox.vert</file> + <file>shaders/gl3/gooch.vert</file> + <file>shaders/gl3/gooch.frag</file> + <file>shaders/es2/gooch.frag</file> + <file>shaders/es2/gooch.vert</file> </qresource> </RCC> diff --git a/src/render/shaders/es2/gooch.frag b/src/render/shaders/es2/gooch.frag new file mode 100644 index 000000000..9fab04921 --- /dev/null +++ b/src/render/shaders/es2/gooch.frag @@ -0,0 +1,57 @@ +#define FP highp + +// TODO: Replace with a uniform block +uniform FP vec4 lightPosition; +uniform FP vec3 lightIntensity; + +// TODO: Replace with a struct +uniform FP vec3 kd; // Diffuse reflectivity +uniform FP vec3 ks; // Specular reflectivity +uniform FP vec3 kblue; // Cool color +uniform FP vec3 kyellow; // Warm color +uniform FP float alpha; // Fraction of diffuse added to kblue +uniform FP float beta; // Fraction of diffuse added to kyellow +uniform FP float shininess; // Specular shininess factor + +varying FP vec3 position; +varying FP vec3 normal; + +FP vec3 goochModel( const in FP vec3 pos, const in FP vec3 n ) +{ + // Based upon the original Gooch lighting model paper at: + // http://www.cs.northwestern.edu/~ago820/SIG98/abstract.html + + // Calculate kcool and kwarm from equation (3) + FP vec3 kcool = clamp(kblue + alpha * kd, 0.0, 1.0); + FP vec3 kwarm = clamp(kyellow + beta * kd, 0.0, 1.0); + + // Calculate the vector from the light to the fragment + FP vec3 s = normalize( vec3( lightPosition ) - pos ); + + // Calculate the cos theta factor mapped onto the range [0,1] + FP float sDotNFactor = ( 1.0 + dot( s, n ) ) / 2.0; + + // Calculate the tone by blending the kcool and kwarm contributions + // as per equation (2) + FP vec3 intensity = mix( kcool, kwarm, sDotNFactor ); + + // Calculate the vector from the fragment to the eye position + // (origin since this is in "eye" or "camera" space) + FP vec3 v = normalize( -pos ); + + // Reflect the light beam using the normal at this fragment + FP vec3 r = reflect( -s, n ); + + // Calculate the specular component + FP float specular = 0.0; + if ( dot( s, n ) > 0.0 ) + specular = pow( max( dot( r, v ), 0.0 ), shininess ); + + // Sum the blended tone and specular highlight + return intensity + ks * specular; +} + +void main() +{ + gl_FragColor = vec4( goochModel( position, normalize( normal ) ), 1.0 ); +} diff --git a/src/render/shaders/es2/gooch.vert b/src/render/shaders/es2/gooch.vert new file mode 100644 index 000000000..03a416213 --- /dev/null +++ b/src/render/shaders/es2/gooch.vert @@ -0,0 +1,17 @@ +attribute vec3 vertexPosition; +attribute vec3 vertexNormal; + +varying vec3 position; +varying vec3 normal; + +uniform mat4 modelView; +uniform mat3 modelViewNormal; +uniform mat4 mvp; + +void main() +{ + 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/gooch.frag b/src/render/shaders/gl3/gooch.frag new file mode 100644 index 000000000..3e141e991 --- /dev/null +++ b/src/render/shaders/gl3/gooch.frag @@ -0,0 +1,59 @@ +#version 150 core + +// TODO: Replace with a uniform block +uniform vec4 lightPosition; +uniform vec3 lightIntensity; + +// TODO: Replace with a struct +uniform vec3 kd; // Diffuse reflectivity +uniform vec3 ks; // Specular reflectivity +uniform vec3 kblue; // Cool color +uniform vec3 kyellow; // Warm color +uniform float alpha; // Fraction of diffuse added to kblue +uniform float beta; // Fraction of diffuse added to kyellow +uniform float shininess; // Specular shininess factor + +in vec3 position; +in vec3 normal; + +out vec4 fragColor; + +vec3 goochModel( const in vec3 pos, const in vec3 n ) +{ + // Based upon the original Gooch lighting model paper at: + // http://www.cs.northwestern.edu/~ago820/SIG98/abstract.html + + // Calculate kcool and kwarm from equation (3) + vec3 kcool = clamp(kblue + alpha * kd, 0.0, 1.0); + vec3 kwarm = clamp(kyellow + beta * kd, 0.0, 1.0); + + // Calculate the vector from the light to the fragment + vec3 s = normalize( vec3( lightPosition ) - pos ); + + // Calculate the cos theta factor mapped onto the range [0,1] + float sDotNFactor = ( 1.0 + dot( s, n ) ) / 2.0; + + // Calculate the tone by blending the kcool and kwarm contributions + // as per equation (2) + vec3 intensity = mix( kcool, kwarm, sDotNFactor ); + + // 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 specular component + float specular = 0.0; + if ( dot( s, n ) > 0.0 ) + specular = pow( max( dot( r, v ), 0.0 ), shininess ); + + // Sum the blended tone and specular highlight + return intensity + ks * specular; +} + +void main() +{ + fragColor = vec4( goochModel( position, normalize( normal ) ), 1.0 ); +} diff --git a/src/render/shaders/gl3/gooch.vert b/src/render/shaders/gl3/gooch.vert new file mode 100644 index 000000000..c0f907f29 --- /dev/null +++ b/src/render/shaders/gl3/gooch.vert @@ -0,0 +1,19 @@ +#version 150 core + +in vec3 vertexPosition; +in vec3 vertexNormal; + +out vec3 position; +out vec3 normal; + +uniform mat4 modelView; +uniform mat3 modelViewNormal; +uniform mat4 mvp; + +void main() +{ + normal = normalize( modelViewNormal * vertexNormal ); + position = vec3( modelView * vec4( vertexPosition, 1.0 ) ); + + gl_Position = mvp * vec4( vertexPosition, 1.0 ); +} |