From a267b4a381681713d3c5c07a0523a0e106349167 Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Tue, 25 Sep 2007 21:11:16 +0000 Subject: [PATCH] Vector and example updates. --- Source/Examples/Shapes/Cube.cs | 10 +++- Source/Examples/Tutorial/T01_Simple_Window.cs | 10 +++- Source/Examples/Tutorial/T04_Lit_Cube.cs | 54 ++++++++++++------- .../Tutorial/T07_Display_Lists_Flower.cs | 2 +- Source/Examples/Tutorial/T10_GLSL_Cube.cs | 22 ++++++-- Source/OpenTK/Math/Vector2.cs | 5 ++ Source/OpenTK/Math/Vector3.cs | 5 ++ Source/OpenTK/Math/Vector4.cs | 5 ++ 8 files changed, 85 insertions(+), 28 deletions(-) diff --git a/Source/Examples/Shapes/Cube.cs b/Source/Examples/Shapes/Cube.cs index 205908b7..cb1701c9 100644 --- a/Source/Examples/Shapes/Cube.cs +++ b/Source/Examples/Shapes/Cube.cs @@ -1,9 +1,15 @@ -using System; +#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 OpenTK.Math; -using System.Drawing; namespace Examples.Shapes { diff --git a/Source/Examples/Tutorial/T01_Simple_Window.cs b/Source/Examples/Tutorial/T01_Simple_Window.cs index 5b2224f9..05d32902 100644 --- a/Source/Examples/Tutorial/T01_Simple_Window.cs +++ b/Source/Examples/Tutorial/T01_Simple_Window.cs @@ -73,11 +73,19 @@ namespace Examples.Tutorial #endregion + public override void OnUpdateFrame(UpdateFrameEventArgs e) + { + base.OnUpdateFrame(e); + + if (Keyboard[0][OpenTK.Input.Key.Escape]) + this.Exit(); + } + #region IExample Members public void Launch() { - this.Run(1.0, 5.0); + this.Run(30.0, 5.0); } #endregion diff --git a/Source/Examples/Tutorial/T04_Lit_Cube.cs b/Source/Examples/Tutorial/T04_Lit_Cube.cs index 22c3db88..16cc82f0 100644 --- a/Source/Examples/Tutorial/T04_Lit_Cube.cs +++ b/Source/Examples/Tutorial/T04_Lit_Cube.cs @@ -7,16 +7,18 @@ using System; using System.Collections.Generic; using System.Text; +using System.Drawing; using OpenTK; using OpenTK.OpenGL; -using System.Drawing; +using Examples.Shapes; namespace Examples.Tutorial { class T04_Lit_Cube : GameWindow, IExample { - float angle; + float x_angle, zoom; + Shape shape = new Plane(16, 16, 4.0f, 4.0f); #region Constructor @@ -35,16 +37,16 @@ namespace Examples.Tutorial GL.ClearColor(Color.MidnightBlue); GL.Enable(GL.Enums.EnableCap.DEPTH_TEST); + GL.Enable(GL.Enums.EnableCap.CULL_FACE); GL.EnableClientState(GL.Enums.EnableCap.VERTEX_ARRAY); GL.EnableClientState(GL.Enums.EnableCap.NORMAL_ARRAY); - GL.VertexPointer(3, GL.Enums.VertexPointerType.FLOAT, 0, Shapes.Cube.Vertices); - GL.NormalPointer(GL.Enums.NormalPointerType.FLOAT, 0, Shapes.Cube.Normals); - //GL.ColorPointer(4, GL.Enums.ColorPointerType.UNSIGNED_BYTE, 0, Shapes.Cube.Colors); + GL.VertexPointer(3, GL.Enums.VertexPointerType.FLOAT, 0, shape.Vertices); + GL.NormalPointer(GL.Enums.NormalPointerType.FLOAT, 0, shape.Normals); // Enable Light 0 and set its parameters. - GL.Lightv(GL.Enums.LightName.LIGHT0, GL.Enums.LightParameter.POSITION, new float[] { 1.0f, 1.0f, 0.0f }); - GL.Lightv(GL.Enums.LightName.LIGHT0, GL.Enums.LightParameter.AMBIENT, new float[] { 0.0f, 0.0f, 0.0f, 1.0f }); + GL.Lightv(GL.Enums.LightName.LIGHT0, GL.Enums.LightParameter.POSITION, new float[] { 1.0f, 1.0f, -0.5f }); + GL.Lightv(GL.Enums.LightName.LIGHT0, GL.Enums.LightParameter.AMBIENT, new float[] { 0.3f, 0.3f, 0.3f, 1.0f }); GL.Lightv(GL.Enums.LightName.LIGHT0, GL.Enums.LightParameter.DIFFUSE, new float[] { 1.0f, 1.0f, 1.0f, 1.0f }); GL.Lightv(GL.Enums.LightName.LIGHT0, GL.Enums.LightParameter.SPECULAR, new float[] { 1.0f, 1.0f, 1.0f, 1.0f }); GL.LightModelv(GL.Enums.LightModelParameter.LIGHT_MODEL_AMBIENT, new float[] { 0.2f, 0.2f, 0.2f, 1.0f }); @@ -52,8 +54,8 @@ namespace Examples.Tutorial GL.Enable(GL.Enums.EnableCap.LIGHT0); // Use GL.Material to set your object's material parameters.. - GL.Materialv(GL.Enums.MaterialFace.FRONT, GL.Enums.MaterialParameter.AMBIENT, new float[] { 0.3f, 0.3f, 0.9f, 1.0f }); - GL.Materialv(GL.Enums.MaterialFace.FRONT, GL.Enums.MaterialParameter.DIFFUSE, new float[] { 0.3f, 0.3f, 0.9f, 1.0f }); + GL.Materialv(GL.Enums.MaterialFace.FRONT, GL.Enums.MaterialParameter.AMBIENT, new float[] { 0.3f, 0.3f, 0.3f, 1.0f }); + GL.Materialv(GL.Enums.MaterialFace.FRONT, GL.Enums.MaterialParameter.DIFFUSE, new float[] { 1.0f, 1.0f, 1.0f, 1.0f }); GL.Materialv(GL.Enums.MaterialFace.FRONT, GL.Enums.MaterialParameter.SPECULAR, new float[] { 1.0f, 1.0f, 1.0f, 1.0f }); GL.Materialv(GL.Enums.MaterialFace.FRONT, GL.Enums.MaterialParameter.EMISSION, new float[] { 0.0f, 0.0f, 0.0f, 1.0f }); } @@ -107,10 +109,18 @@ namespace Examples.Tutorial Fullscreen = !Fullscreen; } - //angle += 180.0f * (float)e.Time; - angle += 3.0f; - if (angle > 720.0f) - angle -= 720.0f; + if (Mouse[0][OpenTK.Input.MouseButton.Left]) + x_angle += Mouse[0].XDelta * 2; + else + x_angle += 0.5f; + + if (Mouse[0][OpenTK.Input.MouseButton.Right]) + zoom += Mouse[0].YDelta * 0.5f; + + if (x_angle > 720.0f) + x_angle -= 720.0f; + else if (x_angle < -720.0f) + x_angle += 720.0f; } #endregion @@ -127,14 +137,20 @@ namespace Examples.Tutorial GL.MatrixMode(GL.Enums.MatrixMode.MODELVIEW); GL.LoadIdentity(); Glu.LookAt( - 0.0, 5.0, 5.0, + 0.0, 0.0, -7.5 + zoom, 0.0, 0.0, 0.0, - 0.0, 1.0, 0.0 - ); - GL.Rotate(angle, 0.0f, 1.0f, 0.0f); + 0.0, 1.0, 0.0); + GL.Rotate(x_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); + unsafe + { + fixed (int* ptr = shape.Indices) + { + GL.DrawElements(GL.Enums.BeginMode.TRIANGLES, shape.Indices.Length, + GL.Enums.All.UNSIGNED_INT, ptr); + } + } + GL.DrawArrays(GL.Enums.BeginMode.POINTS, 0, shape.Vertices.Length); Context.SwapBuffers(); } diff --git a/Source/Examples/Tutorial/T07_Display_Lists_Flower.cs b/Source/Examples/Tutorial/T07_Display_Lists_Flower.cs index ac0702cb..571441e9 100644 --- a/Source/Examples/Tutorial/T07_Display_Lists_Flower.cs +++ b/Source/Examples/Tutorial/T07_Display_Lists_Flower.cs @@ -161,7 +161,7 @@ namespace Examples.Tutorial /// public void Launch() { - this.Run(1.0, 5.0); + this.Run(30.0, 5.0); } #endregion diff --git a/Source/Examples/Tutorial/T10_GLSL_Cube.cs b/Source/Examples/Tutorial/T10_GLSL_Cube.cs index 32980e1b..728aad71 100644 --- a/Source/Examples/Tutorial/T10_GLSL_Cube.cs +++ b/Source/Examples/Tutorial/T10_GLSL_Cube.cs @@ -47,6 +47,7 @@ void main() #endregion static float angle; + int vertex_shader_object, fragment_shader_object, shader_program; #endregion @@ -88,12 +89,10 @@ void main() GL.VertexPointer(3, GL.Enums.VertexPointerType.FLOAT, 0, Shapes.Cube.Vertices); GL.ColorPointer(4, GL.Enums.ColorPointerType.UNSIGNED_BYTE, 0, Shapes.Cube.Colors); - uint vertex_shader_object, fragment_shader_object; int status; - uint shader_program; - vertex_shader_object = (uint)GL.CreateShader(GL.Enums.VERSION_2_0.VERTEX_SHADER); - fragment_shader_object = (uint)GL.CreateShader(GL.Enums.VERSION_2_0.FRAGMENT_SHADER); + vertex_shader_object = GL.CreateShader(GL.Enums.VERSION_2_0.VERTEX_SHADER); + fragment_shader_object = GL.CreateShader(GL.Enums.VERSION_2_0.FRAGMENT_SHADER); unsafe { GL.ShaderSource(vertex_shader_object, vertex_shader_source.Length, vertex_shader_source, (int*)null); } GL.CompileShader(vertex_shader_object); @@ -121,7 +120,7 @@ void main() throw new Exception(info.ToString()); } - shader_program = (uint)GL.CreateProgram(); + shader_program = GL.CreateProgram(); GL.AttachShader(shader_program, fragment_shader_object); GL.AttachShader(shader_program, vertex_shader_object); @@ -229,5 +228,18 @@ void main() } #endregion + + #region public override void Dispose() + + public override void Dispose() + { + GL.DeleteProgram(shader_program); + GL.DeleteShader(fragment_shader_object); + GL.DeleteShader(vertex_shader_object); + + base.Dispose(); + } + + #endregion } } diff --git a/Source/OpenTK/Math/Vector2.cs b/Source/OpenTK/Math/Vector2.cs index 2cbcae3c..d502b4da 100644 --- a/Source/OpenTK/Math/Vector2.cs +++ b/Source/OpenTK/Math/Vector2.cs @@ -280,5 +280,10 @@ namespace OpenTK.Math } #endregion + + public override string ToString() + { + return String.Format("({0}, {1})", X, Y); + } } } diff --git a/Source/OpenTK/Math/Vector3.cs b/Source/OpenTK/Math/Vector3.cs index e2fa598e..c9984047 100644 --- a/Source/OpenTK/Math/Vector3.cs +++ b/Source/OpenTK/Math/Vector3.cs @@ -308,5 +308,10 @@ namespace OpenTK.Math } #endregion + + public override string ToString() + { + return String.Format("({0}, {1}, {2})", X, Y, Z); + } } } diff --git a/Source/OpenTK/Math/Vector4.cs b/Source/OpenTK/Math/Vector4.cs index bdabc378..0ac88408 100644 --- a/Source/OpenTK/Math/Vector4.cs +++ b/Source/OpenTK/Math/Vector4.cs @@ -319,5 +319,10 @@ namespace OpenTK.Math } #endregion + + public override string ToString() + { + return String.Format("({0}, {1}, {2}, {3})", X, Y, Z, W); + } } }