| 1 | // Copyright (C) 2015 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 | |
| 28 | int main(int argc, char* argv[]) |
| 29 | { |
| 30 | QGuiApplication app(argc, argv); |
| 31 | Qt3DExtras::Qt3DWindow view; |
| 32 | view.defaultFrameGraph()->setClearColor(QColor::fromRgbF(0.0, 0.5, 1.0, 1.0)); |
| 33 | |
| 34 | // Root entity |
| 35 | Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity(); |
| 36 | |
| 37 | // Camera |
| 38 | Qt3DRender::QCamera *cameraEntity = view.camera(); |
| 39 | |
| 40 | cameraEntity->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f); |
| 41 | cameraEntity->setPosition(QVector3D(0, 0, 40.0f)); |
| 42 | cameraEntity->setUpVector(QVector3D(0, 1, 0)); |
| 43 | cameraEntity->setViewCenter(QVector3D(0, 0, 0)); |
| 44 | |
| 45 | // For camera controls |
| 46 | Qt3DExtras::QOrbitCameraController *camController = new Qt3DExtras::QOrbitCameraController(rootEntity); |
| 47 | camController->setCamera(cameraEntity); |
| 48 | |
| 49 | // Material |
| 50 | Qt3DRender::QMaterial *material = new Qt3DExtras::QPerVertexColorMaterial(rootEntity); |
| 51 | |
| 52 | // Torus |
| 53 | Qt3DCore::QEntity *customMeshEntity = new Qt3DCore::QEntity(rootEntity); |
| 54 | |
| 55 | // Transform |
| 56 | Qt3DCore::QTransform *transform = new Qt3DCore::QTransform; |
| 57 | transform->setScale(8.0f); |
| 58 | |
| 59 | // Custom Mesh (TetraHedron) |
| 60 | Qt3DRender::QGeometryRenderer *customMeshRenderer = new Qt3DRender::QGeometryRenderer; |
| 61 | Qt3DCore::QGeometry *customGeometry = new Qt3DCore::QGeometry(customMeshRenderer); |
| 62 | |
| 63 | Qt3DCore::QBuffer *vertexDataBuffer = new Qt3DCore::QBuffer(customGeometry); |
| 64 | Qt3DCore::QBuffer *indexDataBuffer = new Qt3DCore::QBuffer(customGeometry); |
| 65 | |
| 66 | // vec3 for position |
| 67 | // vec3 for colors |
| 68 | // vec3 for normals |
| 69 | |
| 70 | /* 2 |
| 71 | /|\ |
| 72 | / | \ |
| 73 | / /3\ \ |
| 74 | 0/___\ 1 |
| 75 | */ |
| 76 | |
| 77 | // 4 distinct vertices |
| 78 | QByteArray vertexBufferData; |
| 79 | vertexBufferData.resize(4 * (3 + 3 + 3) * sizeof(float)); |
| 80 | |
| 81 | // Vertices |
| 82 | QVector3D v0(-1.0f, 0.0f, -1.0f); |
| 83 | QVector3D v1(1.0f, 0.0f, -1.0f); |
| 84 | QVector3D v2(0.0f, 1.0f, 0.0f); |
| 85 | QVector3D v3(0.0f, 0.0f, 1.0f); |
| 86 | |
| 87 | // Faces Normals |
| 88 | QVector3D n023 = QVector3D::normal(v0, v2, v3); |
| 89 | QVector3D n012 = QVector3D::normal(v0, v1, v2); |
| 90 | QVector3D n310 = QVector3D::normal(v3, v1, v0); |
| 91 | QVector3D n132 = QVector3D::normal(v1, v3, v2); |
| 92 | |
| 93 | // Vector Normals |
| 94 | QVector3D n0 = QVector3D(n023 + n012 + n310).normalized(); |
| 95 | QVector3D n1 = QVector3D(n132 + n012 + n310).normalized(); |
| 96 | QVector3D n2 = QVector3D(n132 + n012 + n023).normalized(); |
| 97 | QVector3D n3 = QVector3D(n132 + n310 + n023).normalized(); |
| 98 | |
| 99 | // Colors |
| 100 | QVector3D red(1.0f, 0.0f, 0.0f); |
| 101 | QVector3D green(0.0f, 1.0f, 0.0f); |
| 102 | QVector3D blue(0.0f, 0.0f, 1.0f); |
| 103 | QVector3D white(1.0f, 1.0f, 1.0f); |
| 104 | |
| 105 | const QList<QVector3D> vertices = { v0, n0, red, |
| 106 | v1, n1, blue, |
| 107 | v2, n2, green, |
| 108 | v3, n3, white }; |
| 109 | |
| 110 | float *rawVertexArray = reinterpret_cast<float *>(vertexBufferData.data()); |
| 111 | int idx = 0; |
| 112 | |
| 113 | for (const QVector3D &v : vertices) { |
| 114 | rawVertexArray[idx++] = v.x(); |
| 115 | rawVertexArray[idx++] = v.y(); |
| 116 | rawVertexArray[idx++] = v.z(); |
| 117 | } |
| 118 | |
| 119 | // Indices (12) |
| 120 | QByteArray indexBufferData; |
| 121 | indexBufferData.resize(4 * 3 * sizeof(ushort)); |
| 122 | ushort *rawIndexArray = reinterpret_cast<ushort *>(indexBufferData.data()); |
| 123 | |
| 124 | // Front |
| 125 | rawIndexArray[0] = 0; |
| 126 | rawIndexArray[1] = 1; |
| 127 | rawIndexArray[2] = 2; |
| 128 | // Bottom |
| 129 | rawIndexArray[3] = 3; |
| 130 | rawIndexArray[4] = 1; |
| 131 | rawIndexArray[5] = 0; |
| 132 | // Left |
| 133 | rawIndexArray[6] = 0; |
| 134 | rawIndexArray[7] = 2; |
| 135 | rawIndexArray[8] = 3; |
| 136 | // Right |
| 137 | rawIndexArray[9] = 1; |
| 138 | rawIndexArray[10] = 3; |
| 139 | rawIndexArray[11] = 2; |
| 140 | |
| 141 | vertexDataBuffer->setData(vertexBufferData); |
| 142 | indexDataBuffer->setData(indexBufferData); |
| 143 | |
| 144 | // Attributes |
| 145 | Qt3DCore::QAttribute *positionAttribute = new Qt3DCore::QAttribute(); |
| 146 | positionAttribute->setAttributeType(Qt3DCore::QAttribute::VertexAttribute); |
| 147 | positionAttribute->setBuffer(vertexDataBuffer); |
| 148 | positionAttribute->setVertexBaseType(Qt3DCore::QAttribute::Float); |
| 149 | positionAttribute->setVertexSize(3); |
| 150 | positionAttribute->setByteOffset(0); |
| 151 | positionAttribute->setByteStride(9 * sizeof(float)); |
| 152 | positionAttribute->setCount(4); |
| 153 | positionAttribute->setName(Qt3DCore::QAttribute::defaultPositionAttributeName()); |
| 154 | |
| 155 | Qt3DCore::QAttribute *normalAttribute = new Qt3DCore::QAttribute(); |
| 156 | normalAttribute->setAttributeType(Qt3DCore::QAttribute::VertexAttribute); |
| 157 | normalAttribute->setBuffer(vertexDataBuffer); |
| 158 | normalAttribute->setVertexBaseType(Qt3DCore::QAttribute::Float); |
| 159 | normalAttribute->setVertexSize(3); |
| 160 | normalAttribute->setByteOffset(3 * sizeof(float)); |
| 161 | normalAttribute->setByteStride(9 * sizeof(float)); |
| 162 | normalAttribute->setCount(4); |
| 163 | normalAttribute->setName(Qt3DCore::QAttribute::defaultNormalAttributeName()); |
| 164 | |
| 165 | Qt3DCore::QAttribute *colorAttribute = new Qt3DCore::QAttribute(); |
| 166 | colorAttribute->setAttributeType(Qt3DCore::QAttribute::VertexAttribute); |
| 167 | colorAttribute->setBuffer(vertexDataBuffer); |
| 168 | colorAttribute->setVertexBaseType(Qt3DCore::QAttribute::Float); |
| 169 | colorAttribute->setVertexSize(3); |
| 170 | colorAttribute->setByteOffset(6 * sizeof(float)); |
| 171 | colorAttribute->setByteStride(9 * sizeof(float)); |
| 172 | colorAttribute->setCount(4); |
| 173 | colorAttribute->setName(Qt3DCore::QAttribute::defaultColorAttributeName()); |
| 174 | |
| 175 | Qt3DCore::QAttribute *indexAttribute = new Qt3DCore::QAttribute(); |
| 176 | indexAttribute->setAttributeType(Qt3DCore::QAttribute::IndexAttribute); |
| 177 | indexAttribute->setBuffer(indexDataBuffer); |
| 178 | indexAttribute->setVertexBaseType(Qt3DCore::QAttribute::UnsignedShort); |
| 179 | indexAttribute->setVertexSize(1); |
| 180 | indexAttribute->setByteOffset(0); |
| 181 | indexAttribute->setByteStride(0); |
| 182 | indexAttribute->setCount(12); |
| 183 | |
| 184 | customGeometry->addAttribute(positionAttribute); |
| 185 | customGeometry->addAttribute(normalAttribute); |
| 186 | customGeometry->addAttribute(colorAttribute); |
| 187 | customGeometry->addAttribute(indexAttribute); |
| 188 | |
| 189 | customMeshRenderer->setInstanceCount(1); |
| 190 | customMeshRenderer->setIndexOffset(0); |
| 191 | customMeshRenderer->setFirstInstance(0); |
| 192 | customMeshRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Triangles); |
| 193 | customMeshRenderer->setGeometry(customGeometry); |
| 194 | // 4 faces of 3 points |
| 195 | customMeshRenderer->setVertexCount(12); |
| 196 | |
| 197 | customMeshEntity->addComponent(customMeshRenderer); |
| 198 | customMeshEntity->addComponent(transform); |
| 199 | customMeshEntity->addComponent(material); |
| 200 | |
| 201 | view.setRootEntity(rootEntity); |
| 202 | view.show(); |
| 203 | |
| 204 | return app.exec(); |
| 205 | } |