在屏幕外渲染期间,像素到顶点坐标

时间:2016-04-21 17:23:21

标签: ios objective-c swift metal

我正在绘制一组用纯色填充到屏幕外纹理的三角形。

我的问题是我在结果图像中得到太大的三角形,它看起来像这样:

enter image description here

我的顶点坐标以像素为单位。 (我只是将它们生成为(0,outTexture.width / height)中的随机浮点数)。我不会将它们乘以我的顶点函数中的任何投影(也许这是我的错误?)

所以我的问题是顶点坐标如何与像素坐标相关联?

1 个答案:

答案 0 :(得分:0)

解决方案是制作正射投影并将其作为制服传递。

以下代码对我有用:

func makeOrthographicMatrix(left: Float, right: Float, bottom: Float, top: Float, near: Float, far: Float) -> [Float] {
    let ral = right + left
    let rsl = right - left
    let tab = top + bottom
    let tsb = top - bottom
    let fan = far + near
    let fsn = far - near

    return [2.0 / rsl,  0.0,        0.0,        0.0,
            0.0,        2.0 / tsb,  0.0,        0.0,
            0.0,        0.0,        -2.0 / fsn, 0.0,
            -ral / rsl, -tab / tsb, -fan / fsn, 1.0]
}

我这样称呼:

var projection = makeOrthographicMatrix(0.0, right: Float(inTexture.width), bottom: 0.0, top: Float(inTexture.height), near: -1.0, far: 1.0)
相关问题