Search code examples
c#3dslimdx

View Projection matrix calculation using slimdx


I'm using slimdx to build a small visualizer, however, i recently stumbled into a problem. When i transform the triangle with the mvp matrix it disappears.

Constant buffers are loaded properly because i can see the right color loaded through them. The triangle used as test is seen if i don't transform it in the vertex shader.

So i suppose the problem is in either the view matrix or the projection matrix. Moreover i don't know if i should transpose them..

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

Its been some hours now, and i didn't find any solution.

Oh, here is the vertex shader code:

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

Solution

  • By making some small changes, and in fact transposing the matrix i managed to get the code to work.

    Here is the vertex shader:

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

    Here is the code changed:

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