Added ExampleAttribute to all examples. Improved Textures example. Modified several other examples.

This commit is contained in:
the_fiddler 2007-11-11 18:44:10 +00:00
parent c72890769a
commit 7be851a68a
14 changed files with 348 additions and 260 deletions

View file

@ -15,13 +15,27 @@ namespace Examples
{ {
public readonly string Title; public readonly string Title;
public readonly ExampleCategory Category; public readonly ExampleCategory Category;
public readonly int Order; public readonly int Difficulty;
public readonly bool Visible = true;
public ExampleAttribute(string title, ExampleCategory category, int order) public ExampleAttribute(string title, ExampleCategory category, int difficulty)
{ {
this.Title = title; this.Title = title;
this.Category = category; this.Category = category;
this.Order = order; this.Difficulty = difficulty;
}
public ExampleAttribute(string title, ExampleCategory category, int difficulty, bool visible)
{
this.Title = title;
this.Category = category;
this.Difficulty = difficulty;
this.Visible = visible;
}
public override string ToString()
{
return String.Format("{0} {1}: {2}", Category, Difficulty, Title);
} }
} }
@ -29,7 +43,7 @@ namespace Examples
{ {
OpenGL, OpenGL,
OpenAL, OpenAL,
OpenTK, Tutorial,
GLSL, GLSL,
WinForms, WinForms,
Test, Test,

View file

@ -36,20 +36,18 @@ namespace Examples
/// </summary> /// </summary>
class ExampleInfo class ExampleInfo
{ {
public string Name;
public string Description;
public Type Example; public Type Example;
public ExampleAttribute Attributes;
public ExampleInfo(string name, string description, Type example) public ExampleInfo(Type example, ExampleAttribute attr)
{ {
Name = name;
Description = description;
Example = example; Example = example;
Attributes = attr;
} }
public override string ToString() public override string ToString()
{ {
return Name; return Attributes.ToString();
} }
} }
@ -88,29 +86,24 @@ namespace Examples
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
foreach (Type type in types) foreach (Type type in types)
{ {
if (type.GetInterface("IExample") != null) object[] attributes = type.GetCustomAttributes(false);
{ ExampleAttribute example = null;
sb.Append(type.Namespace); foreach (object attr in attributes)
sb.Replace("Examples.", String.Empty); if (attr is ExampleAttribute)
sb.Append(" "); example = (ExampleAttribute)attr;
int order;
try
{
FieldInfo info = type.GetField("order");
order = (int)info.GetValue(null);
}
catch (NullReferenceException)
{
Debug.Print("Example {0} does not have ordering info", type.FullName);
order = 0;
}
if (order < 10)
sb.Append("0"); // To keep items sorted nicely.
sb.Append(order.ToString());
sb.Append(": ");
sb.Append(type.Name);
listBox1.Items.Add(new ExampleInfo(sb.ToString(), "", type)); if (example != null && example.Visible == true)
{
sb.Append(example.Category);
sb.Append(" ");
if (example.Difficulty < 10)
sb.Append("0"); // To keep items nicely sorted.
sb.Append(example.Difficulty);
sb.Append(": ");
//sb.Append(type.Name);
sb.Append(example.Title);
listBox1.Items.Add(new ExampleInfo(type, example));
// Clean the StringBuilder for the next pass. // Clean the StringBuilder for the next pass.
sb.Remove(0, sb.Length); sb.Remove(0, sb.Length);
@ -123,62 +116,58 @@ namespace Examples
#endregion #endregion
#region private void RunExample()
private void RunExample() private void RunExample()
{ {
if (listBox1.SelectedItem != null) if (listBox1.SelectedItem != null)
{ {
Type example = (listBox1.SelectedItem as ExampleInfo).Example;
Debug.Print("Launching example: {0}", example.ToString());
this.Visible = false;
try try
{ {
if (example.BaseType == typeof(GameWindow)) ExampleInfo info = (ExampleInfo)listBox1.SelectedItem;
{ Type example = info.Example;
using (GameWindow gw = (GameWindow)(example.GetConstructor(Type.EmptyTypes).Invoke(null)))
{ Debug.Print("Launching example: {0}", example.ToString());
(gw as IExample).Launch(); this.Visible = false;
}
} example.GetMethod("Main").Invoke(null, null);
else if (example.BaseType == typeof(Form))
{ GC.Collect();
using (Form f = (Form)example.GetConstructor(Type.EmptyTypes).Invoke(null)) GC.WaitForPendingFinalizers();
{ GC.Collect();
f.ShowDialog(this);
}
}
else
{
// Console application.
IExample ex = (IExample)example.GetConstructor(Type.EmptyTypes).Invoke(null);
ex.Launch();
}
} }
catch (Exception expt) catch (TargetInvocationException expt)
{ {
string ex_info;
if (expt.InnerException != null)
ex_info = expt.InnerException.ToString();
else
ex_info = expt.ToString();
MessageBox.Show(ex_info, "An OpenTK example encountered an error.", MessageBoxButtons.OK, MessageBoxIcon.Warning);
Debug.Print(expt.ToString());
#if DEBUG #if DEBUG
throw; throw;
#else
if (expt.InnerException != null)
MessageBox.Show(expt.InnerException.ToString(), "An error has occured: \"" +
expt.InnerException.Message + "\"", MessageBoxButtons.OK, MessageBoxIcon.Warning);
else
MessageBox.Show(expt.ToString(), "An error has occured: \"" + expt.Message + "\"",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
#endif #endif
} }
catch (NullReferenceException expt)
GC.Collect(); {
GC.WaitForPendingFinalizers(); MessageBox.Show(expt.ToString(), "The Example launcher failed to load the example.", MessageBoxButtons.OK, MessageBoxIcon.Warning);
GC.Collect(); }
finally
this.Visible = true; {
this.Visible = true;
this.TopMost = true; // Bring the ExampleLauncher window to front
this.TopMost = false; // but don't allow the user to cover it with other windows.
}
} }
} }
#endregion
#region Launcher events
private void listBox1_DoubleClick(object sender, EventArgs e) private void listBox1_DoubleClick(object sender, EventArgs e)
{ {
RunExample(); RunExample();
@ -199,6 +188,16 @@ namespace Examples
RunExample(); RunExample();
} }
private void ExampleLauncher_FormClosing(object sender, FormClosingEventArgs e)
{
Debug.Flush();
Debug.Close();
Trace.Flush();
Trace.Close();
}
#endregion
/// <summary> /// <summary>
/// The main entry point for the application. /// The main entry point for the application.
/// </summary> /// </summary>
@ -211,13 +210,5 @@ namespace Examples
Application.Run(exampleLauncher); Application.Run(exampleLauncher);
} }
} }
private void ExampleLauncher_FormClosing(object sender, FormClosingEventArgs e)
{
Debug.Flush();
Debug.Close();
Trace.Flush();
Trace.Close();
}
} }
} }

View file

@ -22,15 +22,22 @@ namespace Examples.Tutorial
/// <summary> /// <summary>
/// Tests Font loading and rendering. /// Tests Font loading and rendering.
/// </summary> /// </summary>
class Fonts : GameWindow, IExample [Example("Fonts", ExampleCategory.Tutorial, 3)]
class Fonts : GameWindow
{ {
public Fonts() : base(new DisplayMode(800, 600), String.Format("OpenTK | Tutorial {0}: Fonts", order)) public Fonts() : base(new DisplayMode(800, 600))
{ } { }
#region --- Fields ---
ITextPrinter printer = new TextPrinter(); ITextPrinter printer = new TextPrinter();
const string text = "Hello, world!"; const string text = "Hello, world!";
TextHandle[] handles; // Used to cache the strings we want to print.
// Load some different TextureFont sizes to compare their quality. // Load some different TextureFont sizes to compare their quality.
// You'll never need to load that many fonts in your application,
// 3 or 4 should be more than enough.
TextureFont[] fonts = new TextureFont[] TextureFont[] fonts = new TextureFont[]
{ {
new TextureFont(new Font(FontFamily.GenericSerif, 8.0f)), new TextureFont(new Font(FontFamily.GenericSerif, 8.0f)),
@ -73,7 +80,10 @@ namespace Examples.Tutorial
new TextureFont(new Font(FontFamily.GenericSansSerif, 36.0f)), new TextureFont(new Font(FontFamily.GenericSansSerif, 36.0f)),
new TextureFont(new Font(FontFamily.GenericSansSerif, 38.0f)), new TextureFont(new Font(FontFamily.GenericSansSerif, 38.0f)),
}; };
TextHandle[] handles; // Used to cache the strings we want to print.
#endregion
#region OnLoad
/// <summary> /// <summary>
/// To maintain high rendering performance, we need to cache the text /// To maintain high rendering performance, we need to cache the text
@ -95,6 +105,10 @@ namespace Examples.Tutorial
printer.Prepare(text, fonts[i], out handles[i]); printer.Prepare(text, fonts[i], out handles[i]);
} }
#endregion
#region OnUnload
/// <summary> /// <summary>
/// It is important that we need to call the Dispose() methods to reclaim of /// It is important that we need to call the Dispose() methods to reclaim of
/// each and every TextHandle and TextureFont to reclaim the unamanged /// each and every TextHandle and TextureFont to reclaim the unamanged
@ -109,17 +123,29 @@ namespace Examples.Tutorial
f.Dispose(); f.Dispose();
} }
#endregion
#region OnResize
protected override void OnResize(OpenTK.Platform.ResizeEventArgs e) protected override void OnResize(OpenTK.Platform.ResizeEventArgs e)
{ {
GL.Viewport(0, 0, Width, Height); GL.Viewport(0, 0, Width, Height);
} }
#endregion
#region OnUpdateFrame
public override void OnUpdateFrame(UpdateFrameEventArgs e) public override void OnUpdateFrame(UpdateFrameEventArgs e)
{ {
if (Keyboard[Key.Escape]) if (Keyboard[Key.Escape])
this.Exit(); this.Exit();
} }
#endregion
#region OnRenderFrame
/// <summary> /// <summary>
/// To render pixel-perfect text, we have to setup a 2d display projection /// To render pixel-perfect text, we have to setup a 2d display projection
/// with a width/height that corresponds to our current Viewport. /// with a width/height that corresponds to our current Viewport.
@ -167,14 +193,23 @@ namespace Examples.Tutorial
SwapBuffers(); SwapBuffers();
} }
#endregion
#region IExample Members #region public static void Main()
public static readonly int order = 6; /// <summary>
/// Entry point of this example.
public void Launch() /// </summary>
[STAThread]
public static void Main()
{ {
Run(30.0, 0.0); using (Fonts example = new Fonts())
{
// Get the title and category of this example using reflection.
ExampleAttribute info = ((ExampleAttribute)example.GetType().GetCustomAttributes(false)[0]);
example.Title = String.Format("OpenTK | {0} {1}: {2}", info.Category, info.Difficulty, info.Title);
example.Run(30.0, 0.0);
}
} }
#endregion #endregion

View file

@ -20,9 +20,10 @@ namespace Examples.Tutorial
/// <summary> /// <summary>
/// Demonstrates the GameWindow class. /// Demonstrates the GameWindow class.
/// </summary> /// </summary>
public class T01_Simple_Window : GameWindow, IExample [Example("Simple Window", ExampleCategory.Tutorial, 1)]
public class T01_Simple_Window : GameWindow
{ {
public T01_Simple_Window() : base(new DisplayMode(800, 600), "OpenTK | Tutorial 1: Simple Window") public T01_Simple_Window() : base(new DisplayMode(800, 600))
{ } { }
#region OnLoad #region OnLoad
@ -101,20 +102,24 @@ namespace Examples.Tutorial
} }
#endregion #endregion
#region IExample Members #region public static void Main()
/// <summary> /// <summary>
/// Only used by the ExampleLauncher application, no need to add a Launch() method in your code. /// Entry point of this example.
/// Add a call to Run() in your Main() function instead.
/// </summary> /// </summary>
public void Launch() [STAThread]
public static void Main()
{ {
this.Run(30.0, 5.0); using (T01_Simple_Window example = new T01_Simple_Window())
{
// Get the title and category of this example using reflection.
ExampleAttribute info = ((ExampleAttribute)example.GetType().GetCustomAttributes(false)[0]);
example.Title = String.Format("OpenTK | {0} {1}: {2}", info.Category, info.Difficulty, info.Title);
example.Run(30.0, 0.0);
}
} }
public static readonly int order = 1;
#endregion #endregion
} }
} }

View file

@ -21,8 +21,10 @@ namespace Examples.Tutorial
{ {
/// <summary> /// <summary>
/// Demonstrates Vertex Arrays (in system memory). Example is incomplete (documentation). /// Demonstrates Vertex Arrays (in system memory). Example is incomplete (documentation).
/// Broken since release 0.3.12.
/// </summary> /// </summary>
class T02_Vertex_Arrays : GameWindow, IExample [Example("Vertex Arrays", ExampleCategory.OpenGL, 5, false)]
class T02_Vertex_Arrays : GameWindow
{ {
float rotation_speed = 3.0f; float rotation_speed = 3.0f;
float angle = 0.0f; float angle = 0.0f;

View file

@ -24,20 +24,19 @@ namespace Examples.Tutorial
/// <summary> /// <summary>
/// Demonstrates immediate mode rendering. Example is incomplete, and will probably go away in the future. /// Demonstrates immediate mode rendering. Example is incomplete, and will probably go away in the future.
/// </summary> /// </summary>
public class T03_Immediate_Mode_Cube : OpenTK.GameWindow, IExample [Example("Immediate mode", ExampleCategory.Tutorial, 2)]
public class T03_Immediate_Mode_Cube : GameWindow
{ {
#region --- Fields --- #region --- Fields ---
/// <summary> float rotation_speed = 3.0f;
/// Denotes the cube rotation. float angle;
/// </summary>
float angle = 0.0f;
#endregion #endregion
#region --- Constructors --- #region --- Constructor ---
public T03_Immediate_Mode_Cube() : base(new DisplayMode(800, 600), "OpenTK Tutorial: Immediate mode rendering") public T03_Immediate_Mode_Cube() : base(new DisplayMode(800, 600))
{ } { }
#endregion #endregion
@ -48,7 +47,7 @@ namespace Examples.Tutorial
{ {
base.OnLoad(e); base.OnLoad(e);
GL.ClearColor(Color.MidnightBlue); GL.ClearColor(Color.SteelBlue);
GL.Enable(EnableCap.DepthTest); GL.Enable(EnableCap.DepthTest);
} }
@ -89,22 +88,11 @@ namespace Examples.Tutorial
/// </remarks> /// </remarks>
public override void OnUpdateFrame(UpdateFrameEventArgs e) public override void OnUpdateFrame(UpdateFrameEventArgs e)
{ {
/*
if (Keyboard[OpenTK.Input.Key.Escape]) if (Keyboard[OpenTK.Input.Key.Escape])
{ {
this.Exit(); this.Exit();
return; return;
} }
if ((Keyboard[OpenTK.Input.Key.AltLeft] || Keyboard[OpenTK.Input.Key.AltRight]) &&
Keyboard[OpenTK.Input.Key.Enter])
{
Fullscreen = !Fullscreen;
}
*/
angle += 3.0f;
if (angle > 720.0f)
angle -= 720.0f;
} }
#endregion #endregion
@ -124,12 +112,12 @@ namespace Examples.Tutorial
0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0); 0.0, 1.0, 0.0);
angle += rotation_speed * (float)e.ScaleFactor;
GL.Rotate(angle, 0.0f, 1.0f, 0.0f); GL.Rotate(angle, 0.0f, 1.0f, 0.0f);
DrawCube(); DrawCube();
Context.SwapBuffers(); this.SwapBuffers();
Thread.Sleep(0);
} }
#endregion #endregion
@ -182,21 +170,23 @@ namespace Examples.Tutorial
#endregion #endregion
#region public void Launch() #region public static void Main()
/// <summary> /// <summary>
/// Launches this example. /// Entry point of this example.
/// </summary> /// </summary>
/// <remarks> [STAThread]
/// Provides a simple way for the example launcher to launch the examples. public static void Main()
/// </remarks>
public void Launch()
{ {
Run(85.0, 0.0); using (T03_Immediate_Mode_Cube example = new T03_Immediate_Mode_Cube())
{
// Get the title and category of this example using reflection.
ExampleAttribute info = ((ExampleAttribute)example.GetType().GetCustomAttributes(false)[0]);
example.Title = String.Format("OpenTK | {0} {1}: {2}", info.Category, info.Difficulty, info.Title);
example.Run(30.0, 0.0);
}
} }
#endregion #endregion
public static readonly int order = 3;
} }
} }

View file

@ -23,7 +23,8 @@ using OpenTK.OpenGL.Enums;
namespace Examples.Tutorial namespace Examples.Tutorial
{ {
public class T07_Display_Lists_Flower : GameWindow, IExample [Example("Display Lists", ExampleCategory.Tutorial, 3)]
public class T07_Display_Lists_Flower : GameWindow
{ {
#region --- Fields --- #region --- Fields ---
@ -35,7 +36,7 @@ namespace Examples.Tutorial
#region --- Constructors --- #region --- Constructors ---
public T07_Display_Lists_Flower() public T07_Display_Lists_Flower()
: base(new DisplayMode(800, 600), "OpenTK | T07: Display Lists") : base(new DisplayMode(800, 600))
{ {
} }
@ -141,21 +142,23 @@ namespace Examples.Tutorial
#endregion #endregion
#region public void Launch() #region public static void Main()
/// <summary> /// <summary>
/// Launches this example. /// Entry point of this example.
/// </summary> /// </summary>
/// <remarks> [STAThread]
/// Provides a simple way for the example launcher to launch the examples. public static void Main()
/// </remarks>
public void Launch()
{ {
this.Run(30.0, 5.0); using (T07_Display_Lists_Flower example = new T07_Display_Lists_Flower())
{
// Get the title and category of this example using reflection.
ExampleAttribute info = ((ExampleAttribute)example.GetType().GetCustomAttributes(false)[0]);
example.Title = String.Format("OpenTK | {0} {1}: {2}", info.Category, info.Difficulty, info.Title);
example.Run(30.0, 0.0);
}
} }
#endregion #endregion
public static readonly int order = 7;
} }
} }

View file

@ -27,8 +27,8 @@ namespace Examples.Tutorial
/// <summary> /// <summary>
/// Demonstrates how to load and use a simple OpenGL shader program. Example is incomplete (documentation). /// Demonstrates how to load and use a simple OpenGL shader program. Example is incomplete (documentation).
/// </summary> /// </summary>
[Example("OpenTK | GLSL Example 1", ExampleCategory.GLSL, 1)] [Example("First shader", ExampleCategory.GLSL, 1)]
public class T10_GLSL_Cube : GameWindow, IExample public class T10_GLSL_Cube : GameWindow
{ {
#region --- Fields --- #region --- Fields ---
@ -43,10 +43,8 @@ namespace Examples.Tutorial
#region --- Constructors --- #region --- Constructors ---
public T10_GLSL_Cube() public T10_GLSL_Cube()
: base(new DisplayMode(800, 600), T10_GLSL_Cube.Title) : base(new DisplayMode(800, 600))
{ { }
}
#endregion #endregion
@ -81,6 +79,10 @@ namespace Examples.Tutorial
out shader_program); out shader_program);
} }
#endregion
#region CreateShaders
void CreateShaders(string vs, string fs, void CreateShaders(string vs, string fs,
out int vertexObject, out int fragmentObject, out int vertexObject, out int fragmentObject,
out int program) out int program)
@ -117,6 +119,8 @@ namespace Examples.Tutorial
GL.UseProgram(program); GL.UseProgram(program);
} }
#endregion
#region private void CreateVBO() #region private void CreateVBO()
void CreateVBO() void CreateVBO()
@ -151,8 +155,6 @@ namespace Examples.Tutorial
#endregion #endregion
#endregion
#region OnUnload #region OnUnload
public override void OnUnload(EventArgs e) public override void OnUnload(EventArgs e)
@ -255,26 +257,23 @@ namespace Examples.Tutorial
#endregion #endregion
#region Example members #region public static void Main()
#region public void Launch()
/// <summary> /// <summary>
/// Launches this example. /// Entry point of this example.
/// </summary> /// </summary>
/// <remarks> [STAThread]
/// Provides a simple way for the example launcher to launch the examples. public static void Main()
/// </remarks>
public void Launch()
{ {
Run(30.0, 0.0); using (T10_GLSL_Cube example = new T10_GLSL_Cube())
{
// Get the title and category of this example using reflection.
ExampleAttribute info = ((ExampleAttribute)example.GetType().GetCustomAttributes(false)[0]);
example.Title = String.Format("OpenTK | {0} {1}: {2}", info.Category, info.Difficulty, info.Title);
example.Run(30.0, 0.0);
}
} }
#endregion #endregion
static readonly ExampleAttribute info = typeof(T10_GLSL_Cube).GetCustomAttributes(false)[0] as ExampleAttribute;
static readonly string Title = info.Title;
#endregion
} }
} }

View file

@ -22,13 +22,14 @@ namespace Examples.Tutorial
/// <summary> /// <summary>
/// Shows how to render and scroll large amounts of text. /// Shows how to render and scroll large amounts of text.
/// </summary> /// </summary>
public class Text : GameWindow, IExample [Example("Text", ExampleCategory.Tutorial, 4)]
public class Text : GameWindow
{ {
TextureFont serif = new TextureFont(new Font(FontFamily.GenericSerif, 24.0f)); TextureFont serif = new TextureFont(new Font(FontFamily.GenericSerif, 24.0f));
TextHandle poem_handle; TextHandle poem_handle;
ITextPrinter text = new TextPrinter(); ITextPrinter text = new TextPrinter();
public Text() : base(new DisplayMode(800, 600), String.Format("OpenTK | Tutorial {0}: Text", order)) public Text() : base(new DisplayMode(800, 600))
{ } { }
string poem = new StreamReader("Data/Poem.txt").ReadToEnd(); string poem = new StreamReader("Data/Poem.txt").ReadToEnd();
@ -138,13 +139,21 @@ namespace Examples.Tutorial
#endregion #endregion
#region --- IExample Members --- #region public static void Main()
public static readonly int order = 7; /// <summary>
/// Entry point of this example.
public void Launch() /// </summary>
[STAThread]
public static void Main()
{ {
Run(30.0, 0.0); using (Text example = new Text())
{
// Get the title and category of this example using reflection.
ExampleAttribute info = ((ExampleAttribute)example.GetType().GetCustomAttributes(false)[0]);
example.Title = String.Format("OpenTK | {0} {1}: {2}", info.Category, info.Difficulty, info.Title);
example.Run(30.0, 0.0);
}
} }
#endregion #endregion

View file

@ -23,13 +23,13 @@ namespace Examples.Tutorial
/// <summary> /// <summary>
/// Demonstrates simple OpenGL Texturing. /// Demonstrates simple OpenGL Texturing.
/// </summary> /// </summary>
public class Textures : GameWindow, IExample [Example("Texture mapping", ExampleCategory.Tutorial, 5)]
public class Textures : GameWindow
{ {
Bitmap bitmap = new Bitmap("Data\\metal.jpg"); Bitmap bitmap = new Bitmap("Data\\logo-dark.jpg");
int texture; int texture;
TextureFont sans = new TextureFont(new Font(FontFamily.GenericSansSerif, 24.0f));
public Textures() : base(new DisplayMode(800, 600), "OpenTK | Tutorial 5: Texturing") { } public Textures() : base(new DisplayMode(800, 600)) { }
#region OnLoad #region OnLoad
@ -39,18 +39,16 @@ namespace Examples.Tutorial
/// <param name="e">Not used.</param> /// <param name="e">Not used.</param>
public override void OnLoad(EventArgs e) public override void OnLoad(EventArgs e)
{ {
GL.ClearColor(Color.MidnightBlue); GL.ClearColor(Color.SteelBlue);
GL.Enable(EnableCap.Texture2d); GL.Enable(EnableCap.Texture2d);
GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest); GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
//bitmap = new OpenTK.Fonts.Renderer().bmp;
//bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
GL.GenTextures(1, out texture); GL.GenTextures(1, out texture);
GL.BindTexture(TextureTarget.Texture2d, texture); GL.BindTexture(TextureTarget.Texture2d, texture);
BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); ImageLockMode.ReadOnly, bitmap.PixelFormat);
GL.TexImage2D(TextureTarget.Texture2d, 0, PixelInternalFormat.Three, bitmap.Width, bitmap.Height, 0, GL.TexImage2D(TextureTarget.Texture2d, 0, PixelInternalFormat.Three, bitmap.Width, bitmap.Height, 0,
OpenTK.OpenGL.Enums.PixelFormat.Bgr, PixelType.UnsignedByte, data.Scan0); OpenTK.OpenGL.Enums.PixelFormat.Bgr, PixelType.UnsignedByte, data.Scan0);
@ -63,6 +61,15 @@ namespace Examples.Tutorial
#endregion #endregion
#region OnUnload
public override void OnUnload(EventArgs e)
{
GL.DeleteTextures(1, ref texture);
}
#endregion
#region OnResize #region OnResize
/// <summary> /// <summary>
@ -72,7 +79,7 @@ namespace Examples.Tutorial
/// <remarks>There is no need to call the base implementation.</remarks> /// <remarks>There is no need to call the base implementation.</remarks>
protected override void OnResize(OpenTK.Platform.ResizeEventArgs e) protected override void OnResize(OpenTK.Platform.ResizeEventArgs e)
{ {
GL.Viewport(0, 0, e.Width, e.Height); GL.Viewport(0, 0, Width, Height);
GL.MatrixMode(MatrixMode.Projection); GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity(); GL.LoadIdentity();
@ -107,40 +114,41 @@ namespace Examples.Tutorial
{ {
GL.Clear(ClearBufferMask.ColorBufferBit); GL.Clear(ClearBufferMask.ColorBufferBit);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity(); GL.LoadIdentity();
GL.BindTexture(TextureTarget.Texture2d, texture); GL.BindTexture(TextureTarget.Texture2d, texture);
GL.Begin(BeginMode.Quads); GL.Begin(BeginMode.Quads);
GL.TexCoord2(0.0f, 1.0f); GL.Vertex2(-0.8f + 0.0375f, -0.8f + 0.0375f); GL.TexCoord2(0.0f, 1.0f); GL.Vertex2(-0.6f, -0.4f);
GL.TexCoord2(1.0f, 1.0f); GL.Vertex2(0.8f + 0.0375f, -0.8f + 0.0375f); GL.TexCoord2(1.0f, 1.0f); GL.Vertex2(0.6f, -0.4f);
GL.TexCoord2(1.0f, 0.0f); GL.Vertex2(0.8f + 0.0375f, 0.8f + 0.0375f); GL.TexCoord2(1.0f, 0.0f); GL.Vertex2(0.6f, 0.4f);
GL.TexCoord2(0.0f, 0.0f); GL.Vertex2(-0.8f + 0.0375f, 0.8f + 0.0375f); GL.TexCoord2(0.0f, 0.0f); GL.Vertex2(-0.6f, 0.4f);
GL.End(); GL.End();
//sans.
GL.Scale(2.0f / Width, 2.0f / Height, 1.0f);
//sans.Print('A');
SwapBuffers(); SwapBuffers();
} }
#endregion #endregion
#region IExample Members #region public static void Main()
/// <summary> /// <summary>
/// Only used by the ExampleLauncher application, no need to add a Launch() method in your code. /// Entry point of this example.
/// Add a call to Run() in your Main() function instead.
/// </summary> /// </summary>
public void Launch() [STAThread]
public static void Main()
{ {
this.Run(10.0, 5.0); 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 #endregion
public static readonly int order = 5;
} }
} }

View file

@ -22,7 +22,8 @@ using OpenTK.OpenGL.Enums;
namespace Examples.WinForms namespace Examples.WinForms
{ {
public partial class W01_First_Window : Form, IExample [Example("Simple GLControl", ExampleCategory.WinForms, 1)]
public partial class W01_First_Window : Form
{ {
public W01_First_Window() public W01_First_Window()
{ {
@ -38,6 +39,8 @@ namespace Examples.WinForms
} }
} }
#region Events
protected override void OnLoad(EventArgs e) protected override void OnLoad(EventArgs e)
{ {
base.OnLoad(e); base.OnLoad(e);
@ -87,15 +90,25 @@ namespace Examples.WinForms
} }
} }
#region IExample Members #endregion
public void Launch() #region public static void Main()
/// <summary>
/// Entry point of this example.
/// </summary>
[STAThread]
public static void Main()
{ {
using (W01_First_Window example = new W01_First_Window())
{
// Get the title and category of this example using reflection.
ExampleAttribute info = ((ExampleAttribute)example.GetType().GetCustomAttributes(false)[0]);
example.Text = String.Format("OpenTK | {0} {1}: {2}", info.Category, info.Difficulty, info.Title);
example.ShowDialog();
}
} }
public static readonly int order = 1;
#endregion #endregion
} }
} }

View file

@ -37,7 +37,7 @@
this.glControl.Dock = System.Windows.Forms.DockStyle.Fill; this.glControl.Dock = System.Windows.Forms.DockStyle.Fill;
this.glControl.Location = new System.Drawing.Point(0, 0); this.glControl.Location = new System.Drawing.Point(0, 0);
this.glControl.Name = "glControl"; this.glControl.Name = "glControl";
this.glControl.Size = new System.Drawing.Size(624, 444); this.glControl.Size = new System.Drawing.Size(784, 564);
this.glControl.TabIndex = 0; this.glControl.TabIndex = 0;
this.glControl.VSync = false; this.glControl.VSync = false;
this.glControl.Layout += new System.Windows.Forms.LayoutEventHandler(this.glControl_Layout); this.glControl.Layout += new System.Windows.Forms.LayoutEventHandler(this.glControl_Layout);
@ -46,7 +46,7 @@
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(624, 444); this.ClientSize = new System.Drawing.Size(784, 564);
this.Controls.Add(this.glControl); this.Controls.Add(this.glControl);
this.Name = "W02_Immediate_Mode_Cube"; this.Name = "W02_Immediate_Mode_Cube";
this.Text = "Cube"; this.Text = "Cube";

View file

@ -25,7 +25,8 @@ using OpenTK.OpenGL.Enums;
namespace Examples.WinForms namespace Examples.WinForms
{ {
public partial class W02_Immediate_Mode_Cube : Form, IExample [Example("GLControl game loop", ExampleCategory.WinForms, 2)]
public partial class W02_Immediate_Mode_Cube : Form
{ {
static float angle = 0.0f; static float angle = 0.0f;
@ -86,29 +87,6 @@ namespace Examples.WinForms
#endregion #endregion
#region private void Render()
private void Render()
{
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
Glu.LookAt(
0.0, 5.0, 5.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0
);
GL.Rotate(angle, 0.0f, 1.0f, 0.0f);
angle += 0.5f;
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
DrawCube();
glControl.SwapBuffers();
}
#endregion
#region GLControl.Resize event handler #region GLControl.Resize event handler
void glControl_Resize(object sender, EventArgs e) void glControl_Resize(object sender, EventArgs e)
@ -151,6 +129,29 @@ namespace Examples.WinForms
Render(); Render();
} }
#endregion
#region private void Render()
private void Render()
{
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
Glu.LookAt(
0.0, 5.0, 5.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0
);
GL.Rotate(angle, 0.0f, 1.0f, 0.0f);
angle += 0.5f;
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
DrawCube();
glControl.SwapBuffers();
}
#endregion #endregion
#region private void DrawCube() #region private void DrawCube()
@ -199,22 +200,30 @@ namespace Examples.WinForms
GL.End(); GL.End();
} }
#endregion
#region IExample Members
public void Launch()
{
}
public static readonly int order = 2;
#endregion #endregion
private void glControl_Layout(object sender, LayoutEventArgs e) private void glControl_Layout(object sender, LayoutEventArgs e)
{ {
glControl_Resize(sender, EventArgs.Empty); glControl_Resize(sender, EventArgs.Empty);
} }
#region public static void Main()
/// <summary>
/// Entry point of this example.
/// </summary>
[STAThread]
public static void Main()
{
using (W02_Immediate_Mode_Cube example = new W02_Immediate_Mode_Cube())
{
// Get the title and category of this example using reflection.
ExampleAttribute info = ((ExampleAttribute)example.GetType().GetCustomAttributes(false)[0]);
example.Text = String.Format("OpenTK | {0} {1}: {2}", info.Category, info.Difficulty, info.Title);
example.ShowDialog();
}
}
#endregion
} }
} }

View file

@ -20,10 +20,11 @@ using OpenTK.OpenGL.Enums;
namespace Examples.WinForms namespace Examples.WinForms
{ {
public partial class W03_Extensions : Form, IExample [Example("Extensions", ExampleCategory.WinForms, 3)]
public partial class W03_Extensions : Form
{ {
GLControl glControl = new GLControl(); //GLControl glControl = new GLControl();
Assembly assembly; GLContext context;
Type glClass; Type glClass;
Type delegatesClass; Type delegatesClass;
Type importsClass; Type importsClass;
@ -34,18 +35,22 @@ namespace Examples.WinForms
{ {
InitializeComponent(); InitializeComponent();
assembly = Assembly.Load("OpenTK"); glClass = typeof(GL);
glClass = assembly.GetType("OpenTK.OpenGL.GL");
delegatesClass = glClass.GetNestedType("Delegates", BindingFlags.Static | BindingFlags.NonPublic); delegatesClass = glClass.GetNestedType("Delegates", BindingFlags.Static | BindingFlags.NonPublic);
importsClass = glClass.GetNestedType("Imports", BindingFlags.Static | BindingFlags.NonPublic); importsClass = glClass.GetNestedType("Imports", BindingFlags.Static | BindingFlags.NonPublic);
glControl.Load += new EventHandler(glControl_Load); //glControl.CreateControl();
glControl.CreateControl(); Application.Idle += StartAsync;
} }
void glControl_Load(object sender, EventArgs e) void StartAsync(object sender, EventArgs e)
{ {
Application.Idle += StartAsync; Application.Idle -= StartAsync;
context = new GLContext(new DisplayMode(), new OpenTK.Platform.WindowInfo(this));
context.CreateContext();
//while (!glControl.Created)
driver = driver =
GL.GetString(StringName.Vendor) + " " + GL.GetString(StringName.Vendor) + " " +
@ -54,11 +59,6 @@ namespace Examples.WinForms
all = delegatesClass.GetFields(BindingFlags.Static | BindingFlags.NonPublic).Length; all = delegatesClass.GetFields(BindingFlags.Static | BindingFlags.NonPublic).Length;
this.Text = String.Format("Loading {0} functions...", all); this.Text = String.Format("Loading {0} functions...", all);
}
void StartAsync(object sender, EventArgs e)
{
Application.Idle -= StartAsync;
this.backgroundWorker1.RunWorkerAsync(); this.backgroundWorker1.RunWorkerAsync();
} }
@ -74,9 +74,7 @@ namespace Examples.WinForms
{ {
object d = f.GetValue(delegatesClass); object d = f.GetValue(delegatesClass);
if (d != null) if (d != null)
{
++supported; ++supported;
}
backgroundWorker1.ReportProgress((int)(((float)i / all) * 100.0f), backgroundWorker1.ReportProgress((int)(((float)i / all) * 100.0f),
String.Format("({0}/{1}) {2}:\t{3}", (++i).ToString(), all, d != null ? "ok" : "failed", f.Name)); String.Format("({0}/{1}) {2}:\t{3}", (++i).ToString(), all, d != null ? "ok" : "failed", f.Name));
@ -88,6 +86,7 @@ namespace Examples.WinForms
throw; throw;
} }
} }
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{ {
listBox1.Items.Add(e.UserState as string); listBox1.Items.Add(e.UserState as string);
@ -105,18 +104,29 @@ namespace Examples.WinForms
*/ */
} }
#region IExample Members
public void Launch() { }
public static readonly int order = 3;
#endregion
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{ {
this.Text = String.Format("{0}: {1}/{2} OpenGL functions supported.", this.Text = String.Format("{0}: {1}/{2} OpenGL functions supported.",
driver, supported, all); driver, supported, all);
} }
#region public static void Main()
/// <summary>
/// Entry point of this example.
/// </summary>
[STAThread]
public static void Main()
{
using (W03_Extensions example = new W03_Extensions())
{
// Get the title and category of this example using reflection.
ExampleAttribute info = ((ExampleAttribute)example.GetType().GetCustomAttributes(false)[0]);
example.Text = String.Format("OpenTK | {0} {1}: {2}", info.Category, info.Difficulty, info.Title);
example.ShowDialog();
}
}
#endregion
} }
} }