英文:
QText2DEntity Does not Render
问题
我有几个问题,都与 QText2DItem 相关,虽然在我有限的时间内无法提供一个最小可复现示例,但我希望听到一些关于可能解决问题的猜测。
我有一个包含 Qt3DExtras::Qt3DWindow
的 QWidget
。窗口的构造如下:
m_view = new Qt3DExtras::Qt3DWindow();
m_container = createWindowContainer(m_view, this);
m_view->renderSettings()->pickingSettings()->setPickResultMode(Qt3DRender::QPickingSettings::NearestPriorityPick);
m_view->renderSettings()->pickingSettings()->setPickMethod(Qt3DRender::QPickingSettings::BoundingVolumePicking);
m_view->renderSettings()->setRenderPolicy(Qt3DRender::QRenderSettings::OnDemand);
m_view->defaultFrameGraph()->setFrustumCullingEnabled(false);
m_camera = m_view->camera();
m_scene = new Qt3DCore::QEntity();
configure_scene();
m_view->setRootEntity(m_scene);
其中 configure_scene
是:
m_light_entity = new Qt3DCore::QEntity(m_scene);
m_light = new Qt3DRender::QPointLight(m_light_entity);
m_ltransform = new Qt3DCore::QTransform(m_scene);
m_ltransform->setTranslation(QVector3D(0.1,10,0));
m_light->setColor("white");
m_light->setIntensity(0.8);
m_light->setEnabled(true);
m_light_entity->addComponent(m_light);
m_light_entity->addComponent(m_ltransform);
constexpr float cam_size = 1000.0f;
m_camera->lens()->setPerspectiveProjection(10.0f, 16.0f/9.0f, 0.1f, cam_size);
m_camera->setPosition(QVector3D(0.1f, 10.0f, 0.1f));
m_camera->setViewCenter(QVector3D(0, 0, 0));
QObject::connect( m_camera, &Qt3DRender::QCamera::viewCenterChanged, m_camera,
[&](const QVector3D&){m_camera->setViewCenter(QVector3D(0,0,0));});
QObject::connect( m_camera, &Qt3DRender::QCamera::positionChanged, m_camera,
[&](const QVector3D& v){if(v.y() <1){m_camera->setPosition({v.x(), 1, v.z()});};});
AxisRender::draw_axes(m_scene, true, true, false);
m_cam_controller = new Qt3DExtras::QOrbitCameraController(m_camera);
m_cam_controller->setLinearSpeed( 50.0f );
m_cam_controller->setLookSpeed( 180.0f );
m_cam_controller->setCamera(m_camera);
在构建场景后,会动态创建多个 Qt3DCore::QEntity
并将它们添加到场景中。在程序的某个时刻,可能需要更改或完全删除实体。每个实体都被分配了一个标签,这是一个 QText2DEntity
。标签和实体会经历多次旋转以将它们放置在场景中,而场景则是根实体。
在程序的某个时刻,我想从头开始创建这个窗口。因此,我从UI中删除了使用上述函数构建的小部件,并创建了一个新的小部件。与程序首次启动时一样,此小部件将经历完全相同的初始化,但是现在标签(QText2DEntity
)不会呈现。我已验证它们已构建(因为它们具有非零地址),但它们在场景中没有显示。
通常我会怀疑这与我处理对象的方式有关,但场景实际上是从头开始恢复的。标签所属的实体以正确的位置渲染,但标签本身却无处可寻。我必须重新启动程序才能恢复标签。
更令人困惑的是,这并不是每次都会发生。有时在删除和重新生成 m_view
时会显示标签。
这是第一个也是最紧迫的问题。第二个问题是 QText2DEntity 的背景不透明。有时实体具有白色背景,其他时候从某些角度看起来是透明的,但从其他角度看起来是有背景的。我查看了这个答案:https://stackoverflow.com/questions/57627805/text2dentity-renders-opaque-and-hides-other-entities-behind-it 但这完全涉及到 QML(我的代码都是 C++),并且一些结构看起来略有不同。此外,关于 QFordwardRenderer
的文档让我感到困惑。
英文:
I have a couple of questions, all surrounding the QText2DItem and while I'm unable to get a MWE in the time that I have to perform this task, I'm hoping to hear some speculation on what could fix the issue.
I have a QWidget
which houses a Qt3DExtras::Qt3DWindow
. This window is constructed as such:
m_view = new Qt3DExtras::Qt3DWindow();
m_container = createWindowContainer(m_view, this);
m_view->renderSettings()->pickingSettings()->setPickResultMode(Qt3DRender::QPickingSettings::NearestPriorityPick);
m_view->renderSettings()->pickingSettings()->setPickMethod(Qt3DRender::QPickingSettings::BoundingVolumePicking);
m_view->renderSettings()->setRenderPolicy(Qt3DRender::QRenderSettings::OnDemand);
m_view->defaultFrameGraph()->setFrustumCullingEnabled(false);
m_camera = m_view->camera();
m_scene = new Qt3DCore::QEntity();
configure_scene();
m_view->setRootEntity(m_scene);
Where configure_scene
is:
m_light_entity = new Qt3DCore::QEntity(m_scene);
m_light = new Qt3DRender::QPointLight(m_light_entity);
m_ltransform = new Qt3DCore::QTransform(m_scene);
m_ltransform->setTranslation(QVector3D(0.1,10,0));
m_light->setColor("white");
m_light->setIntensity(0.8);
m_light->setEnabled(true);
m_light_entity->addComponent(m_light);
m_light_entity->addComponent(m_ltransform);
constexpr float cam_size = 1000.0f;
m_camera->lens()->setPerspectiveProjection(10.0f, 16.0f/9.0f, 0.1f, cam_size);
m_camera->setPosition(QVector3D(0.1f, 10.0f, 0.1f));
m_camera->setViewCenter(QVector3D(0, 0, 0));
QObject::connect( m_camera, &Qt3DRender::QCamera::viewCenterChanged, m_camera,
[&](const QVector3D&){m_camera->setViewCenter(QVector3D(0,0,0));});
QObject::connect( m_camera, &Qt3DRender::QCamera::positionChanged, m_camera,
[&](const QVector3D& v){if(v.y() <1){m_camera->setPosition({v.x(), 1, v.z()});};});
AxisRender::draw_axes(m_scene, true, true, false);
m_cam_controller = new Qt3DExtras::QOrbitCameraController(m_camera);
m_cam_controller->setLinearSpeed( 50.0f );
m_cam_controller->setLookSpeed( 180.0f );
m_cam_controller->setCamera(m_camera);
There are several Qt3DCore::QEntity
s created on the fly and added to the scene once the scene is constructed. At some point in the program, the entities may need to change or be removed entirely. Each Entity has a label assigned to it. The label is a QText2DEntity
. The label and entities undergo several rotations to place them in the scene, with the scene as the root entity.
At some point in the program, I want to start this window from scratch. So, I remove the Widget which is constructed with the aforementioned functions from the UI, and create a new
widget. This widget undergoes precisely the same initialization as it had when the program first started, except now the labels (QText2DEntity
s) won't render. I have verified that they are constructed (as they have non-zero addresses), but they don't show in the scene.
Usually I would suspect that this has something to do with the way I'm handling objects, but the scene is effectively restored from scratch. The entities to which the labels belong are rendered in their correct locations, but the labels themselves are no where to be found. I have to restart the program to restore the labels.
What's even more confounding is that this doesn't happen every time. Sometimes the labels are shown when the m_view
is deleted and regenerated.
That's the first and most pressing issue. The second issue is that the QText2DEntity backgrounds are not transparent. Sometimes the entities have white backgrounds, other times they appear clear from certain angles but have backgrounds from other angles. I've looked at this answer: https://stackoverflow.com/questions/57627805/text2dentity-renders-opaque-and-hides-other-entities-behind-it
But this deals entirely with QML (my code is all C++) and some of the structures appear to be organized slightly differently. Also, the documentation for the QFordwardRenderer
has left me wanting.
答案1
得分: 2
很遗憾,您遇到了尚未解决近四年的错误QTBUG-77139。
在构建QText2DEntity
时,通常应该通过将父实体传递给它来创建它,如下所示:
auto parent = new QEntity;
auto text = new QText2DEntity(parent);
只要场景在第一次创建时工作正常。但是如果您销毁场景并重新构建实体,2D文本可能不再显示。
QTBUG-77139描述了一种解决方法,首先构建QText2DEntity,然后以以下方式显式调用setParent方法:
auto parent = new QEntity;
auto text = new QText2DEntity;
text->setParent(parent);
英文:
Unfortunately you hit bug QTBUG-77139 that hasn't been fixed for nearly 4 years.
When constructing a QText2DEntity
you would normally create it by passing a parent entity to it as follows:
auto parent = new QEntity;
auto text = new QText2DEntity(parent);
As long as the scene is created the first time this works. But if you destroy the scene and reconstruct the entities the 2D-Text may not be displayed anymore.
QTBUG-77139 describes a workaround to first construct the QText2DEntity and later explicitely call the setParent method as follow:
auto parent = new QEntity;
auto text = new QText2DEntity;
text->setParent(parent);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论