[vtkusers] Is a true colour volume possible?

Flitton, Greg g.flitton at Cranfield.ac.uk
Sat Jun 16 11:33:58 EDT 2007


Hi all,

Does anyone know if it is possible to create a true colour volume using VTK? Is there a simple example which can demonstrate this or am I misguided?

I have been trying to create a simple example but have failed - my latest code is at the end of this email.

Ultimately I want to read in a series of colour BMP files, assign a opacity value to each pixel/voxel and render the resulting volume.

I've searched the archive and noticed this question has been asked before, but I cannot find a definitive example that works.

If anyone can help either by spotting where I'm going wrong, or by pointing me in the right direction, I would be very grateful.

Have fun,
   Greg.

Here's my latest attempt:

void MyThirteenthGo(void) // Try to manually create a colour volume...
{
    int Nx = 10; // image will be 10 x 5 x 3
    int Ny = 5;
    int Nz = 3;
    int RGBA = 4; // each point has 4 components: R,G,B,A
    int NumPoints = Nx * Ny * Nz * RGBA;

    // Create some test data
    unsigned char *pRaw = new unsigned char [NumPoints];
    for (int i = 0; i < NumPoints; i++)
        pRaw[i] = i; // test 'colour'

    // create a vtk array for the data
    vtkUnsignedCharArray *pArray = vtkUnsignedCharArray::New();
    pArray->SetArray(pRaw, NumPoints,1);
    delete [] pRaw;

    // create an image
    vtkImageData *pImage = vtkImageData::New();
    pImage->GetPointData()->SetScalars(pArray); // plumb in the data

    pImage->SetDimensions(Nx, Ny, Nz);
    pImage->SetScalarTypeToUnsignedChar();
    pImage->SetNumberOfScalarComponents(4); // 4 = RGBA?
    pImage->SetSpacing(1.0, 1.0, 1.0);
    pImage->SetOrigin(0.0, 0.0, 0.0);

    // Now try to display this image...
    vtkRenderer *aRenderer = vtkRenderer::New();
    vtkRenderWindow *renWin = vtkRenderWindow::New();

    renWin->AddRenderer(aRenderer);
    vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
    iren->SetRenderWindow(renWin);

    vtkPiecewiseFunction *opacityTransferFunction =
	vtkPiecewiseFunction::New();
    opacityTransferFunction->AddPoint(0,1.0);
    opacityTransferFunction->AddPoint(255,0.03);

    vtkVolumeProperty *volumeProperty = vtkVolumeProperty::New();
    volumeProperty->SetScalarOpacity(opacityTransferFunction);
    volumeProperty->IndependentComponentsOn();	

    vtkFixedPointVolumeRayCastMapper *VolumeMapper = vtkFixedPointVolumeRayCastMapper::New();
    VolumeMapper->SetInput(pImage);

    vtkVolume *volume = vtkVolume::New();
    volume->SetMapper(VolumeMapper);
    volume->SetProperty(volumeProperty);

    vtkCamera *aCamera = vtkCamera::New();
    aCamera->SetViewUp (0, 0,-1);
    aCamera->SetPosition (0, 1, 4);
    aCamera->SetFocalPoint (0, 0, 0);
    aCamera->ComputeViewPlaneNormal();

    aRenderer->AddActor(volume);
    aRenderer->SetActiveCamera(aCamera);
    aRenderer->ResetCamera ();
    aRenderer->SetBackground(0,0,1);
    renWin->Render();

    iren->Initialize();
    iren->Start();

    iren->Delete();
    renWin->Delete();
    aRenderer->Delete();
    aCamera->Delete();
    volume->Delete();
    VolumeMapper->Delete();
    volumeProperty->Delete();
    opacityTransferFunction->Delete();
}








More information about the vtkusers mailing list