2007-07-23 00:15:18 +00:00
|
|
|
|
#region --- License ---
|
|
|
|
|
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
|
|
|
|
|
* See license.txt for license info
|
|
|
|
|
*/
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region --- Using Directives ---
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
using OpenTK;
|
|
|
|
|
using OpenTK.OpenGL;
|
|
|
|
|
using OpenTK.Platform;
|
2007-08-01 09:32:49 +00:00
|
|
|
|
using Enums = OpenTK.OpenGL.GL.Enums;
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
namespace Examples.Tutorial
|
|
|
|
|
{
|
|
|
|
|
public class T03_RotatingCube : OpenTK.GameWindow, IExample
|
|
|
|
|
{
|
|
|
|
|
#region --- Fields ---
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Denotes the cube rotation.
|
|
|
|
|
/// </summary>
|
|
|
|
|
float angle = 0.0f;
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region --- Constructors ---
|
|
|
|
|
|
|
|
|
|
public T03_RotatingCube()
|
|
|
|
|
{
|
2007-08-04 12:09:58 +00:00
|
|
|
|
CreateWindow(new DisplayMode(800, 600));
|
|
|
|
|
|
2007-07-23 00:15:18 +00:00
|
|
|
|
Context.MakeCurrent();
|
|
|
|
|
|
|
|
|
|
GL.ClearColor(0.1f, 0.1f, 0.5f, 0.0f);
|
|
|
|
|
GL.Enable(Enums.EnableCap.DEPTH_TEST);
|
|
|
|
|
|
|
|
|
|
this.OnResize(new ResizeEventArgs(this.Width, this.Height));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Resize event handler
|
|
|
|
|
|
|
|
|
|
/// <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>
|
|
|
|
|
protected override void OnResize(OpenTK.Platform.ResizeEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnResize(e);
|
|
|
|
|
|
|
|
|
|
GL.Viewport(0, 0, e.Width, e.Height);
|
|
|
|
|
|
|
|
|
|
double ratio = e.Width / (double)e.Height;
|
|
|
|
|
|
|
|
|
|
GL.MatrixMode(Enums.MatrixMode.PROJECTION);
|
|
|
|
|
GL.LoadIdentity();
|
|
|
|
|
Glu.Perspective(45.0, ratio, 1.0, 64.0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-08-04 12:09:58 +00:00
|
|
|
|
#region OnUpdateFrame function
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
|
|
|
|
/// <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>
|
2007-08-04 12:09:58 +00:00
|
|
|
|
public override void OnUpdateFrame()
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
2007-08-04 12:09:58 +00:00
|
|
|
|
if (Keyboard[0][OpenTK.Input.Keys.Escape])
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
2007-08-04 12:09:58 +00:00
|
|
|
|
this.Exit();
|
2007-07-23 00:15:18 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-04 12:09:58 +00:00
|
|
|
|
if ((Keyboard[0][OpenTK.Input.Keys.LeftAlt] || Keyboard[0][OpenTK.Input.Keys.RightAlt]) &&
|
|
|
|
|
Keyboard[0][OpenTK.Input.Keys.Enter])
|
2007-07-31 21:50:29 +00:00
|
|
|
|
{
|
|
|
|
|
Fullscreen = true;
|
|
|
|
|
}
|
|
|
|
|
|
2007-07-23 00:15:18 +00:00
|
|
|
|
GL.MatrixMode(Enums.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.Rotatef(angle, 0.0f, 1.0f, 0.0f);
|
|
|
|
|
angle += 0.5f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-08-04 12:09:58 +00:00
|
|
|
|
#region OnRenderFrame function
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Place your rendering code here.
|
|
|
|
|
/// </summary>
|
2007-08-04 12:09:58 +00:00
|
|
|
|
public override void OnRenderFrame()
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
|
|
|
|
GL.Clear(Enums.ClearBufferMask.COLOR_BUFFER_BIT | Enums.ClearBufferMask.DEPTH_BUFFER_BIT);
|
|
|
|
|
|
|
|
|
|
DrawCube();
|
|
|
|
|
|
|
|
|
|
Context.SwapBuffers();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-08-03 00:14:31 +00:00
|
|
|
|
#region private void DrawCube()
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-08-03 00:14:31 +00:00
|
|
|
|
private void DrawCube()
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
2007-08-03 00:14:31 +00:00
|
|
|
|
GL.Begin(GL.Enums.BeginMode.QUADS);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-08-03 00:14:31 +00:00
|
|
|
|
GL.Color3(1.0f, 0.0f, 0.0f);
|
2007-08-01 21:14:39 +00:00
|
|
|
|
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);
|
|
|
|
|
|
2007-08-03 00:14:31 +00:00
|
|
|
|
GL.Color3(1.0f, 1.0f, 0.0f);
|
2007-08-01 21:14:39 +00:00
|
|
|
|
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);
|
|
|
|
|
|
2007-08-03 00:14:31 +00:00
|
|
|
|
GL.Color3(1.0f, 0.0f, 1.0f);
|
2007-08-01 21:14:39 +00:00
|
|
|
|
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);
|
|
|
|
|
|
2007-08-03 00:14:31 +00:00
|
|
|
|
GL.Color3(0.0f, 1.0f, 0.0f);
|
2007-08-01 21:14:39 +00:00
|
|
|
|
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);
|
|
|
|
|
|
2007-08-03 00:14:31 +00:00
|
|
|
|
GL.Color3(0.0f, 0.0f, 1.0f);
|
2007-08-01 21:14:39 +00:00
|
|
|
|
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);
|
|
|
|
|
|
2007-08-03 00:14:31 +00:00
|
|
|
|
GL.Color3(0.0f, 1.0f, 1.0f);
|
2007-08-01 21:14:39 +00:00
|
|
|
|
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);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
|
|
|
|
GL.End();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-08-03 00:14:31 +00:00
|
|
|
|
#region public void Launch()
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Launches this example.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Provides a simple way for the example launcher to launch the examples.
|
|
|
|
|
/// </remarks>
|
2007-08-03 00:14:31 +00:00
|
|
|
|
public void Launch()
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
2007-08-04 12:09:58 +00:00
|
|
|
|
Run();
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|