Jump to content

  • Log In with Google      Sign In   
  • Create Account


Multiple Textures with DirectX 11


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 gchris6810   Members   -  Reputation: 195

Like
0Likes
Like

Posted 28 October 2012 - 02:54 AM

Hi,

I have already managed to successfully render one texture to the screen but I can't see how you would render two. There seems to be a key issue with matching textures up to vertex buffers or maybe a shader issue. Here is my code:

The main drawing code.

#include"Game.h"
#include<xnamath.h>

struct VertexPos
{
    XMFLOAT3 pos;
    XMFLOAT2 tex0;
};

Game::Game( ) : solidColorVS_( 0 ), solidColorPS_( 0 ),
							  inputLayout_( 0 ), vertexBuffer_( 0 ),
							  colorMap_( 0 ), colorMapSampler_( 0 )
{
}

Game::~Game( )
{
}

bool Game::LoadContent( )
{
    ID3DBlob* vsBuffer = 0;
    bool compileResult = CompileD3DShader( "TextureMap.fx", "VS_Main", "vs_4_0", &vsBuffer );
    if( compileResult == false )
    {
	    DXTRACE_MSG( "Error compiling the vertex shader!" );
	    return false;
    }
    HRESULT d3dResult;
    d3dResult = d3dDevice_->CreateVertexShader( vsBuffer->GetBufferPointer( ),
	    vsBuffer->GetBufferSize( ), 0, &solidColorVS_ );
    if( FAILED( d3dResult ) )
    {
	    DXTRACE_MSG( "Error creating the vertex shader!" );
	    if( vsBuffer )
		    vsBuffer->Release( );
	    return false;
    }
    D3D11_INPUT_ELEMENT_DESC solidColorLayout[] =
    {
	    { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
	    { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }
    };
    unsigned int totalLayoutElements = ARRAYSIZE( solidColorLayout );
    d3dResult = d3dDevice_->CreateInputLayout( solidColorLayout, totalLayoutElements,
	    vsBuffer->GetBufferPointer( ), vsBuffer->GetBufferSize( ), &inputLayout_ );
    vsBuffer->Release( );
    if( FAILED( d3dResult ) )
    {
	    DXTRACE_MSG( "Error creating the input layout!" );
	    return false;
    }
    ID3DBlob* psBuffer = 0;
    compileResult = CompileD3DShader( "TextureMap.fx", "PS_Main", "ps_4_0", &psBuffer );
    if( compileResult == false )
    {
	    DXTRACE_MSG( "Error compiling pixel shader!" );
	    return false;
    }
    d3dResult = d3dDevice_->CreatePixelShader( psBuffer->GetBufferPointer( ),
	    psBuffer->GetBufferSize( ), 0, &solidColorPS_ );
    psBuffer->Release( );
    if( FAILED( d3dResult ) )
    {
	    DXTRACE_MSG( "Error creating pixel shader!" );
	    return false;
    }
    VertexPos vertices[] =  // Main background
    {
  { XMFLOAT3(  1.0f,  1.0f, 0.0f ), XMFLOAT2( 4.0f, 12.0f ) },
	    { XMFLOAT3(  1.0f, -1.0f, 0.0f ), XMFLOAT2( 4.0f, 0.0f ) },
	    { XMFLOAT3( -1.0f, -1.0f, 0.0f ), XMFLOAT2( 0.0f, 0.0f ) },
	    { XMFLOAT3( -1.0f, -1.0f, 0.0f ), XMFLOAT2( 0.0f, 0.0f ) },
	    { XMFLOAT3( -1.0f,  1.0f, 0.0f ), XMFLOAT2( 0.0f, 12.0f ) },
	    { XMFLOAT3(  1.0f,  1.0f, 0.0f ), XMFLOAT2( 4.0f, 12.0f ) },
    };
VertexPos stone[] =   //Pave terrain feature.
{
  { XMFLOAT3( 0.3f, -0.2f, 0.0f ), XMFLOAT2( 1.0f, 1.0f ) },
  { XMFLOAT3( 0.0f, -0.2f, 0.0f ), XMFLOAT2( 1.0f, 0.0f ) },
  { XMFLOAT3( 0.3f, 0.3f, 0.0f ), XMFLOAT2( 0.0f, 0.0f ) },
  { XMFLOAT3( 0.3f, 0.3f, 0.5f ), XMFLOAT2( 0.0f, 0.0f ) },
  { XMFLOAT3( 0.0f, -0.2f, 0.5f ), XMFLOAT2( 0.0f, 1.0f ) },
  { XMFLOAT3( 0.0f, 0.3f, 0.5f ), XMFLOAT2( 1.0f, 1.0f ) },
};
D3D11_BUFFER_DESC vertexDesc, stoneDesc;
    ZeroMemory( &vertexDesc, sizeof( vertexDesc ) );
    vertexDesc.Usage = D3D11_USAGE_DEFAULT;
    vertexDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    vertexDesc.ByteWidth = sizeof( VertexPos ) * 6;
ZeroMemory( &stoneDesc, sizeof( stoneDesc ) );
    stoneDesc.Usage = D3D11_USAGE_DEFAULT;
    stoneDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    stoneDesc.ByteWidth = sizeof( VertexPos ) * 6;
    D3D11_SUBRESOURCE_DATA resourceData;
D3D11_SUBRESOURCE_DATA paveData;
    ZeroMemory( &resourceData, sizeof( resourceData ) );
    resourceData.pSysMem = vertices;
ZeroMemory( &paveData, sizeof( paveData ) );
paveData.pSysMem = stone;
    d3dDevice_->CreateBuffer( &vertexDesc, &resourceData, &vertexBuffer_ );
d3dResult = d3dDevice_->CreateBuffer( &stoneDesc, &paveData, &paveBuffer_ );
buffers_[0] = vertexBuffer_;
buffers_[1] = paveBuffer_;
    if( FAILED( d3dResult ) )
    {
	    DXTRACE_MSG( "Failed to create vertex buffer!" );
	    return false;
    }
d3dResult = D3DX11CreateShaderResourceViewFromFile( d3dDevice_,
  "detail.png", 0, 0, &pave_, 0 );
    d3dResult = D3DX11CreateShaderResourceViewFromFile( d3dDevice_,
	    "tile.png", 0, 0, &colorMap_, 0 );
    if( FAILED( d3dResult ) )
    {
	    DXTRACE_MSG( "Failed to load the texture image!" );
	    return false;
    }
    D3D11_SAMPLER_DESC colorMapDesc;
D3D11_SAMPLER_DESC paveDesc;
    ZeroMemory( &colorMapDesc, sizeof( colorMapDesc ) );
    colorMapDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
    colorMapDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
    colorMapDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
    colorMapDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
    colorMapDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
    colorMapDesc.MaxLOD = D3D11_FLOAT32_MAX;
ZeroMemory( &paveDesc, sizeof( paveDesc ) );
paveDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
paveDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
paveDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
paveDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
paveDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
paveDesc.MaxLOD = D3D11_FLOAT32_MAX;
    d3dResult = d3dDevice_->CreateSamplerState( &colorMapDesc, &colorMapSampler_ );
d3dResult = d3dDevice_->CreateSamplerState( &paveDesc, &paveSampler_ );
samplers_[0] = colorMapSampler_;
samplers_[1] = paveSampler_;
resourceViews_[1] = pave_;
resourceViews_[0] = colorMap_;
    if( FAILED( d3dResult ) )
    {
	    DXTRACE_MSG( "Failed to create color map sampler state!" );
	    return false;
    }
    return true;
}

void Game::UnloadContent( )
{
if( pave_ ) pave_->Release( );
if( paveSampler_ ) paveSampler_->Release( );
    if( colorMapSampler_ ) colorMapSampler_->Release( );
    if( colorMap_ ) colorMap_->Release( );
    if( solidColorVS_ ) solidColorVS_->Release( );
    if( solidColorPS_ ) solidColorPS_->Release( );
    if( inputLayout_ ) inputLayout_->Release( );
    if( vertexBuffer_ ) vertexBuffer_->Release( );
if( paveBuffer_ ) paveBuffer_->Release( );
pave_ = 0;
paveSampler_ = 0;
    colorMapSampler_ = 0;
    colorMap_ = 0;
pave_ = 0;
paveSampler_ = 0;
    solidColorVS_ = 0;
    solidColorPS_ = 0;
    inputLayout_ = 0;
    vertexBuffer_ = 0;
paveBuffer_ = 0;
}

void Game::Update( float dt )
{
}

void Game::Render( )
{
    if( d3dContext_ == 0 )
	    return;
    float clearColor[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
    d3dContext_->ClearRenderTargetView( backBufferTarget_, clearColor );
unsigned int stride[2] = { sizeof( VertexPos ), sizeof( VertexPos ) };
    unsigned int offset = 0;
    d3dContext_->IASetInputLayout( inputLayout_ );
    d3dContext_->IASetVertexBuffers( 0, 2, buffers_, stride, &offset );
    d3dContext_->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
    d3dContext_->VSSetShader( solidColorVS_, 0, 0 );
    d3dContext_->PSSetShader( solidColorPS_, 0, 0 );
    d3dContext_->PSSetShaderResources( 0, 2, resourceViews_ );
    d3dContext_->PSSetSamplers( 0, 2, samplers_ );
    d3dContext_->Draw( 6, 0 );
    swapChain_->Present( 0, 0 );
}


and the shader code...

Texture2D colorMap_ : register( t0 );
SamplerState colorSampler_ : register( s0 );
struct VS_Input
{
    float4 pos  : POSITION;
    float2 tex0 : TEXCOORD0;
};
struct PS_Input
{
    float4 pos  : SV_POSITION;
    float2 tex0 : TEXCOORD0;
};

PS_Input VS_Main( VS_Input vertex )
{
    PS_Input vsOut = ( PS_Input )0;
    vsOut.pos = vertex.pos;
    vsOut.tex0 = vertex.tex0;
    return vsOut;
}

float4 PS_Main( PS_Input frag ) : SV_TARGET
{
    return colorMap_.Sample( colorSampler_, frag.tex0 );
}

I know it's a quite long code post but I didn't really know what you would need. Thanks in advance for any replies.

Sponsor:

#2 chowarth   Members   -  Reputation: 516

Like
0Likes
Like

Posted 29 October 2012 - 10:27 AM

One instant thing that stands out is that in your pixel shader, you only have one Texture2D defined. If you want to use two textures in the shader then you'll need two Texture2D variables.

Following on from this, as you only have one texture, you're only sampling from one texture. If you want a combination of the two textures to be on your mesh, you'll need to blend between the two colours sampled from the two different textures you passed over.

Can't think of anything else at the mo, just a quick post while at work ;)
Ramblings: www.chowarth.co.uk

#3 gchris6810   Members   -  Reputation: 195

Like
0Likes
Like

Posted 29 October 2012 - 12:15 PM

Thanks for your reply. But I found a way to do it in the end.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS