Qt&Vtk-016-DiffuseSpheres

Posted DreamLife.

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qt&Vtk-016-DiffuseSpheres相关的知识,希望对你有一定的参考价值。

​ 今天搞光照,漫反射。

1 官方示例展示

​ 今天搬运的代码有点和第一天搬运的有点像是,只不过第一天是高光,今天这个是漫反射,先撸代码再说。

2 代码搬运

2.1 diffuseSphere.h

#ifndef DIFFUSESPHERES_H
#define DIFFUSESPHERES_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 "vtkRenderWindowInteractor.h"
#include "vtkProperty.h"
#include "vtkCamera.h"
#include "vtkLight.h"


namespace Ui {
class DiffuseSpheres;
}

class DiffuseSpheres : public QWidget
{
    Q_OBJECT

public:
    explicit DiffuseSpheres(QWidget *parent = 0);
    ~DiffuseSpheres();

private:
    Ui::DiffuseSpheres *ui;

    vtkSphereSource *sphere = nullptr;

    vtkPolyDataMapper *mapper = nullptr;

    vtkActor *sphere1 = nullptr,
            *sphere2 = nullptr,
            *sphere3 = nullptr,
            *sphere4 = nullptr,
            *sphere5 = nullptr,
            *sphere6 = nullptr,
            *sphere7 = nullptr,
            *sphere8 = nullptr;

    vtkRenderer *render = nullptr;
};

#endif // DIFFUSESPHERES_H

2.2 diffuseSpheres.cpp

#include "diffusespheres.h"
#include "ui_diffusespheres.h"

DiffuseSpheres::DiffuseSpheres(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DiffuseSpheres)
{
    ui->setupUi(this);

    sphere = vtkSphereSource::New();
    sphere->SetThetaResolution(100);
    sphere->SetPhiResolution(50);


    mapper = vtkPolyDataMapper::New();
    mapper->SetInputConnection(sphere->GetOutputPort());


    sphere1 = vtkActor::New();
    sphere2 = vtkActor::New();
    sphere3 = vtkActor::New();
    sphere4 = vtkActor::New();
    sphere5 = vtkActor::New();
    sphere6 = vtkActor::New();
    sphere7 = vtkActor::New();
    sphere8 = vtkActor::New();

    sphere1->SetMapper(mapper);
    sphere2->SetMapper(mapper);
    sphere3->SetMapper(mapper);
    sphere4->SetMapper(mapper);
    sphere5->SetMapper(mapper);
    sphere6->SetMapper(mapper);
    sphere7->SetMapper(mapper);
    sphere8->SetMapper(mapper);


    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);

    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);

    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);

    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);

    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);

    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);

    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);

    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);


    render = vtkRenderer::New();
    render->AddActor(sphere1);
    render->AddActor(sphere2);
    render->AddActor(sphere3);
    render->AddActor(sphere4);
    render->AddActor(sphere5);
    render->AddActor(sphere6);
    render->AddActor(sphere7);
    render->AddActor(sphere8);


    ui->widget->GetRenderWindow()->AddRenderer(render);




}

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

3 运行效果

★ 源码 ★

源码分享一时爽,一直分享一直爽, 链接如下:

自取:https://github.com/DreamLife-Jianwei/Qt-Vtk


以上是关于Qt&Vtk-016-DiffuseSpheres的主要内容,如果未能解决你的问题,请参考以下文章

Qt&Vtk-012-CreateTree

Qt&Vtk-010-Cone5

Qt编程基于Qt的词典开发系列&lt;九&gt;--JSON数据解析

Qt & QWT:QwtPlotCurve - 曲线未显示

002-Qt&Vtk-第一个VtkQt程序

Qt & OpenGL:如何尽可能频繁地渲染?