Skip to content


# RotatingSphere

VTKEx/Cxx/Animation/RotatingSphere

### Description

This example demonstrates how to create a rotating sphere. A timer is used to rotate a sphere along z-axis.

Question

If you have a simple question about this example contact us at VTKExProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum

### Code

RotatingSphere.cxx

    #include <vtkActor.h>
    #include <vtkNamedColors.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkProperty.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkRenderer.h>
    #include <vtkSmartPointer.h>
    #include <vtkSphereSource.h>

    class vtkTimerCallback2 : public vtkCommand
    {
    public:
      vtkTimerCallback2() = default;
      ~vtkTimerCallback2() = default;

      int timerId = 0;
      static vtkTimerCallback2* New()
      {
        vtkTimerCallback2* cb = new vtkTimerCallback2;
        cb->TimerCount = 0;
        return cb;
      }
      virtual void Execute(vtkObject* caller, unsigned long eventId,
                           void* vtkNotUsed(callData))
      {
        vtkRenderWindowInteractor* iren =
            dynamic_cast<vtkRenderWindowInteractor*>(caller);
        if (vtkCommand::TimerEvent == eventId)
        {
          ++this->TimerCount;
        }
        if (TimerCount < 36)
        {
          actor->RotateZ(5);
          iren->GetRenderWindow()->Render();
        }
        else
        {
          iren->DestroyTimer();
        }
      }

    private:
      int TimerCount = 0;

    public:
      vtkActor* actor;
    };

    int main(int, char*[])
    {
      auto colors = vtkSmartPointer<vtkNamedColors>::New();

      // Create a sphere
      auto sphereSource = vtkSmartPointer<vtkSphereSource>::New();
      sphereSource->SetCenter(0.0, 0.0, 0.0);
      sphereSource->SetRadius(1.0);
      sphereSource->SetThetaResolution(15);
      sphereSource->SetPhiResolution(15);
      sphereSource->Update();

      // Create a mapper and actor
      auto mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
      mapper->SetInputConnection(sphereSource->GetOutputPort());
      auto actor = vtkSmartPointer<vtkActor>::New();
      actor->SetMapper(mapper);
      actor->RotateX(90);
      actor->GetProperty()->SetRepresentationToWireframe();

      // Create a renderer, render window, and interactor
      auto renderer = vtkSmartPointer<vtkRenderer>::New();
      auto renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
      renderWindow->AddRenderer(renderer);
      renderWindow->SetSize(640, 480);

      auto renderWindowInteractor =
          vtkSmartPointer<vtkRenderWindowInteractor>::New();
      renderWindowInteractor->SetRenderWindow(renderWindow);

      // Add the actor to the scene
      renderer->AddActor(actor);

      renderer->SetBackground(colors->GetColor3d("Seashell").GetData());
      // Render and interact
      renderWindow->Render();

      // Initialize must be called prior to creating timer events.
      renderWindowInteractor->Initialize();

      // Sign up to receive TimerEvent
      auto cb = vtkSmartPointer<vtkTimerCallback2>::New();
      cb->actor = actor;

      renderWindowInteractor->AddObserver(vtkCommand::TimerEvent, cb);
      int timerId = renderWindowInteractor->CreateRepeatingTimer(100);
      cb->timerId = timerId;

      // Start the interaction and timer
      renderWindowInteractor->Start();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(RotatingSphere)

    find_package(VTK COMPONENTS
      vtkCommonColor
      vtkCommonCore
      vtkFiltersSources
      vtkInteractionStyle
      vtkRenderingContextOpenGL2
      vtkRenderingCore
      vtkRenderingFreeType
      vtkRenderingGL2PSOpenGL2
      vtkRenderingOpenGL2 QUIET)
    if (NOT VTK_FOUND)
      message("Skipping RotatingSphere: ${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(RotatingSphere MACOSX_BUNDLE RotatingSphere.cxx )
      target_link_libraries(RotatingSphere PRIVATE ${VTK_LIBRARIES})
    else ()
      # include all components
      add_executable(RotatingSphere MACOSX_BUNDLE RotatingSphere.cxx )
      target_link_libraries(RotatingSphere PRIVATE ${VTK_LIBRARIES})
      # vtk_module_autoinit is needed
      vtk_module_autoinit(
        TARGETS RotatingSphere
        MODULES ${VTK_LIBRARIES}
        )
    endif ()

### Download and Build RotatingSphere

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

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

    ./RotatingSphere

WINDOWS USERS

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