【问题标题】:View Projection matrix calculation using slimdx使用 slimdx 查看投影矩阵计算
【发布时间】:2015-08-05 13:25:23
【问题描述】:

我正在使用 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;
}

【问题讨论】:

    标签: c# 3d slimdx


    【解决方案1】:

    通过做一些小的改变,事实上我转置了矩阵,我设法让代码工作了。

    这是顶点着色器:

    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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-14
      • 2021-04-09
      • 2013-01-09
      • 1970-01-01
      • 1970-01-01
      • 2012-03-27
      • 2011-09-05
      • 2012-04-24
      相关资源
      最近更新 更多