Shadows
Repository source: Shadows
Other languages
See (Python), (PythonicAPI)
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
Shadows.cxx
// Test baking shadow maps.
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCameraPass.h>
#include <vtkCubeSource.h>
#include <vtkInteractorStyleSwitch.h>
#include <vtkLight.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkOpenGLRenderer.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderPassCollection.h>
#include <vtkRenderStepsPass.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSequencePass.h>
#include <vtkShadowMapBakerPass.h>
#include <vtkShadowMapPass.h>
#include <vtkStringFormatter.h>
#include <vtkBYUReader.h>
#include <vtkOBJReader.h>
#include <vtkPLYReader.h>
#include <vtkPolyDataReader.h>
#include <vtkSTLReader.h>
#include <vtkSphereSource.h>
#include <vtkXMLPolyDataReader.h>
#include <vtk_cli11.h>
// #include <vtk_fmt.h>
// // clang-format off
// #include VTK_FMT(fmt/format.h)
// // clang-format on
namespace fs = std::filesystem;
namespace {
vtkNew<vtkPolyData> ReadPolyData(fs::path const& path);
}
//----------------------------------------------------------------------------
int main(int argc, char* argv[])
{
CLI::App app{
"Read a polydata file of a surface and display it with shadows."};
// Define options
std::string fileName{""};
app.add_option("fileName", fileName,
"A optional path to a file that contains vtkPolyData e.g. "
"dragon.ply\nIf no file is entered, a sphere is used.");
CLI11_PARSE(app, argc, argv);
auto path = fs::path(fileName);
if (!path.empty())
{
if (!fs::is_regular_file(path))
{
std::cerr << "Unable to find: " << path << std::endl;
return EXIT_FAILURE;
}
}
auto pd = ReadPolyData(path);
vtkNew<vtkNamedColors> colors;
colors->SetColor("HighNoonSun", 1.0, 1.0, .9843, 1.0); // Color temp. 5400k.
colors->SetColor("100W Tungsten", 1.0, .8392, .6667,
1.0); // Color temp. 2850k.
vtkNew<vtkRenderer> ren;
ren->SetBackground(colors->GetColor3d("Silver").GetData());
vtkNew<vtkRenderWindow> renWin;
renWin->SetSize(640, 480);
renWin->AddRenderer(ren);
auto appFn = fs::path((app.get_name())).stem().string();
renWin->SetWindowName(appFn.c_str());
renWin->SetMultiSamples(0);
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(renWin);
auto is = vtkInteractorStyleSwitch::SafeDownCast(iren->GetInteractorStyle());
if (is)
{
is->SetCurrentStyleToTrackballCamera();
}
vtkNew<vtkLight> light1;
light1->SetFocalPoint(0, 0, 0);
light1->SetPosition(0, 1, 0.2);
light1->SetColor(colors->GetColor3d("HighNoonSun").GetData());
light1->SetIntensity(0.3);
ren->AddLight(light1);
vtkNew<vtkLight> light2;
light2->SetFocalPoint(0, 0, 0);
light2->SetPosition(1.0, 1.0, 1.0);
light2->SetColor(colors->GetColor3d("100W Tungsten").GetData());
light2->SetIntensity(0.8);
ren->AddLight(light2);
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputData(pd);
vtkNew<vtkActor> actor;
actor->SetMapper(mapper);
actor->GetProperty()->SetAmbientColor(
colors->GetColor3d("SaddleBrown").GetData());
actor->GetProperty()->SetDiffuseColor(colors->GetColor3d("Sienna").GetData());
actor->GetProperty()->SetSpecularColor(colors->GetColor3d("White").GetData());
actor->GetProperty()->SetSpecular(0.51);
actor->GetProperty()->SetDiffuse(0.7);
actor->GetProperty()->SetAmbient(0.7);
actor->GetProperty()->SetSpecularPower(30.0);
actor->GetProperty()->SetOpacity(1.0);
ren->AddActor(actor);
// Add a plane.
std::array<double, 6> bounds;
pd->GetBounds(bounds.data());
std::array<double, 3> range;
range[0] = bounds[1] - bounds[0];
range[1] = bounds[3] - bounds[2];
range[2] = bounds[5] - bounds[4];
// vtk::println("Range: {:g}, {:g}, {:g} ", range[0], range[1], range[2]);
double expand = 1.0;
auto thickness = range[2] * 0.1;
vtkNew<vtkCubeSource> plane;
plane->SetCenter((bounds[1] + bounds[0]) / 2.0, bounds[2] - thickness / 2.0,
(bounds[5] + bounds[4]) / 2.0);
plane->SetXLength(bounds[1] - bounds[0] + (range[0] * expand));
plane->SetYLength(thickness);
plane->SetZLength(bounds[5] - bounds[4] + (range[2] * expand));
vtkNew<vtkPolyDataMapper> planeMapper;
planeMapper->SetInputConnection(plane->GetOutputPort());
vtkNew<vtkActor> planeActor;
planeActor->SetMapper(planeMapper);
ren->AddActor(planeActor);
vtkNew<vtkShadowMapPass> shadows;
vtkNew<vtkSequencePass> seq;
vtkNew<vtkRenderPassCollection> passes;
passes->AddItem(shadows->GetShadowMapBakerPass());
passes->AddItem(shadows);
seq->SetPasses(passes);
vtkNew<vtkCameraPass> cameraP;
cameraP->SetDelegatePass(seq);
// Tell the renderer to use our render pass pipeline.
vtkOpenGLRenderer* glren = dynamic_cast<vtkOpenGLRenderer*>(ren.GetPointer());
glren->SetPass(cameraP);
ren->GetActiveCamera()->SetPosition(-0.2, 0.2, 1);
ren->GetActiveCamera()->SetFocalPoint(0, 0, 0);
ren->GetActiveCamera()->SetViewUp(0, 1, 0);
ren->ResetCamera();
ren->GetActiveCamera()->Dolly(2.25);
ren->ResetCameraClippingRange();
renWin->Render();
iren->Start();
return EXIT_SUCCESS;
}
namespace {
vtkNew<vtkPolyData> ReadPolyData(fs::path const& path)
{
vtkNew<vtkPolyData> polyData;
std::string extension = path.extension().generic_string();
std::transform(extension.begin(), extension.end(), extension.begin(),
[](char c) { return std::tolower(c); });
if (extension == ".ply")
{
vtkNew<vtkPLYReader> reader;
reader->SetFileName(path.generic_string().c_str());
reader->Update();
polyData->DeepCopy(reader->GetOutput());
}
else if (extension == ".vtp")
{
vtkNew<vtkXMLPolyDataReader> reader;
reader->SetFileName(path.generic_string().c_str());
reader->Update();
polyData->DeepCopy(reader->GetOutput());
}
else if (extension == ".obj")
{
vtkNew<vtkOBJReader> reader;
reader->SetFileName(path.generic_string().c_str());
reader->Update();
polyData->DeepCopy(reader->GetOutput());
}
else if (extension == ".stl")
{
vtkNew<vtkSTLReader> reader;
reader->SetFileName(path.generic_string().c_str());
reader->Update();
polyData->DeepCopy(reader->GetOutput());
}
else if (extension == ".vtk")
{
vtkNew<vtkPolyDataReader> reader;
reader->SetFileName(path.generic_string().c_str());
reader->Update();
polyData->DeepCopy(reader->GetOutput());
}
else if (extension == ".g")
{
vtkNew<vtkBYUReader> reader;
reader->SetGeometryFileName(path.generic_string().c_str());
reader->Update();
polyData->DeepCopy(reader->GetOutput());
}
else
{
vtkNew<vtkSphereSource> source;
source->SetPhiResolution(50);
source->SetThetaResolution(50);
source->Update();
polyData->DeepCopy(source->GetOutput());
}
return polyData;
}
} // namespace```
###CMakeLists.txt
```cmake
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(Shadows)
find_package(VTK COMPONENTS
CommonColor
CommonCore
FiltersSources
IOGeometry
IOLegacy
IOPLY
IOXML
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "Shadows: Unable to find the VTK build folder.")
endif()
# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(Shadows MACOSX_BUNDLE Shadows.cxx )
target_link_libraries(Shadows PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS Shadows
MODULES ${VTK_LIBRARIES}
)
Download and Build Shadows¶
Click here to download Shadows and its CMakeLists.txt file. Once the tarball Shadows.tar has been downloaded and extracted,
cd Shadows/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:
./Shadows
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.
