diff --git a/Source/Examples/Tutorial/T02_Vertex_Arrays.cs b/Source/Examples/Tutorial/T02_Vertex_Arrays.cs index bc3d1f49..73c6a062 100644 --- a/Source/Examples/Tutorial/T02_Vertex_Arrays.cs +++ b/Source/Examples/Tutorial/T02_Vertex_Arrays.cs @@ -12,6 +12,7 @@ using System.Threading; using OpenTK; using OpenTK.OpenGL; +using System.Diagnostics; namespace Examples.Tutorial { @@ -20,13 +21,20 @@ namespace Examples.Tutorial /// class T02_Vertex_Arrays : GameWindow, IExample { - float angle; + float angle_speed = 3.0f; + float angle = 0.0f; + + Shapes.Shape shape = new Examples.Shapes.Plane(16, 16, 2.0f, 2.0f); + + System.Threading.Timer info_timer; #region Constructor public T02_Vertex_Arrays() { this.CreateWindow(new DisplayMode(800, 600)); + //Thread.CurrentThread.Priority = ThreadPriority.AboveNormal; + //this.VSync = VSyncMode.Off; } #endregion @@ -41,13 +49,30 @@ namespace Examples.Tutorial 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, Shapes.Cube.Vertices); - GL.ColorPointer(4, GL.Enums.ColorPointerType.UNSIGNED_BYTE, 0, Shapes.Cube.Colors); + //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); + + factor_target = TargetRenderPeriod / TargetUpdatePeriod; + info_timer = new Timer(new TimerCallback(PrintDebugInfo), null, 1000, 1000); + } + + double factor_target; + void PrintDebugInfo(object o) + { + double factor = RenderPeriod / UpdatePeriod; + Debug.Print("NORMAL: Frame: {0} ({1}), Update: {2} ({3}), Factor: {4}, Error: {5}", + RenderPeriod.ToString("g8"), RenderFrequency.ToString("g3"), UpdatePeriod.ToString("g8"), UpdateFrequency.ToString("g3"), + factor.ToString("g8"), Math.Abs(factor_target - factor).ToString("g8")); } #endregion + public override void OnUnload(EventArgs e) + { + info_timer.Dispose(); + } + #region OnResize /// @@ -95,10 +120,21 @@ namespace Examples.Tutorial Fullscreen = !Fullscreen; } - //angle += 180.0f * (float)e.Time; - angle += 3.0f; - if (angle > 720.0f) - angle -= 720.0f; + if (Keyboard[OpenTK.Input.Key.Plus]) + TargetRenderFrequency++; + else if (Keyboard[OpenTK.Input.Key.Minus]) + TargetRenderFrequency--; + else if (Keyboard[OpenTK.Input.Key.PageUp]) + TargetUpdateFrequency++; + else if (Keyboard[OpenTK.Input.Key.PageDown]) + TargetUpdateFrequency--; + + if (Keyboard[OpenTK.Input.Key.Space]) + angle_speed = 12.0f; + else + angle_speed = 3.0f; + + //angle += angle_speed; } #endregion @@ -119,12 +155,40 @@ namespace Examples.Tutorial 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 ); + + //angle += angle_speed * (float)(UpdateFrequency / RenderFrequency); + //if (e.Time != 0.0) + if (Math.Abs(e.ScaleFactor - factor_target) > 0.5) + { + /*Debug.Print("JITTER: Frame: {0} ({1}), Update: {2} ({3}), Factor: {4}, Error: {5}", + RenderPeriod.ToString("g8"), RenderFrequency.ToString("g3"), UpdatePeriod.ToString("g8"), UpdateFrequency.ToString("g3"), + e.ScaleFactor.ToString("g8"), Math.Abs(factor_target - e.ScaleFactor).ToString("g8"));*/ + } + angle += angle_speed * (float)e.ScaleFactor; + //angle += angle_speed * (float)e.Time; + if (angle >= 360.0f) + angle -= 360.0f; GL.Rotate(angle, 0.0f, 1.0f, 0.0f); - GL.DrawElements(GL.Enums.BeginMode.TRIANGLES, Shapes.Cube.Indices.Length, - GL.Enums.All.UNSIGNED_SHORT, Shapes.Cube.Indices); + GL.DrawElements(GL.Enums.BeginMode.TRIANGLES, shape.Indices.Length, + GL.Enums.All.UNSIGNED_INT, shape.Indices); - Context.SwapBuffers(); +// 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(); + + int zero = GC.CollectionCount(0); + int one = GC.CollectionCount(1); + int two = GC.CollectionCount(2); + + SwapBuffers(); + //Thread.Sleep(25); } #endregion @@ -139,8 +203,8 @@ namespace Examples.Tutorial /// public void Launch() { - // Lock UpdateFrame and RenderFrame at 60Hz. - Run(60.0, 60.0); + // Lock UpdateFrame rate at 30Hz and RenderFrame rate 85Hz. + Run(30.0, 20.0); } #endregion