#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle)
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkProperty.h>
#include <vtkCamera.h>
#include <vtkLight.h>
namespace Ui {
class MainWindow;
}
//
// 環境光係數(Ambient): 光線照射到物體材質上,經過多次反射後最終遺留在環境中的光線強度,越大時,物體偏亮
// 漫反射係數(Diffuse): 光線照射到物體材質上,經過漫反射後形成的光線強度, 越大時,物體偏亮
// 鏡面反射係數(Specular): 光線照射到物體材質上,經過鏡面反射後形成的光線強度
// 鏡面指數(Specular Power): 取值範圍是0---128,該值越小,表示材質越是粗糙,當點光源發射的光線照射到上面時,可以產生較大的亮點,
// 該值越大,表示材質越是類似於鏡面,光源照射到上面後會產生較小的亮點;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
void addAmbientSpheres();
void addLight();
void addCamera();
private:
vtkSmartPointer<vtkRenderer> pRenderer;
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
pRenderer = vtkSmartPointer<vtkRenderer>::New();
addAmbientSpheres();
addLight();
addCamera();
ui->qvtkWidget->GetRenderWindow()->AddRenderer(pRenderer);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::addAmbientSpheres()
{
vtkSmartPointer<vtkSphereSource> sphere = vtkSmartPointer<vtkSphereSource>::New();
sphere->SetThetaResolution(100);
sphere->SetPhiResolution(50);
vtkSmartPointer<vtkPolyDataMapper> sphereMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
sphereMapper->SetInputConnection(sphere->GetOutputPort());
// 從下到上,從左到右
vtkSmartPointer<vtkActor> sphere1 = vtkSmartPointer<vtkActor>::New();
sphere1->SetMapper(sphereMapper);
sphere1->GetProperty()->SetColor(1,0,0);
sphere1->GetProperty()->SetAmbient(0.3);
sphere1->GetProperty()->SetDiffuse(0.0);
sphere1->GetProperty()->SetSpecular(0.0);
sphere1->GetProperty()->SetSpecularPower(5.0);
vtkSmartPointer<vtkActor> sphere2 = vtkSmartPointer<vtkActor>::New();
sphere2->SetMapper(sphereMapper);
sphere2->GetProperty()->SetColor(1,0,0);
sphere2->GetProperty()->SetAmbient(0.3);
sphere2->GetProperty()->SetDiffuse(0.125);
sphere2->GetProperty()->SetSpecular(0.0);
sphere2->GetProperty()->SetSpecularPower(10.0);
sphere2->AddPosition(1.25,0,0);
vtkSmartPointer<vtkActor> sphere3 = vtkSmartPointer<vtkActor>::New();
sphere3->SetMapper(sphereMapper);
sphere3->GetProperty()->SetColor(1,0,0);
sphere3->GetProperty()->SetAmbient(0.3);
sphere3->GetProperty()->SetDiffuse(0.25);
sphere3->GetProperty()->SetSpecular(0.0);
sphere3->AddPosition(2.5,0,0);
vtkSmartPointer<vtkActor> sphere4 = vtkSmartPointer<vtkActor>::New();
sphere4->SetMapper(sphereMapper);
sphere4->GetProperty()->SetColor(1,0,0);
sphere4->GetProperty()->SetAmbient(0.3);
sphere4->GetProperty()->SetDiffuse(0.375);
sphere4->GetProperty()->SetSpecular(0.0);
sphere4->AddPosition(3.75,0,0);
vtkSmartPointer<vtkActor> sphere5 = vtkSmartPointer<vtkActor>::New();
sphere5->SetMapper(sphereMapper);
sphere5->GetProperty()->SetColor(1,0,0);
sphere5->GetProperty()->SetAmbient(0.3);
sphere5->GetProperty()->SetDiffuse(0.5);
sphere5->GetProperty()->SetSpecular(0.0);
sphere5->AddPosition(0.0,1.25,0);
vtkSmartPointer<vtkActor> sphere6 = vtkSmartPointer<vtkActor>::New();
sphere6->SetMapper(sphereMapper);
sphere6->GetProperty()->SetColor(1,0,0);
sphere6->GetProperty()->SetAmbient(0.3);
sphere6->GetProperty()->SetDiffuse(0.625);
sphere6->GetProperty()->SetSpecular(0.0);
sphere6->AddPosition(1.25,1.25,0);
vtkSmartPointer<vtkActor> sphere7 = vtkSmartPointer<vtkActor>::New();
sphere7->SetMapper(sphereMapper);
sphere7->GetProperty()->SetColor(1,0,0);
sphere7->GetProperty()->SetAmbient(0.3);
sphere7->GetProperty()->SetDiffuse(0.75);
sphere7->GetProperty()->SetSpecular(0.0);
sphere7->AddPosition(2.5,1.25,0);
vtkSmartPointer<vtkActor> sphere8 = vtkSmartPointer<vtkActor>::New();
sphere8->SetMapper(sphereMapper);
sphere8->GetProperty()->SetColor(1,0,0);
sphere8->GetProperty()->SetAmbient(0.3);
sphere8->GetProperty()->SetDiffuse(0.875);
sphere8->GetProperty()->SetSpecular(0.0);
sphere8->AddPosition(3.75,1.25,0);
pRenderer->AddActor(sphere1);
pRenderer->AddActor(sphere2);
pRenderer->AddActor(sphere3);
pRenderer->AddActor(sphere4);
pRenderer->AddActor(sphere5);
pRenderer->AddActor(sphere6);
pRenderer->AddActor(sphere7);
pRenderer->AddActor(sphere8);
pRenderer->SetBackground(0.1, 0.2, 0.4);
}
void MainWindow::addLight()
{
vtkSmartPointer<vtkLight> light = vtkSmartPointer<vtkLight>::New();
light->SetFocalPoint(1.875,0.6125,0);
light->SetPosition(0.875,1.6125,1);
pRenderer->AddLight(light);
}
void MainWindow::addCamera()
{
pRenderer->GetActiveCamera()->SetFocalPoint(0,0,0);
pRenderer->GetActiveCamera()->SetPosition(0,0,1);
pRenderer->GetActiveCamera()->SetViewUp(0,1,0);
pRenderer->GetActiveCamera()->ParallelProjectionOn(); // 啓用正交投影
pRenderer->ResetCamera();
pRenderer->GetActiveCamera()->SetParallelScale(1.5);
}
<?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>879</width>
<height>683</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QVTKWidget" name="qvtkWidget"/>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>879</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>