• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
belfegor

Using stencil operations

4 posts in this topic

I need someone to help me understand concepts around this topic.
I have read the documentation but not quite get it.

[quote]
D3DRS_STENCILFUNC
The comparison function is used to compare the reference value to a stencil buffer entry.
This comparison applies only to the bits in the reference value and stencil buffer entry that are set in the stencil mask (set by the D3DRS_STENCILMASK render state).
If TRUE, the stencil test passes.
[/quote]

Let me go step by step.

1.
First i use Clear function to clear BB & DS surface with stencil of 0.
Then i draw some object with:

[CODE]
D3DDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
D3DDevice->SetRenderState(D3DRS_STENCILREF, 1);
D3DDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
D3DDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
D3DDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP);
D3DDevice->SetRenderState(D3DRS_STENCILREF, 1);// this sets value of 1 to stencil where some object is drawn? Right?
sphere1->Draw();
[/CODE]

Then i draw some other object overlaping first one with:

[CODE]
D3DDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_EQUAL);
D3DDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
D3DDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP);
D3DDevice->SetRenderState(D3DRS_STENCILREF, 1);// value to compare with previous one? Also writes this one to stencil?
sphere2->Draw();
[/CODE]

2. Considering D3DRS_STENCILZFAIL, is this the case (where yellow color is drawn) in this picture?

[img]http://www.dodaj.rs/f/19/uf/tNlxuba/zfail.jpg[/img]

3. About D3DRS_STENCILMASK and D3DRS_STENCILWRITEMASK, how is this used? What is it "masking"/"write-masking"? I don't understand.
Can you please show me some example pseudo code?
0

Share this post


Link to post
Share on other sites
You're a bit vague about exactly what order the stuff in your drawing is drawn in, but I think you've got essentially the right idea. Just keep in mind that stencil ref doesn't do anything by itself, it just feeds the other operations. Your first block of states will set the stencil to 1 only where stencil and depth test pass. In the second block, you tell it to set the stencil to 1 again when both tests pass, which implies that stencil was already 1. So the second draw call will never change the stencil value. You also don't seem to be setting the stencil fail value, I don't know if that's intentional or not.

Check [url="http://msdn.microsoft.com/en-us/library/windows/desktop/bb206123(v=vs.85).aspx"]this article[/url] and see if it helps clarify the masks. It basically does a bitwise operation on the stencil values to determine which bits are significant and which are ignored for the rest of the stencil operation.
1

Share this post


Link to post
Share on other sites
1.
Ok. I get this one:
[code]
(ref & mask) ComparisonOperation (oldValue & mask)
[/code]

But what about D3DRS_STENCILWRITEMASK? I don't see where it is used in this expression?
Docs says its default value is 0xFFFFFFFF same as mask.

2. From article you pointed:
[quote]
You can write your own formula for the value you want written into the stencil buffer as shown in the following example.
[code]
NewStencilBufferValue = (StencilBufferValue & ~StencilWriteMask) |
(StencilWriteMask & StencilOp(StencilBufferValue))
[/code]
[/quote]

How to set render states to get this formula?
This makes my brain hurt.[img]http://public.gamedev.net//public/style_emoticons/default/blink.png[/img]

3.
[quote]
You're a bit vague about exactly what order the stuff in your drawing is drawn in...
[/quote]

First i want to understand this a bit better as i wish later to do some decals/splatting on walls. So it means i first draw walls and then spheres to tag pixels that "intersect".
0

Share this post


Link to post
Share on other sites
Basically what it boils down to is the following code:
[code]
if (<Boolean epression setup by D3DRS_STENCILFUNC>)
{
//Here you specify what operation to do when the stencil test passes
stencilValue = <Equation setup by D3DRS_STENCILPASS>;
}
else
{
//Stencil test fails
stencilValue = <Equation setup by D3DRS_STENCILFAIL>;
}
[/code]

I explain this with a code example as this is what the hardware is actually executing and I find this easier to understand then just looking at the API calls.

The masks involved in these equations are only of interest if you want to only look at say the first few bits of the actual stencil value or write to those bits. So when these are set to 0xFFFFFFFF you will write and read from the whole stencil value, when set to 0xFF000000 you will only look at/write to the first 16 bits of the stencil value (this is a 32bit value, it gets converted to 8 bits later on I believe). Bear in mind that the read and write mask don't need to have the same value.

The actual stencil test equation is just a comparison and [url="http://msdn.microsoft.com/en-us/library/windows/desktop/bb172517(v=vs.85).aspx"]D3DRS_STENCILFUNC[/url] sets which type of boolean expression is used to do the stencil test: "<", ">", "=", etc.
Then stencil [url="http://msdn.microsoft.com/en-us/library/windows/desktop/bb172610(v=vs.85).aspx"]D3DRS_STENCIL[/url]FAIL or D3DRS_STENCILPASS defines what operation is done to the stencilValue: "keep", "+1", "-1", etc when the test passes or fails. Edited by NightCreature83
1

Share this post


Link to post
Share on other sites
I have another related problem. I tried to reder fullscreen quad for directional light, but i want to affect everything except sky, here is my setup (but its wrong):
[code]
// ---------------------- Draw g-buffer
setRenderState(D3DRS_STENCILENABLE, TRUE);
setRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
setRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_KEEP);
setRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP);
setRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
setRenderState(D3DRS_STENCILREF, 5);
setRenderTarget(diffuseRT);
setRenderTarget(positionRT);
setRenderTarget(normalRT);
drawToGBuffer();
setRenderTarget(1, nullptr);
setRenderTarget(2, nullptr);

// ---------------------- Draw sky
setRenderState(D3DRS_STENCILFUNC, D3DCMP_GREATEREQUAL);
setRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_KEEP);
setRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP);
setRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
setRenderState(D3DRS_STENCILREF, 3);
drawSkyBox();
setRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
setRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
setRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
drawCloudsLayers();
setRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
setRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
setRenderState(D3DRS_DESTBLEND, D3DBLEND_ZERO);

// ---------------------- Draw fsquad for dir light
setRenderState(D3DRS_STENCILFUNC, D3DCMP_EQUAL);
setRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_KEEP);
setRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP);
setRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);// If equal to gbuffer (5) draw to it, else keep? But its not?
setRenderState(D3DRS_STENCILREF, 5);
setTexture(positionRT);
setTexture(normalRT);
drawFullscreenQuad();// dir light
[/code]

Please advice. Thanks for your time. Edited by belfegor
0

Share this post


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
Sign in to follow this  
Followers 0