Rakete1111
Rakete1111

Reputation: 48918

Wrong colors are being displayed - Direct3D

So far I made a very basic cube, but the colors I use are wrong. If I define Red in my Vertex Buffer, then there is no red at all anywhere on the cube!

Red goes to Light Green

Yellow goes to Light Blue

....

Some colors like Green, Black, ... are find, just some of them!! Here's how I define my Vertex Buffer (ignore the /*UV Coords*/)

Vertex vertices[] = 
    {
        //Front face vertices
        { XMFLOAT3(-1.0f, +1.0f, +1.0f), /*XMFLOAT2(0.5f, 0.0f)*/ Colors::Yellow },
        { XMFLOAT3(+1.0f, +1.0f, +1.0f), /*XMFLOAT2(1.0f, 0.0f)*/ Colors::Yellow },
        { XMFLOAT3(-1.0f, -1.0f, +1.0f), /*XMFLOAT2(0.5f, 1.0f)*/ Colors::Yellow },
        { XMFLOAT3(+1.0f, -1.0f, +1.0f), /*XMFLOAT2(1.0f, 1.0f)*/ Colors::Yellow },
        //Back face vertices
        { XMFLOAT3(-1.0f, +1.0f, -1.0f), /*XMFLOAT2(0.5f, 0.0f)*/ Colors::Yellow },
        { XMFLOAT3(+1.0f, +1.0f, -1.0f), /*XMFLOAT2(1.0f, 0.0f)*/ Colors::Yellow },
        { XMFLOAT3(-1.0f, -1.0f, -1.0f), /*XMFLOAT2(0.5f, 1.0f)*/ Colors::Yellow },
        { XMFLOAT3(+1.0f, -1.0f, -1.0f), /*XMFLOAT2(1.0f, 1.0f)*/ Colors::Yellow },
        //Right face vertices
        { XMFLOAT3(+1.0f, +1.0f, -1.0f), /*XMFLOAT2(1.0f, 0.0f)*/ Colors::Yellow },
        { XMFLOAT3(+1.0f, -1.0f, -1.0f), /*XMFLOAT2(1.0f, 1.0f)*/ Colors::Yellow },
        { XMFLOAT3(+1.0f, +1.0f, +1.0f), /*XMFLOAT2(0.5f, 0.0f)*/ Colors::Yellow },
        { XMFLOAT3(+1.0f, -1.0f, +1.0f), /*XMFLOAT2(0.5f, 1.0f)*/ Colors::Yellow },
        //Left face vertices
        { XMFLOAT3(-1.0f, +1.0f, -1.0f), /*XMFLOAT2(0.5f, 0.0f)*/ Colors::Yellow },
        { XMFLOAT3(-1.0f, -1.0f, -1.0f), /*XMFLOAT2(0.5f, 1.0f)*/ Colors::Yellow },
        { XMFLOAT3(-1.0f, +1.0f, +1.0f), /*XMFLOAT2(0.0f, 0.0f)*/ Colors::Yellow },
        { XMFLOAT3(-1.0f, -1.0f, +1.0f), /*XMFLOAT2(0.0f, 1.0f)*/ Colors::Yellow },
        //Top face vertices
        { XMFLOAT3(+1.0f, +1.0f, -1.0f), /*XMFLOAT2(0.5f, 1.0f)*/ Colors::Yellow },
        { XMFLOAT3(-1.0f, +1.0f, -1.0f), /*XMFLOAT2(0.0f, 1.0f)*/ Colors::Yellow },
        { XMFLOAT3(+1.0f, +1.0f, +1.0f), /*XMFLOAT2(0.5f, 0.0f)*/ Colors::Yellow },
        { XMFLOAT3(-1.0f, +1.0f, +1.0f), /*XMFLOAT2(0.0f, 0.0f)*/ Colors::Yellow },
        //Bottom face vertices
        { XMFLOAT3(+1.0f, -1.0f, -1.0f), /*XMFLOAT2(0.5f, 1.0f)*/ Colors::Yellow },
        { XMFLOAT3(-1.0f, -1.0f, -1.0f), /*XMFLOAT2(0.0f, 1.0f)*/ Colors::Yellow },
        { XMFLOAT3(+1.0f, -1.0f, +1.0f), /*XMFLOAT2(0.5f, 0.0f)*/ Colors::Yellow },
        { XMFLOAT3(-1.0f, -1.0f, +1.0f), /*XMFLOAT2(0.0f, 0.0f)*/ Colors::Yellow }
    };

Here I tested it with only Yellow, the result: enter image description here

Some info:

Vertex Structure:

struct Vertex
{
    XMFLOAT3 pos;
    XMVECTORF32 color;
};

The Colors Enum is from DirectXColors.h. It uses for the colors XMVECTORF32.

My really basic Pixel Shader:

struct PixelIN
{
    float4 pos : SV_POSITION;
    float4 color : COLOR;
};

float4 main(PixelIN i) : SV_TARGET
{
    return i.color;
}

My Input Layout Description:

D3D11_INPUT_ELEMENT_DESC ie[] = {
            { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
            { "COLOR", 0,  DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0}
    };

BufferDesc.Format has the value of DXGI_FORMAT_R8G8B8A8_UNORM

Thanks for the help!

Upvotes: 0

Views: 1249

Answers (1)

megadan
megadan

Reputation: 1833

Your issue is a mixture of XMFLOAT3 and XMVECTORF32 in your Vertex structure which is inserting extra padding into the structure.

XMVECTOR types align to 16 byte boundaries for performance. This would make your Vertex structure 32 bytes with a 1 byte padding in between pos and color to properly align the XMVECTORF32 data. That extra byte of padding throws off your input layout because you've said the position is 12 bytes followed immediately by 16 bytes which is only 28 bytes.

You can fix that in a few ways. Either:

  1. Change "POSITION" in your input layout to DXGI_FORMAT_R32G32B32A32_FLOAT to account for the extra byte of padding in your structure. If you are using a float4 for the position in your vertex shader, you might need to set this extra value (the w component) to 1 before transforming.

  2. Change color in your structure to an XMFLOAT4.

  3. Change pos in your structure to an XMVECTORF32 or XMFLOAT4 and do #1. This makes the padding not hidden.

Upvotes: 2

Related Questions