VertexBufferObject.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Drawing;
  6. using OpenTK;
  7. using OpenTK.Graphics.OpenGL;
  8. using MathNet.Numerics.LinearAlgebra.Single;
  9. namespace bbiwarg.Graphics
  10. {
  11. class VertexBufferObject : GameWindow
  12. {
  13. int VBOid = new int();
  14. int IBOid = new int();
  15. float[] vertices;
  16. uint[] triangles;
  17. static void Main(string[] args)
  18. {
  19. VertexBufferObject demo = new VertexBufferObject();
  20. demo.initSource();
  21. demo.Run(30);
  22. }
  23. public void initSource()
  24. {
  25. GL.EnableClientState(ArrayCap.VertexArray);
  26. vertices = new float[] {
  27. 1.0f, 0.0f, 0.0f,
  28. 0.0f, 0.0f, -5.0f,
  29. 0.0f, 1.0f, 0.0f,
  30. 0.0f, 1.0f, -5.0f,
  31. 0.0f, 0.0f, 1.0f,
  32. 1.0f, 0.0f, -5.0f};
  33. triangles = new uint[] { 0, 1, 2 };
  34. GL.GenBuffers(1, out VBOid);
  35. GL.GenBuffers(1, out IBOid);
  36. GL.BindBuffer(BufferTarget.ArrayBuffer, VBOid);
  37. GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * sizeof(float)), vertices, BufferUsageHint.StreamDraw);
  38. GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
  39. GL.BindBuffer(BufferTarget.ElementArrayBuffer, IBOid);
  40. GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(triangles.Length * sizeof(int)), triangles, BufferUsageHint.StaticDraw);
  41. GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
  42. //GL.DeleteBuffers(1, ref VBOid); GL.DeleteBuffers(1, ref IBOid);
  43. }
  44. protected override void OnLoad(EventArgs e)
  45. {
  46. base.OnLoad(e);
  47. Title = "vbo test";
  48. GL.ClearColor(Color.Black);
  49. }
  50. protected override void OnRenderFrame(FrameEventArgs e)
  51. {
  52. base.OnRenderFrame(e);
  53. GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  54. Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, -Vector3.UnitZ, Vector3.UnitY);
  55. GL.MatrixMode(MatrixMode.Modelview);
  56. GL.LoadMatrix(ref modelview);
  57. GL.Color3(Color.Red);
  58. GL.BindBuffer(BufferTarget.ArrayBuffer, VBOid);
  59. GL.BindBuffer(BufferTarget.ElementArrayBuffer, IBOid);
  60. GL.InterleavedArrays(InterleavedArrayFormat.C3fV3f, 0, IntPtr.Zero);
  61. GL.DrawElements(BeginMode.Triangles, triangles.Length, DrawElementsType.UnsignedInt, 0);
  62. GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
  63. GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
  64. SwapBuffers();
  65. }
  66. protected override void OnResize(EventArgs e)
  67. {
  68. base.OnResize(e);
  69. GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
  70. Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, Width / (float)Height, 1.0f, 3000.0f);
  71. GL.MatrixMode(MatrixMode.Projection);
  72. GL.LoadMatrix(ref projection);
  73. }
  74. }
  75. }