Qt&Vtk-016-DiffuseSpheres

简介: Qt&Vtk-016-DiffuseSpheres

image.png 今天搞光照,漫反射。


文章目录


1 官方示例展示

2 代码搬运

2.1 diffuseSphere.h

2.2 diffuseSpheres.cpp

3 运行效果

★ 源码 ★

1 官方示例展示

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

image.png

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

image.pngimage.png

目录
相关文章
Qt&Vtk-027-RGrid
Qt&Vtk-027-RGrid
153 0
Qt&Vtk-027-RGrid
Qt&Vtk-026-QScalarsToColors
Qt&Vtk-026-QScalarsToColors
214 0
Qt&Vtk-026-QScalarsToColors
Qt&Vtk-015-Cylinder
Qt&Vtk-015-Cylinder
139 0
Qt&Vtk-015-Cylinder
Qt&Vtk-012-CreateTree
Qt&Vtk-012-CreateTree
155 0
Qt&Vtk-012-CreateTree
Qt&Vtk-006-one
Qt&Vtk-006-one
121 0
Qt&Vtk-006-one
Qt&Vtk-018-FixedPointVolumeRayCastMapperCT
Qt&Vtk-018-FixedPointVolumeRayCastMapperCT
157 0
Qt&Vtk-018-FixedPointVolumeRayCastMapperCT
Qt&Vtk-010-Cone5
Qt&Vtk-010-Cone5
169 0
Qt&Vtk-010-Cone5
Qt&Vtk-008-Cone3
Qt&Vtk-008-Cone3
134 0
Qt&Vtk-008-Cone3
Qt&Vtk-022-LabeledMesh
Qt&Vtk-022-LabeledMesh
162 0
Qt&Vtk-022-LabeledMesh
|
C++
Qt&Vtk-007-Cone2
Qt&Vtk-007-Cone2
136 0
Qt&Vtk-007-Cone2