Example #1
0
        public void TestDrawTransparentTexture()
        {
            var pixels = new[] { new Pixel(), new Pixel(), new Pixel(), new Pixel() };

            _window = new GameWindow {
                Width = 200, Height = 200
            };
            _window.RenderFrame += (caller, args) =>
            {
                IGL gl = new GLWrapper();
                gl.Viewport(0, 0, 200, 200);
                gl.ReadBuffer(ReadBufferMode.Front);

                gl.Enable(EnableCap.Texture2D);
                gl.Enable(EnableCap.Blend);
                gl.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
                gl.ClearColor(1.0f, 1.0f, 0.0f, 1.0f);
                int textureId = GL.GenTexture();
                gl.BindTexture(TextureTarget.Texture2D, textureId);
                gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
                gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
                var bitmap = new Bitmap(@"..\..\Images\testtexture.png");
                var data   = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                                             ImageLockMode.ReadOnly,
                                             System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                gl.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height,
                              0, PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
                bitmap.UnlockBits(data);

                gl.Clear(ClearBufferMask.ColorBufferBit);
                gl.Begin(PrimitiveType.Quads);
                {
                    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.TexCoord2(0.0f, 1.0f);
                    gl.Vertex2(-1.0f, 1.0f);
                }
                gl.End();
                _window.SwapBuffers();
                pixels[0].ReadBuffer(99, 99);
                pixels[1].ReadBuffer(99, 100);
                pixels[2].ReadBuffer(100, 100);
                pixels[3].ReadBuffer(100, 99);
                _window.Close();
            };
            _window.Run();
            Assert.AreEqual(new Pixel(1.0f, 0.0f, 0.0f), pixels[0]);
            Assert.AreEqual(new Pixel(0.0f, 1.0f, 0.0f), pixels[1]);
            Assert.AreEqual(new Pixel(0.0f, 0.0f, 1.0f), pixels[2]);
            Assert.AreEqual(new Pixel(1.0f, 1.0f, 0.0f), pixels[3]);
        }