# Mesh plotting in GLMakie with GeometryBasics - are normals broken?

**URL:** <https://discourse.julialang.org/t/mesh-plotting-in-glmakie-with-geometrybasics-are-normals-broken/118227>\
**Category:** Visualization\
**Tags:** makie, glmakie\
**Created:** [August 15, 2024, 4:19pm UTC](https://discourse.julialang.org/t/mesh-plotting-in-glmakie-with-geometrybasics-are-normals-broken/118227 "2024-08-15T16:19:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Larbino1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/larbino1/32/37831_2.png) [@Larbino1](https://discourse.julialang.org/u/Larbino1)\
**Post date:** [August 15, 2024, 4:19pm UTC](https://discourse.julialang.org/t/mesh-plotting-in-glmakie-with-geometrybasics-are-normals-broken/118227/1 "2024-08-15T16:19:05Z")

</div>

I am trying to produce plots using meshes of basic geometry, and the resulting images are poor. I realised that it is very hard to tell the shape of the cylinder, no matter how I place the lighting, and I think it is because the shading is going wrong, possibly because of incorrect normals.

Take this simple example:

```julia
using GeometryBasics
using GLMakie

cyl = Cylinder(Point3f(0, 0, 0), Point3f(0, 0, 0.5), 0.3f0)
mesh1 = triangle_mesh(cyl)
mesh2 = normal_mesh(cyl)

fig = Figure()
ls1 = LScene(fig[1, 1]; show_axis=false)
ls2 = LScene(fig[1, 2]; show_axis=false)
grey = RGBAf(0.5, 0.5, 0.5, 1.0)
mesh!(ls1, mesh1; color=grey, specular=0)
mesh!(ls2, mesh2; color=grey, specular=0)

```

The resulting meshes show no change in shading on the edge of the cylinder: you can hardly tell they are cylinders.

![image](https://global.discourse-cdn.com/julialang/original/3X/0/3/0357c0d207ab8845817bb45f9b3ff49a562cc239.png)

For comparison look at this cylinder from [here](https://media.printables.com/media/prints/4853/stls/31374_3f9897ac-7b06-47b3-b5cd-079266fa76a7/thumbs/inside/1280x960/png/cylinder_preview.webp)

 ![cylinder_preview](https://global.discourse-cdn.com/julialang/original/3X/d/c/dc63be147f5b221aad46f37b6b5a52cc7c624f28.webp)

Does anyone have any idea what’s going wrong and where? Is it an issue with representing normals in `GeometryBasics.Mesh`? In conversion to an OpenGL mesh? I really would like to help fix this.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [August 15, 2024, 4:59pm UTC](https://discourse.julialang.org/t/mesh-plotting-in-glmakie-with-geometrybasics-are-normals-broken/118227/2 "2024-08-15T16:59:56Z")

</div>

I’d guess that when normals are determined automatically, the edge vertices get normals that are a mixture of the wall and the cap normals, which doesn’t make sense for a cylinder. That’s why with interpolation the edge looks round. Maybe it’s better to duplicate the vertices at the edge, so that the caps and the wall are not actually connected, to avoid the interpolation.

---

<div class="post-metadata">

**Author:** ![Larbino1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/larbino1/32/37831_2.png) [@Larbino1](https://discourse.julialang.org/u/Larbino1)\
**Post date:** [August 15, 2024, 5:11pm UTC](https://discourse.julialang.org/t/mesh-plotting-in-glmakie-with-geometrybasics-are-normals-broken/118227/3 "2024-08-15T17:11:18Z")

</div>

That sounds great - do you know a good way to achieve that with the `GeometryBasics` api, or should I jury rig something.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [August 15, 2024, 5:26pm UTC](https://discourse.julialang.org/t/mesh-plotting-in-glmakie-with-geometrybasics-are-normals-broken/118227/4 "2024-08-15T17:26:17Z")

</div>

I don’t know of a good way, maybe it’s easiest to write a custom function to create the mesh manually. A cylinder is at least simple enough to do so

---

<div class="post-metadata">

**Author:** ![ffreyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffreyer/32/21569_2.png) [@ffreyer](https://discourse.julialang.org/u/ffreyer)\
**Post date:** [August 15, 2024, 6:04pm UTC](https://discourse.julialang.org/t/mesh-plotting-in-glmakie-with-geometrybasics-are-normals-broken/118227/5 "2024-08-15T18:04:44Z")

</div>

You can copy the code from [Makie.jl/src/basic\_recipes/arrows.jl at master · MakieOrg/Makie.jl · GitHub](https://github.com/MakieOrg/Makie.jl/blob/master/src/basic_recipes/arrows.jl) with

```julia
quality = 16
cylinder = merge([
    _circle(Point3f(0,0,-1), 0.5f0, Vec3f(0,0,-1), quality),
    _mantle(Point3f(0,0,-1), Point3f(0), 0.5f0, 0.5f0, quality),
    _circle(Point3f(0,0,0), 0.5f0, Vec3f(0,0,1), quality)
])

```

---

<div class="post-metadata">

**Author:** ![Larbino1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/larbino1/32/37831_2.png) [@Larbino1](https://discourse.julialang.org/u/Larbino1)\
**Post date:** [August 16, 2024, 10:30am UTC](https://discourse.julialang.org/t/mesh-plotting-in-glmakie-with-geometrybasics-are-normals-broken/118227/6 "2024-08-16T10:30:54Z")

</div>

Thank you for the help and pointers! I’ll give it a go.
