c++ - Qt3D:为 Qt3DRender::QGeometryRenderer::Lines 设置线宽
问题描述
我不确定如何在 Qt3D 中设置线条的宽度。
我知道Qt3DRender::QRenderStateSet和QLineWidth,但是如何在以下 MWE 中使用它们(从这里):
#include <QGuiApplication>
#include <Qt3DCore/QEntity>
#include <Qt3DCore/QTransform>
#include <Qt3DExtras/Qt3DWindow>
#include <Qt3DExtras/QSphereMesh>
#include <Qt3DExtras/QPhongMaterial>
#include <Qt3DExtras/QFirstPersonCameraController>
#include <Qt3DExtras/QDiffuseSpecularMaterial>
#include <Qt3DRender/QCamera>
#include <Qt3DRender/QBuffer>
#include <Qt3DRender/QAttribute>
Qt3DCore::QEntity *createEntityBox(const QVector3D &min,
const QVector3D &max,
const QColor &color,
Qt3DCore::QEntity *parent)
{
auto geometry = new Qt3DRender::QGeometry;
QByteArray bufferBytes;
bufferBytes.resize(3 * 8 * sizeof(float));
float *positions = reinterpret_cast<float*>(bufferBytes.data());
*positions++ = min.x(); *positions++ = min.y(); *positions++ = min.z();
*positions++ = max.x(); *positions++ = min.y(); *positions++ = min.z();
*positions++ = max.x(); *positions++ = min.y(); *positions++ = max.z();
*positions++ = min.x(); *positions++ = min.y(); *positions++ = max.z();
*positions++ = min.x(); *positions++ = max.y(); *positions++ = min.z();
*positions++ = max.x(); *positions++ = max.y(); *positions++ = min.z();
*positions++ = max.x(); *positions++ = max.y(); *positions++ = max.z();
*positions++ = min.x(); *positions++ = max.y(); *positions++ = max.z();
auto *vertexBuffer = new Qt3DRender::QBuffer(geometry);
vertexBuffer->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(vertexBuffer);
positionAttribute->setByteStride(3 * sizeof(float));
positionAttribute->setCount(8);
geometry->addAttribute(positionAttribute);
QByteArray indexBytes;
indexBytes.resize(24 * sizeof(unsigned int));
unsigned int *indices = reinterpret_cast<unsigned int*>(indexBytes.data());
*indices++ = 0; *indices++ = 1;
*indices++ = 1; *indices++ = 2;
*indices++ = 2; *indices++ = 3;
*indices++ = 3; *indices++ = 0;
*indices++ = 0; *indices++ = 4;
*indices++ = 1; *indices++ = 5;
*indices++ = 2; *indices++ = 6;
*indices++ = 3; *indices++ = 7;
*indices++ = 4; *indices++ = 5;
*indices++ = 5; *indices++ = 6;
*indices++ = 6; *indices++ = 7;
*indices++ = 7; *indices++ = 4;
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(24);
geometry->addAttribute(indexAttribute);
geometry->setParent(parent);
auto lineEntity = new Qt3DCore::QEntity(parent);
auto line = new Qt3DRender::QGeometryRenderer(lineEntity);
line->setGeometry(geometry);
line->setPrimitiveType(Qt3DRender::QGeometryRenderer::Lines);
auto material = new Qt3DExtras::QDiffuseSpecularMaterial(lineEntity);
material->setAmbient(color);
lineEntity->addComponent(line);
lineEntity->addComponent(material);
return lineEntity;
}
int main(int argc, char *argv[])
{
QGuiApplication application(argc, argv);
Qt3DExtras::Qt3DWindow window;
auto scene = new Qt3DCore::QEntity;
window.setRootEntity(scene);
auto sphere = new Qt3DCore::QEntity(scene);
auto transform = new Qt3DCore::QTransform;
transform->setTranslation(QVector3D(0.0f, 0.0f, -10.0f));
auto material = new Qt3DExtras::QPhongMaterial;
material->setAmbient(Qt::blue);
auto spheremesh = new Qt3DExtras::QSphereMesh;
spheremesh->setRadius(15.0);
spheremesh->setSlices(32);
spheremesh->setRings(32);
sphere->addComponent(transform);
sphere->addComponent(material);
sphere->addComponent(spheremesh);
createEntityBox(QVector3D(-15.0f, -15.0f, -15.0f), QVector3D(15.0f, 15.0f, 15.0f), Qt::red, sphere);
auto camera = window.camera();
camera->lens()->setPerspectiveProjection(60.0f, static_cast<float>(window.width()) / window.height(), 0.1f, 1000.0f);
camera->setPosition(QVector3D(0.0f, 0.0f, 100.0f));
camera->setViewCenter(QVector3D(0.0f, 0.0f, 0.0f));
auto camController = new Qt3DExtras::QFirstPersonCameraController(scene);
camController->setCamera(camera);
window.show();
return application.exec();
}
该项目也可以在GitHub上找到。
解决方案
推荐阅读
- java - 我试图从 struts 2.3 迁移到 2.5。我将 struts 的依赖项更改为 2.5
- tensorflow - 如何从 tensorflow_hub 查看模型中的修改层
- amazon-web-services - AWS Glue Crawler 为每个文件定义一个架构
- qemu - 收到警告:格式错误的选项描述“[enable=]PATTERN”,应该看起来像“opt”
- testing - 如何用jmeter设计电商应用压力测试
- c# - Mongo - 将带有数组的字典保存到 BsonDocument 中
- android-studio - 如何使用 android PrintService 配置 Build In POS 打印机
- java - Firebase Firestore Android 工作室
- python - Jupyter Notebook 上的图像相关问题
- .net-core - DotNet Core 3.1 中 _Layout.cshtml 中的用户名