# Smooth (Gouraud) shading disappears in 8.2

**URL:** https://discourse.vtk.org/t/smooth-gouraud-shading-disappears-in-8-2/2017
**Category:** Support
**Created:** [October 31, 2019, 4:02pm UTC](https://discourse.vtk.org/t/smooth-gouraud-shading-disappears-in-8-2/2017 "2019-10-31T16:02:06Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![Jellby](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jellby/32/135_2.png) [@Jellby](https://discourse.vtk.org/u/Jellby)
#### Post date: [October 31, 2019, 4:02pm UTC](https://discourse.vtk.org/t/smooth-gouraud-shading-disappears-in-8-2/2017/1 "2019-10-31T16:02:06Z")

</div>

I have an application working fine with python and VTK 8.1.2 ([https://gitlab.com/Jellby/Pegamoid](https://gitlab.com/Jellby/Pegamoid)).

Now I wanted to try something else (TTK), and I installed a new conda environment with vtk 8.2. The application still works, but something happens with the normal interpolation applied to a contour filter. Sometimes it works as intended (Gouraud), sometimes it shows as Flat, and at least in some cases it is dependent on the isovalue level set for the contour. Example:

 ![Screenshot_20191031_164555](https://discourse.vtk.org/uploads/default/original/2X/3/3226a1dcc59da46e414b13f89faae4308dd896b0.png)

 ![Screenshot_20191031_164643](https://discourse.vtk.org/uploads/default/original/2X/3/3d33256e29570de809ec37682abeb6d89f96d59e.png)

The only thing that changes is the “Value” slider: anything smaller than 5.3e-5 shows as Flat, anything larger than 5.57e-5 shows as Gouraud. For different objects the threshold is somewhere else.

Is this some kind of new parameter or feature in 8.2? Or is it a bug in 8.2, in my conda installation, or my application?

(By the way, even though I explicitly set “Gouraud”, I have the feeling what I actually see is Phong, see [Is there any difference between Gouraud and Phong?](https://discourse.vtk.org/t/is-there-any-difference-between-gouraud-and-phong/801))

---

<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: [November 1, 2019, 2:15pm UTC](https://discourse.vtk.org/t/smooth-gouraud-shading-disappears-in-8-2/2017/2 "2019-11-01T14:15:43Z")

</div>

Sounds like a bug, but I have no clear idea where in the software stack this might be. You might try exporting the suspect mesh in both good and bad forms and coming up with a minimal vtk level (cxx or python doesn’t matter) program the exhibits the difference. This would isolate the problem enough so that a bug report on the issue tracker would be tractable.

hth

---

<div class="post-metadata">

### Author: ![Jellby](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jellby/32/135_2.png) [@Jellby](https://discourse.vtk.org/u/Jellby)
#### Post date: [November 1, 2019, 3:45pm UTC](https://discourse.vtk.org/t/smooth-gouraud-shading-disappears-in-8-2/2017/3 "2019-11-01T15:45:11Z")

</div>

Maybe someone can double-check, before I create a bug report, in case I’m doing something wrong or stupid. The script is very simple, and the “orb4.vti” file can be found (temporarily) at [https://filebin.net/sns8xwp3h8k20cff/orb4.vti](https://filebin.net/sns8xwp3h8k20cff/orb4.vti) The problem seems like vtkAppendPolyData losing normals when it combines two sources. With value=5e-4 there’s only one source (pos is empty) and it looks smooth, with value=5e-5 there are two sources and it looks faceted, but if I comment out pos, it looks smooth again. With vtk 8.1.2 it always looks smooth.

```
#!/usr/bin/env python

import vtk

# create a rendering window and renderer
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)

# create a renderwindowinteractor
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

# read the data
reader = vtk.vtkXMLImageDataReader()
reader.SetFileName('orb4.vti')

# create contours
# try 5e-5, 5e-4
value = 5e-5
contour = vtk.vtkContourFilter()
contour.SetInputConnection(reader.GetOutputPort())
contour.SetNumberOfContours(2)
contour.SetValue(0, -value)
contour.SetValue(1, value)
# patchwork with positive and negative parts
pos = vtk.vtkClipPolyData()
pos.SetInputConnection(contour.GetOutputPort())
neg = vtk.vtkClipPolyData()
neg.InsideOutOn()
neg.SetInputConnection(contour.GetOutputPort())
rvneg = vtk.vtkReverseSense()
rvneg.SetInputConnection(neg.GetOutputPort())
rvneg.ReverseCellsOn()
rvneg.ReverseNormalsOn()
tot = vtk.vtkAppendPolyData()
tot.AddInputConnection(pos.GetOutputPort())
tot.AddInputConnection(rvneg.GetOutputPort())

# mapper and actor
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(tot.GetOutputPort())
actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetDiffuseColor(1,1,1)
actor.GetProperty().SetDiffuse(1)
actor.GetProperty().SetSpecular(1)
actor.GetProperty().SetSpecularPower(30.0)
actor.GetProperty().SetInterpolationToGouraud()
#actor.GetProperty().SetInterpolationToPhong()

# assign actor to the renderer
ren.AddActor(actor)

light = vtk.vtkLight()
light.SetPosition(10, 10, 10)
light.SetColor(1.0, 1.0, 1.0)
light.SetLightTypeToCameraLight()
ren.AddLight(light)

# enable user interface interactor
print(vtk.vtkVersion.GetVTKVersion())
iren.Initialize()
iren.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
renWin.Render()
iren.Start()
```

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [November 1, 2019, 5:48pm UTC](https://discourse.vtk.org/t/smooth-gouraud-shading-disappears-in-8-2/2017/4 "2019-11-01T17:48:05Z")

</div>

I don’t fully understand what’s going on with the two contours, turning them inside out, and merging them - but if I apply a vtkPolyDataNormal on the polydata before setting it in the mapper then it seems to work well (blobs appear smooth).

---

<div class="post-metadata">

### Author: ![Jellby](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jellby/32/135_2.png) [@Jellby](https://discourse.vtk.org/u/Jellby)
#### Post date: [November 1, 2019, 6:31pm UTC](https://discourse.vtk.org/t/smooth-gouraud-shading-disappears-in-8-2/2017/5 "2019-11-01T18:31:42Z")

</div>

What is it you don’t understand, the reason for doing it or the code under the hood? As for the reason, without that the wireframe representation looks inside out, as can be seen with:

```
mapper.SetInputConnection(contour.GetOutputPort())
actor.GetProperty().SetRepresentation(1)

```

And vtkPolyDataNormals helps (left), but it’s nowhere as good as the original (right), I guess because the original computes the normals from the volume data, while the filter can only compute the normals from the triangles in the contour. Or am I missing something?  
 ![Screenshot](https://discourse.vtk.org/uploads/default/original/2X/f/f98e4f3312a2a0ed5510f83e9d66374ae251c56e.png)

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [November 1, 2019, 7:21pm UTC](https://discourse.vtk.org/t/smooth-gouraud-shading-disappears-in-8-2/2017/6 "2019-11-01T19:21:13Z")

</div>

> [@Jellby](#):
>
> What is it you don’t understand, the reason for doing it or the code under the hood?

I just mean that the process of creating the polydata is not trivial and I did not have the time to understand well enough to assess if it might violate some of VTK’s assumptions. The fact that vtkPolyDataNormals restores smooth rendering indicates that the normal “got lost” at some point in the pipeline. By inspecting normal array at each filter output you should be able to find out which filter does not behave well.

To improve performance, default precision was changed in some filters from double to single. There is a slight chance that if you force filters to use double precision then it will work better.

---

<div class="post-metadata">

### Author: ![Jellby](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jellby/32/135_2.png) [@Jellby](https://discourse.vtk.org/u/Jellby)
#### Post date: [November 1, 2019, 7:33pm UTC](https://discourse.vtk.org/t/smooth-gouraud-shading-disappears-in-8-2/2017/7 "2019-11-01T19:33:16Z")

</div>

> [@lassoan](#):
>
> There is a slight chance that if you force filters to use double precision then it will work better.

How would I do that?

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [November 1, 2019, 7:56pm UTC](https://discourse.vtk.org/t/smooth-gouraud-shading-disappears-in-8-2/2017/8 "2019-11-01T19:56:55Z")

</div>

Depends on the filter. First you need to find out which filter swallows the normal vectors.

---

<div class="post-metadata">

### Author: ![Jellby](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jellby/32/135_2.png) [@Jellby](https://discourse.vtk.org/u/Jellby)
#### Post date: [November 2, 2019, 8:10am UTC](https://discourse.vtk.org/t/smooth-gouraud-shading-disappears-in-8-2/2017/9 "2019-11-02T08:10:44Z")

</div>

OK, `vtkAppendPolyData` discards the normals, but that’s (probably) because `vtkReverseSense` discards the array name. In 8.1.2 the array it kept even if it has different name in the two inputs, in 8.2 it isn’t.

```
8.1.2
** pos
Values
Normals
** rvneg
Values
None
** tot
Values
None

8.2.0
** pos
Values
Normals
** rvneg
Values
None
** tot
Values

```

So… quick and dirty fix:

```
rvneg.Update()
for i in range(rvneg.GetOutput().GetPointData().GetNumberOfArrays()):
  rvneg.GetOutput().GetPointData().GetArray(i).SetName(neg.GetOutput().GetPointData().GetArrayName(i))
```

---

<div class="post-metadata">

### Author: ![Jellby](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jellby/32/135_2.png) [@Jellby](https://discourse.vtk.org/u/Jellby)
#### Post date: [November 2, 2019, 3:06pm UTC](https://discourse.vtk.org/t/smooth-gouraud-shading-disappears-in-8-2/2017/10 "2019-11-02T15:06:30Z")

</div>

Note that the above fix only works for static data, as soon as something changes upstream (e.g. the contour value), the `vtkReverseSense` filter recreates the normals array and removes its name again. Since I couldn’t find a way to rename arrays “on the fly”, I had to create a programmable filter:

```
fix_normals = vtk.vtkProgrammableFilter()
def rename_array():
  input = fix_normals.GetInput()
  output = fix_normals.GetOutput()
  output.ShallowCopy(input)
  output.GetPointData().GetArray(1).SetName('Normals')
fix_normals.SetExecuteMethod(rename_array)
...
#tot.AddInputConnection(rvneg.GetOutputPort())
fix_normals.SetInputConnection(rvneg.GetOutputPort())
tot.AddInputConnection(fix_normals.GetOutputPort())
```

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [November 2, 2019, 5:38pm UTC](https://discourse.vtk.org/t/smooth-gouraud-shading-disappears-in-8-2/2017/11 "2019-11-02T17:38:57Z")

</div>

Do you think something should be changed in VTK?

Append filter behavior looks reasonable to me. Maybe reverse sense filter should be changed to preserve array names?

---

<div class="post-metadata">

### Author: ![Jellby](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jellby/32/135_2.png) [@Jellby](https://discourse.vtk.org/u/Jellby)
#### Post date: [November 2, 2019, 6:41pm UTC](https://discourse.vtk.org/t/smooth-gouraud-shading-disappears-in-8-2/2017/12 "2019-11-02T18:41:45Z")

</div>

Yes, I definitely think `vtkReverseSense` should keep array names. I created an issue ([https://gitlab.kitware.com/vtk/vtk/issues/17715](https://gitlab.kitware.com/vtk/vtk/issues/17715))

---

<div class="post-metadata">

### Author: ![cory.quammen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/cory.quammen/32/6751_2.png) [@cory.quammen](https://discourse.vtk.org/u/cory.quammen)
#### Post date: [November 4, 2019, 4:26pm UTC](https://discourse.vtk.org/t/smooth-gouraud-shading-disappears-in-8-2/2017/13 "2019-11-04T16:26:30Z")

</div>

> [@Jellby](#):
>
> I created an issue ([https://gitlab.kitware.com/vtk/vtk/issues/17715](https://gitlab.kitware.com/vtk/vtk/issues/17715))

And [merge request](https://gitlab.kitware.com/vtk/vtk/merge_requests/6147) that has been merged to `master`, even better! Thank you, @Jellby!

---

<div class="post-metadata">

### Author: ![Jellby](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jellby/32/135_2.png) [@Jellby](https://discourse.vtk.org/u/Jellby)
#### Post date: [November 4, 2019, 4:33pm UTC](https://discourse.vtk.org/t/smooth-gouraud-shading-disappears-in-8-2/2017/14 "2019-11-04T16:33:30Z")

</div>

Now I’m a VTK author! I want a share in the profits! 😁 😛
