[MDX] Direct X 9 Stencils

This topic is 4345 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi everyone, I’m rather new to direct X programming. I have a texture, 32x32 of a solid color, say red. I have another texture, 32x32 of a solid color say blue. I then render two triangles formed as a “Quad?” square that covers a large portion of the screen. I do this by setting the U and V modes to Wrap so my 32x32 blue texture covers the entire quad by tiling. Next I create another square the same way, but this one is smaller and centered one top of and in the middle of the larger blue quad. My Goal and the purpose of this question. How would I go about dissolving the smaller red quad so as it dissolves I can see the blue quad beneath it? I’m not so much interested in fading from red to blue and I don’t want to place more triangles over the red to make it appear as if the red is dissolving. So does that leave me with stencils? Currently I think it does, but I’m not 100% sure. I’m looking for a managed Direct X 9 example (c# or vb) that will help me understand how to do something like this. I have found a few examples that show how to create shadows with stencil buffers but I’m having trouble getting just the stencil part out plus it’s in C++ and not managed, which don’t make it impossible to wade through but does make it harder, for me. Thanks for any ideas or pointers to the right spot for this info! [Edited by - adam_p on March 29, 2006 3:37:08 PM]

Share on other sites
I don't really understand by what you mean by dissolving but I can point you to some C# and Managed DirectX tutorials on stencil shadows and some simple shadows.

I hope the links provided will put you on the right track.
Take care.

Share on other sites
Hi Thanks for the ideas, using one of the examples I was able to come up with the code below, however it does not work right just yet.

Also I wanted to post this screen of an example of what I'm trying to do, a picture is worth a thousand words right :)

A is my current drawing, I put the black lines in to show my triangles.
B is my goal. assuming I'm using a z buffer or depth buffer the order I draw the red or blue quads in should not matter I think.

However I'm not able to make the chunks of the red quad appear missing, as they are in this mockup.

        'clear the color buffer, stencil buffer and depth buffer        mDevice.Clear(ClearFlags.Target, Drawing.Color.Black, 1, 0)        mDevice.Clear(ClearFlags.Stencil, Drawing.Color.Black, 1, 0)        mDevice.Clear(ClearFlags.ZBuffer, Drawing.Color.Black, 1, 0)        'I'm not using any lighting at all        mDevice.RenderState.Lighting = False        'setup ortho projection and view matrix        SetupCamera()        'I don't care about the order of my vertex, draw every triangle        mDevice.RenderState.CullMode = Cull.None        'standard begin scene        mDevice.BeginScene()        'Enable the stencil buffer        mDevice.RenderState.StencilEnable = True        mDevice.RenderState.StencilFunction = Compare.Always 'always pass the stencil test        'Set the render states as to what will happen if individual states fail.        mDevice.RenderState.StencilPass = StencilOperation.Replace        mDevice.RenderState.StencilWriteMask = -1        'disable writes to the zbuffer and disabling writes to the color buffer        mDevice.RenderState.ZBufferWriteEnable = False        mDevice.RenderState.SourceBlend = Blend.One        mDevice.RenderState.DestinationBlend = Blend.One        'Render the area I'm trying to hide to the stencil buffer        'set texture to 32x32 black texture        'mDevice.DrawUserPrimitives(PrimitiveType.TriangleList, Verts.Length \ 3, VertsBlocking)        '        'Enable depth buffer and back buffer writes        mDevice.RenderState.ZBufferWriteEnable = True        mDevice.RenderState.ReferenceStencil = 0 'The reference value to test against the value in the stencil buffer         mDevice.RenderState.StencilFunction = Compare.Greater        mDevice.RenderState.StencilPass = StencilOperation.Keep        'render the red square        'set texture to 32x32 red texture        'mDevice.DrawUserPrimitives(PrimitiveType.TriangleList, Verts.Length \ 3, VertsRed)        '        'turn off the stencil buffer        mDevice.RenderState.StencilEnable = False        'render the blue square        'set texture to 32x32 blue texture        'mDevice.DrawUserPrimitives(PrimitiveType.TriangleList, Verts.Length \ 3, VertsBlue)        '        mDevice.EndScene()        mDevice.Present()

Share on other sites
Do you mean, one image is drawn and another is stencilled over it? Such as, there is a red box. Circles appear with a radius of 1 pixel and increase in radius until they make up the whole rendertarget, so the original image is no longer visible?

Share on other sites
An example of that nature might help. Really I'm trying to hide pixels from a quad when it's drawn. If you see example A, it wouldn't be too hard to imagine how to do that. Take two textures, say a red one and a blue one. Render one quad then the other.

Now the trick is to figure out how to use a stencil buffer to hide pixels from one quad so that the quad below it shows through.

I remember reading about how to create bullet holes and missing chunks of walls with stencils. That must work something like what I'm looking for.

Are they called decals?

I’m starting to think this might not be possible.

Share on other sites
        With D3DDevice.RenderState            .StencilEnable = True            D3DDevice.Clear(ClearFlags.Stencil, Color.Black, 0, 0)            .ColorWriteEnable = Nothing            .StencilFunction = Compare.Always            .StencilPass = StencilOperation.Increment            DXRender2DBox(128, 128, 256, 256, Color.White.ToArgb, 45)            .ColorWriteEnable = ColorWriteEnable.RedGreenBlueAlpha            .StencilFunction = Compare.Equal            .StencilPass = StencilOperation.Keep            DXRender2DBox(0, 0, 512, 512, Color.White.ToArgb, 0)            .StencilEnable = False        End With

DXRender2DBox, my own function, takes the parameters X1,Y1,X2,Y2,Color,Angle

This draws me a box from 0,0 to 512,512 with a nice diamond hole at 128,128 to 256,256.

It should be easy for you to modify this to draw "cut out" holes, which increase in size to reveal something rendered under the image.

Edit: If you just want to make box cutout holes, this code should be what you need, replacing DXRender2DBox with your own functions and just drawing 10 boxes or something.

Share on other sites
great stuff! Thank you very much. I assume in this example

<code>
mDevice.RenderState.ReferenceStencil = 0
</code>

would be used?

So in effect what you saying is that if the buffer data equals the reference number draw the pixel?