| 1 | // Copyright (C) 2016 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 | namespace { |
| 29 | |
| 30 | struct IndirectElementDrawBuffer{ // Element Indirect |
| 31 | uint count; |
| 32 | uint instancesCount; |
| 33 | uint firstIndex; |
| 34 | uint baseVertex; |
| 35 | uint baseInstance; |
| 36 | }; |
| 37 | |
| 38 | struct IndirectArrayDrawBuffer{ // Array Indirect |
| 39 | uint count; |
| 40 | uint instancesCount; |
| 41 | uint first; |
| 42 | uint baseInstance; |
| 43 | }; |
| 44 | |
| 45 | } // anonymous |
| 46 | |
| 47 | int main(int argc, char* argv[]) |
| 48 | { |
| 49 | QGuiApplication app(argc, argv); |
| 50 | Qt3DExtras::Qt3DWindow view; |
| 51 | view.defaultFrameGraph()->setClearColor(QColor::fromRgbF(0.0, 0.5, 1.0, 1.0)); |
| 52 | |
| 53 | // Root entity |
| 54 | Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity(); |
| 55 | |
| 56 | // Camera |
| 57 | Qt3DRender::QCamera *cameraEntity = view.camera(); |
| 58 | |
| 59 | cameraEntity->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f); |
| 60 | cameraEntity->setPosition(QVector3D(0, 0, 40.0f)); |
| 61 | cameraEntity->setUpVector(QVector3D(0, 1, 0)); |
| 62 | cameraEntity->setViewCenter(QVector3D(0, 0, 0)); |
| 63 | |
| 64 | // For camera controls |
| 65 | Qt3DExtras::QOrbitCameraController *camController = new Qt3DExtras::QOrbitCameraController(rootEntity); |
| 66 | camController->setCamera(cameraEntity); |
| 67 | |
| 68 | // Material |
| 69 | Qt3DRender::QMaterial *material = new Qt3DExtras::QPerVertexColorMaterial(rootEntity); |
| 70 | |
| 71 | // vec3 for position |
| 72 | // vec3 for colors |
| 73 | // vec3 for normals |
| 74 | |
| 75 | /* 2 |
| 76 | /|\ |
| 77 | / | \ |
| 78 | / /3\ \ |
| 79 | 0/___\ 1 |
| 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 | // DrawElementIndirect |
| 111 | { |
| 112 | Qt3DCore::QEntity *customDrawElementIndirectEntity = new Qt3DCore::QEntity(rootEntity); |
| 113 | |
| 114 | // Transform |
| 115 | Qt3DCore::QTransform *transform = new Qt3DCore::QTransform; |
| 116 | transform->setScale(8.0f); |
| 117 | transform->setTranslation(QVector3D(-10.0f, 0.0, 0.0f)); |
| 118 | |
| 119 | // Custom Mesh (TetraHedron) |
| 120 | Qt3DRender::QGeometryRenderer *customMeshRenderer = new Qt3DRender::QGeometryRenderer; |
| 121 | Qt3DCore::QGeometry *customGeometry = new Qt3DCore::QGeometry(customMeshRenderer); |
| 122 | |
| 123 | Qt3DCore::QBuffer *vertexDataBuffer = new Qt3DCore::QBuffer(customGeometry); |
| 124 | Qt3DCore::QBuffer *indexDataBuffer = new Qt3DCore::QBuffer(customGeometry); |
| 125 | Qt3DCore::QBuffer *indirectDrawDataBuffer = new Qt3DCore::QBuffer(customGeometry); |
| 126 | |
| 127 | // 4 distinct vertices |
| 128 | QByteArray vertexBufferData; |
| 129 | vertexBufferData.resize(4 * (3 + 3 + 3) * sizeof(float)); |
| 130 | |
| 131 | float *rawVertexArray = reinterpret_cast<float *>(vertexBufferData.data()); |
| 132 | int idx = 0; |
| 133 | |
| 134 | for (const QVector3D &v : vertices) { |
| 135 | rawVertexArray[idx++] = v.x(); |
| 136 | rawVertexArray[idx++] = v.y(); |
| 137 | rawVertexArray[idx++] = v.z(); |
| 138 | } |
| 139 | |
| 140 | // Indices (12) |
| 141 | QByteArray indexBufferData; |
| 142 | indexBufferData.resize(4 * 3 * sizeof(ushort)); |
| 143 | ushort *rawIndexArray = reinterpret_cast<ushort *>(indexBufferData.data()); |
| 144 | |
| 145 | // Front |
| 146 | rawIndexArray[0] = 0; |
| 147 | rawIndexArray[1] = 1; |
| 148 | rawIndexArray[2] = 2; |
| 149 | // Bottom |
| 150 | rawIndexArray[3] = 3; |
| 151 | rawIndexArray[4] = 1; |
| 152 | rawIndexArray[5] = 0; |
| 153 | // Left |
| 154 | rawIndexArray[6] = 0; |
| 155 | rawIndexArray[7] = 2; |
| 156 | rawIndexArray[8] = 3; |
| 157 | // Right |
| 158 | rawIndexArray[9] = 1; |
| 159 | rawIndexArray[10] = 3; |
| 160 | rawIndexArray[11] = 2; |
| 161 | |
| 162 | |
| 163 | QByteArray indirectBufferData; |
| 164 | indirectBufferData.resize(sizeof(IndirectElementDrawBuffer)); |
| 165 | IndirectElementDrawBuffer *indirectStruct = reinterpret_cast<IndirectElementDrawBuffer *>(indirectBufferData.data()); |
| 166 | indirectStruct->count = 12U; |
| 167 | indirectStruct->firstIndex = 0U; |
| 168 | indirectStruct->baseInstance = 0U; |
| 169 | indirectStruct->baseVertex = 0U; |
| 170 | indirectStruct->instancesCount = 1U; |
| 171 | |
| 172 | vertexDataBuffer->setData(vertexBufferData); |
| 173 | indexDataBuffer->setData(indexBufferData); |
| 174 | indirectDrawDataBuffer->setData(indirectBufferData); |
| 175 | |
| 176 | // Attributes |
| 177 | Qt3DCore::QAttribute *positionAttribute = new Qt3DCore::QAttribute(); |
| 178 | positionAttribute->setAttributeType(Qt3DCore::QAttribute::VertexAttribute); |
| 179 | positionAttribute->setBuffer(vertexDataBuffer); |
| 180 | positionAttribute->setVertexBaseType(Qt3DCore::QAttribute::Float); |
| 181 | positionAttribute->setVertexSize(3); |
| 182 | positionAttribute->setByteOffset(0); |
| 183 | positionAttribute->setByteStride(9 * sizeof(float)); |
| 184 | positionAttribute->setCount(4); |
| 185 | positionAttribute->setName(Qt3DCore::QAttribute::defaultPositionAttributeName()); |
| 186 | |
| 187 | Qt3DCore::QAttribute *normalAttribute = new Qt3DCore::QAttribute(); |
| 188 | normalAttribute->setAttributeType(Qt3DCore::QAttribute::VertexAttribute); |
| 189 | normalAttribute->setBuffer(vertexDataBuffer); |
| 190 | normalAttribute->setVertexBaseType(Qt3DCore::QAttribute::Float); |
| 191 | normalAttribute->setVertexSize(3); |
| 192 | normalAttribute->setByteOffset(3 * sizeof(float)); |
| 193 | normalAttribute->setByteStride(9 * sizeof(float)); |
| 194 | normalAttribute->setCount(4); |
| 195 | normalAttribute->setName(Qt3DCore::QAttribute::defaultNormalAttributeName()); |
| 196 | |
| 197 | Qt3DCore::QAttribute *colorAttribute = new Qt3DCore::QAttribute(); |
| 198 | colorAttribute->setAttributeType(Qt3DCore::QAttribute::VertexAttribute); |
| 199 | colorAttribute->setBuffer(vertexDataBuffer); |
| 200 | colorAttribute->setVertexBaseType(Qt3DCore::QAttribute::Float); |
| 201 | colorAttribute->setVertexSize(3); |
| 202 | colorAttribute->setByteOffset(6 * sizeof(float)); |
| 203 | colorAttribute->setByteStride(9 * sizeof(float)); |
| 204 | colorAttribute->setCount(4); |
| 205 | colorAttribute->setName(Qt3DCore::QAttribute::defaultColorAttributeName()); |
| 206 | |
| 207 | Qt3DCore::QAttribute *indexAttribute = new Qt3DCore::QAttribute(); |
| 208 | indexAttribute->setAttributeType(Qt3DCore::QAttribute::IndexAttribute); |
| 209 | indexAttribute->setBuffer(indexDataBuffer); |
| 210 | indexAttribute->setVertexBaseType(Qt3DCore::QAttribute::UnsignedShort); |
| 211 | indexAttribute->setVertexSize(1); |
| 212 | indexAttribute->setByteOffset(0); |
| 213 | indexAttribute->setByteStride(0); |
| 214 | indexAttribute->setCount(12); |
| 215 | |
| 216 | Qt3DCore::QAttribute *indirectAttribute = new Qt3DCore::QAttribute(); |
| 217 | indirectAttribute->setAttributeType(Qt3DCore::QAttribute::DrawIndirectAttribute); |
| 218 | indirectAttribute->setBuffer(indirectDrawDataBuffer); |
| 219 | indirectAttribute->setByteOffset(0); |
| 220 | indirectAttribute->setByteStride(0); |
| 221 | indirectAttribute->setCount(1); |
| 222 | |
| 223 | customGeometry->addAttribute(positionAttribute); |
| 224 | customGeometry->addAttribute(normalAttribute); |
| 225 | customGeometry->addAttribute(colorAttribute); |
| 226 | customGeometry->addAttribute(indexAttribute); |
| 227 | customGeometry->addAttribute(indirectAttribute); |
| 228 | |
| 229 | customMeshRenderer->setInstanceCount(1); |
| 230 | customMeshRenderer->setIndexOffset(0); |
| 231 | customMeshRenderer->setFirstInstance(0); |
| 232 | customMeshRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Triangles); |
| 233 | customMeshRenderer->setGeometry(customGeometry); |
| 234 | // 4 faces of 3 points |
| 235 | customMeshRenderer->setVertexCount(12); |
| 236 | |
| 237 | customDrawElementIndirectEntity->addComponent(customMeshRenderer); |
| 238 | customDrawElementIndirectEntity->addComponent(transform); |
| 239 | customDrawElementIndirectEntity->addComponent(material); |
| 240 | } |
| 241 | |
| 242 | // DrawArrayIndirect |
| 243 | { |
| 244 | Qt3DCore::QEntity *customDrawArrayIndirectEntity = new Qt3DCore::QEntity(rootEntity); |
| 245 | |
| 246 | // Transform |
| 247 | Qt3DCore::QTransform *transform = new Qt3DCore::QTransform; |
| 248 | transform->setScale(8.0f); |
| 249 | transform->setTranslation(QVector3D(10.0f, 0.0, 0.0f)); |
| 250 | |
| 251 | // Custom Mesh (TetraHedron) |
| 252 | Qt3DRender::QGeometryRenderer *customMeshRenderer = new Qt3DRender::QGeometryRenderer; |
| 253 | Qt3DCore::QGeometry *customGeometry = new Qt3DCore::QGeometry(customMeshRenderer); |
| 254 | |
| 255 | Qt3DCore::QBuffer *vertexDataBuffer = new Qt3DCore::QBuffer(customGeometry); |
| 256 | Qt3DCore::QBuffer *indirectDrawDataBuffer = new Qt3DCore::QBuffer(customGeometry); |
| 257 | |
| 258 | |
| 259 | QVector<QVector3D> orderedPositionVertices; |
| 260 | // 12 vertices with position, normal, color |
| 261 | orderedPositionVertices.reserve(12 * (1 + 1 + 1)); |
| 262 | |
| 263 | orderedPositionVertices << v0 << n0 << red; |
| 264 | orderedPositionVertices << v1 << n1 << red; |
| 265 | orderedPositionVertices << v2 << n2 << red; |
| 266 | |
| 267 | orderedPositionVertices << v3 << n3 << green; |
| 268 | orderedPositionVertices << v1 << n1 << green; |
| 269 | orderedPositionVertices << v0 << n0 << green; |
| 270 | |
| 271 | orderedPositionVertices << v0 << n0 << blue; |
| 272 | orderedPositionVertices << v2 << n2 << blue; |
| 273 | orderedPositionVertices << v3 << n3 << blue; |
| 274 | |
| 275 | orderedPositionVertices << v1 << n1 << white; |
| 276 | orderedPositionVertices << v3 << n3 << white; |
| 277 | orderedPositionVertices << v2 << n2 << white; |
| 278 | |
| 279 | QByteArray vertexBufferData; |
| 280 | vertexBufferData.resize(orderedPositionVertices.size() * sizeof(QVector3D)); |
| 281 | memcpy(vertexBufferData.data(), orderedPositionVertices.data(), orderedPositionVertices.size() * sizeof(QVector3D)); |
| 282 | |
| 283 | QByteArray indirectBufferData; |
| 284 | indirectBufferData.resize(sizeof(IndirectArrayDrawBuffer)); |
| 285 | IndirectArrayDrawBuffer *indirectStruct = reinterpret_cast<IndirectArrayDrawBuffer *>(indirectBufferData.data()); |
| 286 | indirectStruct->count = 12U; |
| 287 | indirectStruct->baseInstance = 0U; |
| 288 | indirectStruct->instancesCount = 1U; |
| 289 | indirectStruct->first = 0U; |
| 290 | |
| 291 | vertexDataBuffer->setData(vertexBufferData); |
| 292 | indirectDrawDataBuffer->setData(indirectBufferData); |
| 293 | |
| 294 | // Attributes |
| 295 | Qt3DCore::QAttribute *positionAttribute = new Qt3DCore::QAttribute(); |
| 296 | positionAttribute->setAttributeType(Qt3DCore::QAttribute::VertexAttribute); |
| 297 | positionAttribute->setBuffer(vertexDataBuffer); |
| 298 | positionAttribute->setVertexBaseType(Qt3DCore::QAttribute::Float); |
| 299 | positionAttribute->setVertexSize(3); |
| 300 | positionAttribute->setByteOffset(0); |
| 301 | positionAttribute->setByteStride(9 * sizeof(float)); |
| 302 | positionAttribute->setCount(12); |
| 303 | positionAttribute->setName(Qt3DCore::QAttribute::defaultPositionAttributeName()); |
| 304 | |
| 305 | Qt3DCore::QAttribute *normalAttribute = new Qt3DCore::QAttribute(); |
| 306 | normalAttribute->setAttributeType(Qt3DCore::QAttribute::VertexAttribute); |
| 307 | normalAttribute->setBuffer(vertexDataBuffer); |
| 308 | normalAttribute->setVertexBaseType(Qt3DCore::QAttribute::Float); |
| 309 | normalAttribute->setVertexSize(3); |
| 310 | normalAttribute->setByteOffset(3 * sizeof(float)); |
| 311 | normalAttribute->setByteStride(9 * sizeof(float)); |
| 312 | normalAttribute->setCount(12); |
| 313 | normalAttribute->setName(Qt3DCore::QAttribute::defaultNormalAttributeName()); |
| 314 | |
| 315 | Qt3DCore::QAttribute *colorAttribute = new Qt3DCore::QAttribute(); |
| 316 | colorAttribute->setAttributeType(Qt3DCore::QAttribute::VertexAttribute); |
| 317 | colorAttribute->setBuffer(vertexDataBuffer); |
| 318 | colorAttribute->setVertexBaseType(Qt3DCore::QAttribute::Float); |
| 319 | colorAttribute->setVertexSize(3); |
| 320 | colorAttribute->setByteOffset(6 * sizeof(float)); |
| 321 | colorAttribute->setByteStride(9 * sizeof(float)); |
| 322 | colorAttribute->setCount(12); |
| 323 | colorAttribute->setName(Qt3DCore::QAttribute::defaultColorAttributeName()); |
| 324 | |
| 325 | Qt3DCore::QAttribute *indirectAttribute = new Qt3DCore::QAttribute(); |
| 326 | indirectAttribute->setAttributeType(Qt3DCore::QAttribute::DrawIndirectAttribute); |
| 327 | indirectAttribute->setBuffer(indirectDrawDataBuffer); |
| 328 | indirectAttribute->setByteOffset(0); |
| 329 | indirectAttribute->setByteStride(0); |
| 330 | indirectAttribute->setCount(1); |
| 331 | |
| 332 | customGeometry->addAttribute(positionAttribute); |
| 333 | customGeometry->addAttribute(normalAttribute); |
| 334 | customGeometry->addAttribute(colorAttribute); |
| 335 | customGeometry->addAttribute(indirectAttribute); |
| 336 | |
| 337 | customMeshRenderer->setInstanceCount(1); |
| 338 | customMeshRenderer->setIndexOffset(0); |
| 339 | customMeshRenderer->setFirstInstance(0); |
| 340 | customMeshRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Triangles); |
| 341 | customMeshRenderer->setGeometry(customGeometry); |
| 342 | // 4 faces of 3 points |
| 343 | customMeshRenderer->setVertexCount(12); |
| 344 | |
| 345 | customDrawArrayIndirectEntity->addComponent(customMeshRenderer); |
| 346 | customDrawArrayIndirectEntity->addComponent(transform); |
| 347 | customDrawArrayIndirectEntity->addComponent(material); |
| 348 | } |
| 349 | |
| 350 | view.setRootEntity(rootEntity); |
| 351 | view.show(); |
| 352 | |
| 353 | return app.exec(); |
| 354 | } |