Jump to content
  • Advertisement
Sign in to follow this  
Happy SDE

DX11 Making triangle semi-transparent in DX11

This topic is 2635 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I made simple renderer for a laser beam (it contains only 2 triangles, color is hardcoded in HLSL).

C++ code:
CLaserBeamRenderer::Render()
{

m_spContext->IASetVertexBuffers(0, 1, &m_spVertexBuffer.p, &stride, &offset);

m_spContext->IASetInputLayout(m_spInputLayout);
m_spContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);

m_spPass->Apply(0, m_spContext);
m_spContext->Draw(_countof(m_beamData), 0);
}


HLSL code:
float4 VS(float3 pos: POSITION) : SV_POSITION
{
return float4(pos, 1);
}

float4 PS() : SV_Target
{
return float4(1, 1, 1, 0.5); //<--- Problem: last value changes nothing =(
}


I place different values into 4th element of color in PS, but the beam does not change transparency.
I can’t understand where and what should I do to make alpha effect for a beam.
It seems to me that I should change some state (of what?) either in CLaserBeamRenderer or in HLSL code for current object.

Could anyone explain to me: what place should I modify to make the beam semi-transparent?
(I want only some objects to be transparent, so I think I should not change global state.)

Thanks in advance

Share this post


Link to post
Share on other sites
Advertisement
You need to create and set a blend-state. Use ID3D11Device::CreateBlendState to create it, and set it to your device context with OMSetBlendState.
In the blend desc structure, set RenderTarget[0].BlendEnable to true, and fill in the appropriate values that you want to use in blending. For this you probably want source blend as D3D11_BLEND_SRC_ALPHA and destination blend as D3D11_BLEND_INV_SRC_ALPHA.

Share this post


Link to post
Share on other sites

You need to create and set a blend-state. Use ID3D11Device::CreateBlendState to create it, and set it to your device context with OMSetBlendState.
In the blend desc structure, set RenderTarget[0].BlendEnable to true, and fill in the appropriate values that you want to use in blending. For this you probably want source blend as D3D11_BLEND_SRC_ALPHA and destination blend as D3D11_BLEND_INV_SRC_ALPHA.


Thank you Erik for an answer.
It solved my problem.

The only thing I can't understand is: will OMSetBlendState() call affect other renderers?
In other words, should I undo the state inside CSightRenderer or this call lives untill ID3D11DeviceContext::Draw call and not affect pipeline state after that?

void CLaserBeamRenderer::Render()
{
...
m_spContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
m_spContext->OMSetBlendState( m_spBlendState, nullptr, 0xFFFFFFFF );
m_spContext->Draw(_countof(g_beamData), 0);

//>>>1. Should I undo OMSetBlendState here?
}

void CSightRenderer::Render()
{
m_spContext->IASetVertexBuffers(0, 1, &m_spVertexBuffer.p, &stride, &offset);
...
//>>>2. Should I undo OMSetBlendState here?

m_spContext->Draw(8, 0);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!