StudyVtk(三)vtk中灯光 vtkLight

三维中灯光是重要的组成部分,直接影响三维模型的显示效果,以下是vtk中灯光的初步使用。

相对于相机来说,光照的控制要简单一些。使用较多的方法主要有SetPosition()。SetFocalPoint()以及SetColor()。

Position和FocalPoint分别控制光照的位置和方向。光照的Color是用RGB值来表示。光照能够通过方法SwitchOn()和SwitchOff()打开和关闭,光照的强度则是用方法SetIntensity()来设置。

缺省的vtkLight实例是方向光照(DirectionLight),也就是说光照的位置和焦点所确定的向量与光照的光线是平行的,光照的位置位于无限远处。这意味着假设光照的焦点和位置做平移变换的话,照耀在物体上的光照不会发生变化。

以下是官方英文介绍

 virtual light for 3D rendering

vtkLight is a virtual light for 3D rendering. It provides methods to locate and point the light, turn it on and off, and set its brightness and color. In addition to the basic infinite distance point light source attributes, you also can specify the light attenuation values and cone angle. These attributes are only used if the light is a positional light. The default is a directional light (e.g. infinite point light source).

Lights have a type that describes how the light should move with respect to the camera. A Headlight is always located at the current camera position and shines on the camera's focal point. A CameraLight also moves with the camera, but may not be coincident to it. CameraLights are defined in a normalized coordinate space where the camera is located at (0, 0, 1), the camera is looking at (0, 0, 0), and up is (0, 1, 0). Finally, a SceneLight is part of the scene itself and does not move with the camera. (Renderers are responsible for moving the light based on its type.)

Lights have a transformation matrix that describes the space in which they are positioned. A light's world space position and focal point are defined by their local position and focal point, transformed by their transformation matrix (if it exists).

/*
 * 光源和相机
 *
 * vtkLight
 *  位置光源
 *  平行光源
 *
 * vtkCamera
 *
 */

#include <vtkConeSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkLight.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkCamera.h>


#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2) ;
VTK_MODULE_INIT(vtkInteractionStyle) ;
VTK_MODULE_INIT(vtkRenderingFreeType);


int main(int argc,char* argv[])
{

    vtkConeSource* coneSource = vtkConeSource::New();
    //分辨率
    coneSource->SetResolution(100);
    //高度
    coneSource->SetHeight(5);
    //底面半径
    coneSource->SetRadius(2);

    //映射器
    vtkPolyDataMapper* mapper = vtkPolyDataMapper::New();

    mapper->SetInputConnection(coneSource->GetOutputPort());

    vtkActor* actor = vtkActor::New();
    actor->SetMapper(mapper);

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

    //添加灯光
    //反射光 镜面光 环境光
    vtkLight* light = vtkLight::New();
    //light->SetSpecularColor(1,0,0);
    //light->SetDiffuseColor(0,1,0);
    light->SetColor(0,0,1);
    renderer->AddLight(light);


    //相机
    //直接初始化相机
//    vtkCamera* camera = vtkCamera::New();
//    camera->SetPosition(5,0,0);
//    camera->SetFocalPoint(-1,0,0);
//    camera->SetViewUp(0,1,0);
//    renderer->SetActiveCamera(camera);

    //对内置相机进行设置
    vtkCamera* camera = renderer->GetActiveCamera();
    camera->SetPosition(5,0,0);
    camera->SetFocalPoint(-1,0,0);
    camera->SetViewUp(0,1,0);
    renderer->ResetCamera();

    vtkRenderWindow* window = vtkRenderWindow::New();
    window->SetSize(500,500);
    window->AddRenderer(renderer);

    window->Render();

    vtkRenderWindowInteractor* interactor = vtkRenderWindowInteractor::New();
    interactor->SetRenderWindow(window);

    vtkInteractorStyleTrackballCamera* cameraStyle = vtkInteractorStyleTrackballCamera::New();
    interactor->SetInteractorStyle(cameraStyle);

    window->Render();

    interactor->Initialize();
    interactor->Start();

    window->Delete();
    renderer->Delete();
    mapper->Delete();
    actor->Delete();
    coneSource->Delete();

    return 0;
}

以下是运行效果:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

把我格子衫拿来

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值