# Visualization failed when display two polygon in the same time

**URL:** https://discourse.vtk.org/t/visualization-failed-when-display-two-polygon-in-the-same-time/10397
**Category:** Support
**Created:** [January 6, 2023, 9:11am UTC](https://discourse.vtk.org/t/visualization-failed-when-display-two-polygon-in-the-same-time/10397 "2023-01-06T09:11:29Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [January 6, 2023, 9:11am UTC](https://discourse.vtk.org/t/visualization-failed-when-display-two-polygon-in-the-same-time/10397/1 "2023-01-06T09:11:29Z")

</div>

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

![image](https://discourse.vtk.org/uploads/default/original/2X/c/cafaed67d5360c00a5d6afa4501bfd01823884e0.png)

The green and red color indicates two polygons.

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

![image](https://discourse.vtk.org/uploads/default/original/2X/b/bbba9770966866a1ab9826ecf08d65c7c80dc1ed.png)

![image](https://discourse.vtk.org/uploads/default/original/2X/4/4c06051824f436f83a4d07538a0f21ad246a93bf.png)

![image](https://discourse.vtk.org/uploads/default/original/2X/e/e5ac549d0746b8ee36109019873d5b9a51802abb.png)

My environment is:

```auto
win 10
python 3.7.13
vtk: 9.2.4

```

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

```python
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()

```

---

<div class="post-metadata">

### Author: ![Dave\_DeMarle](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dave_demarle/32/6_2.png) [@Dave\_DeMarle](https://discourse.vtk.org/u/Dave_DeMarle)
#### Post date: [January 7, 2023, 6:33pm UTC](https://discourse.vtk.org/t/visualization-failed-when-display-two-polygon-in-the-same-time/10397/2 "2023-01-07T18:33:54Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [January 8, 2023, 3:09am UTC](https://discourse.vtk.org/t/visualization-failed-when-display-two-polygon-in-the-same-time/10397/3 "2023-01-08T03:09:36Z")

</div>

@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?

---

<div class="post-metadata">

### Author: ![will.schroeder](https://discourse.vtk.org/user_avatar/discourse.vtk.org/will.schroeder/32/233_2.png) [@will.schroeder](https://discourse.vtk.org/u/will.schroeder)
#### Post date: [January 8, 2023, 12:19pm UTC](https://discourse.vtk.org/t/visualization-failed-when-display-two-polygon-in-the-same-time/10397/4 "2023-01-08T12:19:37Z")

</div>

Try using vtkMapper::SetResolveCoincidentTopologyToPolygonOffset()

---

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [January 8, 2023, 12:42pm UTC](https://discourse.vtk.org/t/visualization-failed-when-display-two-polygon-in-the-same-time/10397/5 "2023-01-08T12:42:54Z")

</div>

> [@will.schroeder](#):
>
> Try using vtkMapper::SetResolveCoincidentTopologyToPolygonOffset()

I have tried:

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

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

And they don’t work.

---

<div class="post-metadata">

### Author: ![will.schroeder](https://discourse.vtk.org/user_avatar/discourse.vtk.org/will.schroeder/32/233_2.png) [@will.schroeder](https://discourse.vtk.org/u/will.schroeder)
#### Post date: [January 8, 2023, 12:53pm UTC](https://discourse.vtk.org/t/visualization-failed-when-display-two-polygon-in-the-same-time/10397/6 "2023-01-08T12:53:10Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [January 8, 2023, 1:10pm UTC](https://discourse.vtk.org/t/visualization-failed-when-display-two-polygon-in-the-same-time/10397/7 "2023-01-08T13:10:16Z")

</div>

> [@will.schroeder](#):
>
> There are several VTK tests (both Python and C++) that use these methods.

I find only one [example](https://gitlab.kitware.com/lorensen/vtk/-/blob/master/Examples/LIC/Python/CylinderAndPolarAxes.py#L15) using this method. Could you please recommand some other examples?

---

<div class="post-metadata">

### Author: ![will.schroeder](https://discourse.vtk.org/user_avatar/discourse.vtk.org/will.schroeder/32/233_2.png) [@will.schroeder](https://discourse.vtk.org/u/will.schroeder)
#### Post date: [January 8, 2023, 1:13pm UTC](https://discourse.vtk.org/t/visualization-failed-when-display-two-polygon-in-the-same-time/10397/8 "2023-01-08T13:13:28Z")

</div>

_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.

---

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [January 8, 2023, 1:26pm UTC](https://discourse.vtk.org/t/visualization-failed-when-display-two-polygon-in-the-same-time/10397/9 "2023-01-08T13:26:10Z")

</div>

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

```auto
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?

---

<div class="post-metadata">

### Author: ![ahernsean](https://discourse.vtk.org/user_avatar/discourse.vtk.org/ahernsean/32/5874_2.png) [@ahernsean](https://discourse.vtk.org/u/ahernsean)
#### Post date: [January 12, 2023, 1:55pm UTC](https://discourse.vtk.org/t/visualization-failed-when-display-two-polygon-in-the-same-time/10397/10 "2023-01-12T13:55:42Z")

</div>

> [@zhang-qiang-github](#):
>
> SetResolveCoincidentTopologyPolygonOffsetParameters

See documentation here: [glPolygonOffset - OpenGL 4 Reference Pages](https://registry.khronos.org/OpenGL-Refpages/gl4/html/glPolygonOffset.xhtml)
