Opentk/Source/Examples/Tutorial/Textures.cs

155 lines
4.8 KiB
C#
Raw Normal View History

2007-11-04 15:33:43 +00:00
#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.Graphics.OpenGL;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL.Enums;
2007-11-04 15:33:43 +00:00
namespace Examples.Tutorial
{
/// <summary>
/// Demonstrates simple OpenGL Texturing.
/// </summary>
[Example("Texture mapping", ExampleCategory.Tutorial, 5)]
public class Textures : GameWindow
2007-11-04 15:33:43 +00:00
{
2007-11-11 21:48:00 +00:00
Bitmap bitmap = new Bitmap("Data/logo.jpg");
2007-11-04 15:33:43 +00:00
int texture;
2008-04-13 15:51:09 +00:00
public Textures() : base(800, 600) { }
2007-11-04 15:33:43 +00:00
#region OnLoad
/// <summary>
/// Setup OpenGL and load resources here.
/// </summary>
/// <param name="e">Not used.</param>
public override void OnLoad(EventArgs e)
{
GL.ClearColor(Color.SteelBlue);
2008-01-24 09:25:24 +00:00
GL.Enable(EnableCap.Texture2D);
2007-11-04 15:33:43 +00:00
GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
GL.GenTextures(1, out texture);
2008-01-24 09:25:24 +00:00
GL.BindTexture(TextureTarget.Texture2D, texture);
2007-11-04 15:33:43 +00:00
BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
2007-11-11 21:48:00 +00:00
ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
2007-11-04 15:33:43 +00:00
2008-01-24 09:25:24 +00:00
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0,
OpenTK.Graphics.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
2007-11-04 15:33:43 +00:00
bitmap.UnlockBits(data);
2008-01-24 09:25:24 +00:00
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
2007-11-04 15:33:43 +00:00
}
#endregion
#region OnUnload
public override void OnUnload(EventArgs e)
{
GL.DeleteTextures(1, ref texture);
}
#endregion
2007-11-04 15:33:43 +00:00
#region OnResize
/// <summary>
/// Respond to resize events here.
/// </summary>
/// <param name="e">Contains information on the new GameWindow size.</param>
/// <remarks>There is no need to call the base implementation.</remarks>
protected override void OnResize(OpenTK.Platform.ResizeEventArgs e)
{
GL.Viewport(0, 0, Width, Height);
2007-11-04 15:33:43 +00:00
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
GL.Ortho(-1.0, 1.0, -1.0, 1.0, 0.0, 4.0);
}
#endregion
#region OnUpdateFrame
/// <summary>
/// Add your game logic here.
/// </summary>
/// <param name="e">Contains timing information.</param>
/// <remarks>There is no need to call the base implementation.</remarks>
public override void OnUpdateFrame(UpdateFrameEventArgs e)
{
if (Keyboard[OpenTK.Input.Key.Escape])
this.Exit();
}
#endregion
#region OnRenderFrame
/// <summary>
/// Add your game rendering code here.
/// </summary>
/// <param name="e">Contains timing information.</param>
/// <remarks>There is no need to call the base implementation.</remarks>
public override void OnRenderFrame(RenderFrameEventArgs e)
{
GL.Clear(ClearBufferMask.ColorBufferBit);
GL.MatrixMode(MatrixMode.Modelview);
2007-11-04 15:33:43 +00:00
GL.LoadIdentity();
2008-01-24 09:25:24 +00:00
GL.BindTexture(TextureTarget.Texture2D, texture);
2007-11-04 15:33:43 +00:00
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);
2007-11-04 15:33:43 +00:00
GL.End();
SwapBuffers();
}
#endregion
#region public static void Main()
2007-11-04 15:33:43 +00:00
/// <summary>
/// Entry point of this example.
2007-11-04 15:33:43 +00:00
/// </summary>
[STAThread]
public static void Main()
2007-11-04 15:33:43 +00:00
{
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);
}
2007-11-04 15:33:43 +00:00
}
#endregion
}
}