Jump to content

  • Log In with Google      Sign In   
  • Create Account


Trying to set alpha

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

#1 DarkRonin   Members   -  Reputation: 604

Like
0Likes
Like

Posted 01 July 2014 - 05:34 PM

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.

Sponsor:

#2 L. Spiro   Crossbones+   -  Reputation: 12274

Like
4Likes
Like

Posted 01 July 2014 - 05:50 PM

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

 

 

L. Spiro


It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#3 DarkRonin   Members   -  Reputation: 604

Like
0Likes
Like

Posted 01 July 2014 - 05:54 PM

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, 01 July 2014 - 05:56 PM.


#4 L. Spiro   Crossbones+   -  Reputation: 12274

Like
3Likes
Like

Posted 01 July 2014 - 06:04 PM

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
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#5 DarkRonin   Members   -  Reputation: 604

Like
0Likes
Like

Posted 01 July 2014 - 06:11 PM

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, 01 July 2014 - 06:16 PM.


#6 L. Spiro   Crossbones+   -  Reputation: 12274

Like
2Likes
Like

Posted 01 July 2014 - 06:20 PM

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, 01 July 2014 - 06:25 PM.

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#7 DarkRonin   Members   -  Reputation: 604

Like
0Likes
Like

Posted 01 July 2014 - 06:32 PM

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.





PARTNERS