0
点赞
收藏
分享

微信扫一扫

【VTK】light type


vtkLight的光照类型设置: virtual void SetLightType(int);
三类光照类型:
SceneLight
HeadLight (always located at the camera and is pointed at the camera’s focal point.)
CameraLight(A CameraLight is also attached to the camera, but is not necessarily located at the camera’s position.)

Headlight

vtkRenderer的render方法自动生成的光照类型:



【VTK】light type_CPP

SceneLight

vtkLight默认构造出来的光照类型:



【VTK】light type_自动生成_02

使用例子:

    vtkSmartPointer<vtkLight> light =
vtkSmartPointer<vtkLight>::New();
light->SetColor( 1, 1, 0 );
cout << "lightType: " << light->GetLightType() << endl; // lightType: 3

renderer->AddLight( light );

scene light 和 head light的生成,相关的函数及其调用关系:

void vtkRenderer::CreateLight(void)
{
//...
vtkLight *l = this->MakeLight();
this->CreatedLight = l;
this->CreatedLight->Register(this);
this->AddLight(this->CreatedLight);
l->Delete();

this->CreatedLight->SetLightTypeToHeadlight();
//...
}

vtkLight::vtkLight()
{
//...
this->LightType = VTK_LIGHT_TYPE_SCENE_LIGHT;
//...
}

vtkRenderer::CreateLight
<=
vtkOpenGLRenderer::UpdateLights (vtkOpenGLRenderer is derived class of vtkRenderer)
<=
vtkOpenGLRenderer::DeviceRender
<=
vtkRenderer::Render

CameraLight

CameraLight 和 HeadLight有点类似,但是有位置的区别。
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).
从效果上看,我没发现两者的区别:
分别使用HeadLight和CameraLight照射球体。

【VTK】light type_ide_03

左图是HeadLight,右图是CameraLight。

后经阅读源码发现,每当camera位置、焦点、viewUp发生改变,camera就会重新计算vtkPerspectiveTransform(ViewTransform),再将得到的结果处理更新成CameraLightTransform,light则使用这个CameraLightTransform更新光照效果。
所以,每当我们进行移动物体、缩放等操作的时候,vtk都会更新CameraLight,他的结果就和HeadLight的光照效果一样。
相关源码:

int vtkRenderer::UpdateLightsGeometryToFollowCamera()
{
vtkCamera *camera;
vtkLight *light;
vtkMatrix4x4 *lightMatrix;

//...

lightMatrix = camera->GetCameraLightTransformMatrix();

vtkCollectionSimpleIterator sit;
for(this->Lights->InitTraversal(sit);
(light = this->Lights->GetNextLight(sit)); )
{
//...
else if (light->LightTypeIsHeadlight())
{
// update position and orientation of light to match camera.
light->SetPosition(camera->GetPosition());
light->SetFocalPoint(camera->GetFocalPoint());
}
else if (light->LightTypeIsCameraLight())
{
light->SetTransformMatrix(lightMatrix);
}
//...
}
return 1;
}

vtkMatrix4x4 *vtkCamera::GetCameraLightTransformMatrix()
{
// return the transform
return this->CameraLightTransform->GetMatrix();
}

void vtkCamera::ComputeCameraLightTransform()
{
vtkTransform *t;
double d;

// assumes a valid view transform and valid camera distance
t = this->CameraLightTransform;
t->Identity();
t->SetMatrix(this->ViewTransform->GetMatrix());
t->Inverse();

d = this->Distance;
t->Scale(d, d, d);
t->Translate(0.0, 0.0, -1.0);
}


void vtkPerspectiveTransform::SetupCamera(const double position[3],
const double focalPoint[3],
const double viewUp[3])
{
double matrix[4][4];
vtkMatrix4x4::Identity(*matrix);

// the view directions correspond to the rows of the rotation matrix,
// so we'll make the connection explicit
double *viewSideways = matrix[0];
double *orthoViewUp = matrix[1];
double *viewPlaneNormal = matrix[2];

// set the view plane normal from the view vector
viewPlaneNormal[0] = position[0] - focalPoint[0];
viewPlaneNormal[1] = position[1] - focalPoint[1];
viewPlaneNormal[2] = position[2] - focalPoint[2];
vtkMath::Normalize(viewPlaneNormal);

// orthogonalize viewUp and compute viewSideways
vtkMath::Cross(viewUp,viewPlaneNormal,viewSideways);
vtkMath::Normalize(viewSideways);
vtkMath::Cross(viewPlaneNormal,viewSideways,orthoViewUp);

// translate by the vector from the position to the origin
double delta[4];
delta[0] = -position[0];
delta[1] = -position[1];
delta[2] = -position[2];
delta[3] = 0.0; // yes, this should be zero, not one

vtkMatrix4x4::MultiplyPoint(*matrix,delta,delta);

matrix[0][3] = delta[0];
matrix[1][3] = delta[1];
matrix[2][3] = delta[2];

// apply the transformation
this->Concatenate(*matrix);
}


举报

相关推荐

0 条评论