VTK技术问询:如何实现vtkGlyph3D中单个Glyph三角形的独立着色?
Absolutely, you can get individual triangles within a single glyph to render with their own colors—let’s break down what’s going wrong and how to fix it.
The Root Cause
Your current setup conflicts with how vtkGlyph3DMapper handles color data by default:
- When you don’t call
SelectColorArray, the mapper falls back to using color data from your inputpolydata(which you noted has grayish triangles), leading to uniform gray glyphs. - When you do call
SelectColorArray("Colors"), the mapper treats that array as a per-glyph color (applying one solid color to the entire glyph) instead of preserving the per-point color data from yourPieSource.
The Fix
To retain the per-point (and thus per-triangle) colors from your PieSource, you need to tell the mapper to use the source glyph’s own scalar data directly, rather than mapping colors from the input polydata. Adjust your code like this:
vtkNew<vtkPieSource> pieSource; pieSource->SetData(5, testData); vtkNew<vtkGlyph3DMapper> glyph3Dmapper; glyph3Dmapper->SetSourceConnection(pieSource->GetOutputPort()); glyph3Dmapper->SetInputData(polydata); // Critical: Tell the mapper to use the source glyph's own point scalars (colors) glyph3Dmapper->SetColorModeToDirectScalars(); // DO NOT call SelectColorArray("Colors")—this overrides per-point coloring
Verify Your PieSource Color Setup
Make sure your PieSource correctly assigns colors to every point (triangles inherit color from their vertices). In your RequestData method, add logic to populate the newCols array with per-point colors. For example, if each triangle should be a solid color, assign the same RGB value to all three points of the triangle:
// Inside your RequestData, after creating points // Assume you have a vector of RGB colors, one per triangle std::vector<std::array<double,3>> triangleColors = /* your color data */; for (uint i = 0; i < iNumTriangles; ++i) { int pointIdxBase = i * 3; // Get the color for this triangle auto& color = triangleColors[i]; // Assign the same color to all three points of the triangle newCols->InsertTuple3(pointIdxBase, color[0], color[1], color[2]); newCols->InsertTuple3(pointIdxBase + 1, color[0], color[1], color[2]); newCols->InsertTuple3(pointIdxBase + 2, color[0], color[1], color[2]); }
Why This Works
SetColorModeToDirectScalars() configures the mapper to use the scalar data (your "Colors" array) from the source glyph (PieSource) directly. This preserves the per-point color information, so each triangle in the glyph will render with the color you assigned to its vertices.
内容的提问来源于stack exchange,提问作者user1479670

