【发布时间】:2015-09-21 19:47:07
【问题描述】:
我正在使用 OpenTK(OpenGL 4 的 C# 包装器)制作 2D 游戏,除了多边形的锯齿状边缘以及跳跃和口吃而不是平滑移动之外,一切都很好 - 所以我正在尝试添加多重采样到抗锯齿我的纹理。
我的设置有几个相机,它们将所有场景对象渲染到 FrameBufferObject 纹理上(我希望这是 MSAA),然后将它们全部绘制到屏幕上(不需要多重采样),一个在另一个之上。
没有多重采样,它工作正常,但现在我尝试将我的所有 Texture2D 调用更改为 Texture2DMultisample 等,但现在我收到 FBO Not Complete 错误并且它绘制错误。我相信我也需要更改我的着色器,但我想先解决这个问题。
下面的代码引用了我制作的一些类,例如 Texture,但我认为这不会影响这一点,我不想让帖子混乱 - 如果需要,将提供 mroe 详细信息。
我为每个摄像机设置了 FBO:
private void SetUpFBOTex()
{
_frameBufferTexture = new Texture(GL.GenTexture(), Window.W, Window.H);
GL.BindTexture(TextureTarget.Texture2DMultisample, _frameBufferTexture.ID);
GL.TexImage2DMultisample(TextureTargetMultisample.Texture2DMultisample, 0, PixelInternalFormat.Rgba8, Window.W, Window.H, true);
_frameBufferID = GL.GenFramebuffer();
}
并使用:
public void Render(Matrix4 matrix)
{
GL.Enable(EnableCap.Multisample);
//Bind FBO to be the draw destination and clear it
GL.BindFramebuffer(FramebufferTarget.Framebuffer, _frameBufferID);
GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2DMultisample, _frameBufferTexture.ID, 0);
GL.ClearColor(new Color4(0,0,0,0));
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
//draw stuff here
foreach (Layer l in Layers)
l.Render(Matrix);
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
//Bind the FBO to be drawn
_frameBufferTexture.Bind();
//Translate to camera window position
Matrix4 fbomatrix = matrix * Matrix4.CreateTranslation(_window.x, _window.y, 0) * FBOMatrix;
//Bind shader
shader.Bind(ref fbomatrix, DrawType);
//Some OpenGL setup nonsense, binding vertices and index buffer and telling OpenGL where in the vertex struct things are, pointers &c
GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBuffer);
GL.EnableVertexAttribArray(shader.LocationPosition);
GL.VertexAttribPointer(shader.LocationPosition, 2, VertexAttribPointerType.Float, false, Stride, 0);
if (shader.LocationTexture != -1)
{
GL.EnableVertexAttribArray(shader.LocationTexture);
GL.VertexAttribPointer(shader.LocationTexture, 2, VertexAttribPointerType.Float, false, Stride, 8);
}
GL.EnableVertexAttribArray(shader.LocationColour);
GL.VertexAttribPointer(shader.LocationColour, 4, VertexAttribPointerType.UnsignedByte, true, Stride, 16);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, indexBuffer);
//Draw the damn quad
GL.DrawArrays(DrawType, 0, Vertices.Length);
//Cleanup
GL.DisableVertexAttribArray(shader.LocationPosition);
if (shader.LocationTexture != -1)
GL.DisableVertexAttribArray(shader.LocationTexture);
GL.DisableVertexAttribArray(shader.LocationColour);
}
【问题讨论】:
-
FBO 有深度附件吗?我没有看到它附加在发布的代码中,但您正在清除深度缓冲区这一事实表明存在一个。
-
我不相信它,我什至不确定我什至不知道它是什么..
-
我真的不明白您希望通过创建具有 0 个样本的多重采样纹理来实现什么?这可能就是它不完整的原因。
-
好的@Andon,这大概是我做错了什么?您能否解释一下如何使用 8 个样本创建多重采样纹理?我在这里超出了我的深度..
-
这是您调用
GL.TexImage2DMultisample的第二个参数