3.4 相機
觀衆的眼睛就好比三維渲染場景中的相機,VTK則是用vtkCamera類來表示三維渲染場景中的相機。vtkCamera負責把三維場景投影到二維平面,如螢幕、圖像等。圖3.6為相機投影示意圖。
從圖3.6可以看出與相機投影相關的因素主要有:
l 相機位置:即相機所在的位置,用方法vtkCamera::SetPosition()設定。
l 相機焦點:用方法vtkCamera::SetFocalPoint()設定,預設的焦點位置在世界坐标系的原點。
l 朝上方向:即哪個方向為相機朝上的方向。就好比我們直立看東西,方向為頭朝上,看到的東西也是直立的,如果我們倒立看某個東西,這時方向為頭朝下,看到的東西當然就是倒立的。相機位置、相機焦點和朝上方向三個因素确定了相機的實際方向,即确定相機的視圖。

圖3.6相機vtkCamera投影示意圖
l 投影方向:相機位置到相機焦點的向量方向即為投影方向。
l 投影方法:确定Actor是如何映射到像平面的。vtkCamera定義了兩種投影方法,一種是正交投影(OrthographicProjection),也叫平行投影(Parallel Projection),即進入相機的光線與投影方向是平行的。另一種是透視投影(PerspectiveProjection),即所有的光線相交于一點。
l 視角:透視投影時需要指定相機的視角(View Angle),預設的視角大小為30º,可以用方法vtkCamera::SetViewAngle()設定。
l 前後裁剪平面:裁剪平面與投影方向相交,一般與投影方向也是垂直的。裁剪平面主要用于評估Actor與相機距離的遠近,隻有在前後裁剪平面之間的Actor才是可見的。裁剪平面的位置可以用方法vtkCamera::SetClippingRange()設定。
如果你想擷取vtkRenderer裡預設的相機,可以用方法vtkRenderer::GetActiveCamera()。
相機vtkCamera的使用方法:
vtkSmartPointer<vtkCamera>myCamera = vtkSmartPointer<vtkCamera>::New();
myCamera->SetClippingRange(0.0475,2.3786); //這些值随便設定的,為了示範用法而已
myCamera->SetFocalPoint(0.0573,-0.2134, -0.0523);
myCamera->SetPosition(0.3245,-0.1139, -0.2932);
myCamera->ComputeViewPlaneNormal();
myCamera->SetViewUp(-0.2234,0.9983, 0.0345);
renderer->SetActiveCamera(myCamera);
上述我們用SetClippingRange();SetFocalPoint();SetPosition()分别設定相機的前後裁剪平面,焦點和位置。ComputeViewPlaneNormal()方法是根據設定的相機位置、焦點等資訊,重新計算視平面(View Plane)的法向量。一般該法向量與視平面是垂直的,如果不是垂直的話,Actor等看起來會有一些特殊的效果,如錯切。SetViewUp()方法用于設定相機朝上方向。最後用方法vtkRenderer::SetActiveCamera()把相機設定到渲染場景中。
vtkCamera除了提供設定與相機投影因素相關的方法之外,還提供了大量的控制相機運動的方法,如:vtkCamera::Dolly(),vtkCamera::Roll(),vtkCamera::Azimuth()(緯度),vtkCamera::Yaw(),vtkCamera::Elevation()(經度),vtkCamera::Pitch(),vtkCamera::Zoom()。這些方法具體表示相機是怎麼運動,以及相對哪個位置或者方向運動,請參考圖3.7或者關于類vtkCamera的文檔說明。
圖3.7 相機運動方向示意圖
void vtkCamera::Roll | ( | double | angle | ) |
Rotate the camera about the direction of projection. This will spin the camera about its axis.
void vtkCamera::Azimuth | ( | double | angle | ) |
Rotate the camera about the view up vector centered at the focal point. Note that the view up vector is whatever was set via SetViewUp, and is not necessarily perpendicular to the direction of projection. The result is a horizontal rotation of the camera.
void vtkCamera::Yaw | ( | double | angle | ) |
Rotate the focal point about the view up vector, using the camera's position as the center of rotation. Note that the view up vector is whatever was set via SetViewUp, and is not necessarily perpendicular to the direction of projection. The result is a horizontal rotation of the scene.
void vtkCamera::Elevation | ( | double | angle | ) |
Rotate the camera about the cross product of the negative of the direction of projection and the view up vector, using the focal point as the center of rotation. The result is a vertical rotation of the scene.
void vtkCamera::Pitch | ( | double | angle | ) |
Rotate the focal point about the cross product of the view up vector and the direction of projection, using the camera's position as the center of rotation. The result is a vertical rotation of the camera.