VTK/Examples/Cxx/IO/VRML

From KitwarePublic

< VTK | Examples | Cxx(Redirected from VTK/Examples/IO/VRML)
Jump to: navigation, search
VTK Examples Baseline IO TestVRML.png

This example loads a wrl file and displays it on the screen. An example file is here.

VRML.cxx

#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkVRMLImporter.h>
#include <vtkDataSet.h>
#include <vtkActorCollection.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkPolyDataNormals.h>
#include <vtkActor.h>
#include <vtkSmartPointer.h>
 
int main ( int argc, char *argv[])
{
  if(argc != 2)
    {
    std::cout << "Required arguments: Filename" << std::endl;
    return EXIT_FAILURE;
    }
 
  std::string filename = argv[1];
  std::cout << "Reading " << filename << std::endl;
 
  // VRML Import
  vtkSmartPointer<vtkVRMLImporter> importer = 
    vtkSmartPointer<vtkVRMLImporter>::New();
  importer->SetFileName ( filename.c_str() );
  importer->Read();
  importer->Update();
 
  // Convert to vtkDataSet
  vtkDataSet* pDataset;
  vtkActorCollection* actors = importer->GetRenderer()->GetActors();
  actors->InitTraversal();
  pDataset = actors->GetNextActor()->GetMapper()->GetInput();
 
  // Convert to vtkPolyData
  vtkPolyData *polyData = vtkPolyData::SafeDownCast ( pDataset );
#if VTK_MAJOR_VERSION <= 5
  polyData->Update();
#endif
  // Visualize
  vtkSmartPointer<vtkPolyDataMapper> solidMapper = 
    vtkSmartPointer<vtkPolyDataMapper>::New();
#if VTK_MAJOR_VERSION <= 5
  solidMapper->SetInput(polyData);
#else
  solidMapper->SetInputData(polyData);
#endif
  solidMapper->ScalarVisibilityOff();
 
  vtkSmartPointer<vtkActor> solidActor = 
    vtkSmartPointer<vtkActor>::New();
  solidActor->SetMapper ( solidMapper );
 
  vtkSmartPointer<vtkRenderer> renderer = 
    vtkSmartPointer<vtkRenderer>::New();
  vtkSmartPointer<vtkRenderWindow> renderWindow = 
    vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);
 
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = 
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);
 
  renderer->AddActor(solidActor);
  renderWindow->Render();
  renderWindowInteractor->Start();
 
  return EXIT_SUCCESS;
}


CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
PROJECT(VRML)
 
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
 
if (APPLE)
  add_executable(VRML MACOSX_BUNDLE VRML.cxx)
else()
  add_executable(VRML VRML.cxx)
endif()
 
if(VTK_LIBRARIES)
  target_link_libraries(VRML ${VTK_LIBRARIES})
else()
  target_link_libraries(VRML vtkHybrid )
endif()
Personal tools