#region --- License --- /* 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 { /// /// Demonstrates immediate mode rendering. /// [Example("Immediate mode", ExampleCategory.OpenGL, "1.1", Documentation="ImmediateMode")] public class T03_Immediate_Mode_Cube : GameWindow { #region --- Fields --- float rotation_speed = 3.0f; float angle; #endregion #region --- Constructor --- public T03_Immediate_Mode_Cube() : base(800, 600, new GraphicsMode(16, 16)) { } #endregion #region OnLoad public override void OnLoad(EventArgs e) { base.OnLoad(e); GL.ClearColor(Color.MidnightBlue); GL.Enable(EnableCap.DepthTest); } #endregion #region OnResize /// /// Called when the user resizes the window. /// /// Contains the new width/height of the window. /// /// You want the OpenGL viewport to match the window. This is the place to do it! /// protected override void OnResize(EventArgs e) { GL.Viewport(0, 0, Width, Height); double aspect_ratio = Width / (double)Height; GL.MatrixMode(MatrixMode.Projection); if (Keyboard[OpenTK.Input.Key.Space]) { OpenTK.Matrix4 perspective = OpenTK.Matrix4.Perspective(45, (float)aspect_ratio, 1, 64); GL.LoadMatrix(ref perspective); } else { GL.LoadIdentity(); Glu.Perspective(45, (float)aspect_ratio, 1, 64); } } #endregion #region OnUpdateFrame /// /// Prepares the next frame for rendering. /// /// /// Place your control logic here. This is the place to respond to user input, /// update object positions etc. /// protected override void OnUpdateFrame(FrameEventArgs e) { if (Keyboard[OpenTK.Input.Key.Escape]) { this.Exit(); return; } } #endregion #region OnRenderFrame /// /// Place your rendering code here. /// protected override void OnRenderFrame(FrameEventArgs e) { GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.MatrixMode(MatrixMode.Modelview); GL.LoadIdentity(); Glu.LookAt(0.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); angle += rotation_speed * (float)e.Time; GL.Rotate(angle, 0.0f, 1.0f, 0.0f); DrawCube(); this.SwapBuffers(); } #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() /// /// Entry point of this example. /// [STAThread] public static void Main() { using (T03_Immediate_Mode_Cube example = new T03_Immediate_Mode_Cube()) { Utilities.SetWindowTitle(example); example.Run(30.0, 0.0); } } #endregion } }