0

我刚刚使用金属中的四个顶点创建了一个矩形。我只需要旋转它。所以我使用模型指标。这是我的顶点着色器。

vertex VertexOutTexture vertex_shader_texture(const VertexInTexture vertices [[stage_in]],
                                              constant ModelConstant &modelConstants[[buffer(1)]],

    VertexOutTexture v;
    v.position =  modelConstants.modelMatrix*float4(vertices.position,1);
    v.color = vertices.color;
    v.textureCoordinates = vertices.textureCoordinates;

    return v;

}

它旋转。但是形状变了。所以我使用投影变换将节点的坐标从相机坐标转换为标准化坐标。我创建投影矩阵:

var sceneConstants = ScenceConstants()

设置它的值:

sceneConstants.projectionMatrix = matrix_float4x4(prespectiveDegreesFov:45, aspectRatio:Float(1.0),nearZ:0.1,farZ:100)

init mathod 在 math.h 中的位置

init(prespectiveDegreesFov degreesFov:Float, aspectRatio:Float,nearZ:Float,farZ:Float){
       let  fov = radians(fromDegrees: degreesFov)

        let y = 1/tan(fov*0.5)
        let x = y/aspectRatio
        let z1 = farZ/(nearZ - farZ)
        let w = (z1*nearZ)

        columns = (
            float4(x,  0,  0,  0),
            float4(0,  y,  0,  0),
            float4(0,  0,  z1,-1),
            float4(0,  0,  w,  0)
        )
    }

向 GPU 发送命令:

commandEncoder.setVertexBytes(&sceneConstants, length: MemoryLayout<ScenceConstants>.stride, index: 2)

更改我的顶点着色器:

v.position = sceneConstants.projectionMatrix* modelConstants.modelMatrix*float4(vertices.position ,1 );

但它没有用。

旋转前:在此处输入图像描述

旋转后:在此处输入图像描述

我在下面附加了我正在使用的数学函数。

func radians(fromDegrees degrees:Float) ->Float{
    return (degrees/100)*Float(Double.pi)
}
extension matrix_float4x4 {

    init(prespectiveDegreesFov degreesFov:Float, aspectRatio:Float,nearZ:Float,farZ:Float){
       let  fov = radians(fromDegrees: degreesFov)

        let y = 1/tan(fov*0.5)
        let x = y/aspectRatio
        let z1 = farZ/(nearZ - farZ)
        let w = (z1*nearZ)

        columns = (
            float4(x,  0,  0,  0),
            float4(0,  y,  0,  0),
            float4(0,  0,  z1,-1),
            float4(0,  0,  w,  0)
        )
    }



    mutating func scale(axis: float3){

        var result = matrix_identity_float4x4
        let x :Float  = axis.x
        let y :Float  = axis.y
        let z :Float  = axis.z


        result.columns = (

            float4(x,0,0,0),
            float4(0,y,0,0),
            float4(0,0,z,0),
            float4(0,0,0,1)
        )

        print("self:\(self)")
        self = matrix_multiply(self, result)

    }


    mutating func translate(direction: float3){

        var result = matrix_identity_float4x4

        let x :Float  = direction.x
        let y :Float  = direction.y
        let z :Float  = direction.z


        result.columns = (
            float4(1,0,0,0),
            float4(0,1,0,0),
            float4(0,0,1,0),
            float4(x,y,z,1)
        )
        print("self:\(self)")
        self = matrix_multiply(self, result)
    }


    mutating func rotate(angle: Float ,axis: float3){

        var result = matrix_identity_float4x4

        let x :Float  = axis.x
        let y :Float  = axis.y
        let z :Float  = axis.z

        let c: Float = cos(angle)
        let s:Float = sin(angle)

        let mc :Float = (1 - c)

        let r1c1: Float = x * x * mc + c
        let r2c1: Float = x * y * mc + z * s
        let r3c1: Float = x * z * mc - y * s
        let r4c1: Float = 0.0

        let r1c2: Float = y * x * mc - z * s
        let r2c2: Float = y * y * mc + c
        let r3c2: Float = y * z * mc + x * s
        let r4c2: Float = 0.0

        let r1c3: Float = z * x * mc + y * s
        let r2c3: Float = z * y * mc - x * s
        let r3c3: Float = z * z * mc + c
        let r4c3: Float = 0.0

        let r1c4: Float = 0.0
        let r2c4: Float = 0.0
        let r3c4: Float = 0.0
        let r4c4: Float = 1.0


        result.columns = (
            float4(r1c1,r2c1,r3c1,r4c1),
            float4(r1c2,r2c2,r3c2,r4c2),
            float4(r1c3,r2c3,r3c3,r4c3),
            float4(r1c4,r2c4,r3c4,r4c4)
        )
        print("Result:\(result)")
        self = matrix_multiply(self, result)
    }


}

我该如何解决这个问题?有什么建议吗?

4

0 回答 0