欢迎来到我的博客,希望这篇文章对你有所帮助,如果觉得不错,请点赞搜藏哈。
Qt&Vtk-AmbientSpheres1 效果展示2源码2.1 ambientspheres.h2.2 ambientspheres.cpp3 涉及主要知识点3.1 vtkCamera3.2 vtkLight3.3 vtkPolyDataMapper3.4 vtkActor3.5 vtkRenderWindow3.6 vtkRenderer★ 源码 ★
Qt&Vtk-AmbientSpheres
从本章开始,将开始搬运官方实例代码,顺便开始学习,能理解多少算多少。
1 效果展示
今天搬运第一个Vtk官方示例AmbientSpheres,如下图
2源码
下面看下源代码,这次分别写在了头文件中和源文件中。
2.1 ambientspheres.h
#ifndef AMBIENTSPHERES_H
#define AMBIENTSPHERES_H
?
#include <QWidget>
#include "QVTKOpenGLWidget.h" //新版本,旧版QVTKWidget
#include "vtkAutoInit.h"
#include "vtkSmartPointer.h"
#include "vtkSphereSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkProperty.h"
#include "vtkCamera.h"
#include "vtkLight.h"
?
namespace Ui {
class AmbientSpheres;
}
?
class AmbientSpheres : public QWidget
{
Q_OBJECT
?
public:
explicit AmbientSpheres(QWidget *parent = 0);
~AmbientSpheres();
?
private:
Ui::AmbientSpheres *ui;
vtkSmartPointer<vtkSphereSource> sphere = nullptr;
vtkSmartPointer<vtkPolyDataMapper> sphereMapper = nullptr;
vtkSmartPointer<vtkActor> sphere1 = nullptr,
sphere2 = nullptr,
sphere3 = nullptr,
sphere4 = nullptr,
sphere5 = nullptr,
sphere6 = nullptr,
sphere7 = nullptr,
sphere8 = nullptr;
vtkSmartPointer<vtkRenderer> renderer = nullptr;
vtkSmartPointer<vtkLight> light = nullptr;
};
?
#endif // AMBIENTSPHERES_H
?
?
2.2 ambientspheres.cpp
#include "ambientspheres.h"
#include "ui_ambientspheres.h"
#include <QDebug>
/**
* @brief AmbientSpheres::AmbientSpheres
* @param parent
* 照搬官方实例,有道翻译官方文件中内容如下
* This examples demonstrates the effect of specular lighting.
* 这个例子演示了镜面照明的效果。
* 专业名词后面慢慢学习,代码先撸起来
*/
AmbientSpheres::AmbientSpheres(QWidget *parent) :QWidget(parent),ui(new Ui::AmbientSpheres)
{
ui->setupUi(this);
//创建一个球体
sphere = vtkSmartPointer<vtkSphereSource>::New();
sphere->SetThetaResolution(100);
sphere->SetPhiResolution(50);
//创建一个映射器
sphereMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
sphereMapper->SetInputConnection(sphere->GetOutputPort());
//创建8个小球
sphere1 = vtkSmartPointer<vtkActor>::New();
sphere2 = vtkSmartPointer<vtkActor>::New();
sphere3 = vtkSmartPointer<vtkActor>::New();
sphere4 = vtkSmartPointer<vtkActor>::New();
sphere5 = vtkSmartPointer<vtkActor>::New();
sphere6 = vtkSmartPointer<vtkActor>::New();
sphere7 = vtkSmartPointer<vtkActor>::New();
sphere8 = vtkSmartPointer<vtkActor>::New();
?
sphere1->SetMapper(sphereMapper);
sphere1->GetProperty()->SetColor(1,0,0); //颜色
sphere1->GetProperty()->SetAmbient(0.125); //环境光系数
sphere1->GetProperty()->SetDiffuse(0.0); //漫反射光系数
sphere1->GetProperty()->SetSpecular(0.0); //镜面反射系数
//镜面系数SetSpecularPower()
?
?
sphere2->SetMapper(sphereMapper);
sphere2->GetProperty()->SetColor(1,0,1);
sphere2->GetProperty()->SetAmbient(0.25);
sphere2->GetProperty()->SetDiffuse(0.0);
sphere2->GetProperty()->SetSpecular(0.0);
sphere2->AddPosition(1.25,0,0);
?
?
sphere3->SetMapper(sphereMapper);
sphere3->GetProperty()->SetColor(1,