• Advertisement
Sign in to follow this  

Trying to set alpha

This topic is 1389 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

Hi Guys,

I am trying to make a textured quad render at 50% transparancy. But, nothing is happening (texture is being rendered as per normal).

This is what I have so far.

	D3D11_BLEND_DESC omDesc;
	ZeroMemory(&omDesc,sizeof(D3D11_BLEND_DESC));
	omDesc.RenderTarget[0].BlendEnable=true;
	omDesc.RenderTarget[0].SrcBlend=D3D11_BLEND_SRC_ALPHA;
	omDesc.RenderTarget[0].DestBlend=D3D11_BLEND_INV_SRC_ALPHA;
	omDesc.RenderTarget[0].BlendOp=D3D11_BLEND_OP_ADD;
	omDesc.RenderTarget[0].SrcBlendAlpha=D3D11_BLEND_ONE;
	omDesc.RenderTarget[0].DestBlendAlpha=D3D11_BLEND_ZERO;
	omDesc.RenderTarget[0].BlendOpAlpha=D3D11_BLEND_OP_ADD;
	omDesc.RenderTarget[0].RenderTargetWriteMask=D3D11_COLOR_WRITE_ENABLE_ALL;

	if(FAILED(d3dDevice->CreateBlendState(&omDesc,&pBlendState)))
		return false;


	float BlendFactor[4];
	BlendFactor[0]=0.5;
	BlendFactor[1]=0.5;
	BlendFactor[2]=0.5;
	BlendFactor[3]=0.5;

	d3dContext->OMSetBlendState(pBlendState, BlendFactor, 0xffffffff);
From what I understand the BlendFactor should set the transparency to 50%. Although I could be wrong.

Any guidance would be greatly appreciated.

Share this post


Link to post
Share on other sites
Advertisement

You did not create the blend state with D3D11_BLEND_BLEND_FACTOR, so the blend factors are ignored.

 

 

L. Spiro

Share this post


Link to post
Share on other sites
Ahh cool, thanks for the reply.

Which line does D3D11_BLEND_BLEND_FACTOR need to go in to?

I have tried this but it wont compile

omDesc.RenderTarget[0].DestBlend=D3D11_BLEND_INV_SRC_ALPHA|D3D11_BLEND_BLEND_FACTOR;
As I want to be able to blend the image alpha as per normal as well. Edited by DarkRonin

Share this post


Link to post
Share on other sites
As you can see, these are not binary flags and cannot be combined.


	omDesc.RenderTarget[0].SrcBlend=D3D11_BLEND_BLEND_FACTOR;
	omDesc.RenderTarget[0].DestBlend=D3D11_BLEND_INV_BLEND_FACTOR;

L. Spiro

Share this post


Link to post
Share on other sites
Hmmm ok. I see now.

This is what I have now. But the images tend to get brighter as they fade off. And at maximum alpha (1) the images are still only partially transparent.
 
	D3D11_BLEND_DESC omDesc;
	ZeroMemory(&omDesc,sizeof(D3D11_BLEND_DESC));
	omDesc.RenderTarget[0].BlendEnable=true;
	omDesc.RenderTarget[0].SrcBlend=D3D11_BLEND_SRC_ALPHA;
	omDesc.RenderTarget[0].DestBlend=D3D11_BLEND_BLEND_FACTOR;//D3D11_BLEND_INV_SRC_ALPHA|
	omDesc.RenderTarget[0].BlendOp=D3D11_BLEND_OP_ADD;
	omDesc.RenderTarget[0].SrcBlendAlpha=D3D11_BLEND_ONE;
	omDesc.RenderTarget[0].DestBlendAlpha=D3D11_BLEND_ZERO;
	omDesc.RenderTarget[0].BlendOpAlpha=D3D11_BLEND_OP_ADD;
	omDesc.RenderTarget[0].RenderTargetWriteMask=D3D11_COLOR_WRITE_ENABLE_ALL;

	if(FAILED(d3dDevice->CreateBlendState(&omDesc,&pBlendState)))
		return false;

	float BlendFactor[4];
	BlendFactor[0]=1;
	BlendFactor[1]=1;
	BlendFactor[2]=1;
	BlendFactor[3]=1;

	d3dContext->OMSetBlendState(pBlendState, BlendFactor, 0xffffffff);
Is there a better way to handle transparency? Like in a shader?

This is my current general purpose shader. But, I don't know a great deal about shaders so I am wary about going in and changing too much.
 
Texture2D colorMap_ : register( t0 );
SamplerState colorSampler_ : register( s0 );

cbuffer cbChangesEveryFrame : register(b0)
{
	matrix worldMatrix;
};

cbuffer cbNeverChanges : register(b1)
{
	matrix viewMatrix;
};

cbuffer cbChangeOnResize : register(b2)
{
	matrix projMatrix;
}

struct VS_Input
{
	float4 pos  : POSITION;
	float2 tex0 : TEXCOORD0;
};

struct PS_Input
{
	float4 pos  : SV_POSITION;
	float2 tex0 : TEXCOORD0;
};

PS_Input VShader( VS_Input vertex )
{
	PS_Input vsOut = ( PS_Input )0;

	vsOut.pos=mul(vertex.pos,worldMatrix);
	vsOut.pos=mul(vsOut.pos,viewMatrix);
	vsOut.pos=mul(vsOut.pos,projMatrix);

	// vsOut.pos = vertex.pos;
	vsOut.tex0 = vertex.tex0;

	return vsOut;
}

float4 PShader( PS_Input frag ) : SV_TARGET
{
    return colorMap_.Sample( colorSampler_, frag.tex0 );
}
Any help is greatly appreciated smile.png

L. Spiro your help is always awesome by the way! Edited by DarkRonin

Share this post


Link to post
Share on other sites

as they fade off

As which alpha fades? The source’s or the blend factor’s?
 

And at maximum alpha

Which alpha is maximum? The source’s or the blend factor’s?



Look at the math you are performing.
SRC.RGB * SRC.A + DST.RGB * {1,1,1}
If SRC.A increases, naturally you will have a brighter image. You aren’t modulating the background at all, just adding it.


Is there a better way to handle transparency? Like in a shader?

The shader simply provides an alpha value that can be used in blending to the current render target.
Standard blending is:
	omDesc.RenderTarget[0].SrcBlend=D3D11_BLEND_SRC_ALPHA;
	omDesc.RenderTarget[0].DestBlend=D3D11_BLEND_INV_SRC_ALPHA;
	omDesc.RenderTarget[0].BlendOp=D3D11_BLEND_OP_ADD;
This assumed your texture has an alpha channel. If not, there will be no blending.

If you want to override the blending across the whole image, you can either use the following shader with the above blend-mode:
float4 PShader( PS_Input frag ) : SV_TARGET
{
    float4 fRet = colorMap_.Sample( colorSampler_, frag.tex0 );
    fRet.a = 0.5;
    return fRet;
}
…or you can set the blend factor:
	omDesc.RenderTarget[0].SrcBlend=D3D11_BLEND_BLEND_FACTOR;
	omDesc.RenderTarget[0].DestBlend=D3D11_BLEND_INV_BLEND_FACTOR;
	omDesc.RenderTarget[0].BlendOp=D3D11_BLEND_OP_ADD;

…

	static const float BlendFactor[4] = { 0.5f, 0.5f, 0.5f, 0.5f };

L. Spiro Edited by L. Spiro

Share this post


Link to post
Share on other sites
Ahh cool, the shader example works for me. Easy to understand too. smile.png

Very confusing making the jump from DX9 fixed function to DX11.

Share this post


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

  • Advertisement