Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 KB | None | 0 0
  1. //The following code is public domain
  2.  
  3. public class VertexArray : GraphicsResource
  4. {
  5. protected readonly int ID;
  6. DrawMode _drawMode;
  7. VertexLayout _vertexType;
  8.  
  9. protected BufferUsageHint bufferUsage = BufferUsageHint.StaticDraw;
  10. int vertexCount;
  11. int vertexBufferID;
  12.  
  13. public DrawMode DrawMode
  14. {
  15. get { return _drawMode; }
  16. set { _drawMode = value; }
  17. }
  18. public int VertexCount
  19. {
  20. get { return vertexCount; }
  21. }
  22. public BufferUsageHint BufferUsage
  23. {
  24. get { return bufferUsage; }
  25. set { bufferUsage = value; }
  26. }
  27.  
  28. public VertexArray()
  29. : base()
  30. {
  31. ID = createVAO();
  32. disposed = false;
  33. }
  34. public VertexArray(DrawMode drawMode)
  35. : base()
  36. {
  37. _drawMode = drawMode;
  38. ID = createVAO();
  39. disposed = false;
  40. }
  41. int createVAO()
  42. {
  43. int id;
  44. GL.GenBuffers(1, out vertexBufferID);
  45. GL.GenVertexArrays(1, out id);
  46. GL.BindVertexArray(id);
  47. GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBufferID);
  48. VenGFX.currentVAO = this;
  49. return id;
  50. }
  51. public void Bind()
  52. {
  53. if (vertexCount == 0)
  54. throw new Exception("Vertex buffer has no data, and therefore can't be used");
  55. if (disposed)
  56. throw new ObjectDisposedException(ToString());
  57. GL.BindVertexArray(ID);
  58. VenGFX.currentVAO = this;
  59. }
  60. public void SetData<T>(T[] Array) where T : struct, IVertex
  61. {
  62. GL.BindVertexArray(ID);
  63. if (Array.Length > 0 && Array[0].Layout != _vertexType)
  64. setVertexType(Array[0].Layout);
  65. GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBufferID);
  66. GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(Array.Length * _vertexType.SizeInBytes), Array, bufferUsage);
  67. vertexCount = Array.Length;
  68. VenGFX.currentVAO = this;
  69. }
  70. public void SetData<T>(T[] Array, int length) where T : struct, IVertex
  71. {
  72. GL.BindVertexArray(ID);
  73. if (Array != null && Array.Length > 0 && Array[0].Layout != _vertexType)
  74. setVertexType(Array[0].Layout);
  75. GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBufferID);
  76. GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(length * _vertexType.SizeInBytes), Array, bufferUsage);
  77. vertexCount = length;
  78. VenGFX.currentVAO = this;
  79. }
  80. void setVertexType(VertexLayout layout)
  81. {
  82. _vertexType = layout;
  83. layout.Set();
  84. }
  85. protected override void Release()
  86. {
  87. if (VenGFX.currentVAO == this)
  88. {
  89. GL.BindVertexArray(0);
  90. VenGFX.currentVAO = null;
  91. }
  92.  
  93. int id = ID;
  94. GL.DeleteBuffers(1, ref vertexBufferID);
  95. GL.DeleteVertexArrays(1, ref id);
  96. }
  97. }
  98.  
  99. if (mesh.VertexCount == 0)
  100. return;
  101. if (currentVAO != mesh)
  102. mesh.Bind();
  103.  
  104. GL.DrawArrays((BeginMode)(int)mesh.DrawMode, 0, mesh.VertexCount);
  105.  
  106. public VertexArray()
  107. : base()
  108.  
  109. int id = ID;
  110. GL.DeleteBuffers(1, ref vertexBufferID);
  111. GL.DeleteVertexArrays(1, ref id);
  112.  
  113. DrawMode _drawMode;
  114. public DrawMode DrawMode
  115. {
  116. get { return _drawMode; }
  117. set { _drawMode = value; }
  118. }
  119.  
  120. public DrawMode DrawMode { get; set; }
  121. public BufferUsageHint BufferUsag { get; set; }
  122. public int VertexCount { get; private set; }
  123.  
  124. GL.GenVertexArray();
  125.  
  126. internal sealed class VertexArrayObject : IDisposable
  127. {
  128. internal int Handle { get; private set; }
  129.  
  130. private bool isDisposed = false;
  131.  
  132. internal VertexArrayObject(bool bind = false)
  133. {
  134. this.Handle = GL.GenVertexArray();
  135. if (bind)
  136. {
  137. Bind();
  138. }
  139. }
  140.  
  141. internal void Bind()
  142. {
  143. CheckForDisposed();
  144. GL.BindVertexArray(this.Handle);
  145. }
  146.  
  147. internal void UnBind()
  148. {
  149. CheckForDisposed();
  150. GL.BindVertexArray(0);
  151. }
  152.  
  153. private void CheckForDisposed()
  154. {
  155. if (this.isDisposed)
  156. {
  157. throw new ObjectDisposedException(GetType().FullName);
  158. }
  159. }
  160.  
  161. public void Dispose()
  162. {
  163. Dispose(true);
  164. GC.SuppressFinalize(this);
  165. }
  166.  
  167. private void Dispose(bool manual)
  168. {
  169. if (!this.isDisposed)
  170. {
  171. if (manual)
  172. {
  173. if (this.Handle != -1)
  174. {
  175. GL.DeleteVertexArray(this.Handle);
  176. }
  177. }
  178. this.isDisposed = true;
  179. }
  180. }
  181.  
  182. ~VertexArrayObject()
  183. {
  184. Dispose(false);
  185. }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement