Qt&Vtk-004-AmbientSpheres

简介: Qt&Vtk-004-AmbientSpheres

image.png

文章目录


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

★ 源码 ★

Qt&Vtk-AmbientSpheres

从本章开始,将开始搬运官方实例代码,顺便开始学习,能理解多少算多少。


1 效果展示

今天搬运第一个Vtk官方示例***AmbientSpheres***,如下图

image.pngimage.png

2源码


下面看下源代码,这次分别写在了头文件中和源文件中。

2.1 ambientspheres.h


#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

2.2 ambientspheres.cpp


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

3 涉及主要知识点

3.1 vtkCamera

参考链接: 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


image.pngimage.pngimage.pngimage.pngimage.pngimage.pngimage.pngimage.pngimage.pngimage.pngimage.pngimage.pngimage.pngimage.png

目录
相关文章
|
前端开发 JavaScript 应用服务中间件
VUE3(十八)vue 路由history 模式去掉 URL 中的 # (nginx)
这部分内容比较少。其实更多的是参考一下vue-router4的官方文档就好。 但是,去掉#之后的路由在配合php框架使用的时候可能会有问题,就是这个链接不知道该去后端还是去前端的路由。
1370 0
VUE3(十八)vue 路由history 模式去掉 URL 中的 # (nginx)
|
人工智能 C++
初步编译QT5.15.2+VS2019+VTK8.2.0
本文介绍了在VS2019中编译QT 5.15.2和VTK 8.2.0的过程,包括编译结果、cmake库编译配置和cmake应用配置脚本,以及推荐的参考链接。
666 4
|
SQL 关系型数据库 Shell
SQL整库导出语录及其实用技巧与方法
在数据库管理和备份恢复过程中,整库导出是一项至关重要的任务
|
JavaScript 前端开发 开发者
Vue学习之--------深入理解Vuex、原理详解、实战应用(2022/9/1)
这篇文章详细介绍了Vuex的基本概念、使用场景、安装配置、基本用法、实际应用案例以及注意事项,通过一个数字累加器的实战示例,帮助开发者深入理解Vuex的原理和应用。
|
机器学习/深度学习 传感器 物联网
使用Python实现深度学习模型:智能物联网与智能家居
【8月更文挑战第13天】 使用Python实现深度学习模型:智能物联网与智能家居
441 2
|
SQL 关系型数据库 MySQL
【MySQL 慢查询秘籍】慢SQL无处遁形!实战指南:一步步教你揪出数据库性能杀手!
【8月更文挑战第24天】本文以教程形式深入探讨了MySQL慢SQL查询的分析与优化方法。首先介绍了如何配置MySQL以记录执行时间过长的SQL语句。接着,利用内置工具`mysqlslowlog`及第三方工具`pt-query-digest`对慢查询日志进行了详细分析。通过一个具体示例展示了可能导致性能瓶颈的查询,并提出了相应的优化策略,包括添加索引、缩小查询范围、使用`EXPLAIN`分析执行计划等。掌握这些技巧对于提升MySQL数据库性能具有重要意义。
972 1
【Qt 学习笔记】Qt窗口 | 标准对话框 | 颜色对话框QColorDialog
【Qt 学习笔记】Qt窗口 | 标准对话框 | 颜色对话框QColorDialog
2346 3
|
缓存 网络协议 安全
【常见开源库的二次开发】HTTP之libcurl库——基础知识扫盲(一)
【常见开源库的二次开发】HTTP之libcurl库——基础知识扫盲(一)
659 1
|
消息中间件 存储 缓存
RocketMQ 监控告警:生产环境如何快速通过监控预警发现堆积、收发失败等问题?
本文主要向大家介绍如何利用 RocketMQ 可观测体系中的指标监控,对生产环境中典型场景:消息堆积、消息收发失败等场景配置合理的监控预警,快速发现问题,定位问题。
2045 0
RocketMQ 监控告警:生产环境如何快速通过监控预警发现堆积、收发失败等问题?

热门文章

最新文章