我想每33毫秒在屏幕上绘制一个新的点云。我可以画点云1次。然后,我无法从屏幕上删除所有绘制的点,只能删除一半的位置。
在这里,我描述了在屏幕上绘制的对象的结构。
Sphere.hpp
#include <Qt3DCore/QEntity>
#include <Qt3DExtras/QSphereMesh>
#include <Qt3DCore/QTransform>
#include <Qt3DExtras/QPhongMaterial>
class Sphere : public Qt3DCore::QEntity
{
public:
Sphere();
~Sphere();
void Color(const QColor& color);
void Position(const QVector3D &position);
private:
Qt3DExtras::QSphereMesh* mesh;
Qt3DExtras::QPhongMaterial* material;
Qt3DCore::QTransform* transform;
};
Sphere.cpp
#include "Sphere.hpp"
Sphere::Sphere() :
mesh(new Qt3DExtras::QSphereMesh()),
transform(new Qt3DCore::QTransform()),
material(new Qt3DExtras::QPhongMaterial())
{
this->addComponent(this->mesh);
this->addComponent(this->transform);
this->addComponent(this->material);
}
Sphere::~Sphere()
{
this->removeComponent(this->mesh);
this->removeComponent(this->transform);
this->removeComponent(this->material);
delete this->mesh;
delete this->transform;
delete this->material;
}
void Sphere::Color(const QColor &color)
{
this->material->setDiffuse(color);
}
void Sphere::Position(const QVector3D &position)
{
this->transform->setTranslation(position);
}
在这里我创建一个线程,要在其中添加和删除循环中的点。
main.cpp
#include "Sphere.hpp"
#include <QApplication>
#include <QWidget>
#include <Qt3DExtras/qt3dwindow.h>
#include <Qt3DExtras/QSphereMesh>
#include <Qt3DExtras/QPhongMaterial>
#include <Qt3DCore/QEntity>
#include <Qt3DCore/QTransform>
#include <Qt3DRender/QCamera>
#include <Qt3DRender/QCameraLens>
#include <Qt3DRender/QPointLight>
#include <thread>
#include <chrono>
#include <vector>
std::vector<Sphere*> sphere_vec;
Qt3DCore::QEntity* g_root;
const int sphere_number = 100;
int main(int argc, char **argv)
{
QApplication app(argc, argv);
auto view = new Qt3DExtras::Qt3DWindow();
QWidget* container = QWidget::createWindowContainer(view);
auto camera = view->camera();
camera->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f);
camera->setPosition(QVector3D(0,0,100));
camera->setViewCenter(QVector3D(0,0,0));
g_root = new Qt3DCore::QEntity();
Qt3DCore::QEntity *lightEntity = new Qt3DCore::QEntity(g_root);
Qt3DRender::QPointLight *light = new Qt3DRender::QPointLight(lightEntity);
light->setColor("white");
light->setIntensity(1);
lightEntity->addComponent(light);
Qt3DCore::QTransform *lightTransform = new Qt3DCore::QTransform(lightEntity);
lightTransform->setTranslation(camera->position());
lightEntity->addComponent(lightTransform);
view->setRootEntity(g_root);
for(int i = 0; i < sphere_number;++i)
{
auto sphere = new Sphere();
sphere_vec.push_back(sphere);
}
std::thread thr([]{
for(int i = 0; i < sphere_number; ++i)
{
sphere_vec[i]->Position(QVector3D(i, 0, 0));
sphere_vec[i]->setParent(g_root);
}
std::this_thread::sleep_for(std::chrono::seconds(0.33));
for(int i = 0; i < sphere_number; ++i)
{
delete sphere_vec[i];
}
});
container->show();
int res = app.exec();
thr.join();
return res;
}