I wanna have reflect CellData colors to the object,but the result is platelike,disperse, and does not have color changes,the vtkScalarBarActor also has this problem.vtkPolyData *data;
vtkSmartPointer<vtkPolyData> polyData;
//vtkUnstructuredGrid* unstructuredGrid;
vtkUnstructuredGrid* unstructuredGrid;
//vtkUnstructuredGrid *
QFileInfo info(fname);
bool knownFileType = true;
if (info.suffix() == QString(tr("vtk")))
{
data = readAnPolyData<vtkPolyDataReader >(fname.toStdString().c_str());
vtkSmartPointer<vtkUnstructuredGridReader> reader = vtkSmartPointer<vtkUnstructuredGridReader>::New();
reader->SetFileName(fname.toStdString().c_str());
reader->Update();
unstructuredGrid = vtkUnstructuredGrid::SafeDownCast(reader->GetOutput());
if (unstructuredGrid)
{
// 获取标量范围
double scalarRange[2];
unstructuredGrid->GetScalarRange(scalarRange); // 获取数据的标量范围
std::cout << "Scalar Range: " << scalarRange[0] << ", " << scalarRange[1] << std::endl;
// 在 unstructuredGrid 上设置活动标量数组
unstructuredGrid->GetCellData()->SetActiveScalars("p");
vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New();
mapper->SetInputData(unstructuredGrid); // 使用 unstructuredGrid 作为输入
// 创建查找表 (lookup table)
vtkSmartPointer<vtkLookupTable> lut = vtkSmartPointer<vtkLookupTable>::New();
lut->SetNumberOfTableValues(256); // 设置查找表的颜色数
lut->SetRange(scalarRange); // 设置查找表的范围为标量范围
//lut->SetHueRange(0.667,1.0);
lut->Build();
// 配置 mapper 的查找表
mapper->SetLookupTable(lut);
mapper->SetScalarRange(scalarRange);
mapper->SetScalarModeToUseCellData(); // 使用单元格数据进行标量映射
// 创建 actor 并设置 mapper
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
// 创建 scalar bar (标量条)
vtkSmartPointer<vtkScalarBarActor> scalarBarActor = vtkSmartPointer<vtkScalarBarActor>::New();
scalarBarActor->SetLookupTable(mapper->GetLookupTable()); // 同步 mapper 的 lookupTable
scalarBarActor->SetTitle("Scalar Values"); // 图例标题
scalarBarActor->SetNumberOfLabels(4); // 设置显示的刻度标签数
scalarBarActor->SetMaximumNumberOfColors(256); // 确保显示多个颜色
// 添加 actor 和 scalar bar 到渲染器
ren->AddActor(actor);
ren->AddActor(scalarBarActor);
// 重置相机并刷新渲染
ren->ResetCamera();
ren->ResetCameraClippingRange();
ren->Render();
this->openGLWidget->renderWindow()->Render();