Advertisement
Guest User

opentk stencil test

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