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?