Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.Drawing.Drawing2D;
- using System.Runtime.InteropServices;
- using OpenTK;
- using OpenTK.Graphics;
- using OpenTK.Graphics.OpenGL;
- using OpenTK.Platform;
- using OpenTK.Platform.X11;
- using OpenTK.Input;
- namespace test
- {
- public class MainClass : GameWindow
- {
- public MainClass()
- : base(800, 600, new GraphicsMode(new ColorFormat(0, 0, 0, 0), 24, 8, 2)) {
- this.VSync = VSyncMode.Off;
- }
- public uint fbo;
- public uint stencilBuffer;
- public uint colorTexture;
- public int stencilBufferWidth = 1024;
- public int stencilBufferHeight = 1024;
- public int colorTextureWidth;
- public int colorTextureHeight;
- Random rand = new Random();
- protected override void OnLoad(EventArgs e) {
- GL.Enable(EnableCap.Blend);
- GL.Enable(EnableCap.StencilTest);
- GL.ClearStencil(0);
- GL.StencilMask(0xFFFFFFFF); // read&write
- GL.ShadeModel(ShadingModel.Smooth);
- GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
- GL.Disable(EnableCap.DepthTest);
- GL.Disable(EnableCap.CullFace);
- // Create stencil texture
- GL.GenRenderbuffers(1, out stencilBuffer);
- GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, (RenderbufferStorage)All.Depth24Stencil8Ext, stencilBufferWidth, stencilBufferHeight);
- GL.FramebufferRenderbuffer(FramebufferTarget.FramebufferExt, FramebufferAttachment.DepthAttachmentExt, RenderbufferTarget.RenderbufferExt, stencilBuffer);
- GL.FramebufferRenderbuffer(FramebufferTarget.FramebufferExt, FramebufferAttachment.StencilAttachmentExt, RenderbufferTarget.RenderbufferExt, stencilBuffer);
- Bitmap bmp = new Bitmap("c:/downloads/mona-lisa.bmp");
- colorTextureWidth = bmp.Width;
- colorTextureHeight = bmp.Height;
- //get the data out of the bitmap
- System.Drawing.Imaging.BitmapData bmpBits = bmp.LockBits
- (
- new System.Drawing.Rectangle(0, 0, colorTextureWidth, colorTextureHeight),
- System.Drawing.Imaging.ImageLockMode.ReadOnly,
- System.Drawing.Imaging.PixelFormat.Format32bppRgb
- );
- GL.TexImage2D
- (
- TextureTarget.Texture2D,
- 0,
- PixelInternalFormat.Rgba,
- colorTextureWidth,
- colorTextureHeight,
- 0,
- OpenTK.Graphics.OpenGL.PixelFormat.Bgra,
- PixelType.UnsignedByte,
- bmpBits.Scan0
- );
- 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);
- //free the bitmap data (we dont need it anymore because it has been passed to the OpenGL driver
- bmp.UnlockBits(bmpBits);
- // Create a FBO and attach the textures
- GL.GenFramebuffers(1, out fbo);
- GL.BindFramebuffer(FramebufferTarget.Framebuffer, fbo);
- GL.Ext.FramebufferTexture2D(FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0Ext, TextureTarget.Texture2D, colorTexture, 0);
- GL.Ext.BindRenderbuffer(RenderbufferTarget.RenderbufferExt, stencilBuffer);
- #region Test for Error
- switch (GL.Ext.CheckFramebufferStatus(FramebufferTarget.FramebufferExt)) {
- case FramebufferErrorCode.FramebufferCompleteExt: {
- Console.WriteLine("FBO: The framebuffer is complete and valid for rendering.");
- break;
- }
- case FramebufferErrorCode.FramebufferIncompleteAttachmentExt: {
- Console.WriteLine("FBO: One or more attachment points are not framebuffer attachment complete. This could mean there’s no texture attached or the format isn’t renderable. For color textures this means the base format must be RGB or RGBA and for depth textures it must be a DEPTH_COMPONENT format. Other causes of this error are that the width or height is zero or the z-offset is out of range in case of render to volume.");
- break;
- }
- case FramebufferErrorCode.FramebufferIncompleteMissingAttachmentExt: {
- Console.WriteLine("FBO: There are no attachments.");
- break;
- }
- case FramebufferErrorCode.FramebufferIncompleteDimensionsExt: {
- Console.WriteLine("FBO: Attachments are of different size. All attachments must have the same width and height.");
- break;
- }
- case FramebufferErrorCode.FramebufferIncompleteFormatsExt: {
- Console.WriteLine("FBO: The color attachments have different format. All color attachments must have the same format.");
- break;
- }
- case FramebufferErrorCode.FramebufferIncompleteDrawBufferExt: {
- Console.WriteLine("FBO: An attachment point referenced by GL.DrawBuffers() doesn’t have an attachment.");
- break;
- }
- case FramebufferErrorCode.FramebufferIncompleteReadBufferExt: {
- Console.WriteLine("FBO: The attachment point referenced by GL.ReadBuffers() doesn’t have an attachment.");
- break;
- }
- case FramebufferErrorCode.FramebufferUnsupportedExt: {
- Console.WriteLine("FBO: This particular FBO configuration is not supported by the implementation.");
- break;
- }
- default: {
- Console.WriteLine("FBO: Status unknown. (yes, this is really bad.)");
- break;
- }
- }
- // using FBO might have changed states, e.g. the FBO might not support stereoscopic views or double buffering
- int[] queryinfo = new int[6];
- GL.GetInteger(GetPName.MaxColorAttachmentsExt, out queryinfo[0]);
- GL.GetInteger(GetPName.AuxBuffers, out queryinfo[1]);
- GL.GetInteger(GetPName.MaxDrawBuffers, out queryinfo[2]);
- GL.GetInteger(GetPName.Stereo, out queryinfo[3]);
- GL.GetInteger(GetPName.Samples, out queryinfo[4]);
- GL.GetInteger(GetPName.Doublebuffer, out queryinfo[5]);
- Console.WriteLine("max. ColorBuffers: " + queryinfo[0] + " max. AuxBuffers: " + queryinfo[1] + " max. DrawBuffers: " + queryinfo[2] +
- "\nStereo: " + queryinfo[3] + " Samples: " + queryinfo[4] + " DoubleBuffer: " + queryinfo[5]);
- Console.WriteLine("Last GL Error: " + GL.GetError());
- #endregion Test for Error
- GL.PushAttrib(AttribMask.ViewportBit);
- {
- GL.Viewport(0, 0, stencilBufferWidth, stencilBufferHeight);
- OpenTK.Matrix4 orthogonal = OpenTK.Matrix4.CreateOrthographicOffCenter(0, 1, 0, 1, -3, 3);
- GL.MatrixMode(MatrixMode.Projection);
- GL.LoadMatrix(ref orthogonal);
- Matrix4 lookat = Matrix4.LookAt(0, 0, 1, 0, 0, 0, 0, 1, 0);
- GL.MatrixMode(MatrixMode.Modelview);
- GL.LoadMatrix(ref lookat);
- // clear the screen in red, to make it very obvious what the clear affected. only the FBO, not the real framebuffer
- GL.ClearColor(0f, 0f, 0f, 0f);
- GL.Clear(ClearBufferMask.ColorBufferBit);
- // draw 2 random triangles into the stencil texture
- GL.Begin(BeginMode.Triangles);
- {
- for (int i = 0; i < 2; i++) {
- GL.Color4(((float)(rand.NextDouble())), ((float)(rand.NextDouble())), ((float)(rand.NextDouble())), ((float)(rand.NextDouble())));
- GL.Vertex3(((float)(rand.NextDouble())), ((float)(rand.NextDouble())), 0);
- GL.Color4(((float)(rand.NextDouble())), ((float)(rand.NextDouble())), ((float)(rand.NextDouble())), ((float)(rand.NextDouble())));
- GL.Vertex3(((float)(rand.NextDouble())), ((float)(rand.NextDouble())), 0);
- GL.Color4(((float)(rand.NextDouble())), ((float)(rand.NextDouble())), ((float)(rand.NextDouble())), ((float)(rand.NextDouble())));
- GL.Vertex3(((float)(rand.NextDouble())), ((float)(rand.NextDouble())), 0);
- }
- }
- GL.End();
- }
- GL.PopAttrib();
- GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0); // disable rendering into the FBO
- GL.ClearColor(1f, 1f, 1f, 0.0f);
- GL.Color3(1f, 1f, 1f);
- GL.Enable(EnableCap.Texture2D); // enable Texture Mapping
- GL.Enable(EnableCap.StencilTest);
- GL.BindTexture(TextureTarget.Texture2D, 0); // bind default texture
- }
- protected override void OnUnload(EventArgs e) {
- // Clean up what we allocated before exiting
- GL.DeleteTextures(1, ref stencilBuffer);
- GL.DeleteTextures(1, ref colorTexture);
- GL.Ext.DeleteFramebuffers(1, ref fbo);
- base.OnUnload(e);
- }
- protected override void OnResize(EventArgs e) {
- GL.Viewport(0, 0, Width, Height);
- double aspect_ratio = Width / (double)Height;
- OpenTK.Matrix4 perspective = OpenTK.Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4 * 3 / 2, (float)aspect_ratio, 1, 64);
- GL.MatrixMode(MatrixMode.Projection);
- GL.LoadMatrix(ref perspective);
- Matrix4 lookat = Matrix4.LookAt(0, 0, 3, 0, 0, 0, 0, 1, 0);
- GL.MatrixMode(MatrixMode.Modelview);
- GL.LoadMatrix(ref lookat);
- base.OnResize(e);
- }
- protected override void OnUpdateFrame(FrameEventArgs e) {
- base.OnUpdateFrame(e);
- if (Keyboard[Key.Escape]) {
- this.Exit();
- }
- }
- protected override void OnRenderFrame(FrameEventArgs e) {
- this.Title = "Frames per Second: " + (1.0 / e.Time);
- GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.StencilBufferBit);
- GL.PushMatrix();
- {
- GL.BindTexture(TextureTarget.Texture2D, colorTexture);
- GL.Begin(BeginMode.Quads);
- {
- GL.TexCoord2(0f, 0f);
- GL.Vertex2(-1.0f, 1.0f);
- GL.TexCoord2(0.0f, 1.0f);
- GL.Vertex2(-1.0f, -1.0f);
- GL.TexCoord2(1.0f, 1.0f);
- GL.Vertex2(1.0f, -1.0f);
- GL.TexCoord2(1.0f, 0.0f);
- GL.Vertex2(1.0f, 1.0f);
- }
- GL.End();
- 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.BindTexture(TextureTarget.Texture2D, stencilBuffer);
- 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.Translate(0f, 0f, 0f);
- GL.Disable(EnableCap.StencilTest);
- }
- GL.PopMatrix();
- this.SwapBuffers();
- }
- #region public static void Main()
- /// <summary>
- /// Entry point of this example.
- /// </summary>
- [STAThread]
- public static void Main() {
- using (MainClass example = new MainClass()) {
- example.Title = "FrameBufferObjects";
- example.Run(1.0, 0.0);
- }
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement