VTK vtkLight

最后都变了- 提交于 2021-01-17 14:53:21

示例vtkLight的简单用法

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle)
#include <vtkSmartPointer.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleTrackball.h>
#include <vtkInteractorStyle.h>
#include <vtkCylinderSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkTexture.h>
#include <vtkProperty.h>
#include <vtkLight.h>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void textureSlot();
    void lightPositionSlot();
    void lightFocusSlot();
    void lightIntensitySlot();
    void lightColorSlot();
    void lightSwitchSlot();
private:
    vtkSmartPointer<vtkRenderer>       pRenderer;
    vtkSmartPointer<vtkProperty>       pProperty;
    vtkSmartPointer<vtkPolyDataMapper> pPolyDataMapper;
    vtkSmartPointer<vtkActor>          pCylinderActor;
    vtkSmartPointer<vtkTexture>        pTexture;
    vtkSmartPointer<vtkLight>          pLight;
    vtkSmartPointer<vtkCylinderSource> pCylinderSource;
private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

 

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QColorDialog>
#include <QFileDialog>
#include <vtkJPEGReader.h>
#include <vtkPNGReader.h>
#include <QDebug>

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

    // 渲染的场景
    pRenderer = vtkSmartPointer<vtkRenderer>::New();

    // 圆柱数据
    pCylinderSource = vtkSmartPointer<vtkCylinderSource>::New();
    pCylinderSource->SetCenter(0, 0, 0);
    pCylinderSource->SetResolution(25);
    pCylinderSource->SetHeight(2);
    pCylinderSource->SetRadius(2);

    // 圆柱的Mapper
    pPolyDataMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    pPolyDataMapper->SetInputConnection(pCylinderSource->GetOutputPort());


    // 圆柱的Property
    pProperty = vtkSmartPointer<vtkProperty>::New();
    pProperty->SetColor(1, 0, 0);
    pProperty->SetOpacity(0.5);
    pProperty->SetAmbient(0.3);
    pProperty->SetDiffuse(0.4);
    pProperty->SetSpecular(0.8);
    pProperty->SetShading(false);
    pProperty->SetLineWidth(2.0);
    pProperty->SetPointSize(2.0);

    // 圆柱体
    pCylinderActor = vtkSmartPointer<vtkActor>::New();
    pCylinderActor->SetMapper(pPolyDataMapper);
    pCylinderActor->SetProperty(pProperty);

    pCylinderActor->SetDragable(false);
    pCylinderActor->SetPickable(false);
    pCylinderActor->SetScale(1.0);
    pCylinderActor->SetPosition(0, 0, 0);
    pCylinderActor->SetOrientation(0, 0, 0);
    pCylinderActor->SetOrigin(0, 0, 0);

    pRenderer->AddActor(pCylinderActor);

    // 灯光
    pLight = vtkSmartPointer<vtkLight>::New();

    pLight->SetColor(0, 0, 1);
    pLight->SetIntensity(0.2);
    pLight->SetPosition(10, 10, 0);
    pLight->SetFocalPoint(0, 0, 0);
    pLight->SwitchOn();

    pRenderer->AddLight(pLight);

    // 设置场景
    pRenderer->SetBackground(0, 1, 0);
    pRenderer->SetTwoSidedLighting(true);

    ui->qvtkWidget->GetRenderWindow()->AddRenderer(pRenderer);

    connect(ui->pushButton       , SIGNAL(clicked(bool))    , this, SLOT(textureSlot()));
    connect(ui->colorPushButton  , SIGNAL(clicked(bool))    , this, SLOT(lightColorSlot()));
    connect(ui->intensityLineEdit, SIGNAL(editingFinished()), this, SLOT(lightIntensitySlot()));
    connect(ui->positionLineEdit , SIGNAL(editingFinished()), this, SLOT(lightPositionSlot()));
    connect(ui->focusLineEdit    , SIGNAL(editingFinished()), this, SLOT(lightFocusSlot()));
    connect(ui->switchCheckBox   , SIGNAL(clicked(bool))    , this, SLOT(lightSwitchSlot()));
}

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

void MainWindow::textureSlot()
{
#if 1
    QString selectFilePath = QFileDialog::getOpenFileName(this, QString("选择图像文件"), QString(""), QString("图像(*.jpeg)"));
#else
    QString selectFilePath = QFileDialog::getOpenFileName(this, QString("选择图像文件"), QString(""), QString("图像(*.png)"));
#endif
    if(selectFilePath.isEmpty())
    {
        ui->textBrowser->append("选择图像路径为空!");
        return ;
    }

#if 1
    vtkSmartPointer<vtkJPEGReader> jpegReader =  vtkSmartPointer<vtkJPEGReader>::New();
#else
    vtkSmartPointer<vtkPNGReader> jpegReader =  vtkSmartPointer<vtkPNGReader>::New();
#endif
    jpegReader->SetFileName(selectFilePath.toStdString().c_str());

    pTexture = vtkSmartPointer<vtkTexture>::New();
    pTexture->SetInputConnection(jpegReader->GetOutputPort());
    pTexture->InterpolateOn(); // 启用插值
    pCylinderActor->SetTexture( pTexture);

    pRenderer->Render();
}

void MainWindow::lightColorSlot()
{
    QColor selectColor = QColorDialog::getColor(Qt::red, this, QString("选择背景颜色"));
    pLight->SetColor(selectColor.red() / 255.0, selectColor.green() / 255.0, selectColor.blue() / 255.0);

    qDebug() << "selectColor.red() = " << selectColor.red();

    pRenderer->Render();
}

void MainWindow::lightIntensitySlot()
{
    pLight->SetIntensity(ui->intensityLineEdit->text().toFloat());

    pRenderer->Render();
}

void MainWindow::lightPositionSlot()
{
    QStringList position = ui->positionLineEdit->text().split(';');
    pLight->SetPosition(position[0].toFloat(), position[1].toFloat(), position[2].toFloat());

    pRenderer->Render();
}

void MainWindow::lightFocusSlot()
{
    QStringList position = ui->focusLineEdit->text().split(';');
    pLight->SetFocalPoint(position[0].toFloat(), position[1].toFloat(), position[2].toFloat());

    pRenderer->Render();
}

void MainWindow::lightSwitchSlot()
{
    if(ui->switchCheckBox->isChecked())
    {
        pLight->SwitchOn();
    }
    else
    {
        pLight->SwitchOff();
    }

    pRenderer->Render();
}






QT UI文件

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>894</width>
    <height>662</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <layout class="QHBoxLayout" name="horizontalLayout" stretch="2,5,1">
    <item>
     <layout class="QVBoxLayout" name="verticalLayout">
      <item>
       <widget class="QPushButton" name="pushButton">
        <property name="text">
         <string>选择纹理图像</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QTextBrowser" name="textBrowser"/>
      </item>
     </layout>
    </item>
    <item>
     <widget class="QVTKWidget" name="qvtkWidget"/>
    </item>
    <item>
     <widget class="QGroupBox" name="groupBox">
      <property name="title">
       <string>属性</string>
      </property>
      <layout class="QGridLayout" name="gridLayout">
       <item row="0" column="0">
        <widget class="QLabel" name="label">
         <property name="text">
          <string>灯光颜色</string>
         </property>
        </widget>
       </item>
       <item row="3" column="1">
        <widget class="QLineEdit" name="focusLineEdit">
         <property name="text">
          <string>0;0;0</string>
         </property>
        </widget>
       </item>
       <item row="3" column="0">
        <widget class="QLabel" name="label_5">
         <property name="text">
          <string>灯光焦点位置</string>
         </property>
        </widget>
       </item>
       <item row="1" column="0">
        <widget class="QLabel" name="label_3">
         <property name="text">
          <string>灯光强度</string>
         </property>
        </widget>
       </item>
       <item row="0" column="1">
        <widget class="QPushButton" name="colorPushButton">
         <property name="text">
          <string/>
         </property>
        </widget>
       </item>
       <item row="2" column="0">
        <widget class="QLabel" name="label_4">
         <property name="text">
          <string>灯光位置</string>
         </property>
        </widget>
       </item>
       <item row="1" column="1">
        <widget class="QLineEdit" name="intensityLineEdit">
         <property name="text">
          <string>1.0</string>
         </property>
        </widget>
       </item>
       <item row="2" column="1">
        <widget class="QLineEdit" name="positionLineEdit">
         <property name="text">
          <string>10;10;0</string>
         </property>
        </widget>
       </item>
       <item row="5" column="1">
        <spacer name="verticalSpacer">
         <property name="orientation">
          <enum>Qt::Vertical</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>20</width>
           <height>40</height>
          </size>
         </property>
        </spacer>
       </item>
       <item row="4" column="1">
        <widget class="QCheckBox" name="switchCheckBox">
         <property name="text">
          <string/>
         </property>
         <property name="checked">
          <bool>true</bool>
         </property>
        </widget>
       </item>
       <item row="4" column="0">
        <widget class="QLabel" name="label_2">
         <property name="text">
          <string>灯光开关</string>
         </property>
        </widget>
       </item>
      </layout>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>894</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <customwidgets>
  <customwidget>
   <class>QVTKWidget</class>
   <extends>QWidget</extends>
   <header>QVTKWidget.h</header>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!