Wei Ye
Wei Ye

Reputation: 131

Why my VTK display nothing?

I used VTK for several years, And I found a problem today that is very strange, the code is as follows:

#include <vtkSmartPointer.h>
#include <vtkImageData.h>

#include "vtkBoxWidget.h"
#include "vtkCamera.h"
#include "vtkCommand.h"
#include "vtkColorTransferFunction.h"
#include "vtkPiecewiseFunction.h"
#include "vtkPlanes.h"
#include "vtkProperty.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkVolume.h"
#include "vtkVolumeProperty.h"
#include "vtkFixedPointVolumeRayCastMapper.h"
#include "vtkInteractorStyleTrackballCamera.h"

#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle)

#include <limits>
#include <string>

int main(int argc, char *argv[])
{
/************************************************************************/
/* Generate a RGBA volume                                               */
/************************************************************************/
vtkSmartPointer<vtkImageData> image = vtkSmartPointer<vtkImageData>::New();
int dim[3] = { 10, 10, 10 };
double spacing[3] = { 1.0, 1.0, 1.0 };
double origin[3] = { 0.0, 0.0, 0.0 };
image->SetDimensions(dim);
image->SetSpacing(spacing);
image->SetOrigin(origin);
image->AllocateScalars(VTK_UNSIGNED_CHAR, 4);

unsigned char* ptr = static_cast<unsigned char*>(image->GetScalarPointer());
int idx = 0;
for (int z = 0; z < 10; z++){
    for (int y = 0; y < 10; y++){
        for (int x = 0; x < 10; x++){
            if (z > 6){
                ptr[idx] = 255;
                ptr[idx + 1] = 0;
                ptr[idx + 2] = 0;
                ptr[idx + 3] = 255;
            }
            else{
                ptr[idx] = 0;
                ptr[idx + 1] = 0;
                ptr[idx + 2] = 0;
                ptr[idx + 3] = 0;
            }

            idx += 4;
        }
    }
}

/************************************************************************/
/* Rendering                                                            */
/************************************************************************/
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
renWin->AddRenderer(renderer);

vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
iren->SetRenderWindow(renWin);
iren->SetDesiredUpdateRate(10.0);
iren->GetInteractorStyle()->SetDefaultRenderer(renderer);

// Create our volume and mapper
vtkSmartPointer<vtkVolume> volume = vtkSmartPointer<vtkVolume>::New();
vtkSmartPointer<vtkFixedPointVolumeRayCastMapper> mapper = vtkSmartPointer<vtkFixedPointVolumeRayCastMapper>::New();
mapper->SetInputData(image);
mapper->SetBlendModeToComposite();
mapper->SetSampleDistance(0.1);

vtkSmartPointer<vtkPiecewiseFunction> opacityFun = vtkSmartPointer<vtkPiecewiseFunction>::New();
opacityFun->AddPoint(0, 0.0);
opacityFun->AddPoint(255, 1.0);

// Create the property and attach the transfer functions
vtkSmartPointer<vtkVolumeProperty> property = vtkSmartPointer<vtkVolumeProperty>::New();
property->SetScalarOpacity(opacityFun);
property->IndependentComponentsOff();
//property->SetInterpolationTypeToLinear();

// connect up the volume to the property and the mapper
volume->SetProperty(property);
volume->SetMapper(mapper);

// Set the default window size
renWin->SetSize(400, 400);
renWin->Render();

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

// interact with data
renWin->Render();
iren->Start();

return 1;
}

I am generate a 10*10*10 RGBA volume and display it, but I get nothing in my screen. In line 46, if I change (z > 6) to (z < 2), it display correct box as I expected, also if I change (z > 6) to (z > 6 || z < 2), it also display 2 correct boxes.

Then why (z > 6) display nothing? what should I do to make it run correctly?

Upvotes: 0

Views: 611

Answers (1)

mululu
mululu

Reputation: 411

This look like a bug to me. If you set opacity to 1 instead of 0 for z<=6, you can circumvent your problem, that only seems to occur when opacity is equal to 0.

Upvotes: 1

Related Questions