Visualization failed when display two polygon in the same time

I want to display two polygon in one renderer. And the result is:

image

The green and red color indicates two polygons.

However, when I rotate it, some part of green color disappear:

image

image

image

My environment is:

win 10
python 3.7.13
vtk: 9.2.4

I can 100% reproduce this phenomenon, and the code to reproduce my problem is:

import vtkmodules.all as vtk


def buildPolygon(points):
    polydata = vtk.vtkPolyData()

    vps = vtk.vtkPoints()
    polygon = vtk.vtkPolygon()
    polygon.GetPointIds().SetNumberOfIds(len(points))

    for i in range(len(points)):
        vps.InsertNextPoint(points[i][0], points[i][1], points[i][2])
        polygon.GetPointIds().SetId(i, i)

    polygons = vtk.vtkCellArray()
    polygons.InsertNextCell(polygon)
    polydata.SetPoints(vps)
    polydata.SetPolys(polygons)
    return polydata

polydata1 = buildPolygon([
    [0, 0, 0],
    [10, 0, 0],
    [10, 10, 0],
    [0, 10, 0]
])
map1 = vtk.vtkPolyDataMapper()
map1.SetInputData(polydata1)
actor1 = vtk.vtkActor()
actor1.SetMapper(map1)
actor1.GetProperty().SetColor(1, 0, 0)

polydata2 = buildPolygon([
    [0, 0, 0],
    [5, 0, 0],
    [5, 5, 0],
    [0, 5, 0]
])
map2 = vtk.vtkPolyDataMapper()
map2.SetInputData(polydata2)
actor2 = vtk.vtkActor()
actor2.SetMapper(map2)
actor2.GetProperty().SetColor(0, 1, 0)

render = vtk.vtkRenderer()
render.AddActor(actor1)
render.AddActor(actor2)

renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(render)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
iren.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
iren.Initialize()
iren.Start()

That is called z-fighting. It happens in graphics when polygons overlap and have exactly coincident planes. In your case both polygons are on the z=0 plane thus there is no “right” way for openGL to decide which is in front of the other and the color of the pixels are thus indeterminant.

1 Like

@Dave_DeMarle Thanks for kindly reply, and I can understand the reason of this bug.

But, if I want to show two polygons, how can I correctly visualize it? Can I manually set the render order for the two polygons?

Try using vtkMapper::SetResolveCoincidentTopologyToPolygonOffset()

I have tried:

  1. map1.SetResolveCoincidentTopologyToPolygonOffset(), map2.SetResolveCoincidentTopologyToPolygonOffset()

  2. map1.SetResolveCoincidentTopologyToShiftZBuffer(), map2.SetResolveCoincidentTopologyToShiftZBuffer()

And they don’t work.

There are several VTK tests (both Python and C++) that use these methods. Make sure they are passing for you (AFAIK the VTK testing dashboards are green relative to these tests). If not, let us know. Otherwise see why they work and what you are doing does not work.

I find only one example using this method. Could you please recommand some other examples?

tests as in VTK///Testing/Python/.py and VTK///Testing/Cxx/.cxx. It’s easiest to build VTK with testing enabled to run these tests.

@will.schroeder Thanks for your suggestion, and I have fixed this bug by:

map2.SetResolveCoincidentTopologyToPolygonOffset()
map2.SetResolveCoincidentTopologyPolygonOffsetParameters(0, 60000)

The SetRelativeCoincidentTopologyPolygonOffsetParameters method has two parameters: double factor, double units, how to understand this factor and units?

Actually, in map2.SetResolveCoincidentTopologyPolygonOffsetParameters(0, 60000), I don’t know why 0, 60000 works. Actually, 0, 6000 don’t work, and 1, 60000 work. I don’t know how does this factor and units affect the visualization result.

Could you please give some explanation about this two parameters? Or could you please recommand some information to understand these two parameters?

See documentation here: glPolygonOffset - OpenGL 4 Reference Pages

1 Like