文章目录
Qt&Vtk-Cone4
又双叕来搬运代码了,这次我们还是搬运Cone。先看看官方的效果吧
1 代码搬运
1.1 cone4.h
#ifndef CONE4_H #define CONE4_H #include <QWidget> #include <QTimer> #include "QVTKOpenGLWidget.h" //新版本,旧版QVTKWidget #include "vtkAutoInit.h" #include "vtkConeSource.h" #include "vtkPolyDataMapper.h" #include "vtkRenderWindow.h" #include "vtkCamera.h" #include "vtkActor.h" #include "vtkRenderer.h" #include "vtkProperty.h" namespace Ui { class Cone4; } class Cone4 : public QWidget { Q_OBJECT public: explicit Cone4(QWidget *parent = 0); ~Cone4(); private: Ui::Cone4 *ui; vtkConeSource *cone = nullptr; vtkPolyDataMapper *mapper = nullptr; vtkActor *actor1 = nullptr,*actor2 = nullptr; vtkProperty *property = nullptr; vtkRenderer *render = nullptr; QTimer *rotateTimer = nullptr; }; #endif // CONE4_H
1.2 cone4.cpp
#include "cone4.h" #include "ui_cone4.h" Cone4::Cone4(QWidget *parent) : QWidget(parent), ui(new Ui::Cone4) { ui->setupUi(this); cone = vtkConeSource::New(); cone->SetRadius(1); cone->SetResolution(300); cone->SetHeight(4.0); mapper = vtkPolyDataMapper::New(); mapper->SetInputConnection(cone->GetOutputPort()); actor1 = vtkActor::New(); actor1->SetMapper(mapper); actor1->GetProperty()->SetColor(1.0,0.0,0.0); actor1->GetProperty()->SetDiffuse(0.7); //漫反射 actor1->GetProperty()->SetSpecular(0.4); //高光,镜面光 actor1->GetProperty()->SetSpecularPower(20);//高光强度 property = vtkProperty::New(); property->SetColor(0.0,0.0,1.0); property->SetDiffuse(0.5); property->SetSpecular(0.6); property->SetSpecularPower(30); actor2 = vtkActor::New(); actor2->SetMapper(mapper); actor2->GetProperty()->SetColor(0.0,1.0,0.0); actor2->SetProperty(property); actor2->SetPosition(0,2,0); render = vtkRenderer::New(); render->AddActor(actor1); render->AddActor(actor2); render->SetBackground(0,0,0); ui->widget->GetRenderWindow()->AddRenderer(render); rotateTimer = new QTimer; connect(rotateTimer,&QTimer::timeout,this,[=](){ render->GetActiveCamera()->Azimuth(1); ui->widget->GetRenderWindow()->Render(); }); rotateTimer->start(10); } Cone4::~Cone4() { rotateTimer->stop(); delete ui; }