#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include void createMesh(Qt3DCore::QEntity* parent) { auto geometry = new Qt3DRender::QGeometry; auto vertexBuffer = new Qt3DRender::QBuffer(geometry); auto indexBuffer = new Qt3DRender::QBuffer(geometry); const quint32 elementSize = 3; // 3 floats per vertex const quint32 stride = elementSize * sizeof(float); auto positionAttribute = new Qt3DRender::QAttribute; positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName()); positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute); positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float); positionAttribute->setVertexSize(3); positionAttribute->setByteStride(stride); positionAttribute->setBuffer(vertexBuffer); geometry->addAttribute(positionAttribute); auto indexAttribute = new Qt3DRender::QAttribute; indexAttribute->setAttributeType(Qt3DRender::QAttribute::IndexAttribute); indexAttribute->setVertexBaseType(Qt3DRender::QAttribute::UnsignedInt); indexAttribute->setVertexSize(1); indexAttribute->setByteStride(indexAttribute->vertexSize() * sizeof(uint32_t)); indexAttribute->setBuffer(indexBuffer); geometry->addAttribute(indexAttribute); auto geometryRenderer = new Qt3DRender::QGeometryRenderer; geometryRenderer->setPrimitiveRestartEnabled(true); geometryRenderer->setRestartIndexValue(-1); geometryRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::LineStrip); geometryRenderer->setGeometry(geometry); float vertices[] = { 1,2,0, 0,0,0, 1,-2,0, 4,2,0, 5,0,0, 4,-2,0 }; auto numVertices = (sizeof(vertices) / sizeof(vertices[0])) / 3; uint32_t indices[] = { 0, 1, 2, static_cast(-1), 3, 4, 5, static_cast(-1) }; auto numIndices = sizeof(indices) / sizeof(indices[0]); QByteArray vertexBufferData; vertexBufferData.resize(static_cast(numVertices * stride)); memcpy(vertexBufferData.data(), vertices, numVertices * stride); QByteArray indexBufferData; indexBufferData.resize(static_cast(numIndices * sizeof(uint32_t))); memcpy(indexBufferData.data(), indices, numIndices * sizeof(uint32_t)); vertexBuffer->setData(vertexBufferData); indexBuffer->setData(indexBufferData); positionAttribute->setCount(static_cast(numVertices)); indexAttribute->setCount(static_cast(numIndices)); parent->addComponent(geometryRenderer); } int main(int argc, char* argv[]) { QApplication a(argc, argv); auto view = new Qt3DExtras::Qt3DWindow(); auto pickSettings = view->renderSettings()->pickingSettings(); pickSettings->setPickMethod(Qt3DRender::QPickingSettings::PrimitivePicking); pickSettings->setWorldSpaceTolerance(0.1f); auto rootEntity = new Qt3DCore::QEntity(); view->setRootEntity(rootEntity); auto rayCaster = new Qt3DRender::QScreenRayCaster(rootEntity); rayCaster->setRunMode(Qt3DRender::QAbstractRayCaster::Continuous); rootEntity->addComponent(rayCaster); QObject::connect(rayCaster, &Qt3DRender::QScreenRayCaster::hitsChanged, [&](const Qt3DRender::QAbstractRayCaster::Hits& hits) { for (auto& hit : hits) qDebug() << "Hit data" << hit.entity()->objectName() << hit.worldIntersection() << hit.distance() << hit.type(); }); auto camera = view->camera(); camera->lens()->setPerspectiveProjection(45.0f, 1., 0.1f, 10000.0f); camera->setPosition(QVector3D(0, 0, 5)); camera->setUpVector(QVector3D(0, 1, 0)); camera->setViewCenter(QVector3D(0, 0, 0)); auto camController = new Qt3DExtras::QOrbitCameraController(rootEntity); camController->setCamera(camera); auto sampleLineStrip = new Qt3DCore::QEntity(); createMesh(sampleLineStrip); sampleLineStrip->addComponent(new Qt3DExtras::QDiffuseSpecularMaterial); sampleLineStrip->setParent(rootEntity); auto container = QWidget::createWindowContainer(view); QFrame frame; frame.setLayout(new QVBoxLayout); frame.layout()->addWidget(container); frame.resize(QSize(400, 300)); QTimer::singleShot(100, [&]() { camera->viewAll(); }); QTimer timer; QObject::connect(&timer, &QTimer::timeout, [&]() { rayCaster->trigger(container->mapFromGlobal(QCursor::pos())); qDebug() << "Trigger Raycaster"; }); timer.start(100); frame.show(); return a.exec(); }