using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using OpenTK; using OpenTK.Graphics.OpenGL; using MathNet.Numerics.LinearAlgebra.Single; namespace bbiwarg.Graphics { class VertexBufferObject : GameWindow { int VBOid = new int(); int IBOid = new int(); float[] vertices; uint[] triangles; static void Main(string[] args) { VertexBufferObject demo = new VertexBufferObject(); demo.initSource(); demo.Run(30); } public void initSource() { GL.EnableClientState(ArrayCap.VertexArray); vertices = new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -5.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, -5.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, -5.0f}; triangles = new uint[] { 0, 1, 2 }; GL.GenBuffers(1, out VBOid); GL.GenBuffers(1, out IBOid); GL.BindBuffer(BufferTarget.ArrayBuffer, VBOid); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * sizeof(float)), vertices, BufferUsageHint.StreamDraw); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); GL.BindBuffer(BufferTarget.ElementArrayBuffer, IBOid); GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(triangles.Length * sizeof(int)), triangles, BufferUsageHint.StaticDraw); GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0); //GL.DeleteBuffers(1, ref VBOid); GL.DeleteBuffers(1, ref IBOid); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); Title = "vbo test"; GL.ClearColor(Color.Black); } protected override void OnRenderFrame(FrameEventArgs e) { base.OnRenderFrame(e); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, -Vector3.UnitZ, Vector3.UnitY); GL.MatrixMode(MatrixMode.Modelview); GL.LoadMatrix(ref modelview); GL.Color3(Color.Red); GL.BindBuffer(BufferTarget.ArrayBuffer, VBOid); GL.BindBuffer(BufferTarget.ElementArrayBuffer, IBOid); GL.InterleavedArrays(InterleavedArrayFormat.C3fV3f, 0, IntPtr.Zero); GL.DrawElements(BeginMode.Triangles, triangles.Length, DrawElementsType.UnsignedInt, 0); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0); SwapBuffers(); } protected override void OnResize(EventArgs e) { base.OnResize(e); GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height); Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, Width / (float)Height, 1.0f, 3000.0f); GL.MatrixMode(MatrixMode.Projection); GL.LoadMatrix(ref projection); } } }