#region --- License --- /* This source file is released under the MIT license. See License.txt for more information. * Coded by Erik Ylvisaker and Stephen Apostolopoulos. */ #endregion #region --- Using Directives --- using System; using System.Collections.Generic; using System.Windows.Forms; using OpenTK.OpenGL; using Enums = OpenTK.OpenGL.Enums; #endregion namespace Lesson01 { public class Cube : OpenTK.Framework { static float angle; #region Entry point /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); new Cube().Run(); } #endregion #region Load event handler protected override void OnLoad(object sender, EventArgs e) { base.OnLoad(sender, e); Text = GL.GetString(Enums.StringName.VENDOR) + " " + GL.GetString(Enums.StringName.RENDERER) + " " + GL.GetString(Enums.StringName.VERSION); GL.ClearColor(0.1f, 0.1f, 0.5f, 0.0f); GL.Enable(Enums.EnableCap.DEPTH_TEST); OnResize(this, e); } #endregion #region Resize event handler protected override void OnResize(object sender, EventArgs e) { base.OnResize(sender, e); // if (this.Context == null) // return; 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; GL.MatrixMode(Enums.MatrixMode.PROJECTION); GL.LoadIdentity(); Glu.Perspective(45.0, ratio, 1.0, 64.0); } #endregion #region Paint event handler protected override void OnPaint() { 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; GL.Clear(Enums.ClearBufferMask.COLOR_BUFFER_BIT | Enums.ClearBufferMask.DEPTH_BUFFER_BIT); DrawCube(); ActiveContext.SwapBuffers(); } #endregion #region KeyDown event handler protected override void OnKeyDown(object sender, KeyEventArgs e) { base.OnKeyDown(sender, e); switch (e.KeyData) { case Keys.Escape: Application.Exit(); break; case Keys.F1: this.SetResolution(this.Width, this.Height, this.ColorDepth, !this.IsFullscreen); break; } } #endregion #region DrawCube public void DrawCube() { GL.Begin(Enums.BeginMode.QUADS); GL.Color3f(1, 0, 0); GL.Vertex3f(-1.0f, -1.0f, -1.0f); GL.Vertex3f(-1.0f, 1.0f, -1.0f); GL.Vertex3f(1.0f, 1.0f, -1.0f); GL.Vertex3f(1.0f, -1.0f, -1.0f); GL.Color3f(1, 1, 0); GL.Vertex3f(-1.0f, -1.0f, -1.0f); GL.Vertex3f(1.0f, -1.0f, -1.0f); GL.Vertex3f(1.0f, -1.0f, 1.0f); GL.Vertex3f(-1.0f, -1.0f, 1.0f); GL.Color3f(1, 0, 1); GL.Vertex3f(-1.0f, -1.0f, -1.0f); GL.Vertex3f(-1.0f, -1.0f, 1.0f); GL.Vertex3f(-1.0f, 1.0f, 1.0f); GL.Vertex3f(-1.0f, 1.0f, -1.0f); GL.Color3f(0, 1, 0); GL.Vertex3f(-1.0f, -1.0f, 1.0f); GL.Vertex3f(1.0f, -1.0f, 1.0f); GL.Vertex3f(1.0f, 1.0f, 1.0f); GL.Vertex3f(-1.0f, 1.0f, 1.0f); GL.Color3f(0, 0, 1); GL.Vertex3f(-1.0f, 1.0f, -1.0f); GL.Vertex3f(-1.0f, 1.0f, 1.0f); GL.Vertex3f(1.0f, 1.0f, 1.0f); GL.Vertex3f(1.0f, 1.0f, -1.0f); GL.Color3f(0, 1, 1); GL.Vertex3f(1.0f, -1.0f, -1.0f); GL.Vertex3f(1.0f, 1.0f, -1.0f); GL.Vertex3f(1.0f, 1.0f, 1.0f); GL.Vertex3f(1.0f, -1.0f, 1.0f); GL.End(); } #endregion } }