Skip to content

TextureCutSphere

vtk-examples/Cxx/Texture/TextureCutSphere


Description

Sphere cut with transparent texture.

Other languages

See (Python)

Question

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

Code

TextureCutSphere.cxx

//
// Cut an outer sphere to reveal an inner sphere.
//
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkDataSetMapper.h>
#include <vtkDoubleArray.h>
#include <vtkImplicitTextureCoords.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPlanes.h>
#include <vtkPoints.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>
#include <vtkStructuredPointsReader.h>
#include <vtkTexture.h>

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

  // Hidden sphere.
  vtkNew<vtkSphereSource> sphere1;
  sphere1->SetRadius(0.5);

  vtkNew<vtkPolyDataMapper> innerMapper;
  innerMapper->SetInputConnection(sphere1->GetOutputPort());

  vtkNew<vtkActor> innerSphere;
  innerSphere->SetMapper(innerMapper);
  innerSphere->GetProperty()->SetColor(
      colors->GetColor3d("BlanchedAlmond").GetData());

  // Sphere to texture.
  vtkNew<vtkSphereSource> sphere2;
  sphere2->SetRadius(1.0);
  sphere2->SetPhiResolution(21);
  sphere2->SetThetaResolution(21);

  vtkNew<vtkPlanes> planes;

  double pts[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
  vtkNew<vtkPoints> points;
  points->SetNumberOfPoints(2);
  points->SetPoint(0, pts);
  points->SetPoint(1, pts + 3);

  double nrms[6] = {1.0, 0.0, 0.0, 0.0, 1.0, 0.0};
  vtkNew<vtkDoubleArray> normals;
  normals->SetNumberOfComponents(3);
  normals->SetNumberOfTuples(2);
  normals->SetTuple(0, nrms);
  normals->SetTuple(1, nrms + 3);

  planes->SetPoints(points);
  planes->SetNormals(normals);

  vtkNew<vtkImplicitTextureCoords> tcoords;
  tcoords->SetInputConnection(sphere2->GetOutputPort());
  tcoords->SetRFunction(planes);

  vtkNew<vtkDataSetMapper> outerMapper;
  outerMapper->SetInputConnection(tcoords->GetOutputPort());

  vtkNew<vtkStructuredPointsReader> tmap;
  tmap->SetFileName(argv[1]);

  vtkNew<vtkTexture> texture;
  texture->SetInputConnection(tmap->GetOutputPort());
  texture->InterpolateOff();
  texture->RepeatOff();

  vtkNew<vtkActor> outerSphere;
  outerSphere->SetMapper(outerMapper);
  outerSphere->SetTexture(texture);
  outerSphere->GetProperty()->SetColor(
      colors->GetColor3d("LightSalmon").GetData());

  vtkNew<vtkRenderWindow> renWin;
  vtkNew<vtkRenderWindowInteractor> iren;
  vtkNew<vtkRenderer> aren;
  iren->SetRenderWindow(renWin);
  renWin->AddRenderer(aren);

  aren->AddActor(innerSphere);
  aren->AddActor(outerSphere);
  aren->SetBackground(colors->GetColor3d("SlateGray").GetData());
  aren->GetActiveCamera()->Azimuth(-30);
  aren->GetActiveCamera()->Elevation(-30);
  aren->ResetCamera();

  renWin->SetSize(500, 500);
  renWin->SetWindowName("TextureCutSphere");

  // Interact with the data.
  renWin->Render();

  iren->Initialize();
  iren->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(TextureCutSphere)

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

if (NOT VTK_FOUND)
  message(FATAL_ERROR "TextureCutSphere: 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(TextureCutSphere MACOSX_BUNDLE TextureCutSphere.cxx )
  target_link_libraries(TextureCutSphere PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS TextureCutSphere
  MODULES ${VTK_LIBRARIES}
)

Download and Build TextureCutSphere

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

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

./TextureCutSphere

WINDOWS USERS

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