Skip to content

LODProp3D

vtk-examples/Cxx/Visualization/LODProp3D


Description

If you have a fast graphics card, you may not see a difference.

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

LODProp3D.cxx

#include <vtkCallbackCommand.h>
#include <vtkLODProp3D.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>

namespace {
void RefreshCallback(vtkObject* vtkNotUsed(caller),
                     long unsigned int vtkNotUsed(eventId), void* clientData,
                     void* vtkNotUsed(callData));
}

int main(int, char*[])
{
  vtkNew<vtkNamedColors> colors;

  // High res sphere.
  vtkNew<vtkSphereSource> highResSphereSource;
  int res = 100;
  highResSphereSource->SetThetaResolution(res);
  highResSphereSource->SetPhiResolution(res);
  highResSphereSource->Update();

  vtkNew<vtkPolyDataMapper> highResMapper;
  highResMapper->SetInputConnection(highResSphereSource->GetOutputPort());

  // Low res sphere.
  vtkNew<vtkSphereSource> lowResSphereSource;

  vtkNew<vtkPolyDataMapper> lowResMapper;
  lowResMapper->SetInputConnection(lowResSphereSource->GetOutputPort());

  vtkNew<vtkProperty> propertyLowRes;
  propertyLowRes->SetDiffuseColor(
      colors->GetColor3d("BlanchedAlmond").GetData());
  propertyLowRes->SetInterpolationToFlat();

  vtkNew<vtkProperty> propertyHighRes;
  propertyHighRes->SetDiffuseColor(colors->GetColor3d("MistyRose").GetData());
  propertyHighRes->SetInterpolationToFlat();

  vtkNew<vtkLODProp3D> prop;
  prop->AddLOD(lowResMapper, propertyLowRes, 0.0);
  prop->AddLOD(highResMapper, propertyHighRes, 0.0);

  std::cout << "There are " << prop->GetNumberOfLODs() << " LODs" << std::endl;

  // A renderer and render window.
  vtkNew<vtkRenderer> renderer;
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("LODProp3D");

  // prop->SetAllocatedRenderTime(1e-6,renderer);
  prop->SetAllocatedRenderTime(1e-12, renderer);

  // An interactor
  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(renderWindow);

  // Add the actors to the scene.
  renderer->AddActor(prop);
  renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());

  vtkNew<vtkCallbackCommand> refreshCallback;
  refreshCallback->SetCallback(RefreshCallback);
  refreshCallback->SetClientData(prop);

  renderWindow->AddObserver(vtkCommand::ModifiedEvent, refreshCallback);

  renderWindow->Render();

  // Begin mouse interaction.
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

namespace {
void RefreshCallback(vtkObject* vtkNotUsed(caller),
                     long unsigned int vtkNotUsed(eventId), void* clientData,
                     void* vtkNotUsed(callData))
{
  auto lodProp = static_cast<vtkLODProp3D*>(clientData);
  std::cout << "Last rendered LOD ID: " << lodProp->GetLastRenderedLODID()
            << std::endl;
}
} // namespace

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(LODProp3D)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  FiltersSources
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "LODProp3D: 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(LODProp3D MACOSX_BUNDLE LODProp3D.cxx )
  target_link_libraries(LODProp3D PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS LODProp3D
  MODULES ${VTK_LIBRARIES}
)

Download and Build LODProp3D

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

cd LODProp3D/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:

./LODProp3D

WINDOWS USERS

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