# How can I render a .vtu file with volume?

**URL:** <https://discourse.vtk.org/t/how-can-i-render-a-vtu-file-with-volume/8619>\
**Category:** Development\
**Created:** [May 27, 2022, 2:44pm UTC](https://discourse.vtk.org/t/how-can-i-render-a-vtu-file-with-volume/8619 "2022-05-27T14:44:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![andaji](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@andaji](https://discourse.vtk.org/u/andaji)\
**Post date:** [May 27, 2022, 2:44pm UTC](https://discourse.vtk.org/t/how-can-i-render-a-vtu-file-with-volume/8619/1 "2022-05-27T14:44:39Z")

</div>

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/](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

```auto
    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

---

<div class="post-metadata">

**Author:** ![andaji](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@andaji](https://discourse.vtk.org/u/andaji)\
**Post date:** [June 1, 2022, 2:12pm UTC](https://discourse.vtk.org/t/how-can-i-render-a-vtu-file-with-volume/8619/2 "2022-06-01T14:12:01Z")

</div>

Is there anybody can help me？Thanks very much！
