Jump to content

Game Dev Help, Directx Chroma-Key

Go to solution Solved by Unimportant,

I mean:

D2D1_VECTOR_4F Vec4F = {0.0f, 1.0f, 0.0f, 0,0f};
chromakeyEffect->SetValue(D2D1_CHROMAKEY_PROP_COLOR, Vec4F);

D2D1_VECTOR_4F is defined in D2D1_1.h

The enum you posted is in D2d1effects_2.h so that should also be included.

So am doing a small project but go stuck at chroma-key ID2D1Bitmap. MSDN don't actually explain the application so their syntax clearly. Has anyone had any experience using the following?

 

m_d2dContext->CreateEffect(CLSID_D2D1ChromaKey, &chromakeyEffect);
	chromakeyEffect->SetInput(ChromaValue, GameBmpAsset);
	chromakeyEffect->SetValue(D2D1_CHROMAKEY_PROP_COLOR, { 0.0f, 1.0f, 0.0f, 0.0f });
	chromakeyEffect->SetValue(D2D1_CHROMAKEY_PROP_TOLERANCE, 0.2f);
	chromakeyEffect->SetValue(D2D1_CHROMAKEY_PROP_INVERT_ALPHA, false);
	chromakeyEffect->SetValue(D2D1_CHROMAKEY_PROP_FEATHER, false);

Link to comment
https://linustechtips.com/topic/737097-game-dev-help-directx-chroma-key/
Share on other sites

Link to post
Share on other sites

2 hours ago, xiiijamaican said:

So am doing a small project but go stuck at chroma-key ID2D1Bitmap. MSDN don't actually explain the application so their syntax clearly. Has anyone had any experience using the following?

Can you be more specific about what exactly you're stuck with ? The given example is rather simple:

/* Create a ComPtr (Smart-pointer (with reference count) to a com 
interface, ID2D1Effect in this case, that will automatically release 
the interface when the reference count hits 0). CreateEffect will make 
this pointer point to the newly created effect.*/
ComPtr<ID2D1Effect> chromakeyEffect; 

/* Create effect with ID CLSID_D2D1ChromaKey and make chromakeyEffect 
pointer point to it. List of built in effects: 
https://msdn.microsoft.com/en-us/library/windows/desktop/hh706316(v=vs.85).aspx */
m_d2dContext->CreateEffect(CLSID_D2D1ChromaKey, &chromakeyEffect);

/* Set the input image for the effect. GameBmpAsset is a ID2D1Image pointer, 
pointing to a image that should've been loaded earlier already, ChromaValue 
is a self chosen index for the asset. */
chromakeyEffect->SetInput(ChromaValue, GameBmpAsset);

/* Set the values for this effect...
values enum for chromakey effect: 
https://msdn.microsoft.com/en-us/library/windows/desktop/dn890719(v=vs.85).aspx

D2D1_CHROMAKEY_PROP_COLOR and a vector4 that identifies the color to key, Green in this case (RGBA) */
chromakeyEffect->SetValue(D2D1_CHROMAKEY_PROP_COLOR, { 0.0f, 1.0f, 0.0f, 0.0f });

/*D2D1_CHROMAKEY_PROP_TOLERANCE and a float that'll set the tolarance, The color 
deviation allowed for any given color in the image and the color still beeing keyed*/
chromakeyEffect->SetValue(D2D1_CHROMAKEY_PROP_TOLERANCE, 0.2f);

/*D2D1_CHROMAKEY_PROP_INVERT_ALPHA and a bool that indicates whether 
alpha values should be inverted normally, 0 = transparant and 0xFF is 
opaque, when true, the range is inverted*/
chromakeyEffect->SetValue(D2D1_CHROMAKEY_PROP_INVERT_ALPHA, false);

/*D2D1_CHROMAKEY_PROP_FEATHER and a bool that indicates edges should be 
softened in the alpha channel*/
chromakeyEffect->SetValue(D2D1_CHROMAKEY_PROP_FEATHER, false);

//Draw...
m_d2dContext->BeginDraw();
m_d2dContext->DrawImage(chromakeyEffect.Get());
m_d2dContext->EndDraw();

Also note that functions like createeffect indicate success or failure in their return value, you should always check return values.

Link to post
Share on other sites

OK so I am trying to apply chroma-key to a bitmap. I use the WICFactory to get, create, and convert the bitmap image to an ID2D1Bitmap so that the directx chroma-key function can read it and do the chroma-key. When I implement the function though I am getting 1 build error and I see what the error is saying but I don't understand where it want my to make the changes to satisfy what its asking for... see the images below. If you want to have a look on the source code let me know also.  

error02.PNG

header.PNG

headercpp.PNG

Link to post
Share on other sites

Have you included the required header for vector4 in the code file with the error ?

https://msdn.microsoft.com/en-us/library/nuiapi.vector4.aspx

 

Looks a lot like it fails to recognize it should initialise a vector4 with the brace initialiser {..,..,..,..}.

Also try manually creating a vector4 and passing it to the function if the header is included.

Link to post
Share on other sites

Do you mean the following or I should just do a vector with values?

typedef enum D2D1_CHROMAKEY_PROP { 
  D2D1_CHROMAKEY_PROP_COLOR         = 0,
  D2D1_CHROMAKEY_PROP_TOLERANCE     = 1,
  D2D1_CHROMAKEY_PROP_INVERT_ALPHA  = 2,
  D2D1_CHROMAKEY_PROP_FEATHER       = 3
} D2D1_CHROMAKEY_PROP;

 

Link to post
Share on other sites

I mean:

D2D1_VECTOR_4F Vec4F = {0.0f, 1.0f, 0.0f, 0,0f};
chromakeyEffect->SetValue(D2D1_CHROMAKEY_PROP_COLOR, Vec4F);

D2D1_VECTOR_4F is defined in D2D1_1.h

The enum you posted is in D2d1effects_2.h so that should also be included.

Link to post
Share on other sites

13 minutes ago, Unimportant said:

I mean:


D2D1_VECTOR_4F Vec4F = {0.0f, 1.0f, 0.0f, 0,0f};
chromakeyEffect->SetValue(D2D1_CHROMAKEY_PROP_COLOR, Vec4F);

D2D1_VECTOR_4F is defined in D2D1_1.h

The enum you posted is in D2d1effects_2.h so that should also be included.

Does this matter whether I use ID2D1Effect* chromakeyEffect; or ComPtr<ID2D1Effect> chromakeyEffect; ??

Link to post
Share on other sites

11 hours ago, xiiijamaican said:

Does this matter whether I use ID2D1Effect* chromakeyEffect; or ComPtr<ID2D1Effect> chromakeyEffect; ??

ID2D1Effect inherits from IUnknown (trough ID2D1Properties):

https://msdn.microsoft.com/en-us/library/windows/desktop/ms680509(v=vs.85).aspx

 

When you are done using the effect it should be released (with the release() method).

ComPtr is a RAII smart-pointer that automatically releases the object it holds when it itself goes out of scope.

 

So it's better to use the ComPtr, as it will automate the releasing, preventing bugs and leaks.

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×