无法在OpenTK中启用模板缓冲区,在简单的2D四边形中。

3
我一直尝试在我的OpenTK 2D游戏中实现模板测试,但没有成功 - 我想只绘制纹理的部分,其值在模板缓冲区中低于1。花费了很长时间阅读有关模板和它们如何工作的资料,但无法找到单个C#示例。下面的代码是我尝试从以下链接中的代码移植的结果: opengl stencil buffer - not quite got it working 链接为: http://pastebin.com/vJFumvQz 但我根本没有蒙版掩盖,只有一个绿色矩形内部是蓝色的。我怀疑我需要以某种方式初始化缓冲区?非常感谢任何帮助。
public class StencilTest : GameWindow
{
    int stencilBuffer;

    public StencilTest() :
        base(1, 1, GraphicsMode.Default, "Stencil test")
    {
        Width = 1500;
        Height = 800;
        VSync = VSyncMode.On;
        GL.ClearColor(Color4.Red);
        ClientSize = new Size(1500, 800);
        this.Location = new System.Drawing.Point(100,300);
        GL.Viewport(0, 0, Width, Height);


        GL.GenRenderbuffers(1, out stencilBuffer);
        GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, RenderbufferStorage.Depth24Stencil8, 1500, 800);
        GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, stencilBuffer);
    }

    protected override void OnRenderFrame(FrameEventArgs e)
    {
        GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.StencilBufferBit);

        GL.MatrixMode(MatrixMode.Projection);
        GL.LoadIdentity();
        GL.Ortho(-10, 10, -10, 10, -1, 1);

        GL.MatrixMode(MatrixMode.Modelview);
        GL.LoadIdentity();

        GL.Disable(EnableCap.StencilTest);
        GL.StencilMask(0x0);

        draw_background();

        GL.Enable(EnableCap.StencilTest);
        GL.StencilMask(0x1);
        GL.StencilFunc(StencilFunction.Always, 0x1, 0x1);
        GL.StencilOp(StencilOp.Keep, StencilOp.Keep, StencilOp.Replace);
        GL.ColorMask(false, false, false, false);
        GL.DepthMask(false);

        draw_triangle();

        GL.StencilMask(0x1);
        GL.StencilFunc(StencilFunction.Notequal, 0x1, 0x1);
        GL.StencilOp(StencilOp.Keep, StencilOp.Keep, StencilOp.Keep);
        GL.ColorMask(true, true, true, true);
        GL.DepthMask(true);

        draw_blue_square();

        SwapBuffers();
        base.OnRenderFrame(e);
    }


    void draw_background()
    {
        //big blue square
        GL.Color4(Color4.Blue);
        GL.Begin(PrimitiveType.Quads);
        GL.Vertex3(5, 5, 0);
        GL.Vertex3(-5, 5, 0);
        GL.Vertex3(-5, -5, 0);
        GL.Vertex3(5, -5, 0);
        GL.End();
    }

    void draw_triangle()
    {
        /* transparent triangle */
        GL.Color3(Color.White);
        GL.Begin(PrimitiveType.Triangles);
        GL.Vertex3(-4, -4, 0);
        GL.Vertex3(4, -4, 0);
        GL.Vertex3(0, 4, 0);
        GL.End();
    }

    void draw_blue_square()
    {
        /* blue square */
        GL.Color4(Color4.Green);
        GL.Begin(PrimitiveType.Quads);
        GL.Vertex3(3, 3, 0);
        GL.Vertex3(-3, 3, 0);
        GL.Vertex3(-3, -3, 0);
        GL.Vertex3(3, -3, 0);
        GL.End();
    }
}
1个回答

3

看起来你没有模板缓冲区。你的代码的这部分是尝试获取模板缓冲区:

GL.GenRenderbuffers(1, out stencilBuffer);
GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, RenderbufferStorage.Depth24Stencil8, 1500, 800);
GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, stencilBuffer);

但在这种情况下,你不需要它。这将分配一个模板缓冲区,你可以在渲染到帧缓冲对象(又称 FBO)时使用它,通过将其附加为 FBO 的深度模板附件。但在你的代码中,你正在渲染到默认帧缓冲区,因此上述操作不会产生任何影响。
当渲染到默认帧缓冲区时,通常需要在初始设置期间请求所有所需的缓冲区。我不熟悉 OpenTK,但基于我在 GraphicsContext documentation 中找到的内容,你需要创建一个类似于以下内容的 GraphicsMode 实例:
new GraphicsMode(32, 24, 8, 0)

在设置上下文时,您需要将其传递。这指定您想要一个32位颜色缓冲区,一个24位深度缓冲区和一个8位模板缓冲区。我怀疑这应该替换您在代码中使用的GraphicsMode.Default

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接