• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
gchris6810

Multiple Textures with DirectX 11

2 posts in this topic

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.

[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 );
}
[/CODE]


and the shader code...

[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 );
}
[/CODE]

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.
0

Share this post


Link to post
Share on other sites
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 ;)
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0