OpenVRVolume
Repository source: OpenVRVolume
Description¶
This example renders a simple artificial 10x10x10 volume in OpenVR.
Note that for the interactions to work in VTK >= 9.1, you will have to copy the .json
manifest files from the subfolder Rendering/OpenVR in VTK's source to the working directory in which the tests are run!
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
OpenVRVolume.cxx
#include <vtkActor.h>
#include <vtkColorSeries.h>
#include <vtkColorTransferFunction.h>
#include <vtkImageData.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkOpenVRRenderWindow.h>
#include <vtkOpenVRRenderWindowInteractor.h>
#include <vtkOpenVRRenderer.h>
#include <vtkPiecewiseFunction.h>
#include <vtkSmartVolumeMapper.h>
#include <vtkVolumeProperty.h>
int main(int argc, char* argv[])
{
int dim[3] = {10, 10, 10};
double spc[3] = {0.05, 0.05, 0.05};
vtkNew<vtkImageData> img;
img->SetDimensions(dim);
img->AllocateScalars(VTK_INT, 1);
img->SetSpacing(spc);
for (int x = 0; x < dim[0]; ++x)
for (int y = 0; y < dim[1]; ++y)
for (int z = 0; z < dim[2]; ++z)
{
img->SetScalarComponentFromDouble(x, y, z, 0, x);
}
vtkNew<vtkColorSeries> colors;
colors->SetColorScheme(vtkColorSeries::BREWER_QUALITATIVE_SET3);
vtkNew<vtkColorTransferFunction> ctf;
for (int x = 0; x < dim[0]; ++x)
{
auto c = colors->GetColor(x);
ctf->AddRGBPoint(x, c.GetRed() / 255.0, c.GetGreen() / 255.0,
c.GetBlue() / 255.0);
}
ctf->AddRGBPoint(dim[0], 1.0, 1.0, 1.0);
ctf->Build();
vtkNew<vtkSmartVolumeMapper> volMapper;
vtkNew<vtkVolume> volume;
vtkNew<vtkVolumeProperty> volProp;
volMapper->SetBlendModeToComposite();
volume->SetMapper(volMapper);
volume->SetProperty(volProp);
volume->SetVisibility(true);
volMapper->SetInputData(img);
volProp->SetColor(0, ctf);
vtkNew<vtkPiecewiseFunction> otf;
otf->AddPoint(0.0, 1.0);
otf->AddPoint(dim[0], 1.0);
volProp->SetScalarOpacity(0, otf);
volProp->Modified();
volProp->SetScalarOpacityUnitDistance(1);
vtkNew<vtkOpenVRRenderWindow> renderWindow;
renderWindow->Initialize();
vtkNew<vtkOpenVRRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
vtkNew<vtkOpenVRRenderer> renderer;
renderWindow->AddRenderer(renderer);
vtkNew<vtkNamedColors> namedColors;
renderer->SetBackground(namedColors->GetColor3d("ForestGreen").GetData());
renderer->AddVolume(volume);
volume->SetPosition(0, 0.5, 0);
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(OpenVRVolume)
find_package(VTK COMPONENTS
CommonColor
CommonCore
CommonDataModel
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
RenderingOpenVR
RenderingVolumeOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "OpenVRVolume: 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(OpenVRVolume MACOSX_BUNDLE OpenVRVolume.cxx )
target_link_libraries(OpenVRVolume PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS OpenVRVolume
MODULES ${VTK_LIBRARIES}
)
Download and Build OpenVRVolume¶
Click here to download OpenVRVolume and its CMakeLists.txt file. Once the tarball OpenVRVolume.tar has been downloaded and extracted,
cd OpenVRVolume/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:
./OpenVRVolume
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.