Search code examples
c#.netdirectxdirect3dsharpdx

Getting unexpected colors in SharpDX app


I'm delving into directx via the SharpDX wrapper for .NET, but I'm getting some unexpected results.

This is my expected result:

expected red triangle

and here is the result I'm getting:

enter image description here

Here is my shader:

struct VOut
{
    float4 position : SV_POSITION;
    float4 color : COLOR;
};

VOut vsMain(float4 position : POSITION, float4 color : COLOR)
{
    VOut output;

    output.position = position;
    output.color = color;

    return output;
}

float4 psMain(VOut pInput) : SV_TARGET
{
    return pInput.color;
}

along with the Input Layout:

private D3D11.InputElement[] inputElements = new D3D11.InputElement[]
{
    new D3D11.InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0, D3D11.InputClassification.PerVertexData, 0),
    new D3D11.InputElement("COLOR", 0, Format.R32G32B32A32_Float, 12, 0)
};

I'm passing the following set of vertices through the vertexBuffer

mesh = new NonIndexMesh() {
    vertices = new List<Vertex>() {
        new Vertex(new Vector3(-0.5f, 0.5f, 0.0f), Color.Red),
        new Vertex(new Vector3(0.5f, 0.5f, 0.0f), Color.Red),
        new Vertex(new Vector3(0.0f, -0.5f, 0.0f), Color.Red)
    }
}

my Vertex type looks like this:

public struct Vertex {

    public Vector3 position;
    public Color color;

    public Vertex(Vector3 pos, Color col) {
        position = pos;
        color = col;
    }
}

The data being passed through is correct even at runtime according to what's printed out to the Console, and the positions of each vertex being rendered seem to be correct.

What am I missing here that's causing these weird colors in my rendered triangle?


Solution

  • The color class you are using represents the colors as bytes. So values for RGBA range from 0 - 255. Try using the class Color4 which represents the colors as floats in the range 0.0 - 1.0. This is indeed what the shader expects.