Qt3D 绘制线消耗太多内存
Qt3D draw lines consumes too much memory
如何用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();
}
相关文章:
- 我会导致太多内存泄漏,以至于我的计算机无响应吗?
- Valgrind 声称内存释放中的自由空间太多
- C++ STL 向量保留太多容量会消耗大量内存吗?
- Qt3D 绘制线消耗太多内存
- 程序消耗太多内存
- 从文件的开头读取,而不会浪费太多内存
- Struct占用了太多内存
- Boost Bimap 在调试版本中占用太多内存
- Linux x64 C++ 为链表分配了太多内存;为什么
- 如何停止让-jN消耗太多内存
- GCC消耗太多内存!!图形工具
- 保存未知数量的整数,而不会花费太多时间/内存
- D3D 文本占用了太多的内存
- OpenGL 纹理占用太多内存
- 如何在不使用太多内存的情况下链接大量的c++对象文件
- "std::bad_alloc" : 我是否使用了太多内存?
- vc++ 10项目为对象分配了太多内存
- 文件解析使用太多内存(c++)
- 如何判断v8隔离实例使用了太多内存
- 需要优化 - 算法实现占用太多内存