天天看點

Qt&Vtk-004-AmbientSpheres

Qt&Vtk-004-AmbientSpheres

文章目錄

Qt&Vtk-AmbientSpheres

1 效果展示

2源碼

2.1 ambientspheres.h

2.2 ambientspheres.cpp

3 涉及主要知識點

3.1 vtkCamera

3.2 vtkLight

3.3 vtkPolyDataMapper

3.4 vtkActor

3.5 vtkRenderWindow

3.6 vtkRenderer

★ 源碼 ★

從本章開始,将開始搬運官方執行個體代碼,順便開始學習,能了解多少算多少。

今天搬運第一個Vtk官方示例***AmbientSpheres***,如下圖

Qt&Vtk-004-AmbientSpheres
Qt&Vtk-004-AmbientSpheres

下面看下源代碼,這次分别寫在了頭檔案中和源檔案中。

#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


      

#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,0.1,0);
    sphere3->GetProperty()->SetAmbient(0.375);
    sphere3->GetProperty()->SetDiffuse(0.0);
    sphere3->GetProperty()->SetSpecular(0.0);
    sphere3->AddPosition(2.5,0,0);

    sphere4->SetMapper(sphereMapper);
    sphere4->GetProperty()->SetColor(1,0,0);
    sphere4->GetProperty()->SetAmbient(0.5);
    sphere4->GetProperty()->SetDiffuse(0.0);
    sphere4->GetProperty()->SetSpecular(0.0);
    sphere4->AddPosition(3.75,0,0);


    sphere5->SetMapper(sphereMapper);
    sphere5->GetProperty()->SetColor(1,0,0);
    sphere5->GetProperty()->SetAmbient(0.625);
    sphere5->GetProperty()->SetDiffuse(0.0);
    sphere5->GetProperty()->SetSpecular(0.0);
    sphere5->AddPosition(0.0,1.25,0);


    sphere6->SetMapper(sphereMapper);
    sphere6->GetProperty()->SetColor(1,0,0);
    sphere6->GetProperty()->SetAmbient(0.75);
    sphere6->GetProperty()->SetDiffuse(0.0);
    sphere6->GetProperty()->SetSpecular(0.0);
    sphere6->AddPosition(1.25,1.25,0);

    sphere7->SetMapper(sphereMapper);
    sphere7->GetProperty()->SetColor(1,0,0);
    sphere7->GetProperty()->SetAmbient(0.875);
    sphere7->GetProperty()->SetDiffuse(0.0);
    sphere7->GetProperty()->SetSpecular(0.0);
    sphere7->AddPosition(2.5,1.25,0);

    sphere8->SetMapper(sphereMapper);
    sphere8->GetProperty()->SetColor(1,0,0);
    sphere8->GetProperty()->SetAmbient(1.0);
    sphere8->GetProperty()->SetDiffuse(0.0);
    sphere8->GetProperty()->SetSpecular(0.0);
    sphere8->AddPosition(3.75,1.25,0);


    renderer = vtkSmartPointer<vtkRenderer>::New();         //視窗渲染器
    renderer->AddActor(sphere1);
    renderer->AddActor(sphere2);
    renderer->AddActor(sphere3);
    renderer->AddActor(sphere4);
    renderer->AddActor(sphere5);
    renderer->AddActor(sphere6);
    renderer->AddActor(sphere7);
    renderer->AddActor(sphere8);
    renderer->SetBackground(0.1,0.2,0.4);               //設定背景顔色
    ui->widget->GetRenderWindow()->AddRenderer(renderer);   //

    light = vtkSmartPointer<vtkLight>::New();               //建立光照
    light->SetFocalPoint(1.875,0.6125,0);                   //設定燈光焦點
    light->SetPosition(0.875,1.6125,1);                     //設定燈光位置
                                                            //設定燈光強度SetIntensity()
                                                            //設定燈光錐角SetConeAngle()
                                                            //選擇設定平行光或者聚光PositionalOff/On()
    renderer->AddLight(light);
    
    renderer->GetActiveCamera()->SetFocalPoint(0,0,0);      //設定相機焦點
    renderer->GetActiveCamera()->SetPosition(0,0,1);        //設定相機位置
    renderer->GetActiveCamera()->SetViewUp(0,1,0);          //設定圖像正方向
    renderer->GetActiveCamera()->ParallelProjectionOn();    //平行投影/透視投影
    renderer->ResetCamera();
    renderer->GetActiveCamera()->SetParallelScale(4.0);

}

AmbientSpheres::~AmbientSpheres()
{
    delete ui;
}

      

參考連結:

https://vtk.org/doc/release/5.2/html/a00154.html https://blog.csdn.net/wzheng92/article/details/79935059 https://blog.csdn.net/colddie/article/details/16948231
Qt&amp;Vtk-004-AmbientSpheres
Qt&amp;Vtk-004-AmbientSpheres
Qt&amp;Vtk-004-AmbientSpheres
Qt&amp;Vtk-004-AmbientSpheres
Qt&amp;Vtk-004-AmbientSpheres
Qt&amp;Vtk-004-AmbientSpheres
Qt&amp;Vtk-004-AmbientSpheres
Qt&amp;Vtk-004-AmbientSpheres
Qt&amp;Vtk-004-AmbientSpheres
Qt&amp;Vtk-004-AmbientSpheres
Qt&amp;Vtk-004-AmbientSpheres
Qt&amp;Vtk-004-AmbientSpheres
Qt&amp;Vtk-004-AmbientSpheres
Qt&amp;Vtk-004-AmbientSpheres

繼續閱讀