using System; using OpenTK.Graphics; using OpenTK; using OpenTK.Graphics.OpenGL; using System.Drawing; using System.Drawing.Imaging; namespace Moth { public class MainWindow : GameWindow { static MainWindow Instance { get { return MainWindow.instance; } } static readonly MainWindow instance = new MainWindow(); static void Main(string[] args) { instance.Run(); } public MainWindow() : base(256, 256, GraphicsMode.Default, "AlphaTest") { Keyboard.KeyDown += new EventHandler(Keyboard_KeyDown); } int textureID; protected override void OnLoad(EventArgs e) { base.OnLoad(e); GL.GenTextures(1, out textureID); GL.BindTexture(TextureTarget.Texture2D, textureID); Bitmap bitmap = Bitmap.FromFile("Texture.png") as Bitmap; for (int x = 0; x < bitmap.Width; x++) { for (int y = 0; y < bitmap.Height; y++) { Color color = bitmap.GetPixel(x, y); if (color.A == 0) { color = Color.FromArgb(0, 255, 255, 255); bitmap.SetPixel(x, y, color); } } } BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, (int)bitmap.Width, (int)bitmap.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bitmapData.Scan0); GL.GenerateMipmap(GenerateMipmapTarget.Texture2D); bitmap.UnlockBits(bitmapData); bitmap.Dispose(); GL.Enable(EnableCap.Blend); GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.Zero); } void Keyboard_KeyDown(object sender, OpenTK.Input.KeyboardKeyEventArgs e) { switch (e.Key) { case OpenTK.Input.Key.A: GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); break; case OpenTK.Input.Key.O: GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.Zero); break; } } protected override void OnRenderFrame(FrameEventArgs e) { //GL.ClearColor(Color4.Blue); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.Enable(EnableCap.DepthTest); GL.Enable(EnableCap.Texture2D); GL.BindTexture(TextureTarget.Texture2D, textureID); GL.Enable(EnableCap.AlphaTest); float alphaTest = (Environment.TickCount % 50000) / 25000.0f - 1; alphaTest = (float)Math.Sin(alphaTest * Math.PI * 2) / 2.0f + 0.5f; alphaTest = alphaTest * 0.8f + 0.1f; GL.AlphaFunc(AlphaFunction.Greater, alphaTest); GL.PushMatrix(); for (int a = 0; a < 5; a++) { for (int i = 0; i < 4; i++) { GL.PushMatrix(); GL.Translate(-0.7f, 0, 0); float rotation = (Environment.TickCount % 20000) / 10000.0f - 1; rotation = (float)Math.Sin(rotation * Math.PI * 2) * 10; GL.Rotate(rotation, 0, 0, 1); GL.Translate(0.7f, 0, 0); GL.Begin(BeginMode.Quads); { //GL.Color4(Color4.Green); GL.TexCoord2(0, 0); GL.Vertex3(-0.6f, -0.8f, -0.1f); GL.TexCoord2(1, 0); GL.Vertex3(-0.2f, -0.8f, -0.1f); //GL.Color4(Color4.Red); GL.TexCoord2(1, 1); GL.Vertex3(-0.2f, 0.8f, 0); GL.TexCoord2(0, 1); GL.Vertex3(-0.6f, 0.8f, 0); } GL.End(); GL.PopMatrix(); GL.Rotate(90, 0, 0, 1); } GL.Translate(0, 0, 0.1f); GL.Scale(0.7f, 0.7f, 1); } GL.PopMatrix(); SwapBuffers(); } } }