#region --- License --- /* Copyright (c) 2006, 2007 Stefanos Apostolopoulos * See license.txt for license info */ #endregion using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; using System.IO; using System.Drawing; using System.Drawing.Imaging; using OpenTK; using OpenTK.OpenGL; using OpenTK.Fonts; using OpenTK.OpenGL.Enums; namespace Examples.Tutorial { /// /// Demonstrates simple OpenGL Texturing. /// [Example("Texture mapping", ExampleCategory.Tutorial, 5)] public class Textures : GameWindow { Bitmap bitmap = new Bitmap("Data\\logo-dark.jpg"); int texture; public Textures() : base(new DisplayMode(800, 600)) { } #region OnLoad /// /// Setup OpenGL and load resources here. /// /// Not used. public override void OnLoad(EventArgs e) { GL.ClearColor(Color.SteelBlue); GL.Enable(EnableCap.Texture2d); GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest); GL.GenTextures(1, out texture); GL.BindTexture(TextureTarget.Texture2d, texture); BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat); GL.TexImage2D(TextureTarget.Texture2d, 0, PixelInternalFormat.Three, bitmap.Width, bitmap.Height, 0, OpenTK.OpenGL.Enums.PixelFormat.Bgr, PixelType.UnsignedByte, data.Scan0); bitmap.UnlockBits(data); GL.TexParameter(TextureTarget.Texture2d, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); GL.TexParameter(TextureTarget.Texture2d, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); } #endregion #region OnUnload public override void OnUnload(EventArgs e) { GL.DeleteTextures(1, ref texture); } #endregion #region OnResize /// /// Respond to resize events here. /// /// Contains information on the new GameWindow size. /// There is no need to call the base implementation. protected override void OnResize(OpenTK.Platform.ResizeEventArgs e) { GL.Viewport(0, 0, Width, Height); GL.MatrixMode(MatrixMode.Projection); GL.LoadIdentity(); GL.Ortho(-1.0, 1.0, -1.0, 1.0, 0.0, 4.0); } #endregion #region OnUpdateFrame /// /// Add your game logic here. /// /// Contains timing information. /// There is no need to call the base implementation. public override void OnUpdateFrame(UpdateFrameEventArgs e) { if (Keyboard[OpenTK.Input.Key.Escape]) this.Exit(); } #endregion #region OnRenderFrame /// /// Add your game rendering code here. /// /// Contains timing information. /// There is no need to call the base implementation. public override void OnRenderFrame(RenderFrameEventArgs e) { GL.Clear(ClearBufferMask.ColorBufferBit); GL.MatrixMode(MatrixMode.Modelview); GL.LoadIdentity(); GL.BindTexture(TextureTarget.Texture2d, texture); GL.Begin(BeginMode.Quads); GL.TexCoord2(0.0f, 1.0f); GL.Vertex2(-0.6f, -0.4f); GL.TexCoord2(1.0f, 1.0f); GL.Vertex2(0.6f, -0.4f); GL.TexCoord2(1.0f, 0.0f); GL.Vertex2(0.6f, 0.4f); GL.TexCoord2(0.0f, 0.0f); GL.Vertex2(-0.6f, 0.4f); GL.End(); SwapBuffers(); } #endregion #region public static void Main() /// /// Entry point of this example. /// [STAThread] public static void Main() { using (Textures example = new Textures()) { // Get the title and category of this example using reflection. ExampleAttribute info = ((ExampleAttribute)typeof(Textures).GetCustomAttributes(false)[0]); example.Title = String.Format("OpenTK | {0} {1}: {2}", info.Category, info.Difficulty, info.Title); example.Run(30.0, 0.0); } } #endregion } }