OpenTK模板缓冲区

时间:2012-02-23 09:32:59

标签: c# opengl opentk stencil-buffer

我是opengl的新手,我正在努力让stencilbuffer为一个简单的案例工作; 我有两个纹理,一个是位图,一个是“掩码”,我试图从位图中隐藏一些部分。

当我尝试使用GL.TexImage2D创建模板纹理时设置模板格式时,我似乎无法使用此功能,我得到一个无效的枚举,当我尝试将一个stencilextension附加到FramebufferTexture2D用于fbo我画了我的面具:

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

1 个答案:

答案 0 :(得分:1)

您无法渲染到FBO并期望模板操作,除非您实际上附加了带有模板的缓冲区。 FBO只有你给他们的缓冲区。

由于您使用的是EXT版本的FBO,因此您必须检查是否存在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);

这应该有所帮助。