Skip to content


# ZBuffer

VTKEx/Cxx/Utilities/ZBuffer

Question

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

### Code

ZBuffer.cxx

    // This demo creates depth map for a polydata instance by extracting
    // exact ZBuffer values.
    #include <vtkSmartPointer.h>

    #include <vtkActor.h>
    #include <vtkBMPWriter.h>
    #include <vtkImageShiftScale.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkRenderer.h>
    #include <vtkVersion.h>
    #include <vtkWindowToImageFilter.h>
    #include <vtkXMLPolyDataReader.h>

    int main(int argc, char* argv[])
    {
      if (argc < 3)
      {
        std::cout << "Usage: " << argv[0] << " input(.vtp) output(.bmp)"
                  << std::endl;
        return EXIT_FAILURE;
      }

      vtkSmartPointer<vtkPolyDataMapper> mapper =
          vtkSmartPointer<vtkPolyDataMapper>::New();
      vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
      vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
      vtkSmartPointer<vtkRenderWindow> renWin =
          vtkSmartPointer<vtkRenderWindow>::New();
      vtkSmartPointer<vtkRenderWindowInteractor> interactor =
          vtkSmartPointer<vtkRenderWindowInteractor>::New();
      vtkSmartPointer<vtkXMLPolyDataReader> fileReader =
          vtkSmartPointer<vtkXMLPolyDataReader>::New();
      vtkSmartPointer<vtkWindowToImageFilter> filter =
          vtkSmartPointer<vtkWindowToImageFilter>::New();
      vtkSmartPointer<vtkBMPWriter> imageWriter =
          vtkSmartPointer<vtkBMPWriter>::New();
      vtkSmartPointer<vtkImageShiftScale> scale =
          vtkSmartPointer<vtkImageShiftScale>::New();

      // Read .vtp file
      fileReader->SetFileName(argv[1]);

      // Build visualization enviroment
      mapper->SetInputConnection(fileReader->GetOutputPort());
      actor->SetMapper(mapper);
      renderer->AddActor(actor);
      renWin->AddRenderer(renderer);
      interactor->SetRenderWindow(renWin);
      renWin->Render();

      // Create Depth Map
      filter->SetInput(renWin);
    #if VTK_MAJOR_VERSION >= 8 || VTK_MAJOR_VERSION == 8 && VTK_MINOR_VERSION >= 90
      filter->SetScale(1);
    #else
      filter->SetMagnification(1);
    #endif
      filter->SetInputBufferTypeToZBuffer(); // Extract z buffer value

      scale->SetOutputScalarTypeToUnsignedChar();
      scale->SetInputConnection(filter->GetOutputPort());
      scale->SetShift(0);
      scale->SetScale(-255);

      // Write depth map as a .bmp image
      imageWriter->SetFileName(argv[2]);
      imageWriter->SetInputConnection(scale->GetOutputPort());
      imageWriter->Write();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(ZBuffer)

    find_package(VTK COMPONENTS
      vtkCommonCore
      vtkIOImage
      vtkIOXML
      vtkImagingCore
      vtkInteractionStyle
      vtkRenderingContextOpenGL2
      vtkRenderingCore
      vtkRenderingFreeType
      vtkRenderingGL2PSOpenGL2
      vtkRenderingOpenGL2 QUIET)
    if (NOT VTK_FOUND)
      message("Skipping ZBuffer: ${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(ZBuffer MACOSX_BUNDLE ZBuffer.cxx )
      target_link_libraries(ZBuffer PRIVATE ${VTK_LIBRARIES})
    else ()
      # include all components
      add_executable(ZBuffer MACOSX_BUNDLE ZBuffer.cxx )
      target_link_libraries(ZBuffer PRIVATE ${VTK_LIBRARIES})
      # vtk_module_autoinit is needed
      vtk_module_autoinit(
        TARGETS ZBuffer
        MODULES ${VTK_LIBRARIES}
        )
    endif ()

### Download and Build ZBuffer

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

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

    ./ZBuffer

WINDOWS USERS

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