   [DX9][C++]Stencil buffer basics
  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 stencilthen 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 offsetSphere0WorldMatrix._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 notStencilFunc  = EQUAL;StencilRef   = ?;// <-what to put hereStencilMask  = ?; // <-and hereStencilPass  = REPLACE; // what is replaced, old pixel color or current one? And with whatStencilZFail = KEEP;    // what is keeped, old or current pixel?How to reject/discard yellow color spheres pixels that isnt intersecting blue ones sothat i get intersecting yellow "parts" drawn on blue sphere?   
  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.   ;  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;}