#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle)
#include <vtkSmartPointer.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleTrackball.h>
#include <vtkInteractorStyle.h>
#include <vtkCylinderSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkTexture.h>
#include <vtkProperty.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void textureSlot();
void backgroundColorSlot();
void foregroundColorSlot();
void scaleSlot();
void positionSlot();
void oritationSlot();
void originSlot();
void dragSlot();
void pickSlot();
private:
vtkSmartPointer<vtkRenderer> pRenderer;
vtkSmartPointer<vtkProperty> pProperty;
vtkSmartPointer<vtkPolyDataMapper> pPolyDataMapper;
vtkSmartPointer<vtkActor> pCylinderActor;
vtkSmartPointer<vtkTexture> pTexture;
vtkSmartPointer<vtkCylinderSource> pCylinderSource;
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QColorDialog>
#include <QFileDialog>
#include <vtkJPEGReader.h>
#include <vtkPNGReader.h>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// 渲染的场景
pRenderer = vtkSmartPointer<vtkRenderer>::New();
// 圆柱数据
pCylinderSource = vtkSmartPointer<vtkCylinderSource>::New();
pCylinderSource->SetCenter(0, 0, 0);
pCylinderSource->SetResolution(25);
pCylinderSource->SetHeight(2);
pCylinderSource->SetRadius(2);
// 圆柱的Mapper
pPolyDataMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
pPolyDataMapper->SetInputConnection(pCylinderSource->GetOutputPort());
// 圆柱的Property
pProperty = vtkSmartPointer<vtkProperty>::New();
pProperty->SetColor(1, 0, 0);
pProperty->SetOpacity(0.5);
pProperty->SetAmbient(0.3);
pProperty->SetDiffuse(0.4);
pProperty->SetSpecular(0.8);
pProperty->SetShading(false);
pProperty->SetLineWidth(2.0);
pProperty->SetPointSize(2.0);
// 圆柱体
pCylinderActor = vtkSmartPointer<vtkActor>::New();
pCylinderActor->SetMapper(pPolyDataMapper);
pCylinderActor->SetProperty(pProperty);
pCylinderActor->SetDragable(false);
pCylinderActor->SetPickable(false);
pCylinderActor->SetScale(1.0);
pCylinderActor->SetPosition(0, 0, 0);
pCylinderActor->SetOrientation(0, 0, 0);
pCylinderActor->SetOrigin(0, 0, 0);
pRenderer->AddActor(pCylinderActor);
// 设置场景
pRenderer->SetBackground(0, 1, 0);
pRenderer->SetTwoSidedLighting(true);
ui->qvtkWidget->GetRenderWindow()->AddRenderer(pRenderer);
connect(ui->pushButton , SIGNAL(clicked(bool)) , this, SLOT(textureSlot()));
connect(ui->backgroundPushButton, SIGNAL(clicked(bool)) , this, SLOT(backgroundColorSlot()));
connect(ui->foregroundPushButton, SIGNAL(clicked(bool)) , this, SLOT(foregroundColorSlot()));
connect(ui->scaleLineEdit , SIGNAL(editingFinished()), this, SLOT(scaleSlot()));
connect(ui->positionLineEdit , SIGNAL(editingFinished()), this, SLOT(positionSlot()));
connect(ui->oritationLineEdit , SIGNAL(editingFinished()), this, SLOT(oritationSlot()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::textureSlot()
{
#if 1
QString selectFilePath = QFileDialog::getOpenFileName(this, QString("选择图像文件"), QString(""), QString("图像(*.jpeg)"));
#else
QString selectFilePath = QFileDialog::getOpenFileName(this, QString("选择图像文件"), QString(""), QString("图像(*.png)"));
#endif
if(selectFilePath.isEmpty())
{
ui->textBrowser->append("选择图像路径为空!");
return ;
}
#if 1
vtkSmartPointer<vtkJPEGReader> jpegReader = vtkSmartPointer<vtkJPEGReader>::New();
#else
vtkSmartPointer<vtkPNGReader> jpegReader = vtkSmartPointer<vtkPNGReader>::New();
#endif
jpegReader->SetFileName(selectFilePath.toStdString().c_str());
pTexture = vtkSmartPointer<vtkTexture>::New();
pTexture->SetInputConnection(jpegReader->GetOutputPort());
pTexture->InterpolateOn(); // 启用插值
pCylinderActor->SetTexture( pTexture);
pRenderer->Render();
}
void MainWindow::backgroundColorSlot()
{
QColor selectColor = QColorDialog::getColor(Qt::red, this, QString("选择背景颜色"));
pRenderer->SetBackground(selectColor.red() / 255.0, selectColor.green() / 255.0, selectColor.blue() / 255.0);
pRenderer->Render();
}
void MainWindow::foregroundColorSlot()
{
QColor selectColor = QColorDialog::getColor(Qt::red, this, QString("选择背景颜色"));
pCylinderActor->GetProperty()->SetColor(selectColor.red() / 255.0, selectColor.green() / 255.0, selectColor.blue() / 255.0);
pRenderer->Render();
}
void MainWindow::scaleSlot()
{
QStringList scales = ui->scaleLineEdit->text().split(';');
pCylinderActor->SetScale(scales[0].toFloat(), scales[1].toFloat(), scales[2].toFloat());
pRenderer->Render();
}
void MainWindow::positionSlot()
{
QStringList position = ui->scaleLineEdit->text().split(';');
pCylinderActor->SetPosition(position[0].toFloat(), position[1].toFloat(), position[2].toFloat());
pRenderer->Render();
}
void MainWindow::oritationSlot()
{
QStringList oritation = ui->scaleLineEdit->text().split(';');
pCylinderActor->SetOrientation(oritation[0].toFloat(), oritation[1].toFloat(), oritation[2].toFloat());
pRenderer->Render();
}
void MainWindow::originSlot()
{
QStringList origin = ui->scaleLineEdit->text().split(';');
pCylinderActor->SetOrigin(origin[0].toFloat(), origin[1].toFloat(), origin[2].toFloat());
pRenderer->Render();
}
void MainWindow::dragSlot()
{
pCylinderActor->SetDragable(ui->dragCheckBox->isChecked());
pRenderer->Render();
}
void MainWindow::pickSlot()
{
pCylinderActor->SetPickable(ui->pickCheckBox->isChecked());
pRenderer->Render();
}
QT UI文件
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1004</width>
<height>711</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QHBoxLayout" name="horizontalLayout" stretch="2,5,1">
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>选择纹理图像</string>
</property>
</widget>
</item>
<item>
<widget class="QTextBrowser" name="textBrowser"/>
</item>
</layout>
</item>
<item>
<widget class="QVTKWidget" name="qvtkWidget"/>
</item>
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>属性</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="4" column="1">
<widget class="QLineEdit" name="oritationLineEdit">
<property name="text">
<string>0;0;0</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QPushButton" name="foregroundPushButton">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Actor缩放</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Actor位置</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QPushButton" name="backgroundPushButton">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="scaleLineEdit">
<property name="text">
<string>1.0; 1.0; 1.0</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Actor颜色</string>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Actor旋转</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLineEdit" name="positionLineEdit">
<property name="text">
<string>0;0;0</string>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="label_6">
<property name="text">
<string>Actor拖动</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>背景颜色</string>
</property>
</widget>
</item>
<item row="7" column="1">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="6" column="0">
<widget class="QLabel" name="label_7">
<property name="text">
<string>Actor可选</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QCheckBox" name="dragCheckBox">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="6" column="1">
<widget class="QCheckBox" name="pickCheckBox">
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1004</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
<customwidget>
<class>QVTKWidget</class>
<extends>QWidget</extends>
<header>QVTKWidget.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>