Advertisement
Guest User

opentk stencil test

a guest
Feb 27th, 2012
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.97 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Imaging;
  4. using System.Drawing.Drawing2D;
  5. using System.Runtime.InteropServices;
  6.  
  7. using OpenTK;
  8. using OpenTK.Graphics;
  9. using OpenTK.Graphics.OpenGL;
  10. using OpenTK.Platform;
  11. using OpenTK.Platform.X11;
  12. using OpenTK.Input;
  13.  
  14.  
  15. namespace test
  16. {
  17.     public class MainClass : GameWindow
  18.     {
  19.         public MainClass()
  20.             : base(800, 600, new GraphicsMode(new ColorFormat(0, 0, 0, 0), 24, 8, 2)) {
  21.             this.VSync = VSyncMode.Off;
  22.         }
  23.  
  24.         public uint fbo;
  25.         public uint stencilBuffer;
  26.         public uint colorTexture;
  27.  
  28.         public int stencilBufferWidth = 1024;
  29.         public int stencilBufferHeight = 1024;
  30.  
  31.         public int colorTextureWidth;
  32.         public int colorTextureHeight;
  33.  
  34.         Random rand = new Random();
  35.  
  36.         protected override void OnLoad(EventArgs e) {
  37.             GL.Enable(EnableCap.Blend);
  38.  
  39.  
  40.             GL.Enable(EnableCap.StencilTest);
  41.             GL.ClearStencil(0);
  42.             GL.StencilMask(0xFFFFFFFF); // read&write
  43.  
  44.             GL.ShadeModel(ShadingModel.Smooth);
  45.             GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
  46.             GL.Disable(EnableCap.DepthTest);
  47.             GL.Disable(EnableCap.CullFace);
  48.  
  49.             // Create stencil texture
  50.             GL.GenRenderbuffers(1, out stencilBuffer);
  51.             GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, (RenderbufferStorage)All.Depth24Stencil8Ext, stencilBufferWidth, stencilBufferHeight);
  52.             GL.FramebufferRenderbuffer(FramebufferTarget.FramebufferExt, FramebufferAttachment.DepthAttachmentExt, RenderbufferTarget.RenderbufferExt, stencilBuffer);
  53.             GL.FramebufferRenderbuffer(FramebufferTarget.FramebufferExt, FramebufferAttachment.StencilAttachmentExt, RenderbufferTarget.RenderbufferExt, stencilBuffer);
  54.  
  55.  
  56.             Bitmap bmp = new Bitmap("c:/downloads/mona-lisa.bmp");
  57.  
  58.             colorTextureWidth = bmp.Width;
  59.             colorTextureHeight = bmp.Height;
  60.  
  61.             //get the data out of the bitmap
  62.             System.Drawing.Imaging.BitmapData bmpBits = bmp.LockBits
  63.             (
  64.                 new System.Drawing.Rectangle(0, 0, colorTextureWidth, colorTextureHeight),
  65.                 System.Drawing.Imaging.ImageLockMode.ReadOnly,
  66.                 System.Drawing.Imaging.PixelFormat.Format32bppRgb
  67.             );
  68.  
  69.  
  70.             GL.TexImage2D
  71.             (
  72.                 TextureTarget.Texture2D,
  73.                 0,
  74.                 PixelInternalFormat.Rgba,
  75.                 colorTextureWidth,
  76.                 colorTextureHeight,
  77.                 0,
  78.                 OpenTK.Graphics.OpenGL.PixelFormat.Bgra,
  79.                 PixelType.UnsignedByte,
  80.                 bmpBits.Scan0
  81.             );
  82.  
  83.             GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
  84.             GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
  85.             GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToBorder);
  86.             GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToBorder);
  87.  
  88.  
  89.             //free the bitmap data (we dont need it anymore because it has been passed to the OpenGL driver
  90.             bmp.UnlockBits(bmpBits);
  91.  
  92.  
  93.             // Create a FBO and attach the textures
  94.             GL.GenFramebuffers(1, out fbo);
  95.             GL.BindFramebuffer(FramebufferTarget.Framebuffer, fbo);
  96.             GL.Ext.FramebufferTexture2D(FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0Ext, TextureTarget.Texture2D, colorTexture, 0);
  97.             GL.Ext.BindRenderbuffer(RenderbufferTarget.RenderbufferExt, stencilBuffer);
  98.  
  99.  
  100.             #region Test for Error
  101.             switch (GL.Ext.CheckFramebufferStatus(FramebufferTarget.FramebufferExt)) {
  102.                 case FramebufferErrorCode.FramebufferCompleteExt: {
  103.                         Console.WriteLine("FBO: The framebuffer is complete and valid for rendering.");
  104.                         break;
  105.                     }
  106.                 case FramebufferErrorCode.FramebufferIncompleteAttachmentExt: {
  107.                         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.");
  108.                         break;
  109.                     }
  110.                 case FramebufferErrorCode.FramebufferIncompleteMissingAttachmentExt: {
  111.                         Console.WriteLine("FBO: There are no attachments.");
  112.                         break;
  113.                     }
  114.                 case FramebufferErrorCode.FramebufferIncompleteDimensionsExt: {
  115.                         Console.WriteLine("FBO: Attachments are of different size. All attachments must have the same width and height.");
  116.                         break;
  117.                     }
  118.                 case FramebufferErrorCode.FramebufferIncompleteFormatsExt: {
  119.                         Console.WriteLine("FBO: The color attachments have different format. All color attachments must have the same format.");
  120.                         break;
  121.                     }
  122.                 case FramebufferErrorCode.FramebufferIncompleteDrawBufferExt: {
  123.                         Console.WriteLine("FBO: An attachment point referenced by GL.DrawBuffers() doesn’t have an attachment.");
  124.                         break;
  125.                     }
  126.                 case FramebufferErrorCode.FramebufferIncompleteReadBufferExt: {
  127.                         Console.WriteLine("FBO: The attachment point referenced by GL.ReadBuffers() doesn’t have an attachment.");
  128.                         break;
  129.                     }
  130.                 case FramebufferErrorCode.FramebufferUnsupportedExt: {
  131.                         Console.WriteLine("FBO: This particular FBO configuration is not supported by the implementation.");
  132.                         break;
  133.                     }
  134.                 default: {
  135.                         Console.WriteLine("FBO: Status unknown. (yes, this is really bad.)");
  136.                         break;
  137.                     }
  138.             }
  139.  
  140.             // using FBO might have changed states, e.g. the FBO might not support stereoscopic views or double buffering
  141.             int[] queryinfo = new int[6];
  142.             GL.GetInteger(GetPName.MaxColorAttachmentsExt, out queryinfo[0]);
  143.             GL.GetInteger(GetPName.AuxBuffers, out queryinfo[1]);
  144.             GL.GetInteger(GetPName.MaxDrawBuffers, out queryinfo[2]);
  145.             GL.GetInteger(GetPName.Stereo, out queryinfo[3]);
  146.             GL.GetInteger(GetPName.Samples, out queryinfo[4]);
  147.             GL.GetInteger(GetPName.Doublebuffer, out queryinfo[5]);
  148.             Console.WriteLine("max. ColorBuffers: " + queryinfo[0] + " max. AuxBuffers: " + queryinfo[1] + " max. DrawBuffers: " + queryinfo[2] +
  149.                                "\nStereo: " + queryinfo[3] + " Samples: " + queryinfo[4] + " DoubleBuffer: " + queryinfo[5]);
  150.  
  151.             Console.WriteLine("Last GL Error: " + GL.GetError());
  152.             #endregion Test for Error
  153.  
  154.  
  155.             GL.PushAttrib(AttribMask.ViewportBit);
  156.             {
  157.                 GL.Viewport(0, 0, stencilBufferWidth, stencilBufferHeight);
  158.  
  159.                 OpenTK.Matrix4 orthogonal = OpenTK.Matrix4.CreateOrthographicOffCenter(0, 1, 0, 1, -3, 3);
  160.                 GL.MatrixMode(MatrixMode.Projection);
  161.                 GL.LoadMatrix(ref orthogonal);
  162.  
  163.                 Matrix4 lookat = Matrix4.LookAt(0, 0, 1, 0, 0, 0, 0, 1, 0);
  164.                 GL.MatrixMode(MatrixMode.Modelview);
  165.                 GL.LoadMatrix(ref lookat);
  166.  
  167.  
  168.                 // clear the screen in red, to make it very obvious what the clear affected. only the FBO, not the real framebuffer
  169.                 GL.ClearColor(0f, 0f, 0f, 0f);
  170.                 GL.Clear(ClearBufferMask.ColorBufferBit);
  171.  
  172.                 // draw 2 random triangles into the stencil texture
  173.                 GL.Begin(BeginMode.Triangles);
  174.                 {
  175.                     for (int i = 0; i < 2; i++) {
  176.                         GL.Color4(((float)(rand.NextDouble())), ((float)(rand.NextDouble())), ((float)(rand.NextDouble())), ((float)(rand.NextDouble())));
  177.                         GL.Vertex3(((float)(rand.NextDouble())), ((float)(rand.NextDouble())), 0);
  178.                         GL.Color4(((float)(rand.NextDouble())), ((float)(rand.NextDouble())), ((float)(rand.NextDouble())), ((float)(rand.NextDouble())));
  179.                         GL.Vertex3(((float)(rand.NextDouble())), ((float)(rand.NextDouble())), 0);
  180.                         GL.Color4(((float)(rand.NextDouble())), ((float)(rand.NextDouble())), ((float)(rand.NextDouble())), ((float)(rand.NextDouble())));
  181.                         GL.Vertex3(((float)(rand.NextDouble())), ((float)(rand.NextDouble())), 0);
  182.                     }
  183.                 }
  184.                 GL.End();
  185.             }
  186.             GL.PopAttrib();
  187.             GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0); // disable rendering into the FBO
  188.  
  189.             GL.ClearColor(1f, 1f, 1f, 0.0f);
  190.             GL.Color3(1f, 1f, 1f);
  191.  
  192.             GL.Enable(EnableCap.Texture2D); // enable Texture Mapping
  193.             GL.Enable(EnableCap.StencilTest);
  194.             GL.BindTexture(TextureTarget.Texture2D, 0); // bind default texture
  195.         }
  196.  
  197.         protected override void OnUnload(EventArgs e) {
  198.             // Clean up what we allocated before exiting
  199.             GL.DeleteTextures(1, ref stencilBuffer);
  200.             GL.DeleteTextures(1, ref colorTexture);
  201.             GL.Ext.DeleteFramebuffers(1, ref fbo);
  202.  
  203.             base.OnUnload(e);
  204.         }
  205.  
  206.         protected override void OnResize(EventArgs e) {
  207.             GL.Viewport(0, 0, Width, Height);
  208.  
  209.             double aspect_ratio = Width / (double)Height;
  210.  
  211.             OpenTK.Matrix4 perspective = OpenTK.Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4 * 3 / 2, (float)aspect_ratio, 1, 64);
  212.             GL.MatrixMode(MatrixMode.Projection);
  213.             GL.LoadMatrix(ref perspective);
  214.  
  215.             Matrix4 lookat = Matrix4.LookAt(0, 0, 3, 0, 0, 0, 0, 1, 0);
  216.             GL.MatrixMode(MatrixMode.Modelview);
  217.             GL.LoadMatrix(ref lookat);
  218.  
  219.             base.OnResize(e);
  220.         }
  221.  
  222.         protected override void OnUpdateFrame(FrameEventArgs e) {
  223.             base.OnUpdateFrame(e);
  224.  
  225.             if (Keyboard[Key.Escape]) {
  226.                 this.Exit();
  227.             }
  228.         }
  229.  
  230.         protected override void OnRenderFrame(FrameEventArgs e) {
  231.             this.Title = "Frames per Second: " + (1.0 / e.Time);
  232.  
  233.             GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.StencilBufferBit);
  234.  
  235.             GL.PushMatrix();
  236.             {
  237.                 GL.BindTexture(TextureTarget.Texture2D, colorTexture);
  238.                 GL.Begin(BeginMode.Quads);
  239.                 {
  240.                     GL.TexCoord2(0f, 0f);
  241.                     GL.Vertex2(-1.0f, 1.0f);
  242.  
  243.                     GL.TexCoord2(0.0f, 1.0f);
  244.                     GL.Vertex2(-1.0f, -1.0f);
  245.  
  246.                     GL.TexCoord2(1.0f, 1.0f);
  247.                     GL.Vertex2(1.0f, -1.0f);
  248.  
  249.                     GL.TexCoord2(1.0f, 0.0f);
  250.                     GL.Vertex2(1.0f, 1.0f);
  251.                 }
  252.                 GL.End();
  253.  
  254.                 GL.Enable(EnableCap.StencilTest);
  255.                 GL.ClearStencil(0);
  256.                 GL.ColorMask(false, false, false, false);
  257.                 GL.StencilFunc(StencilFunction.Always, 1, 1);
  258.                 GL.StencilOp(StencilOp.Keep, StencilOp.Keep, StencilOp.Replace);
  259.  
  260.                 // Draw the stencil texture
  261.                 GL.BindTexture(TextureTarget.Texture2D, stencilBuffer);
  262.                 GL.Begin(BeginMode.Quads);
  263.                 {
  264.                     GL.TexCoord2(0f, 1f);
  265.                     GL.Vertex2(-1.0f, 1.0f);
  266.                     GL.TexCoord2(0.0f, 0.0f);
  267.                     GL.Vertex2(-1.0f, -1.0f);
  268.                     GL.TexCoord2(1.0f, 0.0f);
  269.                     GL.Vertex2(1.0f, -1.0f);
  270.                     GL.TexCoord2(1.0f, 1.0f);
  271.                     GL.Vertex2(1.0f, 1.0f);
  272.                 }
  273.                 GL.End();
  274.  
  275.                 GL.Translate(0f, 0f, 0f);
  276.  
  277.                 GL.Disable(EnableCap.StencilTest);
  278.             }
  279.             GL.PopMatrix();
  280.  
  281.             this.SwapBuffers();
  282.         }
  283.  
  284.         #region public static void Main()
  285.  
  286.         /// <summary>
  287.         /// Entry point of this example.
  288.         /// </summary>
  289.         [STAThread]
  290.         public static void Main() {
  291.             using (MainClass example = new MainClass()) {
  292.                 example.Title = "FrameBufferObjects";
  293.                 example.Run(1.0, 0.0);
  294.             }
  295.         }
  296.  
  297.         #endregion
  298.     }
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement