Sign in to follow this  
GrimmBro

Change texture at runtime?

Recommended Posts

GrimmBro    355

Hi there,

 

If I have a 2D array consisting of cubes which represents a dungeon; different values in the array being what that cube will render as. The values all render fine when run and the 2D array can be hard coded to make up a level. However when I change the value of a specific location in the array, say

 dungeonArray[6][6] = 1;
 

to

 

 

dungeonArray[6][6] = 2;
 

This is to change the cube from a path to wall say. The physical properties of the new value are applied but not the change of texture appearance. The graphical elements of the array contain these values which do not seem to visually change no matter what I have tried so far. I am using 

 

d3dContext->UpdateSubresource(       
        constantBuffer[0].Get(),
        0,
        NULL,
        &constantBufferDataUpdate,
        0,
        0
        );
 

in the update function of the Game class; should this not update the textures. As when I press a test key the array does update as I said but no change in appearance. Any help or advice on this would be greatly appreciated.

Share this post


Link to post
Share on other sites
dfcollinson    484

Where's your code to bind the next texture? I'm not familiar with UpdateSubresource, but you need to bind the new texture to render for that state change, otherwise it'll just use the last one that was bound before you render it.

Share this post


Link to post
Share on other sites
Tordin    625

This is nothing related to texture input.

 

How do you handle your textures in the shader? in the cpu code?

 

 

How you assign textures in Dx11 is via PS/DS/HS/VS-SetShaderResources.

Share this post


Link to post
Share on other sites
GrimmBro    355

Sorry for all the code in advance but am I to update the render call somehow after a graphical element in the array is switched?

 

PixelShader.hlsl

Texture2D Texture;
SamplerState samplerState;

struct PixelShaderInput
{
	float4 pos : SV_POSITION;
	float4 color : COLOR;
	float2 texcoord : TEXCOORD;
};

float4 main(PixelShaderInput input) : SV_TARGET
{
	return input.color * Texture.Sample(samplerState, input.texcoord);
}

 

VertexShader.hlsl

cbuffer PerFrameCB : register(b0)
{
	matrix view;
	matrix projection;

	float4 lightVector;
    float4 lightColor;
    float4 ambientColor;
};

cbuffer PerObjectCB : register(b1)
{
	matrix modeltransform;
}

struct VertexShaderInput
{
	float4 pos : POSITION;
	float4 normal : NORMAL;
	float2 texcoord : TEXCOORD;
};

struct VertexShaderOutput
{
	float4 pos : SV_POSITION;
	float4 color : COLOR;
	float2 texcoord : TEXCOORD;
};

VertexShaderOutput main(VertexShaderInput input)
{
	VertexShaderOutput output;
	float4 pos = input.pos;

	// Transform the vertex position into projected space.
	pos = mul(pos, modeltransform);
	pos = mul(pos, view);
	pos = mul(pos, projection);
	output.pos = pos;

	output.color = ambientColor;

	float diffusebrightness = saturate(dot(input.normal, lightVector));
    output.color += lightColor * diffusebrightness;

	output.texcoord = input.texcoord;

	return output;
}

in Dungeon.cpp

void Dungeon::CreateGraphicsElements(Microsoft::WRL::ComPtr<ID3D11Device1>& dev, Microsoft::WRL::ComPtr<ID3D11DeviceContext1>& devCon)
{
	//create graphics elements
	m_DungeonElements.resize(m_DungeonArray.size());
	for (unsigned int i = 0; i < m_DungeonArray.size(); ++i)
	{
		m_DungeonElements[i].resize(m_DungeonArray[i].size());
	}

	float dungeonWidth = KElementSize*m_DungeonElements.size();
	float dungeonHeight = KElementSize *m_DungeonElements[0].size();

	for (unsigned int i = 0; i < m_DungeonElements.size(); i++)
		for (unsigned int j = 0; j < m_DungeonElements[i].size(); j++)
		{
			Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> selectedTexture = nullptr;
			int scaleHeight = 1;
			float elementY = -ElementSize;
			switch (m_DungeonArray[i][j])
			{
			case 1://wall
				scaleHeight = 2;
				elementY = 0;
				selectedTexture = m_TextureWall;
				break;
			case 2://cave
				selectedTexture = m_TextureCave;
				break;
			case 3://vegetation
				scaleHeight = 2;
				elementY = 0;
				selectedTexture = m_TextureVegetation;
				break;
			case 4://liquid
				selectedTexture = m_TextureLiquid;
				scaleHeight = 2;
				elementY = 0;
				break;
			case 5://gold
				selectedTexture = m_TextureGold;
				scaleHeight = 2;			
				elementY = 0;
				break;
			}

			m_DungeonElements[i][j] = new CDungeonElement(Vector3D( 0+j*KElementSize-dungeonWidth/2.0f, elementY, 0-i*KElementSize+dungeonHeight/2.0f), Vector3D(KElementSize, KElementSize*scaleHeight, KElementSize),
				selectedTexture, aDev, aDevCon);
			
	                //states to try and change certain blocks on array which are triggered by key presses.
              		if (m_blockState == BlockState::Path)
			{
				m_DungeonArray[3][3]=5;
			}
			if (m_blockState == BlockState::Wall)
			{
				m_DungeonArray[2][3]=5;
			}
			
			
		}
		}

this is called in

void Dungeon::createDungeon(Microsoft::WRL::ComPtr<ID3D11Device1>& dev, Microsoft::WRL::ComPtr<ID3D11DeviceContext1>& devCon)

in Game.cpp

 

void Game::Render()
{
	// bind the render targets
	m_d3dContext->OMSetRenderTargets(
		1,
		m_d3dRenderTargetView.GetAddressOf(),
		m_d3dDepthStencilView.Get()
		);
	
	// clear both the render target and depth stencil to default values
	const float black[] = { 0.0f, 0.0f, 0.0f, 1.000f };		//Clears screen to black.
	m_d3dContext->ClearRenderTargetView(
		m_d3dRenderTargetView.Get(),
		black
		);

	if (!m_deferredResourcesReady)
	{
		// Only render the loading screen for now.
		m_loadScreen->Render();
		return;
	}

	m_d3dContext->ClearDepthStencilView(
		m_d3dDepthStencilView.Get(),
		D3D11_CLEAR_DEPTH,
		1.0f,
		0
		);

	if (!m_loadingComplete)
	{
		return;
	}	

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

	// set the vertex shader stage state
	m_d3dContext->VSSetShader(
		m_vertexShader.Get(),
		nullptr,
		0
		);

	m_d3dContext->VSSetConstantBuffers(
		0,
		2,
		m_constantBuffer[0].GetAddressOf()
		);

	m_d3dContext->PSSetShader(
		m_pixelShader.Get(),
		nullptr,
		0
		);
	
	mp_Dungeon->Render(m_d3dContext, m_constantBuffer[1]);
	

}

 

void Game::Update(float timeTotal, float timeDelta)
{
	UNREFERENCED_PARAMETER( timeDelta );

#pragma region loading checks

	if (!m_loadingComplete) 
		return;

	if (!m_deferredResourcesReady)
	{		return;
	}
#pragma endregion loading checks

	ProcessInput();
		
	int res = mp_Dungeon->Update(timeDelta);		//update dungeon (only deals with player position for now)
	
		m_d3dContext->UpdateSubresource(
		m_constantBuffer[0].Get(),
		0,
		NULL,
		&m_constantBufferDataPerFrame,
		0,
		0
		);

	        m_d3dContext->UpdateSubresource(
		m_constantBuffer[1].Get(),
		0,
		NULL,
		&m_constantBufferDataPerObject,
		0,
		0
		);

}

Pretty sure I'm going to kick myself when I find out how to do this but I will be so happy when I do. Can anyone let me know how I can switch the graphical element value in the array and have it update in game?

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