diff options
Diffstat (limited to 'tests/manual/rhi/main.cpp')
-rw-r--r-- | tests/manual/rhi/main.cpp | 410 |
1 files changed, 410 insertions, 0 deletions
diff --git a/tests/manual/rhi/main.cpp b/tests/manual/rhi/main.cpp new file mode 100644 index 000000000..69361d10b --- /dev/null +++ b/tests/manual/rhi/main.cpp @@ -0,0 +1,410 @@ +/**************************************************************************** +** +** Copyright (C) 2019 Klaralvdalens Datakonsult AB (KDAB). +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** 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 https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QGuiApplication> + +#include <Qt3DCore/QEntity> +#include <Qt3DRender/QCamera> +#include <Qt3DRender/QCameraLens> +#include <Qt3DCore/QTransform> +#include <Qt3DCore/QAspectEngine> + +#include <Qt3DInput/QInputAspect> + +#include <Qt3DRender/QRenderStateSet> +#include <Qt3DRender/QRenderAspect> +#include <Qt3DExtras/QForwardRenderer> +#include <Qt3DExtras/QPerVertexColorMaterial> + +#include <Qt3DRender/QGeometryRenderer> +#include <Qt3DRender/QGeometry> +#include <Qt3DRender/QAttribute> +#include <Qt3DRender/QBuffer> + +#include <QPropertyAnimation> +#include <Qt3DExtras/qt3dwindow.h> +#include <Qt3DExtras/qorbitcameracontroller.h> +#include <Qt3DRender/QParameter> +#include <Qt3DRender/QEffect> +#include <Qt3DRender/QTechnique> +#include <Qt3DRender/QAbstractTexture> +#include <Qt3DRender/QShaderProgram> +#include <Qt3DRender/QRenderPass> +#include <Qt3DRender/QBlendEquation> +#include <Qt3DRender/QBlendEquationArguments> +#include <Qt3DRender/QFilterKey> +#include <Qt3DRender/QGraphicsApiFilter> +#include <QColor> +#include <QVector2D> +#include <QUrl> +#include <Qt3DRender/QMaterial> +#include <Qt3DRender/QFilterKey> +#include <Qt3DRender/QTechnique> +#include <Qt3DRender/QMaterial> + +static const constexpr auto vertex_shader = R"_(#version 450 + +layout(location = 0) in vec3 vertexPosition; + +layout(location = 0) out vec4 color; + +layout(std140, binding = 0) uniform qt3d_uniforms { + mat4 mvp; +}; +void main() +{ + gl_Position = mvp * vec4( vertexPosition, 1.0 ); +} +)_"; + +static const constexpr auto fragment_shader = R"_(#version 450 + +layout(location = 0) out vec4 fragColor; + +layout(std140, binding = 1) uniform custom_uniforms { + float example; +}; +void main() +{ + fragColor = vec4(example, example, example, example); +} + +)_"; + +class Material : public Qt3DRender::QMaterial +{ +public: + explicit Material(Qt3DCore::QNode *parent = nullptr) + : QMaterial(parent) + , m_effect(new Qt3DRender::QEffect(this)) +{ + setEffect(m_effect); + + m_testParam = new Qt3DRender::QParameter(QStringLiteral("example"), float(0.5)); + + m_effect->addParameter(m_testParam); + + m_filter = new Qt3DRender::QFilterKey(this); + m_filter->setName(QStringLiteral("renderingStyle")); + m_filter->setValue(QStringLiteral("forward")); + + m_technique = new Qt3DRender::QTechnique(m_effect); + m_technique->addFilterKey(m_filter); + + m_effect->addTechnique(m_technique); + + m_program = new Qt3DRender::QShaderProgram(m_effect); + m_program->setVertexShaderCode(vertex_shader); + m_program->setFragmentShaderCode(fragment_shader); + + m_renderPass = new Qt3DRender::QRenderPass(m_effect); + + m_renderPass->setShaderProgram(m_program); + + m_technique->addRenderPass(m_renderPass); + + m_technique->graphicsApiFilter()->setApi(Qt3DRender::QGraphicsApiFilter::RHI); +} +private: + Qt3DRender::QEffect *m_effect{}; + Qt3DRender::QParameter *m_testParam{}; + Qt3DRender::QFilterKey *m_filter{}; + Qt3DRender::QTechnique *m_technique{}; + Qt3DRender::QShaderProgram *m_program{}; + Qt3DRender::QRenderPass *m_renderPass{}; +}; + +int main(int argc, char* argv[]) +{ + QGuiApplication app(argc, argv); + Qt3DExtras::Qt3DWindow view; + view.defaultFrameGraph()->setClearColor(QColor::fromRgbF(0.1, 0.5, 0.0, 1.0)); + + // Root entity + Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity(); + + // Camera + Qt3DRender::QCamera *cameraEntity = view.camera(); + + cameraEntity->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f); + cameraEntity->setPosition(QVector3D(0, 0, 40.0f)); + cameraEntity->setUpVector(QVector3D(0, 1, 0)); + cameraEntity->setViewCenter(QVector3D(0, 0, 0)); + + // For camera controls + Qt3DExtras::QOrbitCameraController *camController = new Qt3DExtras::QOrbitCameraController(rootEntity); + camController->setCamera(cameraEntity); + + // Material + Qt3DRender::QMaterial *material = new Material(rootEntity); + + // Torus + Qt3DCore::QEntity *customMeshEntity = new Qt3DCore::QEntity(rootEntity); + + // Transform + Qt3DCore::QTransform *transform = new Qt3DCore::QTransform; + transform->setScale(8.0f); + + // Custom Mesh (TetraHedron) + Qt3DRender::QGeometryRenderer *customMeshRenderer = new Qt3DRender::QGeometryRenderer; + Qt3DRender::QGeometry *customGeometry = new Qt3DRender::QGeometry(customMeshRenderer); + + Qt3DRender::QBuffer *vertexDataBuffer = new Qt3DRender::QBuffer(customGeometry); + + + // 4 distinct vertices + QByteArray vertexBufferData; + vertexBufferData.resize(9 * sizeof(float)); + + // Vertices + QVector3D v0(-1.0f, 0.0f, -1.0f); + QVector3D v1(1.0f, 0.0f, -1.0f); + QVector3D v2(0.0f, 1.0f, 0.0f); + + + const QVector<QVector3D> vertices = QVector<QVector3D>() + << v0 + << v1 + << v2; + + float *rawVertexArray = reinterpret_cast<float *>(vertexBufferData.data()); + int idx = 0; + + for (const QVector3D &v : vertices) { + rawVertexArray[idx++] = v.x(); + rawVertexArray[idx++] = v.y(); + rawVertexArray[idx++] = v.z(); + } + + vertexDataBuffer->setData(vertexBufferData); + + // Attributes + Qt3DRender::QAttribute *positionAttribute = new Qt3DRender::QAttribute(); + positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute); + positionAttribute->setBuffer(vertexDataBuffer); + positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float); + positionAttribute->setVertexSize(3); + positionAttribute->setByteOffset(0); + positionAttribute->setByteStride(3 * sizeof(float)); + positionAttribute->setCount(3); + positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName()); + + customGeometry->addAttribute(positionAttribute); + + customMeshRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Triangles); + customMeshRenderer->setGeometry(customGeometry); + customMeshRenderer->setVertexCount(3); + + customMeshEntity->addComponent(customMeshRenderer); + customMeshEntity->addComponent(transform); + customMeshEntity->addComponent(material); + + view.setRootEntity(rootEntity); + view.show(); + + return app.exec(); +} +/* +int main(int argc, char* argv[]) +{ + QGuiApplication app(argc, argv); + Qt3DExtras::Qt3DWindow view; + view.defaultFrameGraph()->setClearColor(QColor::fromRgbF(0.0, 0.5, 1.0, 1.0)); + + // Root entity + Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity(); + + // Camera + Qt3DRender::QCamera *cameraEntity = view.camera(); + + cameraEntity->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f); + cameraEntity->setPosition(QVector3D(0, 0, 40.0f)); + cameraEntity->setUpVector(QVector3D(0, 1, 0)); + cameraEntity->setViewCenter(QVector3D(0, 0, 0)); + + // For camera controls + Qt3DExtras::QOrbitCameraController *camController = new Qt3DExtras::QOrbitCameraController(rootEntity); + camController->setCamera(cameraEntity); + + // Material + Qt3DRender::QMaterial *material = new Material(rootEntity); + + // Torus + Qt3DCore::QEntity *customMeshEntity = new Qt3DCore::QEntity(rootEntity); + + // Transform + Qt3DCore::QTransform *transform = new Qt3DCore::QTransform; + transform->setScale(8.0f); + + // Custom Mesh (TetraHedron) + Qt3DRender::QGeometryRenderer *customMeshRenderer = new Qt3DRender::QGeometryRenderer; + Qt3DRender::QGeometry *customGeometry = new Qt3DRender::QGeometry(customMeshRenderer); + + Qt3DRender::QBuffer *vertexDataBuffer = new Qt3DRender::QBuffer(customGeometry); + Qt3DRender::QBuffer *indexDataBuffer = new Qt3DRender::QBuffer(customGeometry); + + // vec3 for position + // vec3 for colors + + // 2 + // /|\ + // / | \ + // / /3\ \ + // 0/___\ 1 + + + // 4 distinct vertices + QByteArray vertexBufferData; + vertexBufferData.resize(4 * (3 + 3 + 3) * sizeof(float)); + + // Vertices + QVector3D v0(-1.0f, 0.0f, -1.0f); + QVector3D v1(1.0f, 0.0f, -1.0f); + QVector3D v2(0.0f, 1.0f, 0.0f); + QVector3D v3(0.0f, 0.0f, 1.0f); + + // Colors + QVector3D red(1.0f, 0.0f, 0.0f); + QVector3D green(0.0f, 1.0f, 0.0f); + QVector3D blue(0.0f, 0.0f, 1.0f); + QVector3D white(1.0f, 1.0f, 1.0f); + + const QVector<QVector3D> vertices = QVector<QVector3D>() + << v0 << red + << v1 << blue + << v2 << green + << v3 << white; + + float *rawVertexArray = reinterpret_cast<float *>(vertexBufferData.data()); + int idx = 0; + + for (const QVector3D &v : vertices) { + rawVertexArray[idx++] = v.x(); + rawVertexArray[idx++] = v.y(); + rawVertexArray[idx++] = v.z(); + } + + // Indices (12) + QByteArray indexBufferData; + indexBufferData.resize(4 * 3 * sizeof(ushort)); + ushort *rawIndexArray = reinterpret_cast<ushort *>(indexBufferData.data()); + + // Front + rawIndexArray[0] = 0; + rawIndexArray[1] = 1; + rawIndexArray[2] = 2; + // Bottom + rawIndexArray[3] = 3; + rawIndexArray[4] = 1; + rawIndexArray[5] = 0; + // Left + rawIndexArray[6] = 0; + rawIndexArray[7] = 2; + rawIndexArray[8] = 3; + // Right + rawIndexArray[9] = 1; + rawIndexArray[10] = 3; + rawIndexArray[11] = 2; + + vertexDataBuffer->setData(vertexBufferData); + indexDataBuffer->setData(indexBufferData); + + // Attributes + Qt3DRender::QAttribute *positionAttribute = new Qt3DRender::QAttribute(); + positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute); + positionAttribute->setBuffer(vertexDataBuffer); + positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float); + positionAttribute->setVertexSize(3); + positionAttribute->setByteOffset(0); + positionAttribute->setByteStride(9 * sizeof(float)); + positionAttribute->setCount(4); + positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName()); + + Qt3DRender::QAttribute *colorAttribute = new Qt3DRender::QAttribute(); + colorAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute); + colorAttribute->setBuffer(vertexDataBuffer); + colorAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float); + colorAttribute->setVertexSize(3); + colorAttribute->setByteOffset(3 * sizeof(float)); + colorAttribute->setByteStride(9 * sizeof(float)); + colorAttribute->setCount(4); + colorAttribute->setName(Qt3DRender::QAttribute::defaultColorAttributeName()); + + Qt3DRender::QAttribute *indexAttribute = new Qt3DRender::QAttribute(); + indexAttribute->setAttributeType(Qt3DRender::QAttribute::IndexAttribute); + indexAttribute->setBuffer(indexDataBuffer); + indexAttribute->setVertexBaseType(Qt3DRender::QAttribute::UnsignedShort); + indexAttribute->setVertexSize(1); + indexAttribute->setByteOffset(0); + indexAttribute->setByteStride(0); + indexAttribute->setCount(12); + + customGeometry->addAttribute(positionAttribute); + customGeometry->addAttribute(colorAttribute); + customGeometry->addAttribute(indexAttribute); + + customMeshRenderer->setInstanceCount(1); + customMeshRenderer->setIndexOffset(0); + customMeshRenderer->setFirstInstance(0); + customMeshRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Triangles); + customMeshRenderer->setGeometry(customGeometry); + // 4 faces of 3 points + customMeshRenderer->setVertexCount(12); + + customMeshEntity->addComponent(customMeshRenderer); + customMeshEntity->addComponent(transform); + customMeshEntity->addComponent(material); + + view.setRootEntity(rootEntity); + view.show(); + + return app.exec(); +} +*/ |