Group Group Group Group Group Group Group Group Group

Chapter 5 Starter Project Stride number when setting normals



Just looking at Chapter 5 starter project and I am at the point where I am adding a vertex descriptor attribute for normals on page 120. The code is as follows:

vertexDescriptor.attributes[1] =
            MDLVertexAttribute(name: MDLVertexAttributeNormal,
            format: .float3, offset: 12, bufferIndex: 0)

I am a little bit confused by setting the offset for normals. Why is it set to 12? I know the book says that is the length of the position attribute, but should not it be rather set to


since position is of format .float3?

I am confused as to why is it hardcoded like that? Since in the examples before when setting the MTLVertexDescriptor the MemoryLayout was always used for strides.

Then later it says to set the layout to 24.

vertexDescriptor.layouts[0] = MDLVertexBufferLayout(stride: 24)

but would it not be better to do :

vertexDescriptor.layouts[0] = MDLVertexBufferLayout(stride: MemoryLayout<float3>.stride + MemoryLayout<float3>stride)

since position and normal attributes are both of float3 format?



@caroline Can you please help with this when you get a chance? Thank you - much appreciated! :]


@henrygorner - yes that would probably be better. I usually do as you suggest - I’m not sure what was going through my head at the time.