Qt3D 绘制线消耗太多内存

Qt3D draw lines consumes too much memory

本文关键字:太多 内存 绘制 Qt3D      更新时间:2023-10-16

如何用Qt3D画线? 如何删除画线?我发现如果您绘制许多行,以下代码会消耗太多内存,尽管它可以工作。此方法为仅绘制一条线分配了太多存储空间,并且不会释放它们。如果使用删除指针,则它崩溃了。如何解决这个问题?

#include <Qt3DCore/QEntity>
#include <Qt3DCore/QTransform>
#include <Qt3DExtras/QPhongMaterial>
#include <Qt3DRender/QAttribute>
#include <Qt3DRender/QBuffer>
#include <Qt3DRender/QGeometry>
drawLine(const QVector3D &start, const QVector3D &end, const QColor &color)
{
if(!m_bShow) {
return;
}
auto *geometry = new Qt3DRender::QGeometry(m_pRootEntity);
QByteArray bufferBytes;
bufferBytes.resize(3*2*sizeof (float));
float *pos = reinterpret_cast<float*>(bufferBytes.data());
*pos++ = start.x();
*pos++ = start.y();
*pos++ = start.z();
*pos++ = end.x();
*pos++ = end.y();
*pos++ = 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<unsigned int*>(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(m_pRootEntity);
line->setGeometry(geometry);
line->setPrimitiveType(Qt3DRender::QGeometryRenderer::Lines);
//material
auto *material = new Qt3DExtras::QDiffuseSpecularMaterial(m_pRootEntity);
material->setAmbient(color);
auto *lineEntity = new Qt3DCore::QEntity(m_pRootEntity);
lineEntity->addComponent(line);
lineEntity->addComponent(material);
}

最后,我解决了这个问题。
首先,将线实体放入容器中:m_lineEntityList.push_back(lineEntity),
然后删除行实体的组件:

while(!m_lineEntityList.isEmpty()) {
Qt3DCore::QEntity* pEntity = m_lineEntityList.last();
Qt3DCore::QComponentVector entityVector = pEntity->components();
while (!entityVector.isEmpty()) {
pEntity->removeComponent(entityVector.last());
entityVector.pop_back();
}
m_lineEntityList.pop_back();
}