Jump to content
  • Advertisement
Sign in to follow this  
belfegor

[DX9][C++]Stencil buffer basics

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

I need help understanding pixel rejecting/discarding by using stencil buffer. First i create depth stencil at device creation time:
PresentParams.AutoDepthStencilFormat = D3DFMT_D24S8;// 8 bit for stencil
then i create 2 spheres of same size.
D3DXCreateSphere(dev, 1.0f, 32, 32, &Sphere0, 0);
...
D3DXCreateSphere(dev, 1.0f, 32, 32, &Sphere1, 0);
then at render time (half pseudo):
dev->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL, 0x00000000, 1.0f, 0L);
dev->BeginScene();

// add litle offset
Sphere0WorldMatrix._41 += 0.5f;
shader->SetTech("tech0");
...// set matrices, color...
Sphere0->DrawSubset(0);// (blue color)
shader->End;

Sphere1WorldMatrix.identity();
shader->SetTech("tech1");
...// set matrices, color...
Sphere1->DrawSubset(0);// (yellow color)
shader->End;

dev->EndScene();
dev->Present(0,0,0,0);
tech0 technique just set witch pixel/vertex shader to use (i guess by this it uses default values for stencil) tech1 is same as tech0 except i add this:
StencilEnable     = true;   // Enable stencil if not
StencilFunc       = EQUAL;
StencilRef        = ?;	// <-what to put here
StencilMask       = ?; // <-and here
StencilPass       = REPLACE; // what is replaced, old pixel color or current one? And with what
StencilZFail      = KEEP;    // what is keeped, old or current pixel?
How to reject/discard yellow color spheres pixels that isnt intersecting blue ones so that i get intersecting yellow "parts" drawn on blue sphere?

Share this post


Link to post
Share on other sites
Advertisement
StencilPass and StencilZFail only control what happens to the value in the stencil buffer, it doesn't affect pixel colors at all. So for instance if StencilPass is "REPLACE", then the stencil buffer value will be replaced with the reference value. StencilZFail does the same thing, except it controls what happens when the stencil test passes but the z-test fails.

Whether the pixel color value is written depends on your StencilFunc, the reference value, and the value in the stencil buffer. So for instance if the ref value is 1 and you set StencilFunc to EQUAL, then pixels will only get written where the stencil buffer has a value of 1.

In your case, what you want to do is first render the blue sphere with a ref value of 1 and StencilFunc = ALWAYS. This will store a value of 1 in the stencil buffer for all pixels where the sphere was drawn. Then when drawing the yellow sphere, switch StencilFunc to EQUAL and the yellow sphere will only get drawn where the blur sphere was already drawn.

Share this post


Link to post
Share on other sites
Thank you.

solution:

tech0 //blue sphere
{
ZWriteEnable = true;
StencilEnable = true;
StencilRef = 1;
StencilMask = 0;
StencilFunc = ALWAYS;
StencilPass = REPLACE;
StencilZFail = KEEP;
}

tech1 //yellow sphere
{
ZWriteEnable = true;
StencilEnable = true;
StencilRef = 1;
StencilMask = 1;
StencilFunc = EQUAL;
StencilPass = REPLACE;
StencilZFail = KEEP;
}

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!