2009-11-09 17:34:24 +00:00
|
|
|
#region --- License ---
|
2009-02-22 10:43:35 +00:00
|
|
|
/* Licensed under the MIT/X11 license.
|
|
|
|
* Copyright (c) 2006-2008 the OpenTK Team.
|
|
|
|
* This notice may not be removed from any source distribution.
|
|
|
|
* See license.txt for licensing detailed licensing details.
|
|
|
|
*/
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region --- Using Directives ---
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Windows.Forms;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Drawing;
|
|
|
|
|
|
|
|
using OpenTK;
|
|
|
|
using OpenTK.Graphics;
|
|
|
|
using OpenTK.Graphics.OpenGL;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
namespace Examples.Tutorial
|
|
|
|
{
|
|
|
|
/// <summary>
|
2009-04-20 07:20:25 +00:00
|
|
|
/// Demonstrates immediate mode rendering.
|
2009-02-22 10:43:35 +00:00
|
|
|
/// </summary>
|
2010-10-09 19:59:06 +00:00
|
|
|
[Example("Immediate mode", ExampleCategory.OpenGL, "1.x", 1, Documentation = "ImmediateMode")]
|
2009-02-22 10:43:35 +00:00
|
|
|
public class T03_Immediate_Mode_Cube : GameWindow
|
|
|
|
{
|
|
|
|
#region --- Fields ---
|
|
|
|
|
2009-11-03 13:03:35 +00:00
|
|
|
const float rotation_speed = 180.0f;
|
2009-02-22 10:43:35 +00:00
|
|
|
float angle;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region --- Constructor ---
|
|
|
|
|
2010-10-09 19:59:06 +00:00
|
|
|
public T03_Immediate_Mode_Cube()
|
|
|
|
: base(800, 600, new GraphicsMode(16, 16))
|
|
|
|
{ }
|
2009-02-22 10:43:35 +00:00
|
|
|
|
2010-10-09 19:59:06 +00:00
|
|
|
#endregion
|
2009-11-09 17:34:24 +00:00
|
|
|
|
2009-02-22 10:43:35 +00:00
|
|
|
#region OnLoad
|
|
|
|
|
2009-10-21 13:33:00 +00:00
|
|
|
protected override void OnLoad(EventArgs e)
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
|
|
|
base.OnLoad(e);
|
|
|
|
|
|
|
|
GL.ClearColor(Color.MidnightBlue);
|
|
|
|
GL.Enable(EnableCap.DepthTest);
|
|
|
|
}
|
|
|
|
|
|
|
|
#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
|
|
|
{
|
2009-11-09 17:34:24 +00:00
|
|
|
base.OnResize(e);
|
|
|
|
|
2009-02-22 10:43:35 +00:00
|
|
|
GL.Viewport(0, 0, Width, Height);
|
|
|
|
|
2009-06-02 15:49:39 +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
|
|
|
{
|
2009-11-09 17:34:24 +00:00
|
|
|
base.OnUpdateFrame(e);
|
|
|
|
|
2009-02-22 10:43:35 +00:00
|
|
|
if (Keyboard[OpenTK.Input.Key.Escape])
|
|
|
|
{
|
|
|
|
this.Exit();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#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
|
|
|
{
|
2009-11-09 17:34:24 +00:00
|
|
|
base.OnRenderFrame(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);
|
2010-10-09 19:59:06 +00:00
|
|
|
|
2009-06-02 15:49:39 +00:00
|
|
|
angle += rotation_speed * (float)e.Time;
|
2009-02-22 10:43:35 +00:00
|
|
|
GL.Rotate(angle, 0.0f, 1.0f, 0.0f);
|
|
|
|
|
|
|
|
DrawCube();
|
|
|
|
|
|
|
|
this.SwapBuffers();
|
2009-11-03 13:03:35 +00:00
|
|
|
Thread.Sleep(1);
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region private void DrawCube()
|
|
|
|
|
|
|
|
private void DrawCube()
|
|
|
|
{
|
|
|
|
GL.Begin(BeginMode.Quads);
|
|
|
|
|
|
|
|
GL.Color3(Color.Silver);
|
|
|
|
GL.Vertex3(-1.0f, -1.0f, -1.0f);
|
|
|
|
GL.Vertex3(-1.0f, 1.0f, -1.0f);
|
|
|
|
GL.Vertex3(1.0f, 1.0f, -1.0f);
|
|
|
|
GL.Vertex3(1.0f, -1.0f, -1.0f);
|
|
|
|
|
|
|
|
GL.Color3(Color.Honeydew);
|
|
|
|
GL.Vertex3(-1.0f, -1.0f, -1.0f);
|
|
|
|
GL.Vertex3(1.0f, -1.0f, -1.0f);
|
|
|
|
GL.Vertex3(1.0f, -1.0f, 1.0f);
|
|
|
|
GL.Vertex3(-1.0f, -1.0f, 1.0f);
|
|
|
|
|
|
|
|
GL.Color3(Color.Moccasin);
|
|
|
|
|
|
|
|
GL.Vertex3(-1.0f, -1.0f, -1.0f);
|
|
|
|
GL.Vertex3(-1.0f, -1.0f, 1.0f);
|
|
|
|
GL.Vertex3(-1.0f, 1.0f, 1.0f);
|
|
|
|
GL.Vertex3(-1.0f, 1.0f, -1.0f);
|
|
|
|
|
|
|
|
GL.Color3(Color.IndianRed);
|
|
|
|
GL.Vertex3(-1.0f, -1.0f, 1.0f);
|
|
|
|
GL.Vertex3(1.0f, -1.0f, 1.0f);
|
|
|
|
GL.Vertex3(1.0f, 1.0f, 1.0f);
|
|
|
|
GL.Vertex3(-1.0f, 1.0f, 1.0f);
|
|
|
|
|
|
|
|
GL.Color3(Color.PaleVioletRed);
|
|
|
|
GL.Vertex3(-1.0f, 1.0f, -1.0f);
|
|
|
|
GL.Vertex3(-1.0f, 1.0f, 1.0f);
|
|
|
|
GL.Vertex3(1.0f, 1.0f, 1.0f);
|
|
|
|
GL.Vertex3(1.0f, 1.0f, -1.0f);
|
|
|
|
|
|
|
|
GL.Color3(Color.ForestGreen);
|
|
|
|
GL.Vertex3(1.0f, -1.0f, -1.0f);
|
|
|
|
GL.Vertex3(1.0f, 1.0f, -1.0f);
|
|
|
|
GL.Vertex3(1.0f, 1.0f, 1.0f);
|
|
|
|
GL.Vertex3(1.0f, -1.0f, 1.0f);
|
|
|
|
|
|
|
|
GL.End();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region public static void Main()
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Entry point of this example.
|
|
|
|
/// </summary>
|
|
|
|
[STAThread]
|
|
|
|
public static void Main()
|
|
|
|
{
|
|
|
|
using (T03_Immediate_Mode_Cube example = new T03_Immediate_Mode_Cube())
|
|
|
|
{
|
2009-06-26 21:07:57 +00:00
|
|
|
Utilities.SetWindowTitle(example);
|
2009-02-22 10:43:35 +00:00
|
|
|
example.Run(30.0, 0.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|