View difference between Paste ID: VNQ7qqzR and iuur2UTM
SHOW: | | - or go back to the newest paste.
1
//http://www.opentk.com/doc/graphics/frame-buffer-objects
2
using System;
3
using System.Drawing;
4
using System.Drawing.Imaging;
5
using System.Drawing.Drawing2D;
6
using System.Runtime.InteropServices;
7
8
using OpenTK;
9
using OpenTK.Graphics;
10
using OpenTK.Graphics.OpenGL;
11
using OpenTK.Platform;
12
using OpenTK.Platform.X11;
13
using OpenTK.Input;
14
15
16
namespace test
17
{
18
    public class MainClass : GameWindow
19
    {
20
        public MainClass()
21
            : base(800, 600, new GraphicsMode(new ColorFormat(0, 0, 0, 0), 24, 8, 2)) {
22
            this.VSync = VSyncMode.Off;
23
        }
24
25
        public uint fbo;
26
        public uint stencilBuffer;
27
        public uint colorTexture;
28
29
        public int stencilBufferWidth = 1024;
30
        public int stencilBufferHeight = 1024;
31
32
        public int colorTextureWidth;
33
        public int colorTextureHeight;
34
35
        Random rand = new Random();
36
37
        protected override void OnLoad(EventArgs e) {
38
            GL.Enable(EnableCap.Blend);
39
40
41
            GL.Enable(EnableCap.StencilTest);
42
            GL.ClearStencil(0);
43
            GL.StencilMask(0xFFFFFFFF); // read&write
44
45
            GL.ShadeModel(ShadingModel.Smooth);
46
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
47
            GL.Disable(EnableCap.DepthTest);
48
            GL.Disable(EnableCap.CullFace);
49
50
            // Create stencil texture
51
            GL.GenRenderbuffers(1, out stencilBuffer);
52-
            GL.FramebufferRenderbuffer(FramebufferTarget.FramebufferExt, FramebufferAttachment.DepthAttachmentExt, RenderbufferTarget.RenderbufferExt, stencilBuffer);
52+
            GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, stencilBuffer);
53-
            GL.FramebufferRenderbuffer(FramebufferTarget.FramebufferExt, FramebufferAttachment.StencilAttachmentExt, RenderbufferTarget.RenderbufferExt, stencilBuffer);
53+
54
            GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachmentExt, RenderbufferTarget.Renderbuffer, stencilBuffer);
55
            GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.StencilAttachmentExt, RenderbufferTarget.Renderbuffer, stencilBuffer);
56
57
            Bitmap bmp = new Bitmap("c:/downloads/mona-lisa.bmp");
58
59
            colorTextureWidth = bmp.Width;
60
            colorTextureHeight = bmp.Height;
61
62
            //get the data out of the bitmap
63
            System.Drawing.Imaging.BitmapData bmpBits = bmp.LockBits
64
            (
65
                new System.Drawing.Rectangle(0, 0, colorTextureWidth, colorTextureHeight),
66
                System.Drawing.Imaging.ImageLockMode.ReadOnly,
67
                System.Drawing.Imaging.PixelFormat.Format32bppRgb
68
            );
69
70
71
            GL.TexImage2D
72
            (
73
                TextureTarget.Texture2D,
74
                0,
75
                PixelInternalFormat.Rgba,
76
                colorTextureWidth,
77
                colorTextureHeight,
78
                0,
79
                OpenTK.Graphics.OpenGL.PixelFormat.Bgra,
80
                PixelType.UnsignedByte,
81
                bmpBits.Scan0
82
            );
83
84
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
85
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
86
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToBorder);
87
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToBorder);
88
89
90
            //free the bitmap data (we dont need it anymore because it has been passed to the OpenGL driver
91
            bmp.UnlockBits(bmpBits);
92
93
94
            // Create a FBO and attach the textures
95
            GL.GenFramebuffers(1, out fbo);
96
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, fbo);
97-
            GL.Ext.BindRenderbuffer(RenderbufferTarget.RenderbufferExt, stencilBuffer);
97+
98
            //GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, stencilBuffer);
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, stencilBufferWidth, stencilBufferHeight);
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 stencilBuffer);
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, stencilBuffer);
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
}