private void PanelLoad(object sender, EventArgs e)
{
// VBO
SetupVBO();
// camera setup
SetupCamera();
// FBO
SetupFBO();
}
private void SetupVBO()
{
GL.Arb.GenBuffers(1, out _vbo.VboID);
GL.Arb.BindBuffer(BufferTargetArb.ArrayBuffer, _vbo.VboID);
GL.Arb.BufferData(BufferTargetArb.ArrayBuffer,
(IntPtr)(data.Length * 10 * sizeof(float)),
data,
BufferUsageArb.StaticDraw);
GL.Arb.GenBuffers(1, out _vbo.EboID);
GL.Arb.BindBuffer(BufferTargetArb.ElementArrayBuffer, _vbo.EboID);
GL.Arb.BufferData(BufferTargetArb.ElementArrayBuffer,
(IntPtr)(indices.Length * sizeof(uint)),
indices,
BufferUsageArb.StaticDraw);
GL.InterleavedArrays(InterleavedArrayFormat.C4fN3fV3f, 0, IntPtr.Zero);
}
private void SetupFBO()
{
GL.Enable(EnableCap.DepthTest);
GL.ClearDepth(1.0);
GL.DepthFunc(DepthFunction.Lequal);
GL.Disable(EnableCap.CullFace);
GL.PolygonMode(MaterialFace.Back, PolygonMode.Line);
// create color tex
GL.GenTextures(1, out _fbo.TextureID);
GL.BindTexture(TextureTarget.Texture2D, _fbo.TextureID);
GL.TexImage2D(TextureTarget.Texture2D,
0,
PixelInternalFormat.Rgb8,
_panel.Width,
_panel.Height,
0,
PixelFormat.Rgba,
PixelType.UnsignedByte,
IntPtr.Zero);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToBorder);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToBorder);
// create depth tex
GL.GenTextures(1, out _fbo.DepthBufferID);
GL.BindTexture(TextureTarget.Texture2D, _fbo.DepthBufferID);
GL.TexImage2D(TextureTarget.Texture2D,
0,
(PixelInternalFormat)All.DepthComponent,
_panel.Width,
_panel.Height,
0,
PixelFormat.DepthComponent,
PixelType.UnsignedInt,
IntPtr.Zero);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToBorder);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToBorder);
// create FBO
GL.Ext.GenFramebuffers(1, out _fbo.FboID);
GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, _fbo.FboID);
GL.Ext.FramebufferTexture2D(FramebufferTarget.FramebufferExt,
FramebufferAttachment.ColorAttachment0Ext,
TextureTarget.Texture2D,
_fbo.TextureID,
0);
GL.Ext.FramebufferTexture2D(FramebufferTarget.FramebufferExt,
FramebufferAttachment.DepthAttachment,
TextureTarget.Texture2D,
_fbo.DepthBufferID,
0);
CheckFBO();
GL.PushAttrib(AttribMask.ViewportBit);
{
GL.Viewport(0, 0, _panel.Width, _panel.Height);
// clear the screen in red, to make it very obvious what the clear affected. only the FBO, not the real framebuffer
GL.ClearColor(1f, 0f, 0f, 0f);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
RenderVBO();
}
GL.PopAttrib();
GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0); // disable rendering into the FBO
//GL.ClearColor(.1f, .2f, .3f, 0f);
GL.ClearColor(Color4.Red);
GL.Color3(1f, 1f, 1f);
GL.Enable(EnableCap.Texture2D); // enable Texture Mapping
GL.BindTexture(TextureTarget.Texture2D, 0); // bind default texture
}
private void RenderVBO()
{
// #region OpenGL-Setup
GL.Viewport(0, 0, _panel.Width, _panel.Height);
var look = cam.ModelViewProjectionMatrix;
GL.MatrixMode(MatrixMode.Projection);
GL.LoadMatrix(ref look);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
//GL.Enable(EnableCap.DepthTest);
//GL.Enable(EnableCap.Blend);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
GL.PolygonMode(MaterialFace.Front, PolygonMode.Fill);
GL.PolygonMode(MaterialFace.Back, PolygonMode.Fill);
//GL.Color3(.3, .3, .3);
GL.EnableClientState(ArrayCap.VertexArray);
GL.EnableClientState(ArrayCap.ColorArray);
GL.EnableClientState(ArrayCap.NormalArray);
GL.Arb.BindBuffer(BufferTargetArb.ArrayBuffer, _vbo.VboID);
GL.Arb.BindBuffer(BufferTargetArb.ElementArrayBuffer, _vbo.EboID);
GL.ColorPointer(4, ColorPointerType.Float, 10 * sizeof(float), IntPtr.Zero);
GL.NormalPointer(NormalPointerType.Float, 10 * sizeof(float), (IntPtr)(4 * sizeof(float)));
GL.VertexPointer(3, VertexPointerType.Float, 10 * sizeof(float), (IntPtr)(7 * sizeof(float)));
GL.DrawElements(BeginMode.Triangles, _vbo.NumElements, DrawElementsType.UnsignedInt, IntPtr.Zero);
GL.DisableClientState(ArrayCap.NormalArray);
GL.DisableClientState(ArrayCap.ColorArray);
GL.DisableClientState(ArrayCap.VertexArray);
}
private void PanelPaint(object sender, EventArgs e)
{
GL.Viewport(0, 0, _panel.Width, _panel.Height);
var look = cam.ModelViewProjectionMatrix;
GL.MatrixMode(MatrixMode.Projection);
GL.LoadMatrix(ref look);
//GL.ClearColor(Color4.DarkGray);
GL.ClearColor(Color4.Red);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.Enable(EnableCap.DepthTest);
GL.Enable(EnableCap.Blend);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
GL.PolygonMode(MaterialFace.Front, PolygonMode.Fill);
GL.PolygonMode(MaterialFace.Back, PolygonMode.Fill);
GL.Color3(.3, .3, .3);
GL.PushMatrix();
{
// Draw the Color Texture
GL.Translate(-1.1f, 0f, 0f);
GL.BindTexture(TextureTarget.Texture2D, _fbo.TextureID);
GL.Begin(BeginMode.Quads);
{
GL.TexCoord2(0f, 1f);
GL.Vertex2(-1.0f, 1.0f);
GL.TexCoord2(0.0f, 0.0f);
GL.Vertex2(-1.0f, -1.0f);
GL.TexCoord2(1.0f, 0.0f);
GL.Vertex2(1.0f, -1.0f);
GL.TexCoord2(1.0f, 1.0f);
GL.Vertex2(1.0f, 1.0f);
}
GL.End();
// Draw the Depth Texture
GL.Translate(+2.2f, 0f, 0f);
GL.BindTexture(TextureTarget.Texture2D, _fbo.DepthBufferID);
GL.Begin(BeginMode.Quads);
{
GL.TexCoord2(0f, 1f);
GL.Vertex2(-1.0f, 1.0f);
GL.TexCoord2(0.0f, 0.0f);
GL.Vertex2(-1.0f, -1.0f);
GL.TexCoord2(1.0f, 0.0f);
GL.Vertex2(1.0f, -1.0f);
GL.TexCoord2(1.0f, 1.0f);
GL.Vertex2(1.0f, 1.0f);
}
GL.End();
}
GL.PopMatrix();
_panel.SwapBuffers();
}