2007-07-23 00:15:18 +00:00
|
|
|
|
#region --- License ---
|
|
|
|
|
/* This source file is released under the MIT license. See License.txt for more information.
|
|
|
|
|
* Coded by Erik Ylvisaker and Stefanos Apostolopoulos.
|
|
|
|
|
*/
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region --- Using directives ---
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Windows.Forms;
|
2007-08-10 16:55:24 +00:00
|
|
|
|
using System.Diagnostics;
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
|
|
|
|
using OpenTK.OpenGL;
|
2007-08-10 16:55:24 +00:00
|
|
|
|
//using Enums = OpenTK.OpenGL.GL.Enums;
|
2007-07-23 00:15:18 +00:00
|
|
|
|
using OpenTK.Platform;
|
2007-08-06 12:13:50 +00:00
|
|
|
|
using System.Threading;
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
namespace Examples.WinForms
|
|
|
|
|
{
|
|
|
|
|
public partial class Cube : Form, IExample
|
|
|
|
|
{
|
|
|
|
|
static float angle;
|
|
|
|
|
|
|
|
|
|
#region --- Constructor ---
|
|
|
|
|
|
|
|
|
|
public Cube()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2007-08-10 16:55:24 +00:00
|
|
|
|
|
2007-07-23 00:15:18 +00:00
|
|
|
|
Application.Idle += Application_Idle;
|
|
|
|
|
|
|
|
|
|
this.ShowDialog();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Closing event
|
|
|
|
|
|
|
|
|
|
protected override void OnClosing(CancelEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnClosing(e);
|
|
|
|
|
|
|
|
|
|
Application.Idle -= Application_Idle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Application_Idle event
|
|
|
|
|
|
|
|
|
|
void Application_Idle(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
while (glControl.IsIdle)
|
|
|
|
|
{
|
|
|
|
|
Render();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region private void Render()
|
|
|
|
|
|
|
|
|
|
private void Render()
|
|
|
|
|
{
|
2007-08-10 16:55:24 +00:00
|
|
|
|
GL.MatrixMode(GL.Enums.MatrixMode.MODELVIEW);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
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;
|
|
|
|
|
|
2007-08-10 16:55:24 +00:00
|
|
|
|
GL.Clear(GL.Enums.ClearBufferMask.COLOR_BUFFER_BIT | GL.Enums.ClearBufferMask.DEPTH_BUFFER_BIT);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
|
|
|
|
DrawCube();
|
|
|
|
|
|
|
|
|
|
glControl.Context.SwapBuffers();
|
2007-08-06 12:13:50 +00:00
|
|
|
|
Thread.Sleep(0);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Load event handler
|
|
|
|
|
|
|
|
|
|
protected override void OnLoad(EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnLoad(e);
|
|
|
|
|
|
|
|
|
|
Text =
|
2007-08-10 16:55:24 +00:00
|
|
|
|
GL.GetString(GL.Enums.StringName.VENDOR) + " " +
|
|
|
|
|
GL.GetString(GL.Enums.StringName.RENDERER) + " " +
|
|
|
|
|
GL.GetString(GL.Enums.StringName.VERSION);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
|
|
|
|
GL.ClearColor(0.1f, 0.1f, 0.5f, 0.0f);
|
2007-08-10 16:55:24 +00:00
|
|
|
|
GL.Enable(GL.Enums.EnableCap.DEPTH_TEST);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
|
|
|
|
glControl.KeyDown += new KeyEventHandler(Cube_KeyDown);
|
|
|
|
|
|
|
|
|
|
OnResize(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Resize event handler
|
|
|
|
|
|
|
|
|
|
protected override void OnResize(EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnResize(e);
|
|
|
|
|
|
|
|
|
|
if (ClientSize.Height == 0)
|
|
|
|
|
ClientSize = new System.Drawing.Size(ClientSize.Width, 1);
|
|
|
|
|
|
|
|
|
|
GL.Viewport(0, 0, ClientSize.Width, ClientSize.Height);
|
|
|
|
|
|
|
|
|
|
double ratio = 0.0;
|
|
|
|
|
ratio = ClientSize.Width / (double)ClientSize.Height;
|
|
|
|
|
//if (ClientSize.Width > ClientSize.Height)
|
|
|
|
|
// ratio = ClientSize.Width / (double)ClientSize.Height;
|
|
|
|
|
//else
|
|
|
|
|
// ratio = ClientSize.Height / (double)ClientSize.Width;
|
|
|
|
|
|
2007-08-10 16:55:24 +00:00
|
|
|
|
GL.MatrixMode(GL.Enums.MatrixMode.PROJECTION);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
GL.LoadIdentity();
|
|
|
|
|
Glu.Perspective(45.0, ratio, 1.0, 64.0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Paint event handler
|
|
|
|
|
|
|
|
|
|
protected override void OnPaint(PaintEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnPaint(e);
|
|
|
|
|
|
|
|
|
|
Render();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region KeyDown event handler
|
|
|
|
|
|
|
|
|
|
void Cube_KeyDown(object sender, KeyEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (e.Alt && e.Shift)
|
|
|
|
|
{
|
|
|
|
|
//this.SetResolution(this.Width, this.Height, this.ColorDepth, !this.IsFullscreen);
|
|
|
|
|
glControl.Fullscreen = !glControl.Fullscreen;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (e.KeyData)
|
|
|
|
|
{
|
|
|
|
|
case Keys.Escape:
|
|
|
|
|
this.Close();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-08-10 20:16:05 +00:00
|
|
|
|
#region private void DrawCube()
|
|
|
|
|
|
|
|
|
|
private void DrawCube()
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
2007-08-10 16:55:24 +00:00
|
|
|
|
GL.Begin(GL.Enums.BeginMode.QUADS);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-08-10 20:16:05 +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-10 20:16:05 +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-10 20:16:05 +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-10 20:16:05 +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-10 20:16:05 +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-10 20:16:05 +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();
|
|
|
|
|
}
|
2007-08-04 12:09:58 +00:00
|
|
|
|
|
2007-08-10 20:16:05 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2007-08-04 12:09:58 +00:00
|
|
|
|
#region IExample Members
|
|
|
|
|
|
|
|
|
|
public void Launch()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
2007-08-10 16:55:24 +00:00
|
|
|
|
}
|