OpenTK斜台(镜头移位)

问题描述 投票:1回答:1

我对OpenGL非常陌生,并且正在使用最新版本的OpenTK和C#。

我的相机班级目前正在执行以下操作,

public Matrix4 GetProjectionMatrix()
{
    return Matrix4.CreatePerspectiveFieldOfView(_fov, AspectRatio, 0.01f, 100f);
}

public Matrix4 GetViewMatrix()
{
    Vector3 lookAt = new Vector3(myObject.Pos.X, myObject.Pos.Y, myObject.Pos.Z);
    return Matrix4.LookAt(Position, lookAt, _up);
}

我有一个有点奇怪的用例,其中我的游戏窗口很长,类似于4:12的比例,并且会呈现一个很长的对象。根据我的阅读,在线呈现我想要的这种方式的最好方法是进行镜头转换(Oblique Frustum)。

我在网上看到有关如何执行此操作的文章,即:http://www.terathon.com/code/oblique.htmlhttps://docs.unity3d.com/Manual/ObliqueFrustum.html

但是我很难将其翻译为OpenTk。

想知道这里是否有人在OpenTK中做了类似的事情。

编辑:这种工作,但不是我一直在寻找:(

private float sgn(float a)
{
    if (a > 0.0F) return (1.0F);
    if (a < 0.0F) return (-1.0F);
    return (0.0F);
}

public Matrix4 CreatePerspectiveFieldOfView(Matrix4 projectionMatrix)
{
    Vector4 clipPlane = new Vector4(0.0f, 0.7f, 1.0f , 1.0f);
    Vector4 q = new Vector4
    {
        X = (sgn(clipPlane.X) + projectionMatrix.M13) / projectionMatrix.M11,
        Y = (sgn(clipPlane.Y) + projectionMatrix.M23) / projectionMatrix.M22,
        Z = -1.0f,
        W = (1.0F + projectionMatrix.M33) / projectionMatrix.M34
    };

    Vector4 c = clipPlane * (2.0F / Vector4.Dot(clipPlane, q));
    projectionMatrix.M31 = c.X;
    projectionMatrix.M32 = c.Y;
    projectionMatrix.M33 = c.Z + 1.0f;
    projectionMatrix.M34 = c.W;

    return projectionMatrix;
}
c# opengl opentk
1个回答
0
投票

存在一些明显的问题。 OpenGL矩阵是列的主要顺序。因此,对于Mij:的[Matrix4属性,i是列,而j

是行。以下各列从上至下,行从左至右,因为这是内存中矩阵字段的表示形式,以及矩阵在调试器中的“外观”:

Matrix4

因此

row1 row2 row3 row4 column1 (M11, M12, M13, M14) column2 (M21, M22, M23, M24) column3 (M31, M32, M33, M34) column4 (M41, M42, M43, M44)

必须翻译为:

q.x = (sgn(clipPlane.x) + matrix[8]) / matrix[0]; q.y = (sgn(clipPlane.y) + matrix[9]) / matrix[5]; q.z = -1.0F; q.w = (1.0F + matrix[10]) / matrix[14];

© www.soinside.com 2019 - 2024. All rights reserved.