使用slimdx计算视图投影矩阵

本文关键字:投影 视图 slimdx 计算 使用 | 更新日期: 2023-09-27 18:06:25

我正在使用slimdx构建一个小型可视化工具,然而,我最近遇到了一个问题。当我用mvp矩阵变换这个三角形时,它就消失了。

常量缓冲区被正确加载,因为我可以看到通过它们加载的正确颜色。如果我不在顶点着色器中变换它,可以看到用作测试的三角形。

所以我想问题是在视图矩阵或投影矩阵。而且我也不知道是否应该把它们调换一下。

        vertices = new DataStream(12 * 3, true, true);
        vertices.Write(new Vector3(0.0f, 0.5f, 0.5f));
        vertices.Write(new Vector3(0.5f, -0.5f, 0.5f));
        vertices.Write(new Vector3(-0.5f, -0.5f, 0.5f));
        vertices.Position = 0;
        vertexBuffer = new Buffer(device, vertices, 12 * 3, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
        // configure the Input Assembler portion of the pipeline with the vertex data
        dc3D.InputAssembler.InputLayout = baseShaders.GetInputLayout();
        dc3D.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
        dc3D.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertexBuffer, 12, 0));
        // set the shaders
        dc3D.VertexShader.Set(baseShaders.GetVertexShader());
        dc3D.PixelShader.Set(baseShaders.GetPixelShader());
        cbufferData = new Buffer(device, new BufferDescription
        {
            Usage = ResourceUsage.Default,
            SizeInBytes = System.Runtime.InteropServices.Marshal.SizeOf(typeof(BaseShaders.ConstantBuffer)),
            BindFlags = BindFlags.ConstantBuffer
        });
        dc3D.VertexShader.SetConstantBuffer(cbufferData, 0);
        Vector3 eye = new Vector3(4, 4, 4);
        Vector3 target = new Vector3(0, 0, 0);
        Vector3 up = new Vector3(0, 1, 0);
        Matrix.LookAtLH(ref eye, ref target, ref up, out cbuffer.view) ;
        //for now width and height are hardcoded.
        Matrix.PerspectiveFovLH((float)Math.PI / 4, 617/643.0f, 1.0f, 100.0f, out cbuffer.projection);
        cbuffer.color = new Vector4(0.1f, 1.0f, 1.0f, 1.0f);
        //Matrix.Transpose(cbuffer.view);
        //Matrix.Transpose(cbuffer.projection);
        // update constant buffers.
        var data = new DataStream(System.Runtime.InteropServices.Marshal.SizeOf(typeof(BaseShaders.ConstantBuffer)), true, true);
        data.Write(cbuffer);
        data.Position = 0;
        dc3D.UpdateSubresource(new DataBox(0, 0, data), cbufferData, 0);

已经好几个小时了,我还是没有找到任何解决办法。

哦,这是顶点着色器代码:

cbuffer ConstantBuffer : register(b0)
{
    matrix view;
    matrix projection;
    float4 color;
}
struct VOut
{
    float4 position : SV_POSITION;
    float4 color : COLOR;
};
VOut main(float4 position : POSITION)
{
    VOut output;
    output.position = mul(mul(position, view), projection);
    output.color = color;
    return output;
}

使用slimdx计算视图投影矩阵

通过做一些小的改变,实际上是调换矩阵,我设法使代码工作。

这是顶点着色器:

cbuffer ConstantBuffer : register(b0)
{
    float4x4 mvp;
    float4 color;
}
struct VOut
{
    float4 position : SV_POSITION;
    float4 color : COLOR;
};
VOut main(float4 position : POSITION)
{
    VOut output;
    output.position = mul(position, mvp);
    output.color = color;
    return output;
}

下面是修改后的代码:

        Vector3 eye = new Vector3(1, 1, 1);
        Vector3 target = new Vector3(0, 0, 0);
        Vector3 up = new Vector3(0, 1, 0);
        Matrix view = new Matrix();
        Matrix projection = new Matrix();
        view = Matrix.LookAtLH(eye, target, up) ;
        projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, 617/643.0f, 0.1f, 100.0f);
        cbuffer.color = new Vector4(0.1f, 1.0f, 1.0f, 1.0f);
        cbuffer.mvp = Matrix.Multiply(view, projection);
        Matrix.Transpose(ref cbuffer.mvp, out cbuffer.mvp);