Skip to content

3DSImporter

web-test/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 question about this example, please use the VTK Discourse Forum

Code

3DSImporter.cxx

#include <vtk3DSImporter.h>
#include <vtkCamera.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.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;
  }

  vtkNew<vtk3DSImporter> importer;
  importer->SetFileName(argv[1]);
  importer->ComputeNormalsOn();

  vtkNew<vtkNamedColors> colors;

  vtkNew<vtkRenderer> renderer;
  vtkNew<vtkRenderWindow> renWin;
  vtkNew<vtkRenderWindowInteractor> iren;

  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->SetWindowName("3DSImporter");

  renWin->Render();

  vtkNew<vtkCamera> camera;
  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.12 FATAL_ERROR)

project(3DSImporter)

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

if (NOT VTK_FOUND)
  message(FATAL_ERROR "3DSImporter: 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(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}
)

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.