| 1 |
// Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB). |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
import QtQuick 2.2 |
| 5 |
import Qt3D.Core 2.0 |
| 6 |
import Qt3D.Render 2.0 |
| 7 |
import Qt3D.Input 2.0 |
| 8 |
import Qt3D.Extras 2.0 |
| 9 |
|
| 10 |
Entity { |
| 11 |
id: sceneRoot |
| 12 |
|
| 13 |
Camera { |
| 14 |
id: camera |
| 15 |
projectionType: CameraLens.PerspectiveProjection |
| 16 |
fieldOfView: 45 |
| 17 |
aspectRatio: 16/9 |
| 18 |
nearPlane : 0.1 |
| 19 |
farPlane : 1000.0 |
| 20 |
position: Qt.vector3d( 0.0, 0.0, 40.0 ) |
| 21 |
upVector: Qt.vector3d( 0.0, 1.0, 0.0 ) |
| 22 |
viewCenter: Qt.vector3d( 0.0, 0.0, 0.0 ) |
| 23 |
} |
| 24 |
|
| 25 |
OrbitCameraController { camera: camera } |
| 26 |
|
| 27 |
components: [ |
| 28 |
RenderSettings { |
| 29 |
activeFrameGraph: ForwardRenderer { |
| 30 |
clearColor: Qt.rgba(0, 0.5, 1, 1) |
| 31 |
camera: camera |
| 32 |
} |
| 33 |
}, |
| 34 |
// Event Source will be set by the Qt3DQuickWindow |
| 35 |
InputSettings { } |
| 36 |
] |
| 37 |
|
| 38 |
TorusMesh { |
| 39 |
id: torusMesh |
| 40 |
radius: 5 |
| 41 |
minorRadius: 1 |
| 42 |
rings: 100 |
| 43 |
slices: 20 |
| 44 |
} |
| 45 |
|
| 46 |
Entity { |
| 47 |
id: torusEntityInlineShader |
| 48 |
|
| 49 |
property Transform transform: Transform { |
| 50 |
matrix: { |
| 51 |
var m = Qt.matrix4x4(); |
| 52 |
m.rotate(45, Qt.vector3d(1, 0, 0)); |
| 53 |
m.translate(Qt.vector3d(-2.0, 0.0, 0.0)); |
| 54 |
return m; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
Timer { |
| 59 |
property bool toggle: false |
| 60 |
interval: 2000 |
| 61 |
repeat: true |
| 62 |
triggeredOnStart: true |
| 63 |
running: true |
| 64 |
|
| 65 |
onTriggered: { |
| 66 |
// Toggle shader code each time we are triggered |
| 67 |
if (toggle) { |
| 68 |
console.log("Setting color fragment shader"); |
| 69 |
changingShader.fragmentShaderCode = " |
| 70 |
#version 150 |
| 71 |
|
| 72 |
in vec3 normal; |
| 73 |
in vec3 position; |
| 74 |
|
| 75 |
uniform vec3 finalColor; |
| 76 |
|
| 77 |
out vec4 fragColor; |
| 78 |
|
| 79 |
void main() |
| 80 |
{ |
| 81 |
vec3 n = normalize(normal); |
| 82 |
vec3 s = normalize(vec3(1.0, 0.0, 1.0) - position); |
| 83 |
vec3 v = normalize(-position); |
| 84 |
float diffuse = max(dot(s, n), 0.0); |
| 85 |
fragColor = vec4(diffuse * finalColor, 1.0); |
| 86 |
} |
| 87 |
"; |
| 88 |
} else { |
| 89 |
console.log("Setting greyscale fragment shader"); |
| 90 |
changingShader.fragmentShaderCode = " |
| 91 |
#version 150 |
| 92 |
|
| 93 |
in vec3 normal; |
| 94 |
in vec3 position; |
| 95 |
|
| 96 |
uniform vec3 finalColor; |
| 97 |
|
| 98 |
out vec4 fragColor; |
| 99 |
|
| 100 |
void main() |
| 101 |
{ |
| 102 |
vec3 n = normalize(normal); |
| 103 |
vec3 s = normalize(vec3(1.0, 0.0, 1.0) - position); |
| 104 |
vec3 v = normalize(-position); |
| 105 |
float diffuse = max(dot(s, n), 0.0); |
| 106 |
fragColor = vec4(diffuse * finalColor, 1.0); |
| 107 |
|
| 108 |
float luminance = fragColor.r * 0.299 + fragColor.g * 0.587 + fragColor.b * 0.114; |
| 109 |
fragColor = vec4( luminance, luminance, luminance, 1.0f ); |
| 110 |
} |
| 111 |
"; |
| 112 |
} |
| 113 |
|
| 114 |
toggle = !toggle; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
// Shader defined inline |
| 119 |
property Material material: Material { |
| 120 |
|
| 121 |
parameters: Parameter { name: "finalColor"; value: "#e76140" /* Inferno Orange */ } |
| 122 |
|
| 123 |
effect: Effect { |
| 124 |
techniques: [ |
| 125 |
Technique { |
| 126 |
filterKeys: FilterKey { name: "renderingStyle"; value: "forward" } |
| 127 |
|
| 128 |
graphicsApiFilter { |
| 129 |
api: GraphicsApiFilter.OpenGL |
| 130 |
majorVersion: 3 |
| 131 |
minorVersion: 2 |
| 132 |
profile: GraphicsApiFilter.CoreProfile |
| 133 |
} |
| 134 |
|
| 135 |
renderPasses: RenderPass { |
| 136 |
shaderProgram: ShaderProgram { |
| 137 |
id: changingShader |
| 138 |
|
| 139 |
vertexShaderCode: " |
| 140 |
#version 150 |
| 141 |
|
| 142 |
in vec3 vertexPosition; |
| 143 |
in vec3 vertexNormal; |
| 144 |
|
| 145 |
out vec3 position; |
| 146 |
out vec3 normal; |
| 147 |
|
| 148 |
uniform mat4 modelView; |
| 149 |
uniform mat3 modelViewNormal; |
| 150 |
uniform mat4 mvp; |
| 151 |
|
| 152 |
void main() |
| 153 |
{ |
| 154 |
normal = normalize(modelViewNormal * vertexNormal); |
| 155 |
position = vec3(modelView * vec4(vertexPosition, 1.0)); |
| 156 |
gl_Position = mvp * vec4(vertexPosition, 1.0); |
| 157 |
} |
| 158 |
" |
| 159 |
|
| 160 |
fragmentShaderCode: "" |
| 161 |
|
| 162 |
} // ShaderProgram |
| 163 |
} // RenderPass |
| 164 |
} // Technique |
| 165 |
] // techniques |
| 166 |
} // Effect |
| 167 |
} // Material |
| 168 |
|
| 169 |
components: [ torusMesh, material, transform ] |
| 170 |
} |
| 171 |
|
| 172 |
Entity { |
| 173 |
id: torusEntitySeparateShader |
| 174 |
|
| 175 |
property Transform transform: Transform { |
| 176 |
matrix: { |
| 177 |
var m = Qt.matrix4x4(); |
| 178 |
m.rotate(15, Qt.vector3d(1, 0, 0)); |
| 179 |
m.translate(Qt.vector3d(2.0, 0.0, 0.0)); |
| 180 |
return m; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
// Shader defined in a separate file |
| 185 |
property Material material: Material { |
| 186 |
|
| 187 |
parameters: Parameter { name: "finalColor"; value: "yellow" } |
| 188 |
|
| 189 |
effect: Effect { |
| 190 |
techniques: [ |
| 191 |
Technique { |
| 192 |
filterKeys: FilterKey { name: "renderingStyle"; value: "forward" } |
| 193 |
|
| 194 |
graphicsApiFilter { |
| 195 |
api: GraphicsApiFilter.OpenGL |
| 196 |
majorVersion: 3 |
| 197 |
minorVersion: 2 |
| 198 |
profile: GraphicsApiFilter.CoreProfile |
| 199 |
} |
| 200 |
|
| 201 |
renderPasses: RenderPass { |
| 202 |
shaderProgram: ShaderProgram { |
| 203 |
vertexShaderCode: loadSource("qrc:/simple-shader.vert") |
| 204 |
fragmentShaderCode: loadSource("qrc:/simple-shader.frag") |
| 205 |
} // ShaderProgram |
| 206 |
} // namespace RenderPass |
| 207 |
} // Technique |
| 208 |
] // techniques |
| 209 |
} // Effect |
| 210 |
} // Material |
| 211 |
|
| 212 |
components: [ torusMesh, material, transform ] |
| 213 |
} |
| 214 |
} |