Advertisement
Guest User

Untitled

a guest
May 5th, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using OpenTK;
  7. using OpenTK.Graphics;
  8. using OpenTK.Graphics.OpenGL;
  9. using OpenTK.Input;
  10. using System.Drawing;
  11. using System.Runtime.InteropServices;
  12.  
  13. namespace VBOTest2
  14. {
  15. [StructLayout(LayoutKind.Sequential)]
  16. public struct Vertex
  17. {
  18. public Vector3 Position;
  19. public byte[] Colour;
  20.  
  21. public Vertex(byte[] colour, Vector3 position)
  22. {
  23. Colour = colour;
  24. Position = position;
  25. }
  26.  
  27. public static readonly int Stride = Marshal.SizeOf(default(Vertex));
  28. }
  29.  
  30. public class VBOTest2 : GameWindow
  31. {
  32. uint vbo;
  33.  
  34. public VBOTest2() :
  35. base(1, 1, new GraphicsMode(32, 24, 8, 0), "Test")
  36. {
  37. Width = 1500;
  38. Height = 800;
  39. VSync = VSyncMode.On;
  40. ClientSize = new Size(1500, 800);
  41. this.Location = new System.Drawing.Point(100, 300);
  42. GL.Viewport(0, 0, Width, Height);
  43. }
  44.  
  45. void CreateVertexBuffer()
  46. {
  47. Vertex[] vertices = new Vertex[3];
  48. vertices[0] = new Vertex(new byte[]{255,255,255,255}, new Vector3(-1f, -1f, 0f));
  49. vertices[1] = new Vertex(new byte[] { 255, 255, 255, 255 }, new Vector3(1f, -1f, 0f));
  50. vertices[2] = new Vertex(new byte[] { 255, 255, 255, 255 }, new Vector3(0f, 1f, 0f));
  51.  
  52. GL.GenBuffers(1, out vbo);
  53. GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
  54. GL.BufferData<Vertex>(BufferTarget.ArrayBuffer, (IntPtr)Vertex.Stride, vertices, BufferUsageHint.StaticDraw);
  55. }
  56.  
  57. protected override void OnLoad(EventArgs e)
  58. {
  59. GL.ClearColor(Color.Brown);
  60. CreateVertexBuffer();
  61. }
  62.  
  63.  
  64. protected override void OnRenderFrame(FrameEventArgs e)
  65. {
  66. base.OnRenderFrame(e);
  67. GL.Clear(ClearBufferMask.ColorBufferBit);
  68.  
  69. GL.EnableClientState(ArrayCap.VertexArray);
  70. GL.EnableClientState(ArrayCap.ColorArray);
  71.  
  72. GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
  73.  
  74. GL.VertexPointer(3, VertexPointerType.Float, Vertex.Stride, (IntPtr)(0));
  75. GL.ColorPointer(4, ColorPointerType.UnsignedByte, Vertex.Stride, (IntPtr)(8 * sizeof(float)));
  76.  
  77. GL.DrawArrays(PrimitiveType.Triangles, 0, 3);
  78.  
  79. //release buffer
  80. GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
  81.  
  82. GL.DisableClientState(ArrayCap.VertexArray);
  83. GL.DisableClientState(ArrayCap.ColorArray);
  84.  
  85. SwapBuffers();
  86. }
  87. }
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement