#region --- License --- /* Copyright (c) 2006, 2007 Stefanos Apostolopoulos * See license.txt for license info */ #endregion using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.Threading; using OpenTK; using OpenTK.OpenGL; using System.Diagnostics; using OpenTK.Input; namespace Examples.Tutorial { /// /// Demonstrates Vertex Arrays (in system memory). Example is incomplete (documentation). /// class T02_Vertex_Arrays : GameWindow, IExample { float rotation_speed = 3.0f; float angle = 0.0f; Shapes.Shape shape = new Examples.Shapes.Plane(16, 16, 2.0f, 2.0f); #region Constructor public T02_Vertex_Arrays() : base(new DisplayMode(800, 600), "OpenTK Tutorial 2: Vertex Arrays") { this.VSync = VSyncMode.On; this.Keyboard.KeyUp += new KeyUpEvent(Keyboard_KeyUp); } void Keyboard_KeyUp(KeyboardDevice sender, Key key) { // F4 cycles between available VSync modes. if (key == Key.F4) { if (this.VSync == VSyncMode.Off) this.VSync = VSyncMode.On; else if (this.VSync == VSyncMode.On) this.VSync = VSyncMode.Adaptive; else this.VSync = VSyncMode.Off; } } #endregion #region OnLoad public override void OnLoad(EventArgs e) { base.OnLoad(e); GL.ClearColor(Color.CadetBlue); GL.Enable(GL.Enums.EnableCap.DEPTH_TEST); GL.EnableClientState(GL.Enums.EnableCap.VERTEX_ARRAY); //GL.EnableClientState(GL.Enums.EnableCap.COLOR_ARRAY); GL.VertexPointer(3, GL.Enums.VertexPointerType.FLOAT, 0, shape.Vertices); //GL.ColorPointer(4, GL.Enums.ColorPointerType.UNSIGNED_BYTE, 0, shape.Colors); } #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(OpenTK.Platform.ResizeEventArgs e) { base.OnResize(e); GL.Viewport(0, 0, Width, Height); double ratio = e.Width / (double)e.Height; GL.MatrixMode(GL.Enums.MatrixMode.PROJECTION); GL.LoadIdentity(); Glu.Perspective(45.0, ratio, 1.0, 64.0); } #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. /// public override void OnUpdateFrame(UpdateFrameEventArgs e) { // Escape quits. if (Keyboard[Key.Escape]) { this.Exit(); return; } // Alt+Enter toggles fullscreen mode. if ((Keyboard[Key.AltLeft] || Keyboard[Key.AltRight]) && Keyboard[Key.Enter]) { Fullscreen = !Fullscreen; } // Plus/Minus change the target render frequency. // PageUp/PageDown change the target update frequency. if (Keyboard[Key.Plus] || Keyboard[Key.KeypadAdd]) TargetRenderFrequency++; if (Keyboard[Key.Minus] || Keyboard[Key.KeypadSubtract]) TargetRenderFrequency--; if (Keyboard[Key.PageUp]) TargetUpdateFrequency++; if (Keyboard[Key.PageDown]) TargetUpdateFrequency--; // Right/Left control the rotation speed and direction. if (Keyboard[Key.Right]) rotation_speed += 0.5f; if (Keyboard[Key.Left]) rotation_speed -= 0.5f; } #endregion #region OnRenderFrame /// /// Place your rendering code here. /// public override void OnRenderFrame(RenderFrameEventArgs e) { GL.Clear(GL.Enums.ClearBufferMask.COLOR_BUFFER_BIT | GL.Enums.ClearBufferMask.DEPTH_BUFFER_BIT); GL.MatrixMode(GL.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 ); angle += rotation_speed * (float)e.ScaleFactor; if (angle >= 360.0f) angle -= 360.0f; GL.Rotate(angle, 0.0f, 1.0f, 0.0f); GL.DrawElements(GL.Enums.BeginMode.TRIANGLES, shape.Indices.Length, GL.Enums.All.UNSIGNED_INT, shape.Indices); // GL.DrawArrays(GL.Enums.BeginMode.LINES, 0, shape.Vertices.Length); GL.Begin(GL.Enums.BeginMode.TRIANGLES); GL.Vertex3(-1.0, -1.0, 5.0); GL.Vertex3(1.0, -1.0, 5.0); GL.Vertex3(1.0, 1.0, 5.0); GL.End(); SwapBuffers(); } #endregion #region public void Launch() /// /// Launches this example. /// /// /// Provides a simple way for the example launcher to launch the examples. /// public void Launch() { // Lock UpdateFrame rate at 30Hz and RenderFrame rate 85Hz. //Run(60.0, 85.0); Run(30.0); } #endregion public static readonly int order = 2; } }