Cube1
Repository source: Cube1
Description¶
Display a cube.
A nice simple example that demonstrates the operation of the VTK pipeline.
Other languages
See (Python)
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
Cube1.cxx
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCubeSource.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.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 renderwindow interactor.
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);
renWin->SetWindowName("Cube1");
// Enable user interface interaction.
iren->Initialize();
renWin->Render();
iren->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(Cube1)
find_package(VTK COMPONENTS
CommonColor
CommonCore
FiltersSources
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "Cube1: 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(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}
)
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.