Skip to content

TexturedSphere

Repository source: TexturedSphere


Other languages

See (PythonicAPI), (Java)

Question

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

Code

TexturedSphere.cxx

#include <vtkActor.h>
#include <vtkCamera.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>

#include <iostream>

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.25;
  }
  translate[1] = 0.0;
  translate[2] = 0.0;
  std::cout << "Translate: (" << 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");

  renderer->ResetCamera();
  // Orient so we are directly above Null Island!
  // The location is at (0°N 0°E), i.e., where the
  //  prime meridian and the equator intersect.
  renderer->GetActiveCamera()->Elevation(-90);

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

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

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(TexturedSphere)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  FiltersSources
  FiltersTexture
  IOImage
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

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

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.