0
\$\begingroup\$

First thread on gamedev stackexchange. I am currently creating a cube with a single texture on each side. I got my cube up and running. But when I apply my texture, I get this:

Something goes wrong

It blends the texture into a single color and apply it. I have various of examples that look near identical but do not have the same problem. Anyone know what can be the cause of this issue?

3D programming in general contains quite a bit of code. I wont copy paste all of it. If you want to see anything specific, you can request it. But here are some of the functions. Thanks in advance.

HRESULT Cubes::Render(ID3D11DeviceContext* _deviceContext, Camera _camera)
{
    HRESULT hr = S_OK;

    unsigned int stride = sizeof(Vertex); 
    unsigned int offset = 0;

    _deviceContext->IASetVertexBuffers(0, 1, &this->mVertexBuffer, &stride, &offset);
    _deviceContext->IASetIndexBuffer(this->mIndexBuffer, DXGI_FORMAT_R32_UINT, 0);
    _deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

    D3D11_MAPPED_SUBRESOURCE ms;
    hr = _deviceContext->Map(this->mMatrixBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &ms );
    if(FAILED(hr))
    {
        OutputDebugString("Failed to map matrixbuffer in Cubes.\n");
        return hr;
    }

    WVPBuffer* dataPtr;
    dataPtr = (WVPBuffer*)ms.pData;
    dataPtr->worldMatrix        = DirectX::XMMatrixTranspose( this->mWorld);
    dataPtr->viewMatrix         = DirectX::XMMatrixTranspose( _camera.GetView());
    dataPtr->projectionMatrix   = DirectX::XMMatrixTranspose( _camera.GetProj()); 

    _deviceContext->Unmap(this->mMatrixBuffer, NULL );

    // Set the position of the constant buffer in the vertex shader.
    int bufferNumber = 0;
    _deviceContext->VSSetConstantBuffers( 0, 1, &this->mMatrixBuffer ); 

    // Set the vertex input layout.
    _deviceContext->IASetInputLayout(this->mInputLayout);

    // Set the vertex and pixel shaders that will be used to render this triangle.
    _deviceContext->VSSetShader(this->mVertexShader, NULL, 0);
    _deviceContext->PSSetShader(this->mPixelShader, NULL, 0);
    _deviceContext->RSSetState(this->mRasterState);

    // Set shader texture resource in the pixel shader.
    _deviceContext->PSSetShaderResources(0, 1, &this->mTexture);
    _deviceContext->PSSetSamplers(0, 1, &this->mSamplerState);

    // Render the triangle.
    _deviceContext->DrawIndexed(this->mNoIndex, 0, 0);

    return hr;
}

HRESULT Cubes::LoadTexture(ID3D11Device* _device)
{
    HRESULT hr = S_OK;

    hr = D3DX11CreateShaderResourceViewFromFile(_device, "../Pictures/cube.png", nullptr, nullptr, &this->mTexture, nullptr);
    if(FAILED(hr))
    {
        OutputDebugString("Failed to create Cubes texture.\n");
        return hr;
    }

    return hr;
}

Pixelshader:

Texture2D shaderTexture;
SamplerState Sampler;

struct PSIn
{
    float4 Position : SV_POSITION;
    float2 Tex : TEX;
};

float4 PS_Cubes(PSIn _input) : SV_TARGET
{
    float4 textureColor;

    // Sample the pixel color from the texture using the sampler at this texture coordinate location.
    textureColor = shaderTexture.Sample(Sampler, _input.Tex);

    return textureColor;
}
\$\endgroup\$
3
  • \$\begingroup\$ The fact that it's exactly the average color of the texture probably means you're sampling from the least detailed MIP level. What is the definition for your input layout, and what is the vertex data itself? \$\endgroup\$
    – MooseBoys
    Nov 15, 2014 at 21:13
  • \$\begingroup\$ Got lazy, added everything into a pastebin. pastebin.com/zKKYw9kC \$\endgroup\$
    – Alex
    Nov 15, 2014 at 21:47
  • \$\begingroup\$ @MooseBoys It's also possible that he is making one sample from a detailed mip level with filtering, but that sample ends up somewhere between a red and a yellow texel to yield orangeish. \$\endgroup\$ Nov 17, 2014 at 7:30

1 Answer 1

2
\$\begingroup\$

Your input layout defines POSITION as R32G32B32_FLOAT but Vertex's first member is an XMFloat4. This results in garbage in the TEX input. To fix the problem, switch to a XMFloat3 for the Vertex position member. Also, the offset for your NORMAL is set to zero - this should be D3D11_APPEND_ALIGNED_ELEMENT as well.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .