#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <vtkNew.h> // 智能指针
#include <QVTKOpenGLNativeWidget.h>
#include <vtkCylinderSource.h> // 圆柱
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkProperty.h>
#include <vtkCamera.h>
#include <vtkGenericOpenGLRenderWindow.h>
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2);
VTK_MODULE_INIT(vtkRenderingFreeType);
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
vtkNew<vtkCylinderSource> cylinder; // 圆柱
vtkNew<vtkPolyDataMapper> mapper;
vtkNew<vtkActor> actor;
vtkNew<vtkRenderer> renderer;
vtkNew<vtkGenericOpenGLRenderWindow> renWin;
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#include “mainwindow.h”
#include “./ui_mainwindow.h”
cylinder->SetResolution(10);
mapper->SetInputConnection(cylinder->GetOutputPort());
actor->SetMapper(mapper);
actor->GetProperty()->SetColor(0.0000, 0.3883, 0.2784);
actor->RotateX(30.0);
actor->RotateY(-45.0);
renderer->AddActor(actor);
renderer->ResetCamera();
renderer->GetActiveCamera()->Zoom(1.5);
renderer->SetBackground(0.1, 0.3, 0.7);
renWin->AddRenderer(renderer);
renWin->SetSize(640, 640);
ui->openGLWidget->setRenderWindow(renWin);
}
MainWindow::~MainWindow()
{
delete ui;
}
`
``