Updated all examples. Added documentation and generally cleaned them up.

This commit is contained in:
the_fiddler 2007-09-26 11:47:30 +00:00
parent 73903b3865
commit 8a3ad855b0
9 changed files with 138 additions and 138 deletions

View file

@ -8,14 +8,14 @@ using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using OpenTK;
using OpenTK.OpenGL;
using System.Threading;
using System.Runtime.Serialization;
using System.IO;
using System.Diagnostics;
using OpenTK;
using OpenTK.OpenGL;
namespace Examples.Tests
{
public class S02_RawInput_Logger : GameWindow//, IExample
@ -61,23 +61,20 @@ namespace Examples.Tests
{
this.CreateWindow(new DisplayMode(100, 100));
foreach (OpenTK.Input.Keyboard k in this.Keyboard)
{
k.KeyDown += new OpenTK.Input.KeyDownEvent(LogKeyDown);
k.KeyUp += new OpenTK.Input.KeyUpEvent(LogKeyUp);
}
Keyboard.KeyDown += new OpenTK.Input.KeyDownEvent(LogKeyDown);
Keyboard.KeyUp += new OpenTK.Input.KeyUpEvent(LogKeyUp);
}
void LogKeyDown(object sender, OpenTK.Input.Key key)
{
Trace.WriteLine(String.Format("OpenTK key {0} pressed on Keyboard: ({1}).",
key, sender as OpenTK.Input.Keyboard));
key, sender as OpenTK.Input.KeyboardDevice));
}
void LogKeyUp(object sender, OpenTK.Input.Key key)
{
Trace.WriteLine(String.Format("OpenTK key {0} released on Keyboard: ({1}).",
key, sender as OpenTK.Input.Keyboard));
key, sender as OpenTK.Input.KeyboardDevice));
}
public override void OnLoad(EventArgs e)

View file

@ -22,119 +22,95 @@ namespace Examples.Tests
{
public partial class S04_Input_Logger : Form//, IExample
{
InputDriver driver;
Thread thread;
GameWindow hidden;
bool start;
Dictionary<IntPtr, ListBox> keyboardListBoxes = new Dictionary<IntPtr, ListBox>(4);
public S04_Input_Logger()
{
InitializeComponent();
thread = new Thread(LaunchGameWindow);
thread.Start();
}
void LaunchGameWindow()
{
hidden = new GameWindow();
hidden.Load += hidden_Load;
hidden.CreateWindow(new DisplayMode(30, 30), "OpenTK | Hidden input window");
hidden.Run(60.0, 1.0);
}
void hidden_Load(object sender, EventArgs e)
{
start = true;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
WindowInfo info = new WindowInfo(this);
driver = new InputDriver(info);
Trace.WriteLine(String.Format("Keyboard count: {0}", driver.Keyboard.Count));
Trace.WriteLine(String.Format("Mouse count: {0}", driver.Mouse.Count));
switch (driver.Keyboard.Count)
while (!start)
{
case 0:
Debug.Print("No keyboard present, or keyboard driver failed to load");
break;
case 1:
keyboardListBoxes.Add(driver.Keyboard[0].DeviceID, listBox1);
break;
case 2:
keyboardListBoxes.Add(driver.Keyboard[0].DeviceID, listBox1);
keyboardListBoxes.Add(driver.Keyboard[1].DeviceID, listBox2);
break;
case 3:
keyboardListBoxes.Add(driver.Keyboard[0].DeviceID, listBox1);
keyboardListBoxes.Add(driver.Keyboard[1].DeviceID, listBox2);
keyboardListBoxes.Add(driver.Keyboard[2].DeviceID, listBox3);
break;
case 4:
keyboardListBoxes.Add(driver.Keyboard[0].DeviceID, listBox1);
keyboardListBoxes.Add(driver.Keyboard[1].DeviceID, listBox2);
keyboardListBoxes.Add(driver.Keyboard[2].DeviceID, listBox3);
keyboardListBoxes.Add(driver.Keyboard[3].DeviceID, listBox4);
break;
default:
Debug.Print("Only the first 4 keyboards will be shown in the keyboard logger.");
keyboardListBoxes.Add(driver.Keyboard[0].DeviceID, listBox1);
keyboardListBoxes.Add(driver.Keyboard[1].DeviceID, listBox2);
keyboardListBoxes.Add(driver.Keyboard[2].DeviceID, listBox3);
keyboardListBoxes.Add(driver.Keyboard[3].DeviceID, listBox4);
break;
Thread.Sleep(100);
}
WindowInfo info = new WindowInfo(this);
keyboardListBoxes.Add(hidden.Keyboard.DeviceID, listBox1);
// Add available mice to the mouse input logger.
if (driver.Mouse.Count > 0)
{
int i = 0;
foreach (Mouse m in driver.Mouse)
{
ChooseMouse.Items.Add(String.Format("Mouse {0} ({1})", ++i, m.Description));
m.ButtonDown += LogMouseButtonDown;
m.ButtonUp += LogMouseButtonUp;
}
ChooseMouse.SelectedIndex = 0;
}
ChooseMouse.Items.Add(String.Format("Mouse {0} ({1})", 0, hidden.Mouse.Description));
hidden.Mouse.ButtonDown += LogMouseButtonDown;
hidden.Mouse.ButtonUp += LogMouseButtonUp;
foreach (OpenTK.Input.Keyboard k in driver.Keyboard)
{
k.KeyDown += new KeyDownEvent(LogKeyDown);
k.KeyUp += new KeyUpEvent(LogKeyUp);
}
hidden.Keyboard.KeyDown += LogKeyDown;
hidden.Keyboard.KeyUp += LogKeyUp;
//Application.Idle += new EventHandler(UpdateDevices);
hidden.UpdateFrame += hidden_UpdateFrame;
Application.Idle += new EventHandler(UpdateDevices);
}
void UpdateDevices(object sender, EventArgs e)
void hidden_UpdateFrame(object sender, UpdateFrameEventArgs e)
{
driver.Poll();
//hidden.Poll();
// Update mouse coordinates.
MouseXText.Text = driver.Mouse[ChooseMouse.SelectedIndex].X.ToString();
MouseYText.Text = driver.Mouse[ChooseMouse.SelectedIndex].Y.ToString();
MouseDXText.Text = driver.Mouse[ChooseMouse.SelectedIndex].XDelta.ToString();
MouseDYText.Text = driver.Mouse[ChooseMouse.SelectedIndex].YDelta.ToString();
MouseWheelText.Text = driver.Mouse[ChooseMouse.SelectedIndex].Wheel.ToString();
MouseXText.Text = hidden.Mouse.X.ToString();
MouseYText.Text = hidden.Mouse.Y.ToString();
MouseDXText.Text = hidden.Mouse.XDelta.ToString();
MouseDYText.Text = hidden.Mouse.YDelta.ToString();
MouseWheelText.Text = hidden.Mouse.Wheel.ToString();
//MouseWheelDelta.Text = driver.Mouse[ChooseMouse.SelectedIndex].WheelDelta.ToString();
}
void LogMouseButtonDown(IMouse sender, MouseButton button)
void LogMouseButtonDown(MouseDevice sender, MouseButton button)
{
//Trace.WriteLine(String.Format("Mouse button down: {0} on device: {1}", button, sender.DeviceID));
if (sender.DeviceID == driver.Mouse[ChooseMouse.SelectedIndex].DeviceID)
if (sender.DeviceID == hidden.Mouse.DeviceID)
MouseButtons.Items.Add(button);
}
void LogMouseButtonUp(IMouse sender, MouseButton button)
void LogMouseButtonUp(MouseDevice sender, MouseButton button)
{
//Trace.WriteLine(String.Format("Mouse button up: {0} on device: {1}", button, sender.DeviceID));
if (sender.DeviceID == driver.Mouse[ChooseMouse.SelectedIndex].DeviceID)
if (sender.DeviceID == hidden.Mouse.DeviceID)
MouseButtons.Items.Remove(button);
}
void LogKeyDown(object sender, Key key)
void LogKeyDown(KeyboardDevice sender, Key key)
{
Trace.WriteLine(String.Format("Key down: {0} on device: {1}", key, (sender as Keyboard).DeviceID));
keyboardListBoxes[(sender as Keyboard).DeviceID].Items.Add(key);
Trace.WriteLine(String.Format("Key down: {0} on device: {1}", key, (sender as KeyboardDevice).DeviceID));
keyboardListBoxes[(sender as KeyboardDevice).DeviceID].Items.Add(key);
}
void LogKeyUp(object sender, Key key)
void LogKeyUp(KeyboardDevice sender, Key key)
{
Trace.WriteLine(String.Format("Key up: {0} on device: {1}", key, (sender as Keyboard).DeviceID));
keyboardListBoxes[(sender as Keyboard).DeviceID].Items.Remove(key);
Trace.WriteLine(String.Format("Key up: {0} on device: {1}", key, (sender as KeyboardDevice).DeviceID));
keyboardListBoxes[(sender as KeyboardDevice).DeviceID].Items.Remove(key);
}
#region IExample Members

View file

@ -15,21 +15,39 @@ using OpenTK.OpenGL;
namespace Examples.Tutorial
{
/// <summary>
/// Demonstrates the GameWindow class.
/// </summary>
public class T01_Simple_Window : GameWindow, IExample
{
public T01_Simple_Window()
{
this.CreateWindow(new DisplayMode(800, 600));
this.CreateWindow(new DisplayMode(800, 600), "OpenTK | Tutorial 1: Simple Window");
}
#region OnLoad
/// <summary>
/// Load resources here.
/// </summary>
/// <param name="e">Not used.</param>
public override void OnLoad(EventArgs e)
{
Trace.WriteLine(String.Format("OpenGL driver information: {0}, {1}, {2}",
GL.GetString(GL.Enums.StringName.RENDERER),
GL.GetString(GL.Enums.StringName.VENDOR),
GL.GetString(GL.Enums.StringName.VERSION)));
}
#endregion
#region OnResize
/// <summary>
/// Override the OnResize method to respond to window resize events.
/// Do not forget to call base.OnResize() so that event listeners
/// will be notified of window resize events!
/// Respond to resize events here.
/// </summary>
/// <param name="e"></param>
/// <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, e.Width, e.Height);
@ -43,14 +61,30 @@ namespace Examples.Tutorial
#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)
{
base.OnUpdateFrame(e);
if (Keyboard[OpenTK.Input.Key.Escape])
this.Exit();
}
#endregion
#region OnRenderFrame
/// <summary>
/// Override the OnRenderFrame method to add your drawing code.
/// Do not forget to call base.OnRenderFrame() so that event listeners
/// will be notified of frame rendering events!
/// Add your game rendering code here.
/// </summary>
/// <param name="e">Not used.</param>
/// <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(GL.Enums.ClearBufferMask.COLOR_BUFFER_BIT);
@ -66,23 +100,17 @@ namespace Examples.Tutorial
GL.End();
Context.SwapBuffers();
base.OnRenderFrame(e);
this.SwapBuffers();
}
#endregion
public override void OnUpdateFrame(UpdateFrameEventArgs e)
{
base.OnUpdateFrame(e);
if (Keyboard[0][OpenTK.Input.Key.Escape])
this.Exit();
}
#region IExample Members
/// <summary>
/// Only used by the ExampleLauncher application, no need to add a Launch() method in your code.
/// Add a call to Run() in your Main() function instead.
/// </summary>
public void Launch()
{
this.Run(30.0, 5.0);

View file

@ -80,14 +80,14 @@ namespace Examples.Tutorial
/// </remarks>
public override void OnUpdateFrame(UpdateFrameEventArgs e)
{
if (Keyboard[0][OpenTK.Input.Key.Escape])
if (Keyboard[OpenTK.Input.Key.Escape])
{
this.Exit();
return;
}
if ((Keyboard[0][OpenTK.Input.Key.AltLeft] || Keyboard[0][OpenTK.Input.Key.AltRight]) &&
Keyboard[0][OpenTK.Input.Key.Enter])
if ((Keyboard[OpenTK.Input.Key.AltLeft] || Keyboard[OpenTK.Input.Key.AltRight]) &&
Keyboard[OpenTK.Input.Key.Enter])
{
Fullscreen = !Fullscreen;
}

View file

@ -87,14 +87,14 @@ namespace Examples.Tutorial
/// </remarks>
public override void OnUpdateFrame(UpdateFrameEventArgs e)
{
if (Keyboard[0][OpenTK.Input.Key.Escape])
if (Keyboard[OpenTK.Input.Key.Escape])
{
this.Exit();
return;
}
if ((Keyboard[0][OpenTK.Input.Key.AltLeft] || Keyboard[0][OpenTK.Input.Key.AltRight]) &&
Keyboard[0][OpenTK.Input.Key.Enter])
if ((Keyboard[OpenTK.Input.Key.AltLeft] || Keyboard[OpenTK.Input.Key.AltRight]) &&
Keyboard[OpenTK.Input.Key.Enter])
{
Fullscreen = !Fullscreen;
}

View file

@ -15,6 +15,9 @@ using Examples.Shapes;
namespace Examples.Tutorial
{
/// <summary>
/// Demonstrates fixed-function OpenGL lighting. Tutorial is incomplete!
/// </summary>
class T04_Lit_Cube : GameWindow, IExample
{
float x_angle, zoom;
@ -24,7 +27,7 @@ namespace Examples.Tutorial
public T04_Lit_Cube()
{
this.CreateWindow(new DisplayMode(800, 600));
this.CreateWindow(new DisplayMode(800, 600), "OpenTK | Vertex Lighting example");
}
#endregion
@ -37,7 +40,7 @@ namespace Examples.Tutorial
GL.ClearColor(Color.MidnightBlue);
GL.Enable(GL.Enums.EnableCap.DEPTH_TEST);
GL.Enable(GL.Enums.EnableCap.CULL_FACE);
//GL.Enable(GL.Enums.EnableCap.CULL_FACE);
GL.EnableClientState(GL.Enums.EnableCap.VERTEX_ARRAY);
GL.EnableClientState(GL.Enums.EnableCap.NORMAL_ARRAY);
@ -50,8 +53,8 @@ namespace Examples.Tutorial
GL.Lightv(GL.Enums.LightName.LIGHT0, GL.Enums.LightParameter.DIFFUSE, new float[] { 1.0f, 1.0f, 1.0f, 1.0f });
GL.Lightv(GL.Enums.LightName.LIGHT0, GL.Enums.LightParameter.SPECULAR, new float[] { 1.0f, 1.0f, 1.0f, 1.0f });
GL.LightModelv(GL.Enums.LightModelParameter.LIGHT_MODEL_AMBIENT, new float[] { 0.2f, 0.2f, 0.2f, 1.0f });
GL.Enable(GL.Enums.EnableCap.LIGHTING);
GL.Enable(GL.Enums.EnableCap.LIGHT0);
//GL.Enable(GL.Enums.EnableCap.LIGHTING);
//GL.Enable(GL.Enums.EnableCap.LIGHT0);
// Use GL.Material to set your object's material parameters..
GL.Materialv(GL.Enums.MaterialFace.FRONT, GL.Enums.MaterialParameter.AMBIENT, new float[] { 0.3f, 0.3f, 0.3f, 1.0f });
@ -97,25 +100,25 @@ namespace Examples.Tutorial
/// </remarks>
public override void OnUpdateFrame(UpdateFrameEventArgs e)
{
if (Keyboard[0][OpenTK.Input.Key.Escape])
if (Keyboard[OpenTK.Input.Key.Escape])
{
this.Exit();
return;
}
if ((Keyboard[0][OpenTK.Input.Key.AltLeft] || Keyboard[0][OpenTK.Input.Key.AltRight]) &&
Keyboard[0][OpenTK.Input.Key.Enter])
if ((Keyboard[OpenTK.Input.Key.AltLeft] || Keyboard[OpenTK.Input.Key.AltRight]) &&
Keyboard[OpenTK.Input.Key.Enter])
{
Fullscreen = !Fullscreen;
}
if (Mouse[0][OpenTK.Input.MouseButton.Left])
x_angle += Mouse[0].XDelta * 2;
if (Mouse[OpenTK.Input.MouseButton.Left])
x_angle += Mouse.XDelta * 2;
else
x_angle += 0.5f;
if (Mouse[0][OpenTK.Input.MouseButton.Right])
zoom += Mouse[0].YDelta * 0.5f;
if (Mouse[OpenTK.Input.MouseButton.Right])
zoom += Mouse.YDelta * 0.5f;
if (x_angle > 720.0f)
x_angle -= 720.0f;
@ -142,17 +145,10 @@ namespace Examples.Tutorial
0.0, 1.0, 0.0);
GL.Rotate(x_angle, 0.0f, 1.0f, 0.0f);
unsafe
{
fixed (int* ptr = shape.Indices)
{
GL.DrawElements(GL.Enums.BeginMode.TRIANGLES, shape.Indices.Length,
GL.Enums.All.UNSIGNED_INT, ptr);
}
}
GL.DrawArrays(GL.Enums.BeginMode.POINTS, 0, shape.Vertices.Length);
GL.DrawElements(GL.Enums.BeginMode.TRIANGLES, shape.Indices.Length,
GL.Enums.All.UNSIGNED_INT, shape.Indices);
Context.SwapBuffers();
SwapBuffers();
}
#endregion

View file

@ -112,7 +112,7 @@ namespace Examples.Tutorial
{
base.OnUpdateFrame(e);
if (Keyboard[0][OpenTK.Input.Key.Escape])
if (Keyboard[OpenTK.Input.Key.Escape])
{
this.Exit();
}

View file

@ -131,7 +131,7 @@ namespace Examples.Tutorial
/// </remarks>
public override void OnUpdateFrame(UpdateFrameEventArgs e)
{
if (Keyboard[0][OpenTK.Input.Key.Escape])
if (Keyboard[OpenTK.Input.Key.Escape])
{
this.Exit();
return;

View file

@ -21,6 +21,9 @@ using OpenTK;
namespace Examples.Tutorial
{
/// <summary>
/// Demonstrates how to load and use a simple OpenGL shader program.
/// </summary>
public class T10_GLSL_Cube : GameWindow, IExample
{
#region --- Fields ---
@ -55,7 +58,7 @@ void main()
public T10_GLSL_Cube()
{
this.CreateWindow(new DisplayMode(800, 600));
this.CreateWindow(new DisplayMode(800, 600), "OpenTK | GLSL Example 1");
}
#endregion
@ -127,7 +130,7 @@ void main()
GL.LinkProgram(shader_program);
GL.UseProgram(shader_program);
OnResize(new OpenTK.Platform.ResizeEventArgs(this.Width, this.Height));
//OnResize(new OpenTK.Platform.ResizeEventArgs(this.Width, this.Height));
}
#endregion
@ -167,14 +170,14 @@ void main()
/// </remarks>
public override void OnUpdateFrame(UpdateFrameEventArgs e)
{
if (Keyboard[0][OpenTK.Input.Key.Escape])
if (Keyboard[OpenTK.Input.Key.Escape])
{
this.Exit();
return;
}
if ((Keyboard[0][OpenTK.Input.Key.AltLeft] || Keyboard[0][OpenTK.Input.Key.AltRight]) &&
Keyboard[0][OpenTK.Input.Key.Enter])
if ((Keyboard[OpenTK.Input.Key.AltLeft] || Keyboard[OpenTK.Input.Key.AltRight]) &&
Keyboard[OpenTK.Input.Key.Enter])
{
Fullscreen = !Fullscreen;
}