1
\$\begingroup\$

I have manually cut out some textures from a sprite sheet and saved them as individual files for now, and I am trying to load in this png and apply it to the quad I have drawn on the screen, but for some reason it doesn't seem to work as I expect.

Here are the vertices, texture coordinates and indices of the quad I am drawing

m_quadVertices.push_back({ DirectX::XMFLOAT3(-1.0f, 1.0f, 0.0f), DirectX::XMFLOAT2(0.0f, 0.0f) });
m_quadVertices.push_back({ DirectX::XMFLOAT3(1.0f, 1.0f, 0.0f), DirectX::XMFLOAT2(1.0f, 0.0f) });
m_quadVertices.push_back({ DirectX::XMFLOAT3(1.0f, -1.0f, 0.0f), DirectX::XMFLOAT2(1.0f, 1.0f) });
m_quadVertices.push_back({ DirectX::XMFLOAT3(-1.0f, -1.0f, 0.0f), DirectX::XMFLOAT2(0.0f, 1.0f) });

m_quadIndices.push_back(0);
m_quadIndices.push_back(1);
m_quadIndices.push_back(3);

m_quadIndices.push_back(1);
m_quadIndices.push_back(2);
m_quadIndices.push_back(3);

The quad draws correctly and is how I want it, but I can't get the texture to wrap correctly.

Texturing is set up and should be sampled in the pixel shader as you can see some distorted lines on the quad in the image. I am setting the texture by using

context->PSSetShaderResources(0, 1, m_texture.GetAddressOf());

enter image description here

enter image description here

I have a feeling I could have the texture coordinates wrong or they are simply not being used correctly as it seems the image is too big for the quad and is not being scaled down properly to fit each corner.

I am loading the texture using the following code

D3D11_SAMPLER_DESC samplerDesc;
        // Create a texture sampler state description.
        samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
        samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
        samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
        samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
        samplerDesc.MipLODBias = 0.0f;
        samplerDesc.MaxAnisotropy = 1;
        samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
        //samplerDesc.BorderColor[0] = 0;
        //samplerDesc.BorderColor[1] = 0;
        //samplerDesc.BorderColor[2] = 0;
        //samplerDesc.BorderColor[3] = 0;
        samplerDesc.MinLOD = 0;
        samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;

        DX::ThrowIfFailed(
            GetDeviceResources().GetD3DDevice()->CreateSamplerState(&samplerDesc, &m_sampleState)
        );

        HRESULT hr = CreateWICTextureFromFile(GetDeviceResources().GetD3DDevice(), GetDeviceResources().GetD3DDeviceContext(),
            L"Assets/player.png", nullptr, m_texture.GetAddressOf());
        DX::ThrowIfFailed(hr);

Does anyone know where I am going wrong? If I am missing some information that is required to debug this issue please let me know.


Here are my entire render functions

bool Main::Render() 
{
    if (m_timer.GetFrameCount() == 0)
    {
        return false;
    }

    auto context = m_deviceResources->GetD3DDeviceContext();

    // Reset the viewport to target the whole screen.
    auto viewport = m_deviceResources->GetScreenViewport();
    context->RSSetViewports(1, &viewport);

    // Reset render targets to the screen.
    ID3D11RenderTargetView *const targets[1] = { m_deviceResources->GetBackBufferRenderTargetView() };
    context->OMSetRenderTargets(1, targets, m_deviceResources->GetDepthStencilView());

    // Clear the back buffer and depth stencil view.
    context->ClearRenderTargetView(m_deviceResources->GetBackBufferRenderTargetView(), DirectX::Colors::CornflowerBlue);
    context->ClearDepthStencilView(m_deviceResources->GetDepthStencilView(), D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);

    // Render the scene objects.
    // TODO: Replace this with your app's content rendering functions.
    Render(vertexBuffer, indexBuffer);

    return true;
}

void Render(ID3D11Buffer ** vertexBuffer, ID3D11Buffer ** indexBuffer)
{

    // Loading is asynchronous. Only draw geometry after it's loaded.
    if (!m_loadingComplete)
    {
        return;
    }

    auto context = GetDeviceResources().GetD3DDeviceContext();

    // Prepare the constant buffer to send it to the graphics device.
    context->UpdateSubresource1(
        m_constantBuffer.Get(),
        0,
        NULL,
        &m_constantBufferData,
        0,
        0,
        0
    );

    // Each vertex is one instance of the VertexPositionColor struct.
    UINT stride = sizeof(ShaderStructures::VertexTextureCoordinates);
    UINT offset = 0;
    context->IASetVertexBuffers(
        0,
        1,
        vertexBuffer,
        &stride,
        &offset
    );

    context->IASetIndexBuffer(
        *indexBuffer,
        DXGI_FORMAT_R16_UINT, // Each index is one 16-bit unsigned integer (short).
        0
    );

    //update texture
    context->PSSetSamplers(0, 1, m_sampleState.GetAddressOf());
    context->PSSetShaderResources(0, 1, m_texture.GetAddressOf());

    ID3D11RasterizerState* rasterState;
    D3D11_RASTERIZER_DESC wfdesc;
    ZeroMemory(&wfdesc, sizeof(D3D11_RASTERIZER_DESC));
    wfdesc.FillMode = D3D11_FILL_SOLID;
    wfdesc.CullMode = D3D11_CULL_NONE;
    GetDeviceResources().GetD3DDevice()->CreateRasterizerState(&wfdesc, &rasterState);
    context->RSSetState(rasterState);


    context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

    context->IASetInputLayout(m_inputLayout.Get());

    // Attach our vertex shader.
    context->VSSetShader(
        m_vertexShader.Get(),
        nullptr,
        0
    );

    // Send the constant buffer to the graphics device.
    context->VSSetConstantBuffers1(
        0,
        1,
        m_constantBuffer.GetAddressOf(),
        nullptr,
        nullptr
    );

    // Attach our pixel shader.
    context->PSSetShader(
        m_pixelShader.Get(),
        nullptr,
        0
    );

    // Draw the objects.
    context->DrawIndexed(
        6,
        0,
        0
    );
\$\endgroup\$
  • \$\begingroup\$ You should check your raster state (particularly back-face culling / winding), blend state, and depth/stencil state as well. Of course, you might just want to use SpriteBatch. \$\endgroup\$ – Chuck Walbourn Dec 11 '18 at 21:41
  • \$\begingroup\$ @ChuckWalbourn Hi, I will be moving to use SpriteBatch in the future but I'd like to see what's wrong with this first. I have updated my OP with my render functions. Most of the states are default that were with the template UWP application apart from the RasterState but it didn't do anything to the result. \$\endgroup\$ – jjmcc Dec 11 '18 at 22:22
  • \$\begingroup\$ @ChuckWalbourn Thanks for the pointers anyway, I've found the issue. It was my input layout. The position/texture coordinates were overlapping. \$\endgroup\$ – jjmcc Dec 11 '18 at 22:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.