/* * Use e.g. the following mesa-issue-qt3d.pro to build: * ---clip--- TEMPLATE = app TARGET = mesa-issue-qt3d QT = core gui 3dcore 3drender 3dinput 3dextras opengl SOURCES += mesa-issue-qt3d.cc * ---clip--- * */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include void drawLine(const QVector3D& start, const QVector3D& end, const QColor& color, Qt3DCore::QEntity *_rootEntity) { auto *geometry = new Qt3DRender::QGeometry(_rootEntity); // position vertices (start and end) QByteArray bufferBytes; bufferBytes.resize(3 * 2 * sizeof(float)); // start.x, start.y, start.end + end.x, end.y, end.z float *positions = reinterpret_cast(bufferBytes.data()); *positions++ = start.x(); *positions++ = start.y(); *positions++ = start.z(); *positions++ = end.x(); *positions++ = end.y(); *positions++ = end.z(); auto *buf = new Qt3DRender::QBuffer(geometry); buf->setData(bufferBytes); auto *positionAttribute = new Qt3DRender::QAttribute(geometry); positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName()); positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float); positionAttribute->setVertexSize(3); positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute); positionAttribute->setBuffer(buf); positionAttribute->setByteStride(3 * sizeof(float)); positionAttribute->setCount(2); geometry->addAttribute(positionAttribute); // We add the vertices in the geometry // connectivity between vertices QByteArray indexBytes; indexBytes.resize(2 * sizeof(unsigned int)); // start to end unsigned int *indices = reinterpret_cast(indexBytes.data()); *indices++ = 0; *indices++ = 1; auto *indexBuffer = new Qt3DRender::QBuffer(geometry); indexBuffer->setData(indexBytes); auto *indexAttribute = new Qt3DRender::QAttribute(geometry); indexAttribute->setVertexBaseType(Qt3DRender::QAttribute::UnsignedInt); indexAttribute->setAttributeType(Qt3DRender::QAttribute::IndexAttribute); indexAttribute->setBuffer(indexBuffer); indexAttribute->setCount(2); geometry->addAttribute(indexAttribute); // We add the indices linking the points in the geometry // mesh auto *line = new Qt3DRender::QGeometryRenderer(_rootEntity); line->setGeometry(geometry); line->setPrimitiveType(Qt3DRender::QGeometryRenderer::Lines); auto *material = new Qt3DExtras::QPhongMaterial(_rootEntity); material->setAmbient(color); // entity auto *lineEntity = new Qt3DCore::QEntity(_rootEntity); lineEntity->addComponent(line); lineEntity->addComponent(material); } int main(int argc, char* argv[]) { QGuiApplication app(argc, argv); Qt3DExtras::Qt3DWindow view; view.show(); Qt3DCore::QEntity *scene = new Qt3DCore::QEntity; drawLine(QVector3D( 5.308311f *1000,-0.163387f*1000,0.616334f*1000 ), QVector3D( 5.388281f*1000,-1.404217f*1000,0.671856f*1000 ),QColor(Qt::red),scene ); // Camera Qt3DRender::QCamera *camera = view.camera(); camera->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 1.0f, 50000.0f); camera->setPosition(QVector3D(-2000.0, -900, 2014.0 )); camera->setUpVector(QVector3D(0.0,0.0,1.0 )); camera->setViewCenter(QVector3D(4423.0, -62.0, 814.0)); view.setRootEntity(scene); view.show(); return app.exec(); }