Masking away primitives in 2D?

Started by
2 comments, last by jacksaccountongamedev 20 years ago
Hello... How would I go about ‘masking’ away areas of a primitive when rendering a two dimensional game? That is – I want to be able to render graphics so as that only part of them show up. I understand that there are several options, some more attractive than others: Stencil Buffer: I think I could tackle this one with relative ease – render the areas to be masked into the stencil buffer then render the scene under the stencil. + From my understanding, easy to do - Apparently not well supported on old hardware Depth Buffer: No idea on how it would be done using the depth buffer but apparently it is possible. + I hear that depth buffers are better supported than Stencil Buffers - No idea how it works Render Targets: Not even sure if this would work or not, but if it does it might just be the best of the lot: Render objects in scene not to be masked Clear render target A with solid white Clear render target B with 0x00000000 Render masked areas to A as 0x00000000 (fully transparent) Render primitives to be masked to target B Somehow (not exactly sure if it can be done) render onLY the alpha component of target A to target B (the big quad). Render target B to backbuffer and present + The objects in the scene can make good use of bilinear filtering + Possible better supported that the Stencil and Depth Buffers - Large amounts of memory consumed in the two render targets - Alpha-blended objects cannot be masked properly If possible, I would like to include ALL of these options in my application so as that the player can chose the one that is the most efficient on their system. Basically I am looking for information on how to use the Depth Buffer to do this… any help or explanations? And information on the last method I described using Render Targets? Specifically I need to know if and how I can render a primitive in a way so as that only the alpha component is rendered, while the RGB components are ignored. Some combination of alpha rendering flags? And help and suggestions are appreciated… Jackson Allan
Advertisement
Yes the stencil buffer would be your best solution and yes it os not often fully supported on older hardware, but then what game supports old hardware! Why do you think you keep buying new video cards every 6/12 months for?

The depth buffer method you mention involves using invisible (fully transparent) primitives to mask sections of sprites. This is achieved by rendering the mask primitive in front of the sprite plane. When the sprite is rendered beneath a portion of this primitive it will be masked out by the depth buffer.

Your render target method is essentially just an alternative stencil buffer. In order to render only the alpha channel look for the appropiate texop flags or use the pixel shader.

Another method that you may not realise is using the alpha channel of the primitive texture itself (or alternatively an additional primative texture level) to mask the primative. Simply draw the masking areas to the second texture level and perform an additional texture operation when rendering.

I hope that has been of help.

Thanks for the information.
Just to see if I got this right (I have not used depth buffers before):
Render world geometry etc with depth buffer off
Render masked areas into the depth buffer just above the Z value that we would be rendering the game objects
Render game objects
Turn off depth buffer and render interface (or would it be better to render the interface just above the mask that is the depth buffer?)

That sound right?

I feel that the render target method needs a bit more explaining as I failed to mention a key point. It is necessary that I reuse the stencil to render a large alpha-blended masked quad over the entire scene - that is why I wish draw the masked areas on a separate surface and then copy the alpha information on from that surface to the second target (as opposed to simply drawing the game objects on a single target and then rendering the masked areas onto that same target).
Anyhow, I''ve done some searching and some trial and error but I cannot figure out the correct combination of flags that allow me to render only the alpha component and ignore the color information. If possible I would like to steer well away from pixel shaders as it is my understanding that they are slow things to use. Any suggestions with this would be great...

I''m afraid I don''t really understand the last method you mentioned, although I believe it may not be suitable as many of my masked primitives will not in fact have textures (such as lines drawn with a line strip).

Thanks for the help,
Jackson Allan
Bump. Sorry, I have having to do this...

This topic is closed to new replies.

Advertisement