vtk显示属性、窗口以及交互设置

在上篇简单的cone模型显示的基础上,本篇blog继续讲解与模型显示属性、窗口显示以及交互方式相关的例子。


1. 模型显示属性设置

通过给actor设置颜色来改变模型的颜色显示,通过property类,可以进一步设置模型显示的反射光、高光、环境光等属性的设置。

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

int main()
{
  vtkConeSource *cone = vtkConeSource::New();
  cone->SetHeight( 3.0 );
  cone->SetRadius( 1.0 );
  cone->SetResolution( 10 );
  
  vtkPolyDataMapper *coneMapper = vtkPolyDataMapper::New();
  coneMapper->SetInputConnection( cone->GetOutputPort() );

  vtkActor *coneActor = vtkActor::New();
  coneActor->SetMapper( coneMapper );
  /* s设置模型显示的属性 */
  coneActor->GetProperty()->SetColor(0.2, 0.63, 0.79);   //设置模型显示的颜色
  coneActor->GetProperty()->SetDiffuse(0.7);             //设置散射系数
  coneActor->GetProperty()->SetSpecular(0.4);            //设置高光系数
  coneActor->GetProperty()->SetSpecularPower(20);        //设置高光的比例参数,具体参数设置可以参加vtk源码的介绍或者phong模型

  
  //通过新建property对象,实现模型显示颜色的改变
  vtkProperty *property = vtkProperty::New();
  property->SetColor(1.0, 0.3882, 0.2784);
  property->SetDiffuse(0.7);
  property->SetSpecular(0.4);
  property->SetSpecularPower(20);

 //添加模型显示属性来显示模型
  vtkActor *coneActor2 = vtkActor::New();
  coneActor2->SetMapper(coneMapper);
  coneActor2->GetProperty()->SetColor(0.2, 0.63, 0.79);
  coneActor2->SetProperty(property);
  coneActor2->SetPosition(0, 2, 0);  //设置模型显示的位置

  vtkRenderer *ren1= vtkRenderer::New();
  ren1->AddActor( coneActor );
  ren1->AddActor( coneActor2 );
  ren1->SetBackground( 0.1, 0.2, 0.4 );

  vtkRenderWindow *renWin = vtkRenderWindow::New();
  renWin->AddRenderer( ren1 );
  renWin->SetSize( 300, 300 );

  int i;
  for (i = 0; i < 360; ++i)
    {
    // render the image
    renWin->Render();
    // rotate the active camera by one degree
    ren1->GetActiveCamera()->Azimuth( 1 );
    }
  
  cone->Delete();
  coneMapper->Delete();
  coneActor->Delete();
  property->Delete();
  coneActor2->Delete();
  ren1->Delete();
  renWin->Delete();

  return 0;
}

vtk显示属性、窗口以及交互设置_第1张图片


2. 窗口显示

vtkrender可以控制场景在窗口中显示的位置,从而能够方便的实现多窗口显示。

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

int main()
{
  vtkConeSource *cone = vtkConeSource::New();
  cone->SetHeight( 3.0 );
  cone->SetRadius( 1.0 );
  cone->SetResolution( 10 );
  
  vtkPolyDataMapper *coneMapper = vtkPolyDataMapper::New();
  coneMapper->SetInputConnection( cone->GetOutputPort() );

  vtkActor *coneActor = vtkActor::New();
  coneActor->SetMapper( coneMapper );

  vtkRenderer *ren1= vtkRenderer::New();
  ren1->AddActor( coneActor );
  ren1->SetBackground( 0.1, 0.2, 0.4 );
  ren1->SetViewport(0.0, 0.0, 0.5, 1.0);  //设置view显示的位置

  vtkRenderer *ren2= vtkRenderer::New();
  ren2->AddActor( coneActor );
  ren2->SetBackground( 0.2, 0.3, 0.5 );
  ren2->SetViewport(0.5, 0.0, 1.0, 1.0);   //设置view显示的位置


  vtkRenderWindow *renWin = vtkRenderWindow::New();
  renWin->AddRenderer( ren1 );
  renWin->AddRenderer( ren2 );
  renWin->SetSize( 600, 300 );

  ren1->ResetCamera();
  ren1->GetActiveCamera()->Azimuth(90);

  int i;
  for (i = 0; i < 360; ++i)
    {
    // render the image
    renWin->Render();
    // rotate the active camera by one degree
    ren1->GetActiveCamera()->Azimuth( 1 );
    ren2->GetActiveCamera()->Azimuth( 1 );
    }
  
  cone->Delete();
  coneMapper->Delete();
  coneActor->Delete();
  ren1->Delete();
  ren2->Delete();
  renWin->Delete();

  return 0;
}
vtk显示属性、窗口以及交互设置_第2张图片


3. 交互设置

vtkRenderWindowInteractor能够支持用户的交互,vtkInteractorStyleTrackballCamera则为其中的一种交互方式。


#include "vtkConeSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkCamera.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkInteractorStyleTrackballCamera.h"

int main()
{
  vtkConeSource *cone = vtkConeSource::New();
  cone->SetHeight( 3.0 );
  cone->SetRadius( 1.0 );
  cone->SetResolution( 10 );
  
  vtkPolyDataMapper *coneMapper = vtkPolyDataMapper::New();
  coneMapper->SetInputConnection( cone->GetOutputPort() );

  vtkActor *coneActor = vtkActor::New();
  coneActor->SetMapper( coneMapper );

  vtkRenderer *ren1= vtkRenderer::New();
  ren1->AddActor( coneActor );
  ren1->SetBackground( 0.1, 0.2, 0.4 );

  vtkRenderWindow *renWin = vtkRenderWindow::New();
  renWin->AddRenderer( ren1 );
  renWin->SetSize( 300, 300 );

  //设置交互
  vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
  iren->SetRenderWindow(renWin);

  //设置交互方式
  vtkInteractorStyleTrackballCamera *style = 
    vtkInteractorStyleTrackballCamera::New();
  iren->SetInteractorStyle(style);

 //初始化和开始交互
  iren->Initialize();
  iren->Start();
  
  cone->Delete();
  coneMapper->Delete();
  coneActor->Delete();
  ren1->Delete();
  renWin->Delete();
  iren->Delete();
  style->Delete();

  return 0;
}

显示结果:
vtk显示属性、窗口以及交互设置_第3张图片


鼠标交互(左键旋转,中键移动,右键缩放)

vtk显示属性、窗口以及交互设置_第4张图片

键盘交互(s面片显示,w网格显示........)

vtk显示属性、窗口以及交互设置_第5张图片


你可能感兴趣的:(vtk显示属性、窗口以及交互设置)