Soundbomber's picture

Cut outs

I am trying to acheive the appearance of cut outs or holes in solid objects. Can anyone tell me a good way to approach this? I have considered using alpha blending but without much success.
I would like my "cut-out object" to be transparent when in front of my "solid object".
Where the "cut-out object" intersects my "solid object", I would like the intersection to be visible.
Is this acheivable?


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Inertia's picture

http://www.opentk.com/node/765

Is some documentation about the Stencil Test, way more detailed than OpenGL spec.

Please note that I said in my 2nd post that CSG *is* a hard problem. Doing a google search for 'OpenGL CSG stencil' should lead you towards implementations that you can port to C# or at least dllimport, if the link I provided above doesn't help you further. I'd have written a test app the very same day if the websearch didn't yield so many hits regarding that topic, but since it has been done ~10 years ago already I don't really have much interest in following it further, since CSG is not of practical use to me. Hope you understand ;)

objarni's picture

Soundbomber: are you using a GameWindow or the GLControl?

The built-in GLControl does not request a stencil buffer. So you cannot use stencil techniques with GLControl!

the Fiddler's picture

To be pedantic, you can use the stencil buffer with the GLControl> You just have to inherit from it and request a stencil buffer from the new constructor, as discussed here.

Soundbomber's picture

Yes I am using the GLControl!!!!!
Inertia - Totally understand. I am just glad for any leads at all.

objarni's picture

Soundbomber: then you should be able to follow Fiddlers advice "as discussed here" above.

Fiddler: wouldn't it be nice of the GLControl had properties for what mode to instantiate/what buffers to allocate? A category "Mode" with bools "StencilBuffer", "DepthBuffer" and so on? Is there some technical problem with this approach maybe?

the Fiddler's picture
objarni wrote:

Fiddler: wouldn't it be nice of the GLControl had properties for what mode to instantiate/what buffers to allocate? A category "Mode" with bools "StencilBuffer", "DepthBuffer" and so on? Is there some technical problem with this approach maybe?

We are lacking a custom designer for the GraphicsMode class. If someone creates one, I will gladly integrate it into OpenTK.

Also note that it's not easy to change these parameters after startup. You'll have to recreate the control for the changes to "take", which has the unfortunate side-effect of destroying all OpenGL resources associated with that context.

Inertia's picture

I've been curious whether the theory works, but it's not sufficient and a couple more passes are needed. The basic theory to use stencil and depth test to find intersections was correct though, it just didn't handle both: the front and back side.

Cube - Sphere

AttachmentSize
StencilCSG.jpg14.3 KB
objarni's picture

Inertia - that's fantastic! Great stuff.

kanato's picture
the Fiddler wrote:

We are lacking a custom designer for the GraphicsMode class. If someone creates one, I will gladly integrate it into OpenTK.

This can be done really easily with ExpandableObjectConverter, but that would require making the properties of GraphicsMode have public setters. How would you feel about changing this, maybe with an internal readonly property that is set in the context, and causes an exception to be thrown if the graphics mode is changed after creating a context?

amakeda's picture

A simple example in VB:

        GL.Enable(EnableCap.StencilTest)
        GL.ClearStencil(0)
        GL.StencilMask(1)        
 
        GL.StencilFunc(StencilFunction.Never, 1, 1)
        GL.StencilOp(StencilOp.Replace, StencilOp.Replace, StencilOp.Replace)
 
        GL.Color4(255, 0, 0, 1.0)
        GL.Begin(BeginMode.Polygon)
        GL.Vertex3(20, 20, 0)
        GL.Vertex3(-20, 20, 0)
        GL.Vertex3(-20, -20, 0)
        GL.Vertex3(20, -20, 0)
        GL.End()
 
        GL.StencilFunc(StencilFunction.Greater, 1, 1)
        GL.StencilOp(StencilOp.Keep, StencilOp.Keep, StencilOp.Keep)
 
        GL.Color4(0, 0, 255, 1.0)
        GL.Begin(BeginMode.Polygon)
        GL.Vertex3(30, 30, 0)
        GL.Vertex3(-30, 30, 0)
        GL.Vertex3(-30, -30, 0)
        GL.Vertex3(30, -30, 0)
        GL.End()
 
        GL.Disable(EnableCap.StencilTest)