# LightKit

VTKExamples/Cxx/Rendering/LightKit

### 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>

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

      vtkSmartPointer<vtkLightKit> 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
      vtkSmartPointer<vtkPlaneSource> planeSource = vtkSmartPointer<vtkPlaneSource>::New();
      planeSource->SetResolution(100, 100);
      planeSource->Update();

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

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

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

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

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

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

      vtkSmartPointer<vtkRenderWindowInteractor> 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
      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.