2

我是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

4

1 回答 1

1

除非您实际将带有模板的缓冲区附加到它,否则您不能渲染到 FBO 并期望模板操作能够工作。FBO 只有你给他们的缓冲区。

由于您使用的是 FBO 的 EXT 版本,因此您必须检查是否存在EXT_packed_depth_stencil扩展。如果存在,那么您可以执行以下操作(注意:这是 C。您必须将其转换为 OpenTK 和 C# 代码):

GLuint renderBuffer;
glGenRenderbuffers(1, &renderBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, renderBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8_EXT, stencilTextureWidth, stencilTextureHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER, renderBuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER, renderBuffer);

那应该有帮助。

于 2012-02-23T16:25:19.663 回答