VTK/ExamplesBoneYard/VisualizeSphere

From KitwarePublic
Jump to navigationJump to search

This example shows the basics of rendering to a window. It creates a sphere and then displays it in an interactive window.

Fix: Add CMakeLists.txt

VisualizeSphere.cxx

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

int main (int argc, char *argv[])
{

  vtkSmartPointer<vtkSphereSource> sphereSource = vtkSmartPointer<vtkSphereSource>::New();
  sphereSource->SetCenter(0.0, 0.0, 0.0);
  sphereSource->SetRadius(5.0);
  vtkPolyData* polydata = sphereSource->GetOutput();

  //create a mapper
  vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
  mapper->SetInput(polydata);

  // create an actor
  vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
  actor->SetMapper(mapper);

  // a renderer and render window
  vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
  vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);

  // an interactor
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = 
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);

  // add the actors to the scene
  renderer->AddActor(actor);
  renderer->SetBackground(1,1,1); // Background color white

  // render an image (lights and cameras are created automatically)
  renderWindow->Render();

  // begin mouse interaction
  renderWindowInteractor->Start();
  
  return 0;
}

Please try the new VTKExamples website.

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

PROJECT(VisualizeSphere)

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

add_executable(VisualizeSphere MACOSX_BUNDLE VisualizeSphere.cxx)

if(VTK_LIBRARIES)
  target_link_libraries(VisualizeSphere ${VTK_LIBRARIES})
else()
  target_link_libraries(VisualizeSphere vtkHybrid vtkWidgets)
endif()

Download and Build VisualizeSphere

Click here to download VisualizeSphere. and its CMakeLists.txt file.

Once the tarball VisualizeSphere.tar has been downloaded and extracted,

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

./VisualizeSphere

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