From 37f94717de3d55f4ec15b70ce6829c8037f81da4 Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Tue, 6 Nov 2007 21:01:35 +0000 Subject: [PATCH] Tried to implement some things, but it's broken. --- Source/Examples/Tutorial/T08_VBO.cs | 112 ++++++++++++++++++---------- 1 file changed, 73 insertions(+), 39 deletions(-) diff --git a/Source/Examples/Tutorial/T08_VBO.cs b/Source/Examples/Tutorial/T08_VBO.cs index 5a0a9ab2..b3ce5531 100644 --- a/Source/Examples/Tutorial/T08_VBO.cs +++ b/Source/Examples/Tutorial/T08_VBO.cs @@ -15,6 +15,8 @@ using OpenTK.OpenGL; using OpenTK.Platform; using System.Threading; using OpenTK.OpenGL.Enums; +using System.Runtime.InteropServices; +using OpenTK.Math; #endregion @@ -24,12 +26,18 @@ namespace Examples.Tutorial { #region --- Private Fields --- - Shapes.Shape shape = new Examples.Shapes.Cube(); - //new Examples.Shapes.Plane(16, 16, 2.0f, 2.0f); - - int handle_vertex_buffer, ibo, nbo; // vertex, index and normal buffer objects. + Shapes.Shape cube = new Examples.Shapes.Cube(); + Shapes.Shape plane = new Examples.Shapes.Plane(16, 16, 2.0f, 2.0f); + + struct Vbo + { + public int VboID, EboID, NumElements; + } + Vbo[] vbo = new Vbo[2]; float angle; + public static readonly int order = 8; + #endregion #region --- Constructor --- @@ -44,44 +52,23 @@ namespace Examples.Tutorial { base.OnLoad(e); - if (!GL.SupportsExtension("VERSION_1_4")) + if (!GL.SupportsExtension("VERSION_1_5")) { - System.Windows.Forms.MessageBox.Show("You need at least OpenGL 1.4 to run this example. Aborting.", "VBOs not supported", + System.Windows.Forms.MessageBox.Show("You need at least OpenGL 1.5 to run this example. Aborting.", "VBOs not supported", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation); this.Exit(); } GL.ClearColor(0.1f, 0.1f, 0.5f, 0.0f); GL.Enable(EnableCap.DepthTest); - GL.EnableClientState(EnableCap.VertexArray); - //GL.EnableClientState(GL.Enums.EnableCap.INDEX_ARRAY); // Create the Vertex Buffer Object: // 1) Generate the buffer handles. // 2) Bind the Vertex Buffer and upload your vertex data. Check that the data was uploaded correctly. // 3) Bind the Index Buffer and upload your index data. Check that the data was uploaded correctly. - int size; // To check whether the buffers were uploaded correctly. - - // Generate handles. - GL.GenBuffers(1, out handle_vertex_buffer); - //GL.GenBuffers(1, out ibo); - - // Upload the vertex data. - GL.BindBuffer(Version15.ArrayBuffer, handle_vertex_buffer); - GL.BufferData(Version15.ArrayBuffer, (IntPtr)shape.Vertices.Length, shape.Vertices, Version15.StaticDraw); - GL.VertexPointer(3, VertexPointerType.Float, 0, IntPtr.Zero); - GL.GetBufferParameter(Version15.ArrayBuffer, Version15.BufferSize, out size); - if (shape.Vertices.Length != size) - throw new ApplicationException("Vertex array not uploaded correctly"); - /* - // Upload the index data. - GL.BindBuffer(GL.Enums.VERSION_1_5.ELEMENT_ARRAY_BUFFER, ibo); - GL.BufferData(GL.Enums.VERSION_1_5.ELEMENT_ARRAY_BUFFER, (IntPtr)shape.Indices.Length, shape.Indices, GL.Enums.VERSION_1_5.STATIC_DRAW); - GL.GetBufferParameter(GL.Enums.VERSION_1_5.ELEMENT_ARRAY_BUFFER, GL.Enums.VERSION_1_5.BUFFER_SIZE, out size); - if (shape.Indices.Length != size) - throw new ApplicationException("Index array not uploaded correctly"); - */ + vbo[0] = Load(cube.Vertices, cube.Indices); + vbo[1] = Load(cube.Vertices, cube.Indices); } #endregion @@ -128,17 +115,14 @@ namespace Examples.Tutorial 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 - ); + Glu.LookAt(0.0, 5.0, 5.0, + 0.0, 0.0, 0.0, + 0.0, 1.0, 0.0); - GL.Color3(1.0f, 1.0f, 1.0f); - GL.DrawElements(BeginMode.Triangles, shape.Indices.Length, - All.UnsignedInt, shape.Indices); + GL.Color4(System.Drawing.Color.Black); + Draw(vbo[0]); - Context.SwapBuffers(); + SwapBuffers(); } #endregion @@ -158,6 +142,56 @@ namespace Examples.Tutorial #endregion - public static readonly int order = 8; + Vbo Load(Vector3[] vertices, int[] indices) + { + Vbo handle = new Vbo(); + int size; + + GL.GenBuffers(1, out handle.VboID); + GL.BindBuffer(Version15.ArrayBuffer, handle.VboID); + GL.BufferData(Version15.ArrayBuffer, (IntPtr)(vertices.Length * Vector3.SizeInBytes), vertices, + Version15.StaticDraw); + GL.GetBufferParameter(Version15.ArrayBuffer, Version15.BufferSize, out size); + if (vertices.Length * Vector3.SizeInBytes != size) + throw new ApplicationException("Vertex array not uploaded correctly"); + //GL.BindBuffer(Version15.ArrayBuffer, 0); + + GL.GenBuffers(1, out handle.EboID); + GL.BindBuffer(Version15.ElementArrayBuffer, handle.EboID); + GL.BufferData(Version15.ElementArrayBuffer, (IntPtr)(indices.Length * sizeof(int)), indices, + Version15.StaticDraw); + GL.GetBufferParameter(Version15.ElementArrayBuffer, Version15.BufferSize, out size); + if (indices.Length * sizeof(int) != size) + throw new ApplicationException("Element array not uploaded correctly"); + //GL.BindBuffer(Version15.ElementArrayBuffer, 0); + + handle.NumElements = indices.Length; + return handle; + } + + void Draw(Vbo handle) + { + //GL.PushClientAttrib(ClientAttribMask.ClientVertexArrayBit); + + //GL.EnableClientState(EnableCap.TextureCoordArray); + GL.EnableClientState(EnableCap.VertexArray); + + GL.BindBuffer(Version15.StaticDraw, handle.VboID); + GL.BindBuffer(Version15.ElementArrayBuffer, handle.EboID); + + //GL.TexCoordPointer(2, TexCoordPointerType.Float, vector2_size, (IntPtr)vector2_size); + GL.VertexPointer(3, VertexPointerType.Float, Vector3.SizeInBytes, IntPtr.Zero); + + GL.DrawElements(BeginMode.Triangles, handle.NumElements, All.UnsignedInt, IntPtr.Zero); + //GL.DrawArrays(BeginMode.LineLoop, 0, vbo.element_count); + + GL.BindBuffer(Version15.ArrayBuffer, 0); + GL.BindBuffer(Version15.ElementArrayBuffer, 0); + + GL.DisableClientState(EnableCap.VertexArray); + //GL.DisableClientState(EnableCap.TextureCoordArray); + + //GL.PopClientAttrib(); + } } }