我想在C ++中使用vtkImageData渲染一个简单的图像。我知道vtkImageDataGeometryFilter可以完成这项工作,但我问是否有其他方法可以在不使用PolyData的情况下获得相同的结果?这是我的代码,但我在渲染时有访问违规读取,我不知道为什么......
vtkSmartPointer<vtkImageData> imageData =
vtkSmartPointer<vtkImageData>::New();
imageData->SetExtent(0, 5, 0, 5, 0, 1);
imageData->SetSpacing(1, 1, 1);
imageData->SetOrigin(0, 0, 0);
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
imageData->SetScalarComponentFromFloat(i,j,0,0,0);
}
}
vtkSmartPointer<vtkImageActor> imageActor =
vtkSmartPointer<vtkImageActor>::New();
imageActor->SetInputData(imageData);
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->AddActor(imageActor);
renderWindow->Render();
renderWindowInteractor->Start();
先谢谢你的帮助^^
答案 0 :(得分:2)
我终于找到了。线&#34; imageData-&gt; AllocateScalars(VTK_FLOAT,3);&#34;失踪。以下代码显示绿色方块:
vtkSmartPointer<vtkImageData> imageData =
vtkSmartPointer<vtkImageData>::New();
imageData->SetExtent(0, 50, 0, 50, 0, 1);
imageData->SetSpacing(1, 1, 1);
imageData->SetOrigin(0, 0, 0);
imageData->AllocateScalars(VTK_FLOAT,3);
for (int i = 0; i < 50; i++)
{
for (int j = 0; j < 50; j++)
{
imageData->SetScalarComponentFromFloat(i,j,0,1,255);
}
}
vtkSmartPointer<vtkImageActor> imageActor =
vtkSmartPointer<vtkImageActor>::New();
imageActor->SetInputData(imageData);
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->AddActor(imageActor);
renderer->SetBackground(1, 0, 0.5);
renderWindow->Render();
renderWindowInteractor->Start();