Qt&Vtk-016-DiffuseSpheres

举报
DreamLife 发表于 2022/04/15 00:41:23 2022/04/15
【摘要】 ​ 今天搞光照,漫反射。 文章目录 1 官方示例展示2 代码搬运2.1 diffuseSphere.h2.2 diffuseSpheres.cpp 3 运行效果★ 源码 ★ 1 官...

头图

​ 今天搞光照,漫反射。

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


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

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


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110

3 运行效果

在这里插入图片描述

★ 源码 ★

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

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

在这里插入图片描述


博客签名2021

文章来源: dreamlife.blog.csdn.net,作者:DreamLife.,版权归原作者所有,如需转载,请联系作者。

原文链接:dreamlife.blog.csdn.net/article/details/119632696

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。