VTK基本概念之灯光

灯光

  vtkLight可以分为位置灯光(Position Light)和方向灯光(Direction Light)。位置灯光是光源位置在渲染场景中的某个位置,可以指定灯光的衰减值、锥角等;方向灯光即光源在无穷远,可以认为光线是平行的,比如自然界中的太阳光。光源的位置和焦点的连线定义为光线的方向,默认的vtkLight为方向灯光。
  Render里可以有多个灯光,用renderer->AddLight(light)。如果没有灯光,只能显示二维。

  vtkLight对象可以打开关闭、设置灯光位置、照射位置(焦点)、灯光所在的位置 、强度等。常用的方法如下:

  • SetColor():设置灯光的颜色,以RGB的形式指定颜色
  • SetPosition():设置灯光位置。
  • SetFocalPoint():设置灯光焦点。
  • SetIntensity():设置灯光强度。
  • SetSwitch()/SwitchOn()/SiwtichOff():打开 或关闭对应的灯光。

示例说明

CMakeLists.txt文件代码如下:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(LightExample)
FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})
ADD_EXECUTABLE(LightExample LightExample.cpp)
TARGET_LINK_LIBRARIES(LightExample ${VTK_LIBRARIES})

LightExample.cpp文件代码如下:

#include <vtkSmartPointer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkCylinderSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkProperty.h>
#include "vtkLight.h"
#include "vtkCamera.h"

int main()
{
    vtkSmartPointer<vtkCylinderSource> cylinder = 
        vtkSmartPointer<vtkCylinderSource>::New();
    cylinder->SetHeight( 3.0 );
    cylinder->SetRadius( 1.0 );
    cylinder->SetResolution( 10 ); 

    vtkSmartPointer<vtkPolyDataMapper> cylinderMapper = 
        vtkSmartPointer<vtkPolyDataMapper>::New();
    cylinderMapper->SetInputConnection( cylinder->GetOutputPort() ); 

    vtkSmartPointer<vtkActor> cylinderActor = 
        vtkSmartPointer<vtkActor>::New();
    cylinderActor->SetMapper( cylinderMapper );
    cylinderActor->GetProperty()->SetColor(1.0, 1.0, 1.0);

    vtkSmartPointer<vtkRenderer> renderer = 
        vtkSmartPointer<vtkRenderer>::New();
    renderer->AddActor( cylinderActor );
    renderer->SetBackground( 1.0, 1.0, 1.0 );

    //add a light
    vtkSmartPointer<vtkLight> greenLight = vtkSmartPointer<vtkLight>::New();
    greenLight->SetColor(0, 1, 0);
    greenLight->SetPosition(0, 0, 1);
    greenLight->SetFocalPoint(renderer->GetActiveCamera()->GetFocalPoint());
    renderer->AddLight(greenLight);

    vtkSmartPointer<vtkRenderWindow> renWin = 
        vtkSmartPointer<vtkRenderWindow>::New();
    renWin->AddRenderer( renderer );
    renWin->SetSize( 640, 480 );
    renWin->Render();
    renWin->SetWindowName("LightExample");

    vtkSmartPointer<vtkRenderWindowInteractor> iren = 
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
    iren->SetRenderWindow(renWin);

    vtkSmartPointer<vtkInteractorStyleTrackballCamera> style = 
        vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
    iren->SetInteractorStyle(style);

    iren->Initialize();
    iren->Start();

    return EXIT_SUCCESS;
}

运行结果:

这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值