# How should we do volume rendering for this simple data file in VTK?

**URL:** https://discourse.vtk.org/t/how-should-we-do-volume-rendering-for-this-simple-data-file-in-vtk/292
**Category:** Support
**Created:** [February 19, 2019, 2:15am UTC](https://discourse.vtk.org/t/how-should-we-do-volume-rendering-for-this-simple-data-file-in-vtk/292 "2019-02-19T02:15:44Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![singularity](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/s/898d66/32.png) [@singularity](https://discourse.vtk.org/u/singularity)
#### Post date: [February 19, 2019, 2:15am UTC](https://discourse.vtk.org/t/how-should-we-do-volume-rendering-for-this-simple-data-file-in-vtk/292/1 "2019-02-19T02:15:44Z")

</div>

Hi. I am new to VTK and trying to write a simple code for volumetric rendering. I used examples from the VTK website to write this following code.

```
#include <vtkNamedColors.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkNew.h>
#include <vtkStructuredPointsReader.h>
#include <vtkPiecewiseFunction.h>
#include <vtkSmartVolumeMapper.h>
#include <vtkColorTransferFunction.h>
#include <vtkVolume.h>
#include <vtkVolumeProperty.h>

int main( int argc, char* argv[] )
{
 
  // Add named color library
  
  vtkNew<vtkNamedColors> colors ;

  // Create renderer

  vtkNew<vtkRenderer> renderer ;

  // Create a new render window

  vtkNew<vtkRenderWindow> renWin ;
  renWin->AddRenderer(renderer);

  // Make the render window interacting

  vtkNew<vtkRenderWindowInteractor> iren ;
  iren->SetRenderWindow(renWin);

  //Create a Structure Points or Image data reader and read the data

  vtkNew<vtkStructuredPointsReader> reader;
  reader->SetFileName (argv[1]);
  reader->Update();

  // For volume rendering, we need to first define a map from scalar values to 
  // colors and opacity (transparency) values. Then add these maps to volume 
  // property

  // Add a piece-wise function for color transfer functions. Piece-wise means 
  // adding control (interpolation) points.
  
  vtkNew<vtkPiecewiseFunction> opacityTransferFunction;
  opacityTransferFunction->AddPoint(0,0.0);
  opacityTransferFunction->AddPoint(50,0.0);

  // Piece-wise function cannot be used for colors because colors are vectors

  vtkNew<vtkColorTransferFunction> colorTransferFunction;
  colorTransferFunction->AddRGBPoint(0,0.0,0.0,1.0);
  colorTransferFunction->AddRGBPoint(25,1.0,0.0,0.0);
  colorTransferFunction->AddRGBPoint(50,1.0,1.0,1.0);

  // Set volume rendering properties
  
  vtkNew<vtkVolumeProperty> volumeProperty;
  volumeProperty->SetColor(colorTransferFunction);
  volumeProperty->SetScalarOpacity(opacityTransferFunction);
  volumeProperty->ShadeOn();
  volumeProperty->SetInterpolationTypeToLinear();

  // Add a mapper to create graphic primitives from the data

  vtkNew<vtkSmartVolumeMapper> mapper;
  mapper->SetBlendModeToComposite();
  mapper->SetInputConnection(reader->GetOutputPort());

  // Create a new actor(the actual graphics object) and add the mapped data to 
  // it
  
  vtkNew<vtkVolume> volume;
  volume->SetMapper(mapper);
  volume->SetProperty(volumeProperty);

  // Add the volume actor to the renderer
  renderer->AddVolume(volume);

  // Set the background color
  
  renderer->SetBackground(colors->GetColor3d("Black").GetData());

  // Set the size of the render window
  renWin->SetSize(512,512);

  // Render the data 
  renWin->Render();

  // Start the interactor
  iren->Start();

  return EXIT_SUCCESS;
}

```

I am using the following data file.

```
# vtk DataFile Version 3.0
First time trying vtk import \n
ASCII
DATASET STRUCTURED_POINTS
DIMENSIONS 3 4 6
ORIGIN 0 0 0
SPACING 1 1 1
POINT_DATA 72
SCALARS volume_scalars unsigned_char 1
LOOKUP_TABLE default
0 0 0 0 0 0 0 0 0 0 0 0
0 5 10 15 20 25 25 20 15 10 5 0
0 10 20 30 40 50 50 40 30 20 10 0
0 10 20 30 40 50 50 40 30 20 10 0
0 5 10 15 20 25 25 20 15 10 5 0
0 0 0 0 0 0 0 0 0 0 0 0

```

I tried using this file in paraview and it worked perfectly. I was able to produce a nicely rendered 3D object. But, with the above VTK code, I only get a black screen with no volumetric plot. I created a simple points plot using PolyDataMapper in VTK for the same data and that worked fine as well. I am not sure what to do here. Any help would be really appreciated. Best regards.

---

<div class="post-metadata">

### Author: ![Kenichiro-Yoshimi](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/k/ecc23a/32.png) [@Kenichiro-Yoshimi](https://discourse.vtk.org/u/Kenichiro-Yoshimi)
#### Post date: [February 21, 2019, 4:08am UTC](https://discourse.vtk.org/t/how-should-we-do-volume-rendering-for-this-simple-data-file-in-vtk/292/2 "2019-02-21T04:08:35Z")

</div>

As this line is setting the opacity of both points to 0.0, the volume becomes transparent.

```auto
  vtkNew<vtkPiecewiseFunction> opacityTransferFunction;
  opacityTransferFunction->AddPoint(0,0.0);
  opacityTransferFunction->AddPoint(50,0.0);

```

You should set opacity greater than 0.0. For example:

```auto
  opacityTransferFunction->AddPoint(0,0.0);
  opacityTransferFunction->AddPoint(50,1.0);

```

---

<div class="post-metadata">

### Author: ![singularity](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/s/898d66/32.png) [@singularity](https://discourse.vtk.org/u/singularity)
#### Post date: [February 22, 2019, 3:20am UTC](https://discourse.vtk.org/t/how-should-we-do-volume-rendering-for-this-simple-data-file-in-vtk/292/3 "2019-02-22T03:20:44Z")

</div>

Kenichiro, thank you for the help. This solved the problem for me.
