Hi,
I’m new to VTK. I’m trying to add multiple cubes/hexahedrons using a single actor and single renderer. The issue I’m running into is that if I have just one polygon in my list the hexahedron is rendered correctly. When I try to put this code in a loop, nothing gets rendered to the screen ?? I’m not sure what I’m missing. Please let me know what am I doing wrong. Any help/suggestions are greatly appreciated !
Thanks,
Ujjwala
My code snippet :-
vtkNew colors;
colors->SetName(“Colors”);
colors->SetNumberOfComponents(3);
// Create the points.
vtkNew<vtkPoints> points;
unsigned char cubecolor[3];
// create the unstructuredgrid to hold all the hexahedrons.
vtkUnstructuredGrid* theGrid = vtkUnstructuredGrid::New();
theGrid->Allocate(polygonList.size());
for (const polygon3DInfo & polyInfo : polygonList)
{
// set the colors for this particular cube.
cubecolor[0] = polyInfo.redColor;
cubecolor[1] = polyInfo.greenColor;
cubecolor[2] = polyInfo.blueColor;
colors -> InsertNextTypedTuple(cubecolor);
// For the hexahedron; setup the coordinates of eight points.
// The two faces must be in counter clockwise order as viewed from the outside.
points->InsertNextPoint( polyInfo.xmin, polyInfo.ymin, polyInfo.zmax); // Face 1
points->InsertNextPoint( polyInfo.xmin, polyInfo.ymin, polyInfo.zmin);
points->InsertNextPoint( polyInfo.xmax, polyInfo.ymin, polyInfo.zmin);
points->InsertNextPoint( polyInfo.xmax, polyInfo.ymin, polyInfo.zmax);
points->InsertNextPoint( polyInfo.xmin, polyInfo.ymax, polyInfo.zmax); // Face 2
points->InsertNextPoint( polyInfo.xmin, polyInfo.ymax, polyInfo.zmin);
points->InsertNextPoint( polyInfo.xmax, polyInfo.ymax, polyInfo.zmin);
points->InsertNextPoint( polyInfo.xmax, polyInfo.ymax, polyInfo.zmax);
// Create a hexahedron from the points.
vtkNew<vtkHexahedron> hex;
for (int i = 0; i < 8; ++i) {
hex->GetPointIds()->SetId(i, i);
}
vtkNew<vtkCellArray> cellArray;
cellArray->InsertNextCell(hex);
theGrid->InsertNextCell( hex->GetCellType(), hex->GetPointIds());
}
theGrid -> SetPoints(points);
theGrid -> GetCellData() -> SetScalars(colors);
theGrid -> GetCellData() -> SetActiveScalars("color");
// Visualize.
vtkNew<vtkDataSetMapper> mapper;
mapper->SetInputData(theGrid);
vtkNew<vtkActor> actor;
actor->SetMapper(mapper);
// Create a renderer, render window, and interactor
vtkNew<vtkGenericOpenGLRenderWindow> renderWindow;
this->ui->qvtkWidget->setRenderWindow(renderWindow);
vtkNew<vtkRenderer> renderer;
// Connect VTK with Qt
this->ui->qvtkWidget->renderWindow()->AddRenderer(renderer);
// Create interactor
(this->ui->qvtkWidget->interactor())->SetRenderWindow( renderWindow);
//create an actor
renderer->AddActor(actor);
// Set window background color
renderer->SetBackground(0.4, 0.4, 0.4);
// Render an image (lights and cameras are created automatically)
renderWindow->Render();