diff options
author | Paul Lemire <paul.lemire@kdab.com> | 2015-05-08 14:46:13 +0200 |
---|---|---|
committer | Paul Lemire <paul.lemire@kdab.com> | 2015-05-12 14:27:41 +0000 |
commit | cbc5f1cf75c1a047af3b991ac8ea96daea420028 (patch) | |
tree | 11ed7f81594ea76a31c98efd79deba87e2356af4 | |
parent | ebff027b83f9c0a48439fa49b52cbebd07f62cce (diff) |
Add an example demonstrating inline and in separate file shaders
Change-Id: I9ded38f9d65e8b528abc729f0eb6f35558b0ce6d
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
-rw-r--r-- | examples/qt3d/qt3d.pro | 3 | ||||
-rw-r--r-- | examples/qt3d/simple-shaders-qml/main.cpp | 63 | ||||
-rw-r--r-- | examples/qt3d/simple-shaders-qml/main.qml | 196 | ||||
-rw-r--r-- | examples/qt3d/simple-shaders-qml/simple-shader.frag | 29 | ||||
-rw-r--r-- | examples/qt3d/simple-shaders-qml/simple-shader.vert | 21 | ||||
-rw-r--r-- | examples/qt3d/simple-shaders-qml/simple-shaders-qml.pro | 13 | ||||
-rw-r--r-- | examples/qt3d/simple-shaders-qml/simple-shaders-qml.qrc | 7 |
7 files changed, 331 insertions, 1 deletions
diff --git a/examples/qt3d/qt3d.pro b/examples/qt3d/qt3d.pro index d8979dabd..ce4ec4576 100644 --- a/examples/qt3d/qt3d.pro +++ b/examples/qt3d/qt3d.pro @@ -30,7 +30,8 @@ SUBDIRS += \ enabled-qml \ skybox \ controls \ - anaglyph-rendering + anaglyph-rendering \ + simple-shaders-qml # TODO Port the old examples to new APIs #SUBDIRS += qt3d diff --git a/examples/qt3d/simple-shaders-qml/main.cpp b/examples/qt3d/simple-shaders-qml/main.cpp new file mode 100644 index 000000000..1141958fd --- /dev/null +++ b/examples/qt3d/simple-shaders-qml/main.cpp @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** 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 <Qt3DCore/window.h> +#include <Qt3DRenderer/qrenderaspect.h> +#include <Qt3DInput/QInputAspect> +#include <Qt3DQuick/QQmlAspectEngine> + +#include <QGuiApplication> + +int main(int argc, char* argv[]) +{ + QGuiApplication app(argc, argv); + + Qt3D::Window view; + Qt3D::Quick::QQmlAspectEngine engine; + + engine.aspectEngine()->registerAspect(new Qt3D::QRenderAspect()); + engine.aspectEngine()->registerAspect(new Qt3D::QInputAspect()); + engine.aspectEngine()->initialize(); + QVariantMap data; + data.insert(QStringLiteral("surface"), QVariant::fromValue(static_cast<QSurface *>(&view))); + data.insert(QStringLiteral("eventSource"), QVariant::fromValue(&view)); + engine.aspectEngine()->setData(data); + engine.setSource(QUrl("qrc:/main.qml")); + + view.show(); + + return app.exec(); +} diff --git a/examples/qt3d/simple-shaders-qml/main.qml b/examples/qt3d/simple-shaders-qml/main.qml new file mode 100644 index 000000000..22b6322be --- /dev/null +++ b/examples/qt3d/simple-shaders-qml/main.qml @@ -0,0 +1,196 @@ +/**************************************************************************** +** +** 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.Render 2.0 +import QtQuick 2.2 as QQ2 + +Entity { + id: sceneRoot + + Camera { + id: camera + projectionType: CameraLens.PerspectiveProjection + fieldOfView: 45 + aspectRatio: 16/9 + nearPlane : 0.1 + farPlane : 1000.0 + position: Qt.vector3d( 0.0, 0.0, -40.0 ) + upVector: Qt.vector3d( 0.0, 1.0, 0.0 ) + viewCenter: Qt.vector3d( 0.0, 0.0, 0.0 ) + } + + Configuration { + controlledCamera: camera + } + + components: [ + FrameGraph { + activeFrameGraph: ForwardRenderer { + clearColor: Qt.rgba(0, 0.5, 1, 1) + camera: camera + } + } + ] + + TorusMesh { + id: torusMesh + radius: 5 + minorRadius: 1 + rings: 100 + slices: 20 + } + + Entity { + id: torusEntityInlineShader + + property Transform transform: Transform { + Translate { translation: Qt.vector3d(-2.0, 0.0, 0.0) } + Rotate { + angle: 45 + axis: Qt.vector3d(1, 0, 0) + } + } + + // Shader defined inline + property Material material: Material { + + parameters: Parameter { name: "finalColor"; value: "#e76140" /* Inferno Orange */ } + + effect: Effect { + techniques: [ + Technique { + openGLFilter { + api: OpenGLFilter.Desktop + majorVersion: 3 + minorVersion: 2 + profile: OpenGLFilter.Core + } + + renderPasses: RenderPass { + shaderProgram: ShaderProgram { + + vertexShaderCode: " + #version 150 + + 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); + } + " + + fragmentShaderCode: " + #version 150 + + in vec3 normal; + in vec3 position; + + uniform vec3 finalColor; + + out vec4 fragColor; + + void main() + { + vec3 n = normalize(normal); + vec3 s = normalize(vec3(1.0, 0.0, 1.0) - position); + vec3 v = normalize(-position); + float diffuse = max(dot(s, n), 0.0); + fragColor = vec4(diffuse * finalColor, 1.0); + } + " + + } // ShaderProgram + } // RenderPass + } // Technique + ] // techniques + } // Effect + } // Material + + components: [ torusMesh, material, transform ] + } + + Entity { + id: torusEntitySeparateShader + + property Transform transform: Transform { + Translate { translation: Qt.vector3d(2.0, 0.0, 0.0) } + Rotate { + angle: 15 + axis: Qt.vector3d(1, 0, 0) + } + } + + // Shader defined in a separate file + property Material material: Material { + + parameters: Parameter { name: "finalColor"; value: "yellow" } + + effect: Effect { + techniques: [ + Technique { + openGLFilter { + api: OpenGLFilter.Desktop + majorVersion: 3 + minorVersion: 2 + profile: OpenGLFilter.Core + } + + renderPasses: RenderPass { + shaderProgram: ShaderProgram { + vertexShaderCode: loadSource("qrc:/simple-shader.vert") + fragmentShaderCode: loadSource("qrc:/simple-shader.frag") + } // ShaderProgram + } // RenderPass + } // Technique + ] // techniques + } // Effect + } // Material + + components: [ torusMesh, material, transform ] + } +} diff --git a/examples/qt3d/simple-shaders-qml/simple-shader.frag b/examples/qt3d/simple-shaders-qml/simple-shader.frag new file mode 100644 index 000000000..330899070 --- /dev/null +++ b/examples/qt3d/simple-shaders-qml/simple-shader.frag @@ -0,0 +1,29 @@ +#version 150 + +in vec3 normal; +in vec3 position; +in vec2 texCoord; + +uniform vec3 finalColor; + +out vec4 fragColor; + +void main() +{ + vec3 n = normalize(normal); + vec3 s = normalize(vec3(1.0, 0.0, 1.0) - position); + vec3 v = normalize(-position); + + float diffuse = max(dot(s, n), 0.0); + + if (diffuse > 0.95) + diffuse = 1.0; + else if (diffuse > 0.5) + diffuse = 0.5; + else if (diffuse > 0.25) + diffuse = 0.25; + else + diffuse = 0.1; + + fragColor = vec4(diffuse * finalColor, 1.0); +} diff --git a/examples/qt3d/simple-shaders-qml/simple-shader.vert b/examples/qt3d/simple-shaders-qml/simple-shader.vert new file mode 100644 index 000000000..ab8e4a0af --- /dev/null +++ b/examples/qt3d/simple-shaders-qml/simple-shader.vert @@ -0,0 +1,21 @@ +#version 150 + +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; + +void main() +{ + normal = normalize(modelViewNormal * vertexNormal); + position = vec3(modelView * vec4(vertexPosition, 1.0)); + texCoord = vertexTexCoord; + gl_Position = mvp * vec4(vertexPosition, 1.0); +} diff --git a/examples/qt3d/simple-shaders-qml/simple-shaders-qml.pro b/examples/qt3d/simple-shaders-qml/simple-shaders-qml.pro new file mode 100644 index 000000000..cd283c37b --- /dev/null +++ b/examples/qt3d/simple-shaders-qml/simple-shaders-qml.pro @@ -0,0 +1,13 @@ +TEMPLATE = app +QT += 3dcore 3drenderer 3dinput 3dquick qml quick + +SOURCES += \ + main.cpp + +OTHER_FILES += \ + main.qml \ + simple-shader.vert \ + simple-shader.frag + +RESOURCES += \ + simple-shaders-qml.qrc diff --git a/examples/qt3d/simple-shaders-qml/simple-shaders-qml.qrc b/examples/qt3d/simple-shaders-qml/simple-shaders-qml.qrc new file mode 100644 index 000000000..d0469bcb4 --- /dev/null +++ b/examples/qt3d/simple-shaders-qml/simple-shaders-qml.qrc @@ -0,0 +1,7 @@ +<RCC> + <qresource prefix="/"> + <file>main.qml</file> + <file>simple-shader.frag</file> + <file>simple-shader.vert</file> + </qresource> +</RCC> |