I’m a newer in vtk. I know .vtu file can rendered with volume and I read the official example https://kitware.github.io/vtk-examples/site/Cxx/VolumeRendering/IntermixedUnstructuredGrid/
and I make some codes, but it always has an error “vtkUnstructuredGridVolumeRayCastMapper (0x13ef06f60): Can’t use the ray cast mapper without scalars!” when run it. Can anyone tell me how to change my code to make the .vtu file show?
my codes is
vtkNew<vtkXMLUnstructuredGridReader> reader;
reader->SetFileName(inputFilename.c_str());
vtkNew<vtkPCellDataToPointData> c2p;
// c2p->AddInputConnection(reader->GetOutputPort());
c2p->SetInputConnection(reader->GetOutputPort());
c2p->Update();
vtkNew<vtkDataSetTriangleFilter> triangleFilter;
triangleFilter->TetrahedraOnlyOn();
triangleFilter->SetInputConnection(c2p->GetOutputPort());
// Create transfer mapping scalar value to opacity
vtkNew<vtkPiecewiseFunction> opacityTransferFunction;
opacityTransferFunction->AddPoint(80, 0.0);
opacityTransferFunction->AddPoint(120, 0.2);
opacityTransferFunction->AddPoint(255, 0.2);
// Create transfer mapping scalar value to color
vtkNew<vtkColorTransferFunction> colorTransferFunction;
colorTransferFunction->AddRGBPoint(80.0, 0.0, 0.0, 0.0);
colorTransferFunction->AddRGBPoint(120.0, 0.0, 0.0, 1.0);
colorTransferFunction->AddRGBPoint(160.0, 1.0, 0.0, 0.0);
colorTransferFunction->AddRGBPoint(200.0, 0.0, 1.0, 0.0);
colorTransferFunction->AddRGBPoint(255.0, 0.0, 1.0, 1.0);
// The property describes how the data will look
vtkNew<vtkVolumeProperty> volumeProperty;
volumeProperty->SetColor(colorTransferFunction);
volumeProperty->SetScalarOpacity(opacityTransferFunction);
volumeProperty->ShadeOn();
volumeProperty->SetInterpolationTypeToLinear();
vtkNew<vtkUnstructuredGridBunykRayCastFunction> compositeFunction;
// The mapper / ray cast function know how to render the data
vtkNew<vtkUnstructuredGridVolumeRayCastMapper> volumeMapper;
volumeMapper->SetRayCastFunction(compositeFunction);
volumeMapper->SetInputConnection(triangleFilter->GetOutputPort());
// volumeMapper->SetInputConnection(reader->GetOutputPort());
volumeMapper->Update();
vtkNew<vtkRenderWindow> renWin;
renWin->SetSize(640, 512);
vtkNew<vtkRenderer> ren1;
// contour the second dataset
// vtkNew<vtkContourFilter> contour;
// contour->SetValue(0, 80);
// contour->SetInputConnection(reader2->GetOutputPort());
//
// // create a mapper for the polygonal data
// vtkNew<vtkPolyDataMapper> mapper;
// mapper->SetInputConnection(contour->GetOutputPort());
// mapper->ScalarVisibilityOff();
//
// // create an actor for the polygonal data
// vtkNew<vtkActor> actor;
// actor->SetMapper(mapper);
//
// ren1->AddViewProp(actor);
ren1->SetBackground(0.1, 0.4, 0.2);
renWin->AddRenderer(ren1);
renWin->SetWindowName("IntermixedUnstructuredGrid");
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(renWin);
vtkNew<vtkVolume> volume;
volume->SetMapper(volumeMapper);
volume->SetProperty(volumeProperty);
// volume->SetVisibility(false);
ren1->AddVolume(volume);
ren1->ResetCamera();
ren1->GetActiveCamera()->Zoom(1.5);
// Render composite. In default mode. For coverage.
renWin->Render();
iren->Start();
and my .vtu file is