OpenTK 模板缓冲区
我是 opengl 的新手,我正在努力让模板缓冲区适用于一个简单的情况; 我有两种纹理,一种是位图,一种是“蒙版”,我试图用它来隐藏位图中的某些部分。
我似乎无法让它工作,当我尝试设置使用 GL.TexImage2D 创建模板纹理的模板格式时,我得到一个无效的枚举,当我尝试将 stencilextension 附加到 FramebufferTexture2D 的 fbo 时,我绘制了我的mask in:
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
I'm new to opengl and I'm struggling to get the stencilbuffer to work for a simple case;
I have two textures, one being a bitmap and one being a "mask", with which I'm trying to hide some parts from the bitmap.
I can't seem to get this to work, when I try to set the stencil format on creating my stencil texture using GL.TexImage2D I get an invalid enum, and when I try to attach a stencilextension to FramebufferTexture2D for the fbo I draw my mask in:
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);
I can't seem to find any examples that demonstrate this for a simple 2d case (masking textures using textures).
EDIT:
Updated version here: http://pastebin.com/iuur2UTM
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法渲染到 FBO 并期望模板操作能够正常工作,除非您实际上将带有模板的缓冲区附加到它。 FBO 仅有您提供的缓冲区。
由于您使用的是 FBO 的 EXT 版本,因此必须检查是否存在 EXT_packed_depth_stencil 扩展。如果存在,那么您可以执行以下操作(注意:这是 C。您必须将其转换为 OpenTK 和 C# 代码):
这应该会有所帮助。
You can't render to an FBO and expect stencil operations to work unless you actually attach a buffer with stencil to it. FBOs only have the buffers you give them.
Since you're using the EXT version of FBO, you have to check for the presence of the EXT_packed_depth_stencil extension. If that's there, then you can do the following (note: this is C. You'll have to translate it to OpenTK and C# code):
That should help.