# Navigation chapter and simple mesh

**URL:** <https://forums.kodeco.com/t/navigation-chapter-and-simple-mesh/178712>\
**Category:** Metal by Tutorials\
**Created:** [September 22, 2022, 5:18am UTC](https://forums.kodeco.com/t/navigation-chapter-and-simple-mesh/178712 "2022-09-22T05:18:05Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![marek-kuticka](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/marek-kuticka/32/102075_2.png) [@marek-kuticka](https://forums.kodeco.com/u/marek-kuticka)\
**Post date:** [September 22, 2022, 5:18am UTC](https://forums.kodeco.com/t/navigation-chapter-and-simple-mesh/178712/1 "2022-09-22T05:18:05Z")

</div>

Hi @caroline

one more thing… based on my previous questions, I am trying to modify the navigation chapter sample to be able to render simple mesh ( quad from chapter 4 ) instead of the full model, but it currently fails.

```auto
renderEncoder.drawPrimitives(
        type: .triangle,
        vertexStart: 0,
        vertexCount: quad.indices.count)

```

is my render call, I have already added uniforms and params buffers, but with option .triangle I get only one triangle, with .line just single line, and .linestrip gives me 2 lines.

vertex buffer has optimized version of 4 vertices, with proper indices being sent.

```auto
vertex VertexOut vertex_main(
  const VertexIn in [[stage_in]],
  constant Uniforms &uniforms [[buffer(UniformsBuffer)]])
{
  float4 position =
    uniforms.projectionMatrix * uniforms.viewMatrix
    * uniforms.modelMatrix * in.position;
  float3 normal = in.normal;
  VertexOut out {
    .position = position,
    .normal = normal,
    .uv = in.uv
  };
  return out;
}

```

I know this has something to do with the vertex function in .metal shader file, but how to combine it? applying matrices AND using indexed primitives from buffers?

thx a lot

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [September 22, 2022, 6:15am UTC](https://forums.kodeco.com/t/navigation-chapter-and-simple-mesh/178712/2 "2022-09-22T06:15:54Z")

</div>

Does your vertex descriptor match the actual data format?

Have you inspected the geometry buffers on the GPU?

---

<div class="post-metadata">

**Author:** ![marek-kuticka](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/marek-kuticka/32/102075_2.png) [@marek-kuticka](https://forums.kodeco.com/u/marek-kuticka)\
**Post date:** [September 22, 2022, 10:45am UTC](https://forums.kodeco.com/t/navigation-chapter-and-simple-mesh/178712/3 "2022-09-22T10:45:51Z")

</div>

I did… and indeed, it does not match at all. will look more into descriptor chapter

---

<div class="post-metadata">

**Author:** ![marek-kuticka](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/marek-kuticka/32/102075_2.png) [@marek-kuticka](https://forums.kodeco.com/u/marek-kuticka)\
**Post date:** [September 22, 2022, 12:20pm UTC](https://forums.kodeco.com/t/navigation-chapter-and-simple-mesh/178712/4 "2022-09-22T12:20:01Z")

</div>

| Row | Offset | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| 0 | 0x0 | -0.800000 | 0.800000 | 0.000000 | 0.800000 | 0.800000 | 0.000000 | -0.800000 | -0.800000 |

this is how my VertexBuffer looks in frame capture.

weird is, that I have only numbers 1, or 0 in Quad code. however if I compare the code, I see no differences between web and my code.

probably, there is also an error in displaying data for vertex buffer. shall I set up the type for float3? 4? packed\_float currently I have already both triangles displayed, but displaced.

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [September 22, 2022, 1:04pm UTC](https://forums.kodeco.com/t/navigation-chapter-and-simple-mesh/178712/5 "2022-09-22T13:04:41Z")

</div>

I find it very difficult to debug code with just snippets 🙃.

When I debug, I examine the MTLBuffer and vertex descriptor on the CPU side as well as looking at the frame capture. The problem could be anything, but I would guess you are scaling your quad on the CPU side, and that you are sending 0.8 to the GPU, and that your vertex descriptor doesn’t match the data.

---

<div class="post-metadata">

**Author:** ![marek-kuticka](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/marek-kuticka/32/102075_2.png) [@marek-kuticka](https://forums.kodeco.com/u/marek-kuticka)\
**Post date:** [September 22, 2022, 1:35pm UTC](https://forums.kodeco.com/t/navigation-chapter-and-simple-mesh/178712/6 "2022-09-22T13:35:31Z")

</div>

[Navigation.zip](https://forums.kodeco.com/uploads/short-url/riZwryv8iiqDnROhe14RiunVEoR.zip) (2.6 MB)  
Attached see my current project.

---

<div class="post-metadata">

**Author:** ![marek-kuticka](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/marek-kuticka/32/102075_2.png) [@marek-kuticka](https://forums.kodeco.com/u/marek-kuticka)\
**Post date:** [September 23, 2022, 9:44am UTC](https://forums.kodeco.com/t/navigation-chapter-and-simple-mesh/178712/7 "2022-09-23T09:44:20Z")

</div>

Hi again

attached see image of my debugger. I have already found out, that vertices have to correspond to vertex descriptor… so 4 floats for position, 3 for normals, 2 for UV. in left bottom corner of screenshot, contents of ‘in’ is visible, but… it says out of range in the right side of screen, and I cannot understand this.

any hints here?  
thx a lot

![Snímek obrazovky 2022-09-23 v 11.42.04](https://files.chunter.kodeco.com/original/3X/c/2/c2f0160ea972de6a8297d8788c0242781009d36a.png)

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [September 23, 2022, 11:34pm UTC](https://forums.kodeco.com/t/navigation-chapter-and-simple-mesh/178712/8 "2022-09-23T23:34:39Z")

</div>

1. Your quad mesh consists of Position: Float x 3. However, your vertex descriptor and vertex shader function has position, normal and uv, so they are never going to match up.

2. You don’t need to explicitly send the index buffer to the GPU in a buffer, as the indexed draw call references the index buffer, so it will automatically go to the GPU.

3. Your vertices array consists of packed floats, which the vertex descriptor can’t handle. You could send them to the GPU without using a vertex descriptor, and take the packed float array directly into the vertex shader without using `stage_in`. Or you can define your array as `[float3]`

[Navigation.zip](https://forums.kodeco.com/uploads/short-url/fNZkzoN5tMQlYKXFI9jagMCITDh.zip) (2.6 MB)

The attached fixes all these issues.

1. **VertexDescriptor.swift** now defines a vertex descriptor for `quadLayout` with just a `float3`, and `Renderer` uses this vertex descriptor when creating the pipeline state object. I’ve commented out the normal and uv in the **Shaders.metal** , as you’re not using those.

2. I’ve commented out the `setVertexBuffer` for the index buffer.

3. In **Quad.swift** , I’ve set up a `float3` array for `vertices and changed the length of the vertex buffer.

---

<div class="post-metadata">

**Author:** ![marek-kuticka](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/marek-kuticka/32/102075_2.png) [@marek-kuticka](https://forums.kodeco.com/u/marek-kuticka)\
**Post date:** [September 24, 2022, 6:53am UTC](https://forums.kodeco.com/t/navigation-chapter-and-simple-mesh/178712/9 "2022-09-24T06:53:19Z")

</div>

thx a lot for the fix, has definitely helped.

May I ask, what changes would be needed in the code, in order to include also normals? as I plan to use them for lightning purposes.

have made several attempts, but all of them failed.

as for those “packed” floats… so using Float data type means it will be packed? and float3(0,0,0) will not be packed and shader can work well with it? this has surprised me a bit, although I have read this in the book

many thanks for guidance in advance

Marek

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [September 24, 2022, 8:24am UTC](https://forums.kodeco.com/t/navigation-chapter-and-simple-mesh/178712/10 "2022-09-24T08:24:01Z")

</div>

To add normals, you’d calculate them, and then add them to an array like the position `vertices` as `float3`, and add them to the vertex descriptor and also to the structure in the shader. You’d pass the vertex buffer before the draw call, just like the position vertex buffer.

Have a read of this answer to see if it helps with `float3` vs packed floats.

> <https://stackoverflow.com/questions/47061717/how-to-describe-packed-float3-in-metal-vertex-shader-mtlvertexattributedescripto/52452870#52452870>

---

<div class="post-metadata">

**Author:** ![marek-kuticka](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/marek-kuticka/32/102075_2.png) [@marek-kuticka](https://forums.kodeco.com/u/marek-kuticka)\
**Post date:** [September 24, 2022, 8:28am UTC](https://forums.kodeco.com/t/navigation-chapter-and-simple-mesh/178712/11 "2022-09-24T08:28:46Z")

</div>

so it’s another buffer? I thought it can be combined in quad vertices variable… but there I’d have to have a tuple of (float3, float3) and this approach does not seem to work.  
so… if normals are just another buffer, and they will be only combined in the shader… still bit confused

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [September 24, 2022, 8:29am UTC](https://forums.kodeco.com/t/navigation-chapter-and-simple-mesh/178712/12 "2022-09-24T08:29:59Z")

</div>

It doesn’t have to be another buffer. If you layout the vertex descriptor properly, and you define your `vertices` array with a structure that contains position and normal, then you can interleave position / normal / position / normal in the same buffer.

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [September 24, 2022, 8:31am UTC](https://forums.kodeco.com/t/navigation-chapter-and-simple-mesh/178712/13 "2022-09-24T08:31:01Z")

</div>

`defaultVertexDescriptor` shows both approaches. It has position and normal interleaved in buffer 0, and uvs in buffer 1

---

<div class="post-metadata">

**Author:** ![marek-kuticka](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/marek-kuticka/32/102075_2.png) [@marek-kuticka](https://forums.kodeco.com/u/marek-kuticka)\
**Post date:** [September 24, 2022, 8:34am UTC](https://forums.kodeco.com/t/navigation-chapter-and-simple-mesh/178712/14 "2022-09-24T08:34:17Z")

</div>

… wait. so its still only array of .float3 , but I have to interleave them? so…  
instead of

```auto
  var vertices2: [float3] = [
    float3(-1, 1, 0),
    float3(1, 1, 0),
    float3(-1, -1, 0),
    float3(1, -1, 0),
    float3(-1,-1,-1),
    float3(1,-1,-1)
    
  ]

```

I’d have

```auto
  var vertices2: [float3] = [
    float3(-1, 1, 0), // vertex
    float3(1, 1, 0), //normal
    float3(-1, -1, 0), //vertex
    float3(1, -1, 0), //normal
    float3(-1,-1,-1),
    float3(1,-1,-1)
    
  ]

```

… and so on?

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [September 24, 2022, 8:37am UTC](https://forums.kodeco.com/t/navigation-chapter-and-simple-mesh/178712/15 "2022-09-24T08:37:01Z")

</div>

It’s better to use

```auto
  struct Vertex {
    var position: float3
    var normal: float3
  }

```

as in this example:

[Navigation.zip](https://forums.kodeco.com/uploads/short-url/h2wnjVCaPfhtj7q0CJD2zzZLZOO.zip) (2.6 MB)

(that’s with [0,0,0] in the normals, but the positions are correct)

---

<div class="post-metadata">

**Author:** ![marek-kuticka](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/marek-kuticka/32/102075_2.png) [@marek-kuticka](https://forums.kodeco.com/u/marek-kuticka)\
**Post date:** [September 24, 2022, 8:39am UTC](https://forums.kodeco.com/t/navigation-chapter-and-simple-mesh/178712/16 "2022-09-24T08:39:27Z")

</div>

> [@marek-kuticka](#):
>
> ` `

thx a lot!! now I start to understand this. slowly… 😃

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [September 24, 2022, 8:41am UTC](https://forums.kodeco.com/t/navigation-chapter-and-simple-mesh/178712/17 "2022-09-24T08:41:20Z")

</div>

P.S. Irrelevant if you’re using my latest example, but I notice now that you had another error when you were using `defaultLayout` (which my latest example doesn’t).

```auto
    vertexDescriptor.attributes[Normal.index] = MDLVertexAttribute(
      name: MDLVertexAttributeNormal,
      format: .float3,
      offset: offset,
      bufferIndex: VertexBuffer.index)
    offset += MemoryLayout<Float>.stride

```

That offset is using the stride of `Float` and not `float3`, so it’s not allocating enough space.

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [September 24, 2022, 8:43am UTC](https://forums.kodeco.com/t/navigation-chapter-and-simple-mesh/178712/18 "2022-09-24T08:43:11Z")

</div>

It’s a good idea to spend a lot of time on buffers, vertex descriptors and checking the GPU frame capture to see what goes onto the GPU. In my opinion, that and matrices is the hardest thing about computer graphics with Metal.

---

<div class="post-metadata">

**Author:** ![marek-kuticka](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/marek-kuticka/32/102075_2.png) [@marek-kuticka](https://forums.kodeco.com/u/marek-kuticka)\
**Post date:** [September 24, 2022, 9:08am UTC](https://forums.kodeco.com/t/navigation-chapter-and-simple-mesh/178712/19 "2022-09-24T09:08:38Z")

</div>

yes… have tried a lot now to look into GPU capture… starting to understand it more and more. which timezone are you living, btw? I also realized, that for my purposes ( wireframe of something ressembling Minecraft, I need not share vertices, as the normals used for lighting cannot be shared. it would mess up lighting then

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [September 24, 2022, 9:43am UTC](https://forums.kodeco.com/t/navigation-chapter-and-simple-mesh/178712/20 "2022-09-24T09:43:13Z")

</div>

I’m on Brisbane Australia time, but I do keep odd hours sometimes 🥱

[Next page](https://forums.kodeco.com/t/navigation-chapter-and-simple-mesh/178712.md?page=2)
