Skip to content

TexturedSphere

vtk-examples/Cxx/Texture/TexturedSphere


Other languages

See (Java)

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

TexturedSphere.cxx

#include <vtkActor.h>
#include <vtkImageReader.h>
#include <vtkImageReader2Factory.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkTexture.h>
#include <vtkTextureMapToSphere.h>
#include <vtkTexturedSphereSource.h>
#include <vtkTransformTextureCoords.h>

int main(int argc, char* argv[])
{
  vtkNew<vtkNamedColors> colors;

  if (argc < 2)
  {
    std::cout << "Usage: " << argv[0] << " texture(.png/.ppm) e.g. earth.ppm"
              << " [translate]" << std::endl;
    return EXIT_FAILURE;
  }
  double translate[3];
  if (argc > 2)
  {
    translate[0] = atof(argv[2]);
  }
  else
  {
    translate[0] = 0.0;
  }
  translate[1] = 0.0;
  translate[2] = 0.0;
  std::cout << translate[0] << ", " << translate[1] << ", " << translate[2]
            << "\n";
  // Create a sphere with texture coordinates
  vtkNew<vtkTexturedSphereSource> source;
  source->SetThetaResolution(100);
  source->SetPhiResolution(100);

  // Read texture file
  vtkNew<vtkImageReader2Factory> readerFactory;
  vtkSmartPointer<vtkImageReader2> imageReader;
  imageReader.TakeReference(readerFactory->CreateImageReader2(argv[1]));
  imageReader->SetFileName(argv[1]);

  // Create texture
  vtkNew<vtkTexture> texture;
  texture->SetInputConnection(imageReader->GetOutputPort());

  vtkNew<vtkTransformTextureCoords> transformTexture;
  transformTexture->SetInputConnection(source->GetOutputPort());
  transformTexture->SetPosition(translate);

  vtkNew<vtkPolyDataMapper> mapper;
  mapper->SetInputConnection(transformTexture->GetOutputPort());

  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);
  actor->SetTexture(texture);

  vtkNew<vtkRenderer> renderer;
  renderer->AddActor(actor);
  renderer->SetBackground(colors->GetColor3d("Black").GetData());

  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("TexturedSphere");

  vtkNew<vtkRenderWindowInteractor> renWinInteractor;
  renWinInteractor->SetRenderWindow(renderWindow);

  renderWindow->Render();
  renWinInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(TexturedSphere)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkFiltersSources
  vtkFiltersTexture
  vtkIOImage
  vtkInteractionStyle
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping TexturedSphere: ${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(TexturedSphere MACOSX_BUNDLE TexturedSphere.cxx )
  target_link_libraries(TexturedSphere PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(TexturedSphere MACOSX_BUNDLE TexturedSphere.cxx )
  target_link_libraries(TexturedSphere PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS TexturedSphere
    MODULES ${VTK_LIBRARIES}
    )
endif ()

Download and Build TexturedSphere

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

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

./TexturedSphere

WINDOWS USERS

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