Cube1
VTKExamples/Cxx/GeometricObjects/Cube1
Description¶
Display a cube.
A nice simple example that demonstrates the operation of the VTK pipeline.
Other Languages
See (Python)
Code¶
Cube1.cxx
#include <vtkActor.h> #include <vtkCamera.h> #include <vtkCubeSource.h> #include <vtkNamedColors.h> #include <vtkPolyDataMapper.h> #include <vtkRenderWindow.h> #include <vtkRenderWindowInteractor.h> #include <vtkRenderer.h> #include <vtkNew.h> #include <vtkProperty.h> int main(int, char *[]) { vtkNew<vtkNamedColors> colors; // Create a rendering window and renderer. vtkNew<vtkRenderer> ren; vtkNew<vtkRenderWindow> renWin; renWin->SetWindowName("Cube"); renWin->AddRenderer(ren); // Create a renderwindowinteractor vtkNew<vtkRenderWindowInteractor> iren; iren->SetRenderWindow(renWin); // Create a cube. vtkNew<vtkCubeSource> cube; cube->Update(); // mapper vtkNew<vtkPolyDataMapper> cubeMapper; cubeMapper->SetInputData(cube->GetOutput()); // Actor. vtkNew<vtkActor> cubeActor; cubeActor->SetMapper(cubeMapper); cubeActor->GetProperty()->SetColor(colors->GetColor3d("Banana").GetData()); // Assign actor to the renderer. ren->AddActor(cubeActor); ren->ResetCamera(); ren->GetActiveCamera()->Azimuth(30); ren->GetActiveCamera()->Elevation(30); ren->ResetCameraClippingRange(); ren->SetBackground(colors->GetColor3d("Silver").GetData()); renWin->SetSize(300, 300); // Enable user interface interactor. iren->Initialize(); renWin->Render(); iren->Start(); return EXIT_SUCCESS; }
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.3 FATAL_ERROR) project(Cube1) find_package(VTK COMPONENTS vtkCommonColor vtkCommonCore vtkFiltersSources vtkInteractionStyle vtkRenderingCore vtkRenderingFreeType vtkRenderingOpenGL2 QUIET) if (NOT VTK_FOUND) message("Skipping Cube1: ${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(Cube1 MACOSX_BUNDLE Cube1.cxx ) target_link_libraries(Cube1 PRIVATE ${VTK_LIBRARIES}) else () # include all components add_executable(Cube1 MACOSX_BUNDLE Cube1.cxx ) target_link_libraries(Cube1 PRIVATE ${VTK_LIBRARIES}) # vtk_module_autoinit is needed vtk_module_autoinit( TARGETS Cube1 MODULES ${VTK_LIBRARIES} ) endif ()
Download and Build Cube1¶
Click here to download Cube1 and its CMakeLists.txt file. Once the tarball Cube1.tar has been downloaded and extracted,
cd Cube1/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:
./Cube1
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.