LightKit

VTKExamples/Cxx/Rendering/LightKit

Question

If you have a simple question about this example contact us at VTKExamplesProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum

Code

LightKit.cxx

#include <vtkSmartPointer.h>
#include <vtkLightKit.h>

#include <vtkSuperquadricSource.h>

#include <vtkLightCollection.h>
#include <vtkProperty.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkPlaneSource.h>
#include <vtkSphereSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkLight.h>
#include <vtkLightActor.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkNamedColors.h>

int main(int, char *[])
{
  auto renderer =
    vtkSmartPointer<vtkRenderer>::New();

  auto lightKit =
    vtkSmartPointer<vtkLightKit>::New();
  lightKit->SetKeyLightWarmth(0.0);
  lightKit->SetFillLightWarmth(1.0);
  lightKit->SetHeadLightWarmth(.7);
  lightKit->AddLightsToRenderer(renderer);

  vtkLightCollection* originalLights = renderer->GetLights();
  std::cout << "Originally there are " << originalLights->GetNumberOfItems() << " lights." << std::endl;

  // Create a plane for the light to shine on
  auto planeSource = vtkSmartPointer<vtkPlaneSource>::New();
  planeSource->SetResolution(100, 100);
  planeSource->Update();

  auto planeMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
  planeMapper->SetInputData(planeSource->GetOutput());

  auto planeActor = vtkSmartPointer<vtkActor>::New();
  planeActor->SetMapper(planeMapper);
  renderer->AddActor(planeActor);

  // Create a superquadric
  auto squad =
    vtkSmartPointer<vtkSuperquadricSource>::New();
  squad->SetPhiResolution(20);
  squad->SetThetaResolution(25);
  squad->SetPhiRoundness(1.5);
  squad->SetThickness(0.43);
  squad->SetThetaRoundness(0.7);
  squad->ToroidalOn();

  auto squadMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
  squadMapper->SetInputConnection(squad->GetOutputPort());

  auto squadActor = vtkSmartPointer<vtkActor>::New();
  squadActor->SetMapper(squadMapper);

  auto colors =
    vtkSmartPointer<vtkNamedColors>::New();
  renderer->AddActor(squadActor);
  renderer->SetBackground(colors->GetColor3d("Wheat").GetData());

  auto renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);
  renderWindow->SetSize(640, 480);

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

  renderWindow->Render();
  std::cout << "Now there are " << originalLights->GetNumberOfItems() << " lights." << std::endl;

  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(LightKit)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkFiltersSources
  vtkInteractionStyle
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2 QUIET)
if (NOT VTK_FOUND)
  message("Skipping LightKit: ${VTK_NOT_FOUND_MESSAGE}")
  return ()
endif()
message (STATUS "VTK_VERSION: ${VTK_VERSION}")
if (VTK_VERSION VERSION_LESS "8.90.0")
  # old system
  include(${VTK_USE_FILE})
  add_executable(LightKit MACOSX_BUNDLE LightKit.cxx )
  target_link_libraries(LightKit PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(LightKit MACOSX_BUNDLE LightKit.cxx )
  target_link_libraries(LightKit PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS LightKit
    MODULES ${VTK_LIBRARIES}
    )
endif () 

Download and Build LightKit

Click here to download LightKit and its CMakeLists.txt file. Once the tarball LightKit.tar has been downloaded and extracted,

cd LightKit/build 

If VTK is installed:

cmake ..

If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:

cmake -DVTK_DIR:PATH=/home/me/vtk_build ..

Build the project:

make

and run it:

./LightKit

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.