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 <vtkPolyData.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.3 FATAL_ERROR)

project(LODProp3D)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkCommonDataModel
  vtkFiltersSources
  vtkInteractionStyle
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping LODProp3D: ${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(LODProp3D MACOSX_BUNDLE LODProp3D.cxx )
  target_link_libraries(LODProp3D PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  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}
    )
endif ()

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.