# OBJImporter

VTKExamples/Cxx/IO/OBJImporter

### Code

OBJImporter.cxx

    #include <vtkSmartPointer.h>

    #include <vtkOBJImporter.h>
    #include <vtkRenderer.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkPolyDataNormals.h>
    #include <vtkCleanPolyData.h>
    #include <vtkPolyData.h>
    #include <vtkTexture.h>
    #include <vtkNamedColors.h>

    int main (int argc, char *argv[])
    {
      if (argc < 4)
      {
        std::cout << "Usage: " << argv[0]
                  << " objfile mtlfile texturepath"
                  << std::endl;
        return EXIT_FAILURE;
      }
      vtkSmartPointer<vtkOBJImporter> importer =
        vtkSmartPointer<vtkOBJImporter>::New();
      importer->SetFileName(argv[1]);
      importer->SetFileNameMTL(argv[2]);
      importer->SetTexturePath(argv[3]);

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

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

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

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

      vtkSmartPointer<vtkActorCollection> actors =
        vtkSmartPointer<vtkActorCollection>::New();
      actors = renderer->GetActors();
      actors->InitTraversal();
      std::cout << "There are " << actors->GetNumberOfItems() << " actors" << std::endl;

      for (vtkIdType a = 0; a < actors->GetNumberOfItems(); ++a)
      {
        std::cout << importer->GetOutputDescription(a) << std::endl;

        vtkActor * actor = actors->GetNextActor();

        // OBJImporter turns texture interpolation off
        if (actor->GetTexture())
        {
          actor->GetTexture()->InterpolateOn();
        }

        vtkPolyData *pd = vtkPolyData::SafeDownCast(actor->GetMapper()->GetInput());
        vtkSmartPointer<vtkCleanPolyData> clean =
          vtkSmartPointer<vtkCleanPolyData>::New();
        clean->SetInputData(pd);
        clean->Update();

        vtkSmartPointer<vtkPolyDataNormals> normals =
          vtkSmartPointer<vtkPolyDataNormals>::New();
        normals->SetInputConnection(clean->GetOutputPort());
        normals->SetInputData(pd);
        normals->SplittingOff();
        normals->Update();
        vtkPolyDataMapper *mapper = vtkPolyDataMapper::SafeDownCast(actor->GetMapper());
        mapper->SetInputData(normals->GetOutput());
      }
      renWin->Render();
      iren->Start();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 2.8)

    PROJECT(OBJImporter)

    find_package(VTK REQUIRED)
    include(${VTK_USE_FILE})

    add_executable(OBJImporter MACOSX_BUNDLE OBJImporter.cxx )

    target_link_libraries(OBJImporter ${VTK_LIBRARIES})

### Download and Build OBJImporter

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

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

    ./OBJImporter

WINDOWS USERS

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