Original Post
My alpha blending is not correct. I am not sure what to use. Am I doing my alpha blend state incorrectly? My Result:
1 of 3 the flare images they privided (it has no alpha channel):
What I think the problem is: Thier code: My code:
1 of 3 the flare images they privided (it has no alpha channel):
What I think the problem is:
BlendState SrcAlphaAdd
{
BlendEnable[0] = TRUE;
SrcBlend = SRC_ALPHA;
DestBlend = ONE;
BlendOp = ADD;
SrcBlendAlpha = ONE;
DestBlendAlpha = ONE;
BlendOpAlpha = ADD;
RenderTargetWriteMask[0] = 0x0F;
};
foreach (Flare flare in flares)
{
// Compute the position of this flare sprite.
Vector2 flarePosition = lightPosition + flareVector * flare.Position;
// Set the flare alpha based on the previous occlusion query result.
Vector4 flareColor = flare.Color.ToVector4();
flareColor.W *= occlusionAlpha;
// Center the sprite texture.
Vector2 flareOrigin = new Vector2(flare.Texture.Width,
flare.Texture.Height) / 2;
// Draw the flare.
spriteBatch.Draw(flare.Texture, flarePosition, null,
new Color(flareColor), 1, flareOrigin,
flare.Scale, SpriteEffects.None, 0);
}
spriteBatch.End();
//--------------------------------------------------------------------------------------
// File: LensFlare.fx
//
//--------------------------------------------------------------------------------------
#include "EffectPool.fxh"
matrix world : World;
matrix worldInverseTranspose : WorldInverseTranspose;
//-------------------
// Diffuse Variables
//
// Diffuse Color can be a single color or sampled from a texture
float4 diffuseColor = float4(1.0f, 1.0f, 1.0f, 1.0f);
Texture2D diffuseTexture;
SamplerState samplerLinear
{
Filter = MIN_MAG_MIP_LINEAR;
AddressU = Wrap;
AddressV = Wrap;
};
//////// CONNECTOR DATA STRUCTURES ///////////
struct VS_IN_OCCLUSION
{
float4 position : POSITION;
};
struct PS_IN_OCCLUSION
{
float4 position : SV_POSITION;
};
struct VS_IN_TINT
{
float4 position : POSITION;
float2 texCoord : TEXCOORD;
};
struct PS_IN_TINT
{
float4 position : SV_POSITION;
float2 texCoord : TEXCOORD;
};
//////////// STATES ///////////////
BlendState SrcAlphaBlend
{
BlendEnable[0] = TRUE;
SrcBlend = SRC_ALPHA;
DestBlend = INV_SRC_ALPHA;
BlendOp = ADD;
SrcBlendAlpha = ONE;
DestBlendAlpha = ONE;
BlendOpAlpha = ADD;
RenderTargetWriteMask[0] = 0x0F;
};
BlendState SrcAlphaAdd
{
BlendEnable[0] = TRUE;
SrcBlend = SRC_ALPHA;
DestBlend = ONE;
BlendOp = ADD;
SrcBlendAlpha = ONE;
DestBlendAlpha = ONE;
BlendOpAlpha = ADD;
RenderTargetWriteMask[0] = 0x0F;
};
BlendState OcclusionAlphaBlend
{
BlendEnable[0] = FALSE;
RenderTargetWriteMask[0] = 0x00; // Write no colors
};
DepthStencilState OcclusionDepthStencil
{
DepthEnable = TRUE;
DepthWriteMask = ZERO;
DepthFunc = Less;
StencilEnable = FALSE;
};
//--------------------------------------------------------------------------------------
// Vertex Shaders
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
// Transforms positions
//
PS_IN_OCCLUSION VS_Occlusion( VS_IN_OCCLUSION input )
{
PS_IN_OCCLUSION output = (PS_IN_OCCLUSION)0;
// Transform the incoming model-space position to projection space
matrix worldViewProjection = mul( mul(world, view), projection);
output.position = mul( input.position, worldViewProjection);
return output;
}
//--------------------------------------------------------------------------------------
// Transforms positions and copies texture coordinates
//
PS_IN_TINT VS_Tint( VS_IN_TINT input )
{
PS_IN_TINT output = (PS_IN_TINT)0;
// Transform the incoming model-space position to projection space
matrix worldViewProjection = mul( mul(world, view), projection);
output.position = mul( input.position, worldViewProjection);
// Copy the texture coordinate
output.texCoord = input.texCoord;
return output;
}
//--------------------------------------------------------------------------------------
// Pixel Shaders
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
// Colors the object with the diffuse color
//
float4 PS_Occlusion( PS_IN_OCCLUSION input ) : SV_Target
{
return diffuseColor;
}
//--------------------------------------------------------------------------------------
// Colors the object with the diffuse color multiplied by the texture color
//
float4 PS_Tint( PS_IN_TINT input ) : SV_Target
{
return diffuseColor * diffuseTexture.Sample(samplerLinear, input.texCoord);
}
//--------------------------------------------------------------------------------------
// Techniques
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
//
//
technique10 OcclusionQuery
{
pass P0
{
SetVertexShader( CompileShader( vs_4_0, VS_Occlusion() ) );
SetGeometryShader( NULL );
SetPixelShader( CompileShader( ps_4_0, PS_Occlusion() ) );
SetBlendState( OcclusionAlphaBlend, float4(0.0f, 0.0f, 0.0f, 0.0f), 0xFFFFFFFF);
SetDepthStencilState( DefaultDepthStencil, 0);
}
}
//--------------------------------------------------------------------------------------
//
//
technique10 RenderGlow
{
pass P0
{
SetVertexShader( CompileShader( vs_4_0, VS_Tint() ) );
SetGeometryShader( NULL );
SetPixelShader( CompileShader( ps_4_0, PS_Tint() ) );
SetBlendState( SrcAlphaBlend, float4(0.0f, 0.0f, 0.0f, 0.0f), 0xFFFFFFFF);
SetDepthStencilState( DefaultDepthStencil, 0);
}
}
//--------------------------------------------------------------------------------------
// Renders an object at 100% opacity, with no lights or shadows
//
technique10 RenderFlare
{
pass P0
{
SetVertexShader( CompileShader( vs_4_0, VS_Tint() ) );
SetGeometryShader( NULL );
SetPixelShader( CompileShader( ps_4_0, PS_Tint() ) );
SetBlendState( SrcAlphaAdd, float4(0.0f, 0.0f, 0.0f, 0.0f), 0xFFFFFFFF);
SetDepthStencilState( DefaultDepthStencil, 0);
}
}
D3DXVECTOR2 flareVector = screenCenter - lightPosition;
// Bind the input layout
m_device.IASetInputLayout(m_flareInputLayout);
m_device.IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
// Bind the vertex buffers
ID3D10Buffer * buffers[2] = {m_positionBuffer->GetD3DBuffer(),
m_texCoordBuffer->GetD3DBuffer()};
UINT strides[2] = {GetStride(m_positionBuffer->GetContentType()),
GetStride(m_texCoordBuffer->GetContentType())};
UINT offsets[2] = {0,
0};
m_device.IASetVertexBuffers(0,
2,
buffers,
strides,
offsets);
for( std::vector<Flare>::iterator it = m_flares.begin(); it != m_flares.end(); ++it)
{
// Set the world matrix and material
D3DXMATRIX matScale;
D3DXMATRIX matTranslation;
D3DXMATRIX world;
D3DXVECTOR2 flarePosition = lightPosition + flareVector * it->m_position;
D3DXMatrixScaling(&matScale, it->m_scale, it->m_scale, 1.0f);
D3DXMatrixTranslation(&matTranslation, flarePosition.x, flarePosition.y, 0.0f);
D3DXMatrixMultiply(&world, &matScale, &matTranslation);
D3DXCOLOR flareColor = it->m_color;
flareColor.a = m_occlusionAlpha;
m_flareMaterial->SetFloat4("diffuseColor", static_cast<D3DXVECTOR4>(flareColor));
m_flareMaterial->SetTextureName("diffuseTexture", it->m_textureName);
try
{
m_effect->SetWorldMatrix(world);
m_effect->SetMaterial(*m_flareMaterial);
}
catch(BaseException & e)
{
throw e;
}
// Apply the pass
m_flarePass->Apply();
// Draw the flare
m_device.Draw(4, 0);
}




