为什么我的形状在绕 z 轴旋转时变形?

Why is my shape distorted on rotation about the z axis?

我刚开始学习 metal,下面的一系列屏幕截图最能向您展示我的挫败感。从上到下我们有 (1) 我的模型,其中模型矩阵是单位矩阵 (2) 我的模型用正交投影绕 x 轴旋转 60 度 (3) 我的模型用正交投影绕 y 轴旋转 60 度 (4) 我的模型绕z轴旋转了60度

所以我使用以下函数转换为标准化设备坐标:

- (CGPoint)normalizedDevicePointForViewPoint:(CGPoint)point
{
    CGPoint p = [self convertPoint:point toCoordinateSpace:self.window.screen.fixedCoordinateSpace];
    CGFloat halfWidth = CGRectGetMidX(self.window.screen.bounds);
    CGFloat halfHeight = CGRectGetMidY(self.window.screen.bounds);
    CGFloat px = ( p.x - halfWidth ) / halfWidth;
    CGFloat py = ( p.y - halfHeight ) / halfHeight;
    return CGPointMake(px, -py);
}

以下旋转和正交投影模型:

- (matrix_float4x4)zRotation
{
    self.rotationZ = M_PI / 3;
    const vector_float3 zAxis = { 0, 0, 1 };
    const matrix_float4x4 zRot = matrix_float4x4_rotation(zAxis, self.rotationZ);
    const matrix_float4x4 modelMatrix = zRot;

    return matrix_multiply( matrix_float4x4_orthogonal_projection_on_z_plane(), modelMatrix );
}

正如您所见,当我使用完全相同的方法绕其他两个轴旋转时,它看起来很好——没有变形。我究竟做错了什么?我应该在某处设置某种 scaling/aspect 比率的东西吗?可能是什么东西?我已经盯着这个看了很长一段时间了,所以任何 help/ideas 可以引导我走向正确方向的人都非常感激。提前谢谢你。

您的旋转或投影矩阵没有任何问题。视觉上的奇怪是因为您在旋转之前将顶点移动到 NDC space。在 NDC space 中旋转时,矩形不会保留其纵横比,因为从 NDC 到屏幕坐标的映射不是 1:1。

我建议在顶点管道结束之前不要在 NDC 中工作(即,将顶点 传递到 您在 "world" space 中的顶点函数, 和 out 到光栅器作为 NDC)。您可以使用正射投影矩阵的经典构造来执行此操作,该投影矩阵缩放和偏置顶点,正确考虑 window 坐标的非正方形纵横比。