3

私は OpenTK 2D ゲームでステンシル テストを機能させることに成功していません。ステンシル バッファーで、たとえば 1 の値を下回るテクスチャの部分のみを描画したいと考えています。ステンシルとその仕組みについて何年もかけて読んできましたが、C# で 1 つの例を見つけることができません。以下のコードは、以下のリンクからコードを移植しようとした私の試みです。

opengl ステンシル バッファ - うまく動作していません

つまり: 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();
    }
}
4

1 に答える 1