默认效果:

加上红光,旋转并调整大小:

    vtkSmartPointer<vtkLight> light = vtkSmartPointer<vtkLight>::New();
    light->SetColor( 1, 0, 0 );
    light->SetFocalPoint( renderer->GetActiveCamera()->GetFocalPoint() );
    light->SetPosition( renderer->GetActiveCamera()->GetPosition() );

    renderer->AddLight( light );
    renderer->ResetCamera();


如果加上 light->SetLightTypeToCameraLight();light->SetLightTypeToHeadlight
无论用户如何旋转球体,用户看到的始终是红球。

由此可知,vtkLight默认的type是SceneLight.
light->SetLightTypeToSceneLight();是默认被自动调用的。

#include <iostream>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkActor.h>
#include <vtkConeSource.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkLight.h>
#include <vtkCamera.h>

using namespace std;

int main()
{
    vtkSmartPointer<vtkConeSource> cone =
            vtkSmartPointer<vtkConeSource>::New();

    vtkSmartPointer<vtkPolyDataMapper> mapper =
            vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputConnection( cone->GetOutputPort() );

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

    vtkSmartPointer<vtkRenderer> renderer =
            vtkSmartPointer<vtkRenderer>::New();
    renderer->AddActor(actor);
    renderer->SetBackground( 0, 0, 0 );

    vtkSmartPointer<vtkRenderWindow> renderWindow =
            vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer( renderer );

    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
            vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow( renderWindow );

    vtkSmartPointer<vtkLight> light = vtkSmartPointer<vtkLight>::New();
    //light->SetLightTypeToCameraLight();
    //light->SetLightTypeToHeadlight();
    //light->SetLightTypeToSceneLight();
    light->SetColor( 1, 0, 0 );
    light->SetFocalPoint( renderer->GetActiveCamera()->GetFocalPoint() );
    light->SetPosition( renderer->GetActiveCamera()->GetPosition() );

    renderer->AddLight( light );
    renderer->ResetCamera();

    renderWindow->Render();
    renderWindowInteractor->Start();

    return 0;
}

分类: C plus plus

发表评论

电子邮件地址不会被公开。 必填项已用*标注