Last active
March 28, 2016 20:52
-
-
Save vasumahesh1/13773644bc50493c34de to your computer and use it in GitHub Desktop.
Gist explaining Blending State and where to put what while coding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Step 1 is to add: | |
// The alpha state to your Mesh Object. | |
// In my case I have a base MeshObject which is derived to all other objects | |
// -- MeshObject.h | |
ref class MeshObject | |
{ | |
// | |
// Other code | |
// | |
Microsoft::WRL::ComPtr<ID3D11BlendState> m_alphaBlendState; | |
// | |
// Other Code | |
// | |
virtual void Render(_In_ ID3D11DeviceContext *context); // You'll probably have something similar like this | |
} | |
// Step 2 is to Create a BlendingState and while creating this State we pass the `m_alphaBlendState` reference | |
// -- TileMesh.cpp : public MeshObject | |
TileMesh(_In_ ID3D11Device *device) | |
{ | |
// Constructor | |
// | |
// Other Code | |
// | |
D3D11_BLEND_DESC blendStateDescription; | |
ZeroMemory(&blendStateDescription, sizeof(D3D11_BLEND_DESC)); | |
// [More Info](https://blogs.msdn.microsoft.com/rchiodo/2012/11/20/blending-demystified/) | |
blendStateDescription.RenderTarget[0].BlendEnable = TRUE; | |
blendStateDescription.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD; // Set the Blend Mode to ADD which will elimate the black color | |
blendStateDescription.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD; // Same Here | |
blendStateDescription.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA; // Inverting the SRC Alpha | |
blendStateDescription.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO; // Blend 0 means 1.0 * 0.0 = 0.0 (OP_ADD) | |
blendStateDescription.RenderTarget[0].RenderTargetWriteMask = 0x0f; | |
blendStateDescription.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE; // Blend 1 means 1.0 * 1.0 = 1.0 (OP_ADD) | |
blendStateDescription.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE; // Blend 0 means 1.0 * 1.0 = 1.0 (OP_ADD) | |
AzuraException::ThrowIfFailed( | |
device->CreateBlendState(&blendStateDescription, &m_alphaBlendState) // Notice the Reference from the Base class's Property | |
); | |
} | |
// Step 3 is to attach this m_alphaBlendState to the Render Phase where we tell the Device to use this Alpha while rendering this mesh | |
// -- MeshObject.cpp | |
void Render(_In_ ID3D11DeviceContext *context) | |
{ | |
// | |
// Other Code | |
// | |
// 1st Param is our Alpha State | |
// 2nd Param is BlendFactors: we can set that to NULL as it didn't have an effect when I set it to other values | |
// 3rd is Mask: We can use the default value | |
// Ref: https://msdn.microsoft.com/en-us/library/windows/desktop/ff476462(v=vs.85).aspx | |
context->OMSetBlendState(m_alphaBlendState.Get(), NULL, 0xffffffff); | |
// | |
// Other Code | |
// | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment