三维中灯光是重要的组成部分,直接影响三维模型的显示效果,以下是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;
}
以下是运行效果: