You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VTK技术问询:如何实现vtkGlyph3D中单个Glyph三角形的独立着色?

How to Achieve Per-Triangle Coloring for Individual Glyphs in VTK

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 input polydata (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 your PieSource.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 14:07:46