FaytesEnd's picture

New to OpenTK and OpenGL - Question about Stencil and buffers

Hello all,
I'm pretty new to this, so please bear with me. I'm following two books (I read the first one, but it used TAO, so I'm slowly swapping over to opentk).

http://my.safaribooksonline.com/book/programming/csharp/9781435455566 C# game programming and,

I'm trying to follow though an example of using the stencil buffer. (if anyone happens to have the book, page 121)
Though it seems it doesn't work at all. In their example, using freeglut, they request a stencil buffer at application startup. Can someone
provide me with a simple learning example of this in opentk? I'm new to these forums and I'm not sure if I should post any code, or attach any projects.
I welcome all feedback and help and will gladly provide code or more information.

PS. The example goes as follows, clear the stencil buffer. Set it to fail all test. Draw a white spiral (I guess to the stencil buffer). Then draw a red square with a not equal flag set on the stencil buffer. This way the spiral is seen on the red square as it moves around.

Edit (info from the post I should have read first)
opengl problem
OS - Win7 64bit
gfx - Nvidia GeForce GTX 560 Ti
cpu - I7 (standard - non-oc)
.net 4.0
vs 2010

Specifically - it looks like the stencil buffer isn't being used at all. The red square, and white lines both draw completely on the blue background.


Comment viewing options

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


I wish I could help you out, as I'm struggling with the same issue.
I linked the source which should run out of the box, for something that should use the stencil buffer to mask a texture given a mask texture with some random triangles rendered into it.
Maybe an opentk expert could have a look and show us both how it should work: sourcecode (you'll only need to alter the code to point to a dummy texture)

PS: I posted this problem a few days ago on stackoverflow too: http://stackoverflow.com/questions/9410293/opentk-stencil-buffer

migueltk's picture

I would like to help. Everything would be easier if you share the source code where you have the problem.

Very good link of the book "C# Game Programming: For Serious Game Creation", I'm trying to compile the engine but general_font.tga, general_font.fnt, explode.tga, background_p.tga and background.tga files are missing.

Does anyone can provide me these files?

martijnh's picture
migueltk wrote:

I would like to help. Everything would be easier if you share the source code where you have the problem.

Hi Miguel,

I linked sourcecode which should be similar to what the OP is trying to achieve (I think), you can find it here: http://pastebin.com/iuur2UTM

FaytesEnd's picture

Ok, so I have it figured out.

You have to do a couple of things. First extend the GLControl class.

public class CustomOpenGLControl : GLControl
    public CustomOpenGLControl()
      : base(new GraphicsMode(32, 24, 8))

Then in your form designer... in place of the original SimpleOpenGLControl...

this.glControl1 = new CustomOpenGLControl();

When you extend the class and overwrite its constructor, you request a stencil buffer (the last argument, 8). Because its a custom control, you can't (at least to my knowledge) add it to your toolbox, so just tweak the designer a bit. This will provide the stencil buffer.

@martijnh I'm not using the game window, but it looks like the code posted does the same thing with overriding the base class.
@migueltk The files are on the disc that came with the book. But really, you could just make your own textures if you don't have the disc (wink wink). I would use paint.net for this, its easy and free.

I have a bit more working now from the superbible book. I have all chapter 3 examples working openTK with some design philosophy from the game creation book. I'll be checking and posting on these forums for help and to help.

Thanks guys.

PS. Check out the OpenTK solution, there's a stencil example in there and some debug classes for checking what the graphics card can handle. (OpenTK\1.0\Source\Examples\OpenTK\Test\TestGraphicsModes.cs)