Qt&Vtk-009-Cone4

Posted DreamLife.

tags:

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

Qt&Vtk-Cone4

又双叕来搬运代码了,这次我们还是搬运Cone。先看看官方的效果吧

1 代码搬运

1.1 cone4.h

#ifndef CONE4_H
#define CONE4_H

#include <QWidget>
#include <QTimer>
#include "QVTKOpenGLWidget.h"               //新版本,旧版QVTKWidget
#include "vtkAutoInit.h"

#include "vtkConeSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkCamera.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkProperty.h"

namespace Ui {
class Cone4;
}

class Cone4 : public QWidget
{
    Q_OBJECT

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

private:
    Ui::Cone4 *ui;

    vtkConeSource *cone = nullptr;

    vtkPolyDataMapper *mapper = nullptr;

    vtkActor *actor1 = nullptr,*actor2 = nullptr;

    vtkProperty *property = nullptr;

    vtkRenderer *render = nullptr;

    QTimer *rotateTimer = nullptr;


};

#endif // CONE4_H

1.2 cone4.cpp

#include "cone4.h"
#include "ui_cone4.h"

Cone4::Cone4(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Cone4)
{
    ui->setupUi(this);
    cone = vtkConeSource::New();
    cone->SetRadius(1);
    cone->SetResolution(300);
    cone->SetHeight(4.0);
    mapper = vtkPolyDataMapper::New();
    mapper->SetInputConnection(cone->GetOutputPort());
    actor1 = vtkActor::New();
    actor1->SetMapper(mapper);
    actor1->GetProperty()->SetColor(1.0,0.0,0.0);
    actor1->GetProperty()->SetDiffuse(0.7);     //漫反射
    actor1->GetProperty()->SetSpecular(0.4);    //高光,镜面光
    actor1->GetProperty()->SetSpecularPower(20);//高光强度
    property = vtkProperty::New();
    property->SetColor(0.0,0.0,1.0);
    property->SetDiffuse(0.5);
    property->SetSpecular(0.6);
    property->SetSpecularPower(30);
    actor2 = vtkActor::New();
    actor2->SetMapper(mapper);
    actor2->GetProperty()->SetColor(0.0,1.0,0.0);
    actor2->SetProperty(property);
    actor2->SetPosition(0,2,0);
    render = vtkRenderer::New();
    render->AddActor(actor1);
    render->AddActor(actor2);
    render->SetBackground(0,0,0);
    ui->widget->GetRenderWindow()->AddRenderer(render);
    rotateTimer = new QTimer;
    connect(rotateTimer,&QTimer::timeout,this,[=](){
        render->GetActiveCamera()->Azimuth(1);
        ui->widget->GetRenderWindow()->Render();
    });
    rotateTimer->start(10);
}
Cone4::~Cone4()
{
    rotateTimer->stop();
    delete ui;
}

2 运行效果

3 知识点

3.1 vtkProperty

参考链接:https://vtk.org/doc/nightly/html/classvtkProperty.html

表现方式

GetProperty()->SetRepresentationToPoints() //点
GetProperty()->SetRepresentationToWireframe() //网格
GetProperty()->SetRepresentationToSurface() //面

★ 源码 ★

这里就要有人问了呀,这么优秀的代码,能不能分享下呀,当然可以呀,我不生产代码,我只是代码的搬运工,链接如下:

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


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

26.Qt Quick QML-RotationAnimationPathAnimationSmoothedAnimationBehaviorPauseAnimationSequential(代码片段

对这个带有 & 不带 = 的代码片段返回类型感到非常困惑

Sphinx - 在代码块片段中使用省略号 (...)

如何在 Javadoc 中使用 @ 和 符号格式化代码片段?

Qt编程遇到的问题,我在qt中直接使用C语言的程序片段,有问题 ,求解

为 Blogger 上的博客格式化代码片段 [关闭]