Debian Codegraph @

tests/manual/rhi/simple-rhi-cpp/main.cpp

raw symbols SCIP
1 // Copyright (C) 2019 Klaralvdalens Datakonsult AB (KDAB).
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include <QGuiApplication>
5
6 #include <Qt3DCore/QEntity>
7 #include <Qt3DRender/QCamera>
8 #include <Qt3DRender/QCameraLens>
9 #include <Qt3DCore/QTransform>
10 #include <Qt3DCore/QAspectEngine>
11 #include <Qt3DCore/QGeometry>
12 #include <Qt3DCore/QAttribute>
13 #include <Qt3DCore/QBuffer>
14
15 #include <Qt3DInput/QInputAspect>
16
17 #include <Qt3DRender/QRenderStateSet>
18 #include <Qt3DRender/QRenderAspect>
19 #include <Qt3DExtras/QForwardRenderer>
20 #include <Qt3DExtras/QPerVertexColorMaterial>
21
22 #include <Qt3DRender/QGeometryRenderer>
23
24 #include <QPropertyAnimation>
25 #include <Qt3DExtras/qt3dwindow.h>
26 #include <Qt3DExtras/qorbitcameracontroller.h>
27 #include <Qt3DRender/QParameter>
28 #include <Qt3DRender/QEffect>
29 #include <Qt3DRender/QTechnique>
30 #include <Qt3DRender/QAbstractTexture>
31 #include <Qt3DRender/QShaderProgram>
32 #include <Qt3DRender/QRenderPass>
33 #include <Qt3DRender/QBlendEquation>
34 #include <Qt3DRender/QBlendEquationArguments>
35 #include <Qt3DRender/QFilterKey>
36 #include <Qt3DRender/QGraphicsApiFilter>
37 #include <Qt3DRender/QRenderSurfaceSelector>
38 #include <Qt3DRender/QViewport>
39 #include <Qt3DRender/QCameraSelector>
40 #include <Qt3DRender/QNoDraw>
41 #include <QColor>
42 #include <QVector2D>
43 #include <QUrl>
44 #include <QTimer>
45 #include <Qt3DRender/QMaterial>
46 #include <Qt3DRender/QFilterKey>
47 #include <Qt3DRender/QTechnique>
48 #include <Qt3DRender/QMaterial>
49 #include <Qt3DRender/QTexture>
50 #include <qmath.h>
51 #include <Qt3DRender/qt3drender-config.h>
52
53 static const constexpr auto vertex_shader = R"_(#version 450
54
55 /**
56 * Unicode comment: Ѧ𝙱ƇᗞΣ
57 */
58
59 layout(location = 0) in vec3 vertexPosition;
60 layout(location = 1) in vec3 vertexColor;
61 layout(location = 0) out vec3 color;
62
63 layout(std140, binding = 0) uniform qt3d_render_view_uniforms {
64 mat4 viewMatrix;
65 mat4 projectionMatrix;
66 mat4 uncorrectedProjectionMatrix;
67 mat4 clipCorrectionMatrix;
68 mat4 viewProjectionMatrix;
69 mat4 inverseViewMatrix;
70 mat4 inverseProjectionMatrix;
71 mat4 inverseViewProjectionMatrix;
72 mat4 viewportMatrix;
73 mat4 inverseViewportMatrix;
74 vec4 textureTransformMatrix;
75 vec3 eyePosition;
76 float aspectRatio;
77 float gamma;
78 float exposure;
79 float time;
80 };
81 layout(std140, binding = 1) uniform qt3d_command_uniforms {
82 mat4 modelMatrix;
83 mat4 inverseModelMatrix;
84 mat4 modelViewMatrix;
85 mat3 modelNormalMatrix;
86 mat4 inverseModelViewMatrix;
87 mat4 mvp;
88 mat4 inverseModelViewProjectionMatrix;
89 };
90 void main()
91 {
92 color = vertexColor;
93 gl_Position = mvp * vec4(vertexPosition, 1.0);
94 }
95 )_";
96
97 static const constexpr auto fragment_shader = R"_(#version 450
98
99 /**
100 * Unicode comment: Ѧ𝙱ƇᗞΣ
101 */
102
103 layout(location = 0) out vec4 fragColor;
104 layout(location = 0) in vec3 color;
105
106 layout(std140, binding = 0) uniform qt3d_render_view_uniforms {
107 mat4 viewMatrix;
108 mat4 projectionMatrix;
109 mat4 uncorrectedProjectionMatrix;
110 mat4 clipCorrectionMatrix;
111 mat4 viewProjectionMatrix;
112 mat4 inverseViewMatrix;
113 mat4 inverseProjectionMatrix;
114 mat4 inverseViewProjectionMatrix;
115 mat4 viewportMatrix;
116 mat4 inverseViewportMatrix;
117 vec4 textureTransformMatrix;
118 vec3 eyePosition;
119 float aspectRatio;
120 float gamma;
121 float exposure;
122 float time;
123 };
124 layout(std140, binding = 1) uniform qt3d_command_uniforms {
125 mat4 modelMatrix;
126 mat4 inverseModelMatrix;
127 mat4 modelViewMatrix;
128 mat3 modelNormalMatrix;
129 mat4 inverseModelViewMatrix;
130 mat4 mvp;
131 mat4 inverseModelViewProjectionMatrix;
132 };
133 layout(std140, binding = 2) uniform custom_ubo {
134 vec3 colorFactor;
135 };
136
137 layout(binding = 3) uniform sampler2D myTexture;
138 void main()
139 {
140 vec2 texCoord = color.xz;
141 vec2 rhiTexCoord = textureTransformMatrix.xy * texCoord+ textureTransformMatrix.zw;
142 fragColor = vec4(color * colorFactor, 1.0);
143
144 fragColor *= texture(myTexture, rhiTexCoord);
145 }
146
147 )_";
148
149 class Material : public Qt3DRender::QMaterial
150 {
151 public:
152 explicit Material(Qt3DCore::QNode *parent = nullptr)
153 : QMaterial(parent)
154 , m_effect(new Qt3DRender::QEffect(this))
155 {
156 setEffect(m_effect);
157
158 m_testParam = new Qt3DRender::QParameter(QStringLiteral("example"), float(0.5));
159
160 m_effect->addParameter(m_testParam);
161
162 m_filter = new Qt3DRender::QFilterKey(this);
163 m_filter->setName(QStringLiteral("renderingStyle"));
164 m_filter->setValue(QStringLiteral("forward"));
165
166 m_technique = new Qt3DRender::QTechnique(m_effect);
167 m_technique->addFilterKey(m_filter);
168
169 m_effect->addTechnique(m_technique);
170
171 m_program = new Qt3DRender::QShaderProgram(m_effect);
172 m_program->setVertexShaderCode(vertex_shader);
173 m_program->setFragmentShaderCode(fragment_shader);
174
175 m_renderPass = new Qt3DRender::QRenderPass(m_effect);
176
177 m_renderPass->setShaderProgram(m_program);
178
179 m_technique->addRenderPass(m_renderPass);
180
181 m_technique->graphicsApiFilter()->setApi(Qt3DRender::QGraphicsApiFilter::RHI);
182 }
183 private:
184 Qt3DRender::QEffect *m_effect{};
185 Qt3DRender::QParameter *m_testParam{};
186 Qt3DRender::QFilterKey *m_filter{};
187 Qt3DRender::QTechnique *m_technique{};
188 Qt3DRender::QShaderProgram *m_program{};
189 Qt3DRender::QRenderPass *m_renderPass{};
190 };
191
192 int main(int argc, char* argv[])
193 {
194 qputenv("QT3D_RENDERER", "rhi");
195 QGuiApplication app(argc, argv);
196
197 auto api = Qt3DRender::API::OpenGL;
198 if (argc >= 2) {
199
200 #ifdef Q_OS_WIN
201 if (argv[1] == QByteArrayLiteral("--d3d11")) api = Qt3DRender::API::DirectX;
202 #endif
203
204 #if QT_CONFIG(qt3d_vulkan)
205 if (argv[1] == QByteArrayLiteral("--vulkan")) api = Qt3DRender::API::Vulkan;
206 #endif
207
208 #if defined(Q_OS_MACOS) || defined(Q_OS_IOS)
209 if (argv[1] == QByteArrayLiteral("--metal")) api = Qt3DRender::API::Metal;
210 #endif
211 }
212
213 Qt3DExtras::Qt3DWindow view{nullptr, api};
214
215 // Root entity
216 Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity();
217
218 // Camera
219 Qt3DRender::QCamera *topViewCamera = new Qt3DRender::QCamera(rootEntity);
220 topViewCamera->setPosition(QVector3D(0, 40, 0));
221 topViewCamera->setViewCenter(QVector3D(0, 0, 0));
222 topViewCamera->setUpVector(QVector3D(0, 0, 1));
223 topViewCamera->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f);
224
225 Qt3DRender::QCamera *cameraEntity = view.camera();
226 cameraEntity->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f);
227 cameraEntity->setPosition(QVector3D(0, 0, 40.0f));
228 cameraEntity->setUpVector(QVector3D(0, 1, 0));
229 cameraEntity->setViewCenter(QVector3D(0, 0, 0));
230
231 {
232 // Custom FG
233 auto *surfaceSelector = new Qt3DRender::QRenderSurfaceSelector();
234 surfaceSelector->setSurface(&view);
235
236 // RV 1
237 auto *clearBuffer = new Qt3DRender::QClearBuffers(surfaceSelector);
238 clearBuffer->setBuffers(Qt3DRender::QClearBuffers::ColorDepthBuffer);
239 clearBuffer->setClearColor(QColor::fromRgbF(0.1, 0.5, 0.0, 1.0));
240 auto *noDraw = new Qt3DRender::QNoDraw(clearBuffer);
241 Q_UNUSED(noDraw);
242
243 // RV 2
244 auto *cameraSelector1 = new Qt3DRender::QCameraSelector(surfaceSelector);
245 cameraSelector1->setCamera(view.camera());
246 auto *viewport1 = new Qt3DRender::QViewport(cameraSelector1);
247 viewport1->setNormalizedRect(QRectF(0.0f, 0.0f, 0.5f, 0.5f));
248
249 // RV3
250 auto *cameraSelector2 = new Qt3DRender::QCameraSelector(surfaceSelector);
251 cameraSelector2->setCamera(topViewCamera);
252 auto *viewport2 = new Qt3DRender::QViewport(cameraSelector2);
253 viewport2->setNormalizedRect(QRectF(0.5f, 0.5f, 0.5f, 0.5f));
254
255 view.setActiveFrameGraph(surfaceSelector);
256 }
257
258 QTimer *cameraAnimationTimer = new QTimer(&view);
259 QObject::connect(cameraAnimationTimer, &QTimer::timeout,
260 cameraEntity, [cameraEntity] {
261 static int angle = 0;
262 const float radius = 40.0f;
263 const float anglef = qDegreesToRadians(float(angle));
264 cameraEntity->setPosition(QVector3D(qSin(anglef), 0.0f, qCos(anglef)) * radius);
265 angle += 1;
266 });
267 cameraAnimationTimer->start(16);
268
269 // For camera controls
270 Qt3DExtras::QOrbitCameraController *camController = new Qt3DExtras::QOrbitCameraController(rootEntity);
271 camController->setCamera(cameraEntity);
272
273 // Material
274 Qt3DRender::QMaterial *material = new Material(rootEntity);
275 Qt3DRender::QParameter *parameter = new Qt3DRender::QParameter(QStringLiteral("colorFactor"), QColor(Qt::white));
276 material->addParameter(parameter);
277
278 Qt3DRender::QTextureLoader *textureLoader = new Qt3DRender::QTextureLoader{};
279 textureLoader->setSource(QUrl{QStringLiteral("qrc:///qtlogo.png")});
280 Qt3DRender::QParameter *texture = new Qt3DRender::QParameter(QStringLiteral("myTexture"), textureLoader);
281 material->addParameter(texture);
282
283 QTimer *parameterAnimationTimer = new QTimer(&view);
284 QObject::connect(parameterAnimationTimer, &QTimer::timeout,
285 parameter, [parameter] {
286 static int angle = 0;
287 const float anglef = qDegreesToRadians(float(angle));
288 parameter->setValue(QColor::fromRgbF(fabs(qCos(anglef)), fabs(qSin(anglef)), 1.0f));
289 angle += 10;
290 });
291 parameterAnimationTimer->start(16);
292
293 // Torus
294 Qt3DCore::QEntity *customMeshEntity = new Qt3DCore::QEntity(rootEntity);
295
296 // Transform
297 Qt3DCore::QTransform *transform = new Qt3DCore::QTransform;
298 transform->setScale(8.0f);
299
300 // Custom Mesh (TetraHedron)
301 Qt3DRender::QGeometryRenderer *customMeshRenderer = new Qt3DRender::QGeometryRenderer;
302 Qt3DCore::QGeometryView *customMeshView = new Qt3DCore::QGeometryView;
303 Qt3DCore::QGeometry *customGeometry = new Qt3DCore::QGeometry;
304
305 Qt3DCore::QBuffer *vertexDataBuffer = new Qt3DCore::QBuffer(customGeometry);
306 Qt3DCore::QBuffer *indexDataBuffer = new Qt3DCore::QBuffer(customGeometry);
307
308 // 5 vertices of 3 vertices + 3 colors
309 QByteArray vertexBufferData;
310 vertexBufferData.resize(5 * (3 + 3) * sizeof(float));
311
312 // Vertices
313 QVector3D v0(-1.0f, 0.0f, -1.0f);
314 QVector3D v1(1.0f, 0.0f, -1.0f);
315 QVector3D v2(-1.0f, 0.0f, 1.0f);
316 QVector3D v3(1.0f, 0.0f, 1.0f);
317 QVector3D v4(0.0f, 2.0f, 0.0f);
318
319 QVector3D red(1.0f, 0.0f, 0.0f);
320 QVector3D green(0.0f, 1.0f, 0.0f);
321 QVector3D blue(0.0f, 0.0f, 1.0f);
322 QVector3D white(1.0f, 1.0f, 1.0f);
323 QVector3D grey(0.5f, 0.5f, 0.5f);
324
325 const QList<QVector3D> vertices = { v0, red,
326 v1, green,
327 v2, blue,
328 v3, grey,
329 v4, white };
330
331 memcpy(vertexBufferData.data(), vertices.constData(), vertices.size() * sizeof(QVector3D));
332 vertexDataBuffer->setData(vertexBufferData);
333
334 QByteArray indexBufferData;
335 // 6 triangle faces
336 indexBufferData.resize(6 * 3 * sizeof(ushort));
337 ushort *rawIndexArray = reinterpret_cast<ushort *>(indexBufferData.data());
338
339 rawIndexArray[0] = 0;
340 rawIndexArray[1] = 1;
341 rawIndexArray[2] = 2;
342
343 rawIndexArray[3] = 2;
344 rawIndexArray[4] = 1;
345 rawIndexArray[5] = 3;
346
347 rawIndexArray[6] = 2;
348 rawIndexArray[7] = 3;
349 rawIndexArray[8] = 4;
350
351 rawIndexArray[9] = 3;
352 rawIndexArray[10] = 1;
353 rawIndexArray[11] = 4;
354
355 rawIndexArray[12] = 1;
356 rawIndexArray[13] = 0;
357 rawIndexArray[14] = 4;
358
359 rawIndexArray[15] = 0;
360 rawIndexArray[16] = 2;
361 rawIndexArray[17] = 4;
362
363 indexDataBuffer->setData(indexBufferData);
364
365 // Attributes
366 Qt3DCore::QAttribute *positionAttribute = new Qt3DCore::QAttribute();
367 positionAttribute->setAttributeType(Qt3DCore::QAttribute::VertexAttribute);
368 positionAttribute->setBuffer(vertexDataBuffer);
369 positionAttribute->setVertexBaseType(Qt3DCore::QAttribute::Float);
370 positionAttribute->setVertexSize(3);
371 positionAttribute->setByteOffset(0);
372 positionAttribute->setByteStride(6 * sizeof(float));
373 positionAttribute->setCount(5);
374 positionAttribute->setName(Qt3DCore::QAttribute::defaultPositionAttributeName());
375
376 Qt3DCore::QAttribute *colorAttribute = new Qt3DCore::QAttribute();
377 colorAttribute->setAttributeType(Qt3DCore::QAttribute::VertexAttribute);
378 colorAttribute->setBuffer(vertexDataBuffer);
379 colorAttribute->setVertexBaseType(Qt3DCore::QAttribute::Float);
380 colorAttribute->setVertexSize(3);
381 colorAttribute->setByteOffset(3 * sizeof(float));
382 colorAttribute->setByteStride(6 * sizeof(float));
383 colorAttribute->setCount(5);
384 colorAttribute->setName(Qt3DCore::QAttribute::defaultColorAttributeName());
385
386 Qt3DCore::QAttribute *indexAttribute = new Qt3DCore::QAttribute();
387 indexAttribute->setAttributeType(Qt3DCore::QAttribute::IndexAttribute);
388 indexAttribute->setBuffer(indexDataBuffer);
389 indexAttribute->setVertexBaseType(Qt3DCore::QAttribute::UnsignedShort);
390 indexAttribute->setVertexSize(1);
391 indexAttribute->setByteOffset(0);
392 indexAttribute->setByteStride(0);
393 indexAttribute->setCount(18);
394
395 customGeometry->addAttribute(positionAttribute);
396 customGeometry->addAttribute(colorAttribute);
397 customGeometry->addAttribute(indexAttribute);
398
399 customMeshView->setPrimitiveType(Qt3DCore::QGeometryView::Triangles);
400 customMeshView->setGeometry(customGeometry);
401 customMeshRenderer->setView(customMeshView);
402
403 customMeshEntity->addComponent(customMeshRenderer);
404 customMeshEntity->addComponent(transform);
405 customMeshEntity->addComponent(material);
406
407 view.setRootEntity(rootEntity);
408 view.show();
409
410 return app.exec();
411 }