3DSImporter

VTKExamples/Cxx/IO/3DSImporter


Description

This example illustrates Importing files in VTK. An importer creates a vtkRenderWindow that describes the scene.

Other Languages

See (Python)

Question

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

Code

3DSImporter.cxx

#include <vtk3DSImporter.h>
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkNamedColors.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>

int main(int argc, char* argv[])
{
  if (argc < 2)
  {
    std::cout << "Usage: " << argv[0] << " file.3ds e.g. iflamingo.3ds"
              << std::endl;
    return EXIT_FAILURE;
  }

  auto importer = vtkSmartPointer<vtk3DSImporter>::New();
  importer->SetFileName(argv[1]);
  importer->ComputeNormalsOn();

  auto colors = vtkSmartPointer<vtkNamedColors>::New();

  auto renderer = vtkSmartPointer<vtkRenderer>::New();
  auto renWin = vtkSmartPointer<vtkRenderWindow>::New();
  auto iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();

  renWin->AddRenderer(renderer);
  renderer->SetBackground2(colors->GetColor3d("Gold").GetData());
  renderer->SetBackground(colors->GetColor3d("Wheat").GetData());
  renderer->GradientBackgroundOn();

  iren->SetRenderWindow(renWin);
  importer->SetRenderWindow(renWin);
  importer->Update();

  auto actors = renderer->GetActors();
  std::cout << "There are " << actors->GetNumberOfItems() << " actors"
            << std::endl;

  renWin->Render();
  auto camera = vtkSmartPointer<vtkCamera>::New();
  camera->SetPosition(0, -1, 0);
  camera->SetFocalPoint(0, 0, 0);
  camera->SetViewUp(0, 0, 1);
  camera->Azimuth(150);
  camera->Elevation(30);

  renderer->SetActiveCamera(camera);
  renderer->ResetCamera();
  renderer->ResetCameraClippingRange();

  iren->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(3DSImporter)

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

Download and Build 3DSImporter

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

cd 3DSImporter/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:

./3DSImporter

WINDOWS USERS

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