我必须遵循以下代码:
public void BeginDraw()
{
if(bufferMaterial != null)
{
bufferMaterial.Textures[0].Surface.BindFramebuffer();
beganDraw = true;
}
}
public void EndDraw()
{
if (beganDraw)
{
beganDraw = false;
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
if (bufferMaterial != null)
{
GL.BindBuffer(BufferTarget.ArrayBuffer, screenMesh.VBO);
GL.BindVertexArray(VAO);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, screenMesh.VEO);
bufferMaterial.Use();
screenMesh.ApplyDrawHints(bufferMaterial.Shader);
GL.DrawElements(PrimitiveType.Triangles, 2, DrawElementsType.UnsignedInt, 0);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
GL.BindVertexArray(0);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.UseProgram(0);
}
}
}
在这两个之间我绘制了我的场景,因此应该将图像绘制到bufferMaterial.Textures [0] .Surface。
现在“screenMesh”创建如下:
screenMesh = new Mesh();
screenMesh.SetVertices(new float[] {
0,0,
1,0,
1,1,
0,1
});
screenMesh.SetIndices(new uint[] {
0,3,2,
0,1,2
});
screenMesh.SetDrawHints(new VertexObjectDrawHint("pos", 2, 2, 0));
- > 2个组件,步幅为2,0,偏移
着色器看起来像这样:
++++++++
Screenbuffer
++++++++
[Shader vertexScreen]
#version 150 core
in vec2 pos;
uniform float _time;
uniform sampler2D tex;
void main() {
gl_Position = vec4(pos, -1, 1);
}
[Shader fragmentScreen]
#version 150 core
#define PI 3.1415926535897932384626433832795
out vec4 outColor;
uniform float _time;
uniform sampler2D tex;
//texture2D(tex,
void main() {
outColor = vec4(1,1,1,1);
}
现在,我希望这会在屏幕上绘制一个白色矩形,但屏幕保持黑色。我玩了一下screenMesh的指数,所以可能会关闭,但它们永远不可见......
我不需要像这样的着色器的任何投影矩阵吗?
网格类:http://pastebin.com/PcwEYqGH
编辑:好的,现在渲染缓冲区,但我仍然需要让深度缓冲区正常工作!表面创建代码如下所示:
public void Create(int width, int height, SurfaceFormat format)
{
Width = width;
Height = height;
textureHandle = GL.GenTexture();
//bind texture
GL.BindTexture(TextureTarget.Texture2D, textureHandle);
Log.Error("Bound Texture: " + GL.GetError());
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)format.WrapMode);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)format.WrapMode);
Log.Error("Created Texture Parameters: " + GL.GetError());
GL.TexImage2D(TextureTarget.Texture2D, 0, format.InternalFormat, Width, Height, 0, format.PixelFormat, format.SourceType, format.Pixels);
Log.Error("Created Image: " + GL.GetError());
//unbind texture
GL.BindTexture(TextureTarget.Texture2D, 0);
//create depthbuffer
if (format.DepthBuffer)
{
GL.GenRenderbuffers(1, out dbHandle);
GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, dbHandle);
GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, RenderbufferStorage.DepthComponent24, Width, Height);
}
//create fbo
fboHandle = GL.GenFramebuffer();
GL.BindFramebuffer(FramebufferTarget.Framebuffer, fboHandle);
GL.FramebufferTexture2D(FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0Ext, TextureTarget.Texture2D, textureHandle, 0);
if(format.DepthBuffer)
GL.FramebufferRenderbuffer(FramebufferTarget.FramebufferExt, FramebufferAttachment.DepthAttachment, RenderbufferTarget.Renderbuffer, dbHandle);
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
Log.Error("Created Framebuffer: " + GL.GetError());
}
这是绑定它的代码:
public void BindTexture(TextureUnit slot = TextureUnit.Texture0)
{
GL.ActiveTexture(slot);
GL.BindTexture(TextureTarget.Texture2D, textureHandle);
}
public void BindFramebuffer()
{
GL.BindFramebuffer(FramebufferTarget.Framebuffer, fboHandle);
}
然而,如果启用深度缓冲区,屏幕将再次变黑。
编辑:忘记清除深度缓冲区!
答案 0 :(得分:3)
您的平局电话有问题:
GL.DrawElements(PrimitiveType.Triangles, 2, DrawElementsType.UnsignedInt, 0);
第二个参数是用于渲染的索引数。只有2个指数,这对于一个完整的三角形是不够的。索引数组中有6个索引,因此它应该是:
GL.DrawElements(PrimitiveType.Triangles, 6, DrawElementsType.UnsignedInt, 0);
除此之外,我还无法找到您正在使用的Mesh
类的代码或文档,因此无法确定网格中存储的顶点数据是否设置正确。