Search code examples
renderdirectx-11texture2d

DirectX Texture rendered incorretly


I'm trying to render a cube with a texture, the cube works, but the texture is weirdly stretched on the front, back, right and left: enter image description here

The texture I'm using: enter image description here

As you may notice the texture is stretched over the sides. This is my code (not full, only parts that you may need :D):

How I create the Shader Resource View:

//Create Shader Resouce View from file  and sampler
    HR(CreateDDSTextureFromFile(mDevice, L"D:\\DXSDK\\Projects\\Test\\Debug\\brick.dds", NULL, &mSRV));

    //Create Sampler State
    D3D11_SAMPLER_DESC sd;
    ZeroMemory(&sd, sizeof(D3D11_SAMPLER_DESC));
    sd.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
    sd.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
    sd.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
    sd.ComparisonFunc = D3D11_COMPARISON_NEVER;
    sd.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
    sd.MaxLOD = D3D11_FLOAT32_MAX;

    HR(mDevice->CreateSamplerState(&sd, &mSampler));

How my create my buffers:

    //Create Vertex Buffer
    Vertex vertices[] = {
            //Front face vertices
            { XMFLOAT3(-1.0f, +1.0f, -1.0f), Colors::Blue, XMFLOAT2(0.0f, 0.0f) },
            { XMFLOAT3(+1.0f, +1.0f, -1.0f), Colors::Red, XMFLOAT2(1.0f, 0.0f) },
            { XMFLOAT3(-1.0f, -1.0f, -1.0f), Colors::Yellow, XMFLOAT2(0.0f, 1.0f) },
            { XMFLOAT3(+1.0f, -1.0f, -1.0f), Colors::White, XMFLOAT2(1.0f, 1.0f) },
            //Back face vertices
            { XMFLOAT3(-1.0f, +1.0f, +1.0f), Colors::Black, XMFLOAT2(0.0f, 0.0f) },
            { XMFLOAT3(+1.0f, +1.0f, +1.0f), Colors::Violet, XMFLOAT2(1.0f, 0.0f) },
            { XMFLOAT3(-1.0f, -1.0f, +1.0f), Colors::Aqua, XMFLOAT2(0.0f, 1.0f) },
            { XMFLOAT3(+1.0f, -1.0f, +1.0f), Colors::Green, XMFLOAT2(1.0f, 1.0f) }
    };

    D3D11_BUFFER_DESC vbDesc;
    ZeroMemory(&vbDesc, sizeof(D3D11_BUFFER_DESC));
    vbDesc.Usage = D3D11_USAGE_DEFAULT;
    vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    vbDesc.ByteWidth = sizeof(Vertex) * 8;

    D3D11_SUBRESOURCE_DATA vbSub;
    ZeroMemory(&vbSub, sizeof(D3D11_SUBRESOURCE_DATA));
    vbSub.pSysMem = vertices;

    HR(mDevice->CreateBuffer(&vbDesc, &vbSub, &mBoxVB));

    //Create Index Buffer
    UINT indices[] = {
        //Front face
        0, 1, 2,
        1, 2, 3,
        //Back face
        4, 5, 6,
        5, 6, 7,
        //Right face
        1, 3, 5,
        3, 5, 7,
        //Left face
        0, 2, 4,
        2, 4, 6,
        //Top face
        0, 1, 5,
        0, 4, 5,
        //Bottom face
        2, 3, 7,
        2, 6, 7
    };

    D3D11_BUFFER_DESC ibDesc;
    ZeroMemory(&ibDesc, sizeof(D3D11_BUFFER_DESC));
    ibDesc.Usage = D3D11_USAGE_IMMUTABLE;
    ibDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
    ibDesc.ByteWidth = sizeof(UINT) * 36;

    D3D11_SUBRESOURCE_DATA ibSub;
    ZeroMemory(&ibSub, sizeof(D3D11_SUBRESOURCE_DATA));
    ibSub.pSysMem = indices;

    HR(mDevice->CreateBuffer(&ibDesc, &ibSub, &mBoxIB));

    //Create Constant Buffer
    D3D11_BUFFER_DESC cbDesc;
    ZeroMemory(&cbDesc, sizeof(D3D11_BUFFER_DESC));
    cbDesc.Usage = D3D11_USAGE_DEFAULT;
    cbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
    cbDesc.ByteWidth = sizeof(XMFLOAT4X4);

    HR(mDevice->CreateBuffer(&cbDesc, NULL, &mBoxCB));

Also, I double checked everything, so everything is bound to the pipeline, etc.

If someone could help, that would be great! Thanks in advance!


Solution

  • Problem is that you use only 8 vertices, which is not correct in case of a cube since for each face vertex needs different uv coordinates (it will also be the same with normals if you need them later).

    So you need to create 4 vertices per face, in order to have correct uv set for each of them.

    Here is an example in DirectX Toolkit