Chapters 6 & 9: World space (losing control of the camera)

while working on these chapters, I thought it would be nice to put a little 3D-crosshair in the middle of my scenes so that I could see the world space origin as I tuned parameters.

The cross hair is easy to build. My model is a long thin cylinder:

MDLMesh(cylinderWithExtent: [0.002,1.0,0.002],
        segments: [UInt32(25),UInt32(25)],
        inwardNormals: false,
         topCap: true,
         bottomCap: true,
         geometryType: .triangles,
         allocator: MTKMeshBufferAllocator(device: device))

This cylinder stands upright, with its centre at the origin so I use three of them and built a little function to place them:

func renderAxes(encoder: MTLRenderCommandEncoder) {
   let rightAngle = Float(90).degreesToRadians
   //yAxis
   axis.rotation = float3(0,0,0)                        // REMARK 1
   uniforms.modelMatrix = axis.transform.modelMatrix
   encoder.setVertexBytes(&uniforms,
                          length: MemoryLayout<Uniforms>.stride,
                          index: 11)
   axis.draw(encoder: encoder)
   //xAxis
   axis.rotation = float3(0,0,rightAngle)
   uniforms.modelMatrix = axis.transform.modelMatrix
   encoder.setVertexBytes(&uniforms,
                          length: MemoryLayout<Uniforms>.stride,
                          index: 11)
   axis.draw(encoder: encoder)
   //zAxis
   //axis.scale = 3.0                                  // REMARK 2
   //axis.position = [0,0,1.5]                         // REMARK 2
   axis.rotation = float3(rightAngle,0,0)
   uniforms.modelMatrix = axis.transform.modelMatrix
   encoder.setVertexBytes(&uniforms,
                          length: MemoryLayout<Uniforms>.stride,
                          index: 11)
   axis.draw(encoder: encoder)
}

This simply resets the rotation matrix3 times and does a draw call after each.
I moved my camera slightly north and east of the z-axis so that I’d get a good view of the cross-hair.

And I did!! So far so good.

Nevertheless, I got two big surprises.

See //REMARK 1 above. If I comment out this roration the y-axis disappears!! Now I’m pretty sure that Transform already sets rotation to [0,0,0] so I shouldn’t have to.

See //REMARK 2 above. I felt that foreshortening was overdone on my y-Axis so I decided to make it longer lengthening it from [-0.5,0.5] on y to [-0.5,2.5]. However, if I uncomment these two lines the scale of the entire world changes. This was a surprise and I’m trying to explain it as happening either during Perspective divide or NDC to screen. It seems to me that before drawing Y, I had a scene in view. Then I dropped another model into my scene and instead of just fitting in, it zoomed out two, and I lost control of my camera.

Can anybody explain that?

Remark 1:

On the very first render, y axis axis.rotation is [0, 0, 0]. However, you’re updating axis.rotation every frame for every axis. So On the second frame, if you remove that initialisation, the y axis will have the same value as the z axis. So the y axis is not disappearing, but being overdrawn by the z axis. You can confirm this in the Metal debugger. (Also the print command.)

Remark 2:

I don’t see this happening in my code:

Navigation.zip (78.8 KB)

However, again you’re changing the scale and position for all three axes. And it is possible that you’re not updating uniforms.modelMatrix with the models’ matrices, but leaving the matrix as is from the previous z axis render.

If you need some help with debugging, I would suggest this process:

  1. Use the print command (or breakpoints) to make sure variables contain what you think they contain.

  2. For GPU contents, use the Metal debugger and examine the contents of buffers.

  3. Upload the entire project here, so that I can see what’s happening. (You may have to remove models and textures if the project’s too big.)

1 Like

Spot on as always Caroline

I had fixed the first problem but didn’t realise why.
The second was the same problem.
I forgot that drawing is a loop.

Thanks. I’ll try to stay off the forum and let you get on with more important things.

1 Like

Haha - I’m glad I could help.

1 Like