我是opengl的新手,我正在努力让stencilbuffer为一个简单的案例工作;我有两种纹理,一种是位图,另一种是“蒙版”,我试图用它来隐藏位图中的某些部分。
我似乎无法让它工作,当我尝试设置模板格式以使用 GL.TexImage2D 创建我的模板纹理时,我得到一个无效的枚举,当我尝试为 fbo 将模板扩展附加到 FramebufferTexture2D 时,我绘制了我的掩码:
GL.Enable(EnableCap.StencilTest);
GL.ClearStencil(0);
GL.StencilMask(0xFFFFFFFF); // read&write
// Create stencil texture
GL.GenTextures(1, out stencilTexture);
GL.BindTexture(TextureTarget.Texture2D, stencilTexture);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, stencilTextureWidth, stencilTextureHeight, 0, OpenTK.Graphics.OpenGL.PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);
//DOES NOT WORK: GL.TexImage2D(TextureTarget.Texture2D, 0, (PixelInternalFormat)All.StencilIndex, stencilTextureWidth, stencilTextureHeight, 0, OpenTK.Graphics.OpenGL.PixelFormat.StencilIndex, PixelType.UnsignedByte, IntPtr.Zero);
CREATE COLORTEXTURE FROM BITMAP
// Create a FBO and attach the stencil texture
GL.Ext.GenFramebuffers(1, out fbo);
GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, fbo);
GL.Ext.FramebufferTexture2D(FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0Ext, TextureTarget.Texture2D, stencilTexture, 0);
//DOES NOT WORK?: GL.Ext.FramebufferTexture2D(FramebufferTarget.FramebufferExt, FramebufferAttachment.StencilAttachmentExt, TextureTarget.Texture2D, stencilTexture, 0);
DRAW SOME STUFF INTO THE STENCILTEXTURE TO FUNCTION AS A MASK
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.StencilBufferBit);
DRAW COLOR TEXTURE
GL.Enable(EnableCap.StencilTest);
GL.ClearStencil(0);
GL.ColorMask(false, false, false, false);
GL.StencilFunc(StencilFunction.Always, 1, 1);
GL.StencilOp(StencilOp.Keep, StencilOp.Keep, StencilOp.Replace);
DRAW THE STENCIL TEXTURE
GL.Disable(EnableCap.StencilTest);
对于简单的 2d 案例(使用纹理掩盖纹理),我似乎找不到任何示例来证明这一点。
编辑:此处更新版本:http: //pastebin.com/iuur2UTM