2009-02-22 10:43:35 +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.Drawing;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
|
|
using OpenTK;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using OpenTK.Input;
|
|
|
|
|
using OpenTK.Graphics;
|
2009-08-14 13:13:28 +00:00
|
|
|
|
using OpenTK.Graphics.OpenGL;
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
|
|
|
|
namespace Examples.Tutorial
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Demonstrates Vertex Arrays (in system memory). Example is incomplete (documentation).
|
|
|
|
|
/// Broken since release 0.3.12.
|
|
|
|
|
/// </summary>
|
2010-10-02 18:52:34 +00:00
|
|
|
|
[Example("Vertex Arrays", ExampleCategory.OpenGL, "1.x", 5, false)]
|
2009-02-22 10:43:35 +00:00
|
|
|
|
class T02_Vertex_Arrays : GameWindow
|
|
|
|
|
{
|
|
|
|
|
float rotation_speed = 3.0f;
|
|
|
|
|
float angle = 0.0f;
|
|
|
|
|
|
|
|
|
|
Shapes.Shape shape = new Examples.Shapes.Plane(16, 16, 2.0f, 2.0f);
|
|
|
|
|
//TextureFont sans = new TextureFont(new Font(FontFamily.GenericSansSerif, 32, FontStyle.Regular,
|
|
|
|
|
// GraphicsUnit.Pixel));
|
|
|
|
|
|
|
|
|
|
#region Constructor
|
|
|
|
|
|
|
|
|
|
public T02_Vertex_Arrays()
|
|
|
|
|
: base(800, 600)
|
|
|
|
|
{
|
|
|
|
|
//this.VSync = VSyncMode.On;
|
2009-09-04 22:10:50 +00:00
|
|
|
|
this.Keyboard.KeyUp += Keyboard_KeyUp;
|
2009-02-22 10:43:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-09-04 22:10:50 +00:00
|
|
|
|
void Keyboard_KeyUp(object sender, KeyboardKeyEventArgs e)
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
|
|
|
|
// F4 cycles between available VSync modes.
|
2009-09-04 22:10:50 +00:00
|
|
|
|
if (e.Key == Key.F4)
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
|
|
|
|
if (this.VSync == VSyncMode.Off)
|
|
|
|
|
this.VSync = VSyncMode.On;
|
|
|
|
|
else if (this.VSync == VSyncMode.On)
|
|
|
|
|
this.VSync = VSyncMode.Adaptive;
|
|
|
|
|
else
|
|
|
|
|
this.VSync = VSyncMode.Off;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region OnLoad
|
|
|
|
|
|
2009-10-21 13:33:00 +00:00
|
|
|
|
protected override void OnLoad(EventArgs e)
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
|
|
|
|
GL.Enable(EnableCap.Texture2D);
|
|
|
|
|
|
|
|
|
|
GL.ClearColor(Color.CadetBlue);
|
|
|
|
|
GL.Enable(EnableCap.DepthTest);
|
|
|
|
|
|
2010-10-09 19:56:33 +00:00
|
|
|
|
GL.EnableClientState(ArrayCap.VertexArray);
|
2009-02-22 10:43:35 +00:00
|
|
|
|
//GL.EnableClientState(GL.Enums.EnableCap.COLOR_ARRAY);
|
|
|
|
|
GL.VertexPointer(3, VertexPointerType.Float, 0, shape.Vertices);
|
|
|
|
|
//GL.ColorPointer(4, GL.Enums.ColorPointerType.UNSIGNED_BYTE, 0, shape.Colors);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region OnResize
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Called when the user resizes the window.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="e">Contains the new width/height of the window.</param>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// You want the OpenGL viewport to match the window. This is the place to do it!
|
|
|
|
|
/// </remarks>
|
2009-06-02 15:49:39 +00:00
|
|
|
|
protected override void OnResize(EventArgs e)
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
|
|
|
|
base.OnResize(e);
|
|
|
|
|
|
|
|
|
|
GL.Viewport(0, 0, Width, Height);
|
|
|
|
|
|
2009-08-14 13:13:28 +00:00
|
|
|
|
double aspect_ratio = Width / (double)Height;
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
2009-08-15 17:52:49 +00:00
|
|
|
|
OpenTK.Matrix4 perspective = OpenTK.Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, (float)aspect_ratio, 1, 64);
|
2009-02-22 10:43:35 +00:00
|
|
|
|
GL.MatrixMode(MatrixMode.Projection);
|
2009-08-14 13:13:28 +00:00
|
|
|
|
GL.LoadMatrix(ref perspective);
|
2009-02-22 10:43:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region OnUpdateFrame
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Prepares the next frame for rendering.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Place your control logic here. This is the place to respond to user input,
|
|
|
|
|
/// update object positions etc.
|
|
|
|
|
/// </remarks>
|
2009-06-02 15:49:39 +00:00
|
|
|
|
protected override void OnUpdateFrame(FrameEventArgs e)
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
|
|
|
|
// Escape quits.
|
|
|
|
|
if (Keyboard[Key.Escape])
|
|
|
|
|
{
|
|
|
|
|
this.Exit();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-03 19:04:42 +00:00
|
|
|
|
if (Keyboard[Key.F11])
|
2009-02-22 10:43:35 +00:00
|
|
|
|
if (WindowState != WindowState.Fullscreen)
|
|
|
|
|
WindowState = WindowState.Fullscreen;
|
|
|
|
|
else
|
|
|
|
|
WindowState = WindowState.Normal;
|
|
|
|
|
|
|
|
|
|
// Plus/Minus change the target render frequency.
|
|
|
|
|
// PageUp/PageDown change the target update frequency.
|
|
|
|
|
if (Keyboard[Key.Plus] || Keyboard[Key.KeypadAdd]) TargetRenderFrequency++;
|
|
|
|
|
if (Keyboard[Key.Minus] || Keyboard[Key.KeypadSubtract]) TargetRenderFrequency--;
|
|
|
|
|
if (Keyboard[Key.PageUp]) TargetUpdateFrequency++;
|
|
|
|
|
if (Keyboard[Key.PageDown]) TargetUpdateFrequency--;
|
|
|
|
|
|
|
|
|
|
// Right/Left control the rotation speed and direction.
|
|
|
|
|
if (Keyboard[Key.Right]) rotation_speed += 0.5f;
|
|
|
|
|
if (Keyboard[Key.Left]) rotation_speed -= 0.5f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region OnRenderFrame
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Place your rendering code here.
|
|
|
|
|
/// </summary>
|
2009-06-02 15:49:39 +00:00
|
|
|
|
protected override void OnRenderFrame(FrameEventArgs e)
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
|
|
|
|
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
|
|
|
|
|
|
2009-08-14 13:13:28 +00:00
|
|
|
|
Matrix4 lookat = Matrix4.LookAt(0, 5, 5, 0, 0, 0, 0, 1, 0);
|
2009-02-22 10:43:35 +00:00
|
|
|
|
GL.MatrixMode(MatrixMode.Modelview);
|
2009-08-14 13:13:28 +00:00
|
|
|
|
GL.LoadMatrix(ref lookat);
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
2009-06-02 15:49:39 +00:00
|
|
|
|
angle += rotation_speed * (float)e.Time;
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
|
|
|
|
if (angle >= 360.0f)
|
|
|
|
|
angle -= 360.0f;
|
|
|
|
|
GL.Rotate(angle, 0.0f, 1.0f, 0.0f);
|
|
|
|
|
|
|
|
|
|
GL.DrawElements(BeginMode.Triangles, shape.Indices.Length,
|
|
|
|
|
DrawElementsType.UnsignedInt, shape.Indices);
|
|
|
|
|
|
|
|
|
|
//GL.Begin(GL.Enums.BeginMode.TRIANGLES);
|
|
|
|
|
|
|
|
|
|
//GL.Vertex3(-1.0, -1.0, 5.0);
|
|
|
|
|
//GL.Vertex3(1.0, -1.0, 5.0);
|
|
|
|
|
//GL.Vertex3(1.0, 1.0, 5.0);
|
|
|
|
|
|
|
|
|
|
//GL.End();
|
|
|
|
|
|
|
|
|
|
GL.PushMatrix();
|
|
|
|
|
|
|
|
|
|
GL.MatrixMode(MatrixMode.Projection);
|
|
|
|
|
GL.LoadIdentity();
|
|
|
|
|
GL.Ortho(-1.0, 1.0, -1.0, 1.0, 0.0, 1.0);
|
|
|
|
|
GL.MatrixMode(MatrixMode.Modelview);
|
|
|
|
|
GL.LoadIdentity();
|
|
|
|
|
|
|
|
|
|
//GL.Translate(0.7f, 1.0f, 0.0f);
|
|
|
|
|
//sans.Print(String.format("{0:f1}", RenderFrequency));
|
|
|
|
|
|
|
|
|
|
GL.PopMatrix();
|
|
|
|
|
|
|
|
|
|
SwapBuffers();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region public void Launch()
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Launches this example.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Provides a simple way for the example launcher to launch the examples.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public void Launch()
|
|
|
|
|
{
|
|
|
|
|
// Lock UpdateFrame rate at 30Hz and RenderFrame rate 85Hz.
|
|
|
|
|
//Run(60.0, 85.0);
|
|
|
|
|
Run(30.0, 85.0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
public static readonly int order = 2;
|
|
|
|
|
}
|
|
|
|
|
}
|