Direct3D 11 WorldViewProjection 矩阵转换不起作用

发布于 2024-10-18 01:56:57 字数 1811 浏览 7 评论 0原文

我有一个简单的正方形,我正在使用 Direct3D 11 和 SlimDX 在 3D 空间中绘制,具有以下坐标(我知道它会渲染)

0,0,0.5
0,0.5,0.5
0.5,0.5,0.5
0.5,0,0.5

我有一个相机类,它通过对视图矩阵应用矩阵变换来处理相机移动。例如Camera.MoveForward(float x) 将相机向前移动x。它还保存视图和投影矩阵。我使用以下代码实例化它们:

Matrix view = Matrix.LookAtLH(
    new Vector3(0f, 0f, -5f),
    new Vector3(0f, 0f, 0f),
    new Vector3(0f, 1f, 0f));
Matrix projection = Matrix.PerspectiveFovLH(
    (float)Math.PI/2,
    WINDOWWIDTH/WINDOWHEIGHT,
    0.1f,
    110f);

世界矩阵设置为单位矩阵。

在我的着色器代码中,我使用以下代码转换我的坐标:

PS_IN VS(VS_IN input)
{
    PS_IN output = (PS_IN)0;

    output.pos = mul( input.pos, WorldViewProjection );
    output.col = float4(1,1,1,1);

    return output;
}

在我的代码中使用以下代码设置 WorldViewProjection:

Matrix worldview = Matrix.Multiply(world, camera.ViewMatrix);    
Matrix worldviewprojection = Matrix.Multiply(worldview, camera.ProjectionMatrix);

我知道相机类的转换正在工作,因为它是我为 MDX 应用程序编写的旧代码,运行良好,但是当它运行时我看到我的正方形,向前和向后移动效果很好,但左右移动似乎是围绕 Y(向上)轴旋转正方形而不是平移它,向上和向下移动具有类似的效果,而不是围绕 X 轴旋转(水平)轴。

作为参考,相机移动功能:

public void MoveRight(float distance)
{
    position.Z += (float)(Math.Sin(-angle.Y) * -distance);
    position.X += (float)(Math.Cos(-angle.Y) * -distance);
    updateViewMatrix();
}

public void MoveForward(float distance)
{
    position.X += (float)(Math.Sin(-angle.Y) * -distance);
    position.Z += (float)(Math.Cos(-angle.Y) * -distance);
    updateViewMatrix();
}

public void MoveUp(float distance)
{
    position.Y -= distance;
    updateViewMatrix();
}

private void updateViewMatrix()
{
    ViewMatrix = Matrix.Translation(position.X, position.Y, position.Z) * Matrix.RotationY(angle.Y) * Matrix.RotationX(angle.X) * Matrix.RotationZ(angle.Z);
}

I have a simple square I'm drawing in 3D space using Direct3D 11 and SlimDX, with the following coordinates (I know it renders)

0,0,0.5
0,0.5,0.5
0.5,0.5,0.5
0.5,0,0.5

I have a camera class that handles camera movement by applying matrix transformations to the viewmatrix. e.g. Camera.MoveForward(float x) moves the camera forward by x. It also holds the View and Projection matrices. I instantiate them using the following code:

Matrix view = Matrix.LookAtLH(
    new Vector3(0f, 0f, -5f),
    new Vector3(0f, 0f, 0f),
    new Vector3(0f, 1f, 0f));
Matrix projection = Matrix.PerspectiveFovLH(
    (float)Math.PI/2,
    WINDOWWIDTH/WINDOWHEIGHT,
    0.1f,
    110f);

The world matrix is set to the identity matrix.

In my shader code I transform my coordinates using the following code:

PS_IN VS(VS_IN input)
{
    PS_IN output = (PS_IN)0;

    output.pos = mul( input.pos, WorldViewProjection );
    output.col = float4(1,1,1,1);

    return output;
}

Where WorldViewProjection is set in my code using the following:

Matrix worldview = Matrix.Multiply(world, camera.ViewMatrix);    
Matrix worldviewprojection = Matrix.Multiply(worldview, camera.ProjectionMatrix);

I know the camera class's transformations are working as it's old code I wrote for an MDX application which worked fine, however when this runs I see my square, and moving forwards and backwards works just fine, but moving left and right seems to rotate the square around the Y (up) axis instead of translating it, moving up and down has a similar effect, rotating instead about the X (horizontal) axis.

For reference, the camera movement functions:

public void MoveRight(float distance)
{
    position.Z += (float)(Math.Sin(-angle.Y) * -distance);
    position.X += (float)(Math.Cos(-angle.Y) * -distance);
    updateViewMatrix();
}

public void MoveForward(float distance)
{
    position.X += (float)(Math.Sin(-angle.Y) * -distance);
    position.Z += (float)(Math.Cos(-angle.Y) * -distance);
    updateViewMatrix();
}

public void MoveUp(float distance)
{
    position.Y -= distance;
    updateViewMatrix();
}

private void updateViewMatrix()
{
    ViewMatrix = Matrix.Translation(position.X, position.Y, position.Z) * Matrix.RotationY(angle.Y) * Matrix.RotationX(angle.X) * Matrix.RotationZ(angle.Z);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷途知返 2024-10-25 01:56:57

问题可能在于:

output.pos = mul( input.pos, WorldViewProjection );

我将把output.pos更改为float4,然后执行:

output.pos = mul( float4(input.pos, 1), WorldViewProjection );

让我知道这是如何实现的。如果这不是问题,我会更深入地了解可能发生的情况。

The issue may lie in the line:

output.pos = mul( input.pos, WorldViewProjection );

I would change output.pos to be a float4 and then do:

output.pos = mul( float4(input.pos, 1), WorldViewProjection );

Let me know how this works out. If this isn't the problem, I'll take a deeper look at what may be going on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文