Source: dependencies/pixi-gl-core/VertexArrayObject.js

dependencies/pixi-gl-core/VertexArrayObject.js

  1. // state object//
  2. var setVertexAttribArrays = require( './setVertexAttribArrays' );
  3. /**
  4. * Helper class to work with WebGL VertexArrayObjects (vaos)
  5. * Only works if WebGL extensions are enabled (they usually are)
  6. *
  7. * @class
  8. * @memberof PIXI.glCore
  9. * @param gl {WebGLRenderingContext} The current WebGL rendering context
  10. */
  11. function VertexArrayObject(gl, state)
  12. {
  13. this.nativeVaoExtension = null;
  14. if(!VertexArrayObject.FORCE_NATIVE)
  15. {
  16. this.nativeVaoExtension = gl.getExtension('OES_vertex_array_object') ||
  17. gl.getExtension('MOZ_OES_vertex_array_object') ||
  18. gl.getExtension('WEBKIT_OES_vertex_array_object');
  19. }
  20. this.nativeState = state;
  21. if(this.nativeVaoExtension)
  22. {
  23. this.nativeVao = this.nativeVaoExtension.createVertexArrayOES();
  24. var maxAttribs = gl.getParameter(gl.MAX_VERTEX_ATTRIBS);
  25. // VAO - overwrite the state..
  26. this.nativeState = {
  27. tempAttribState: new Array(maxAttribs),
  28. attribState: new Array(maxAttribs)
  29. };
  30. }
  31. /**
  32. * The current WebGL rendering context
  33. *
  34. * @member {WebGLRenderingContext}
  35. */
  36. this.gl = gl;
  37. /**
  38. * An array of attributes
  39. *
  40. * @member {Array}
  41. */
  42. this.attributes = [];
  43. /**
  44. * @member {PIXI.glCore.GLBuffer}
  45. */
  46. this.indexBuffer = null;
  47. /**
  48. * A boolean flag
  49. *
  50. * @member {Boolean}
  51. */
  52. this.dirty = false;
  53. }
  54. VertexArrayObject.prototype.constructor = VertexArrayObject;
  55. module.exports = VertexArrayObject;
  56. /**
  57. * Some devices behave a bit funny when using the newer extensions (im looking at you ipad 2!)
  58. * If you find on older devices that things have gone a bit weird then set this to true.
  59. */
  60. /**
  61. * Lets the VAO know if you should use the WebGL extension or the native methods.
  62. * Some devices behave a bit funny when using the newer extensions (im looking at you ipad 2!)
  63. * If you find on older devices that things have gone a bit weird then set this to true.
  64. * @static
  65. * @property {Boolean} FORCE_NATIVE
  66. */
  67. VertexArrayObject.FORCE_NATIVE = false;
  68. /**
  69. * Binds the buffer
  70. */
  71. VertexArrayObject.prototype.bind = function()
  72. {
  73. if(this.nativeVao)
  74. {
  75. this.nativeVaoExtension.bindVertexArrayOES(this.nativeVao);
  76. if(this.dirty)
  77. {
  78. this.dirty = false;
  79. this.activate();
  80. return this;
  81. }
  82. if (this.indexBuffer)
  83. {
  84. this.indexBuffer.bind();
  85. }
  86. }
  87. else
  88. {
  89. this.activate();
  90. }
  91. return this;
  92. };
  93. /**
  94. * Unbinds the buffer
  95. */
  96. VertexArrayObject.prototype.unbind = function()
  97. {
  98. if(this.nativeVao)
  99. {
  100. this.nativeVaoExtension.bindVertexArrayOES(null);
  101. }
  102. return this;
  103. };
  104. /**
  105. * Uses this vao
  106. */
  107. VertexArrayObject.prototype.activate = function()
  108. {
  109. var gl = this.gl;
  110. var lastBuffer = null;
  111. for (var i = 0; i < this.attributes.length; i++)
  112. {
  113. var attrib = this.attributes[i];
  114. if(lastBuffer !== attrib.buffer)
  115. {
  116. attrib.buffer.bind();
  117. lastBuffer = attrib.buffer;
  118. }
  119. gl.vertexAttribPointer(attrib.attribute.location,
  120. attrib.attribute.size,
  121. attrib.type || gl.FLOAT,
  122. attrib.normalized || false,
  123. attrib.stride || 0,
  124. attrib.start || 0);
  125. }
  126. setVertexAttribArrays(gl, this.attributes, this.nativeState);
  127. if(this.indexBuffer)
  128. {
  129. this.indexBuffer.bind();
  130. }
  131. return this;
  132. };
  133. /**
  134. *
  135. * @param buffer {PIXI.gl.GLBuffer}
  136. * @param attribute {*}
  137. * @param type {String}
  138. * @param normalized {Boolean}
  139. * @param stride {Number}
  140. * @param start {Number}
  141. */
  142. VertexArrayObject.prototype.addAttribute = function(buffer, attribute, type, normalized, stride, start)
  143. {
  144. this.attributes.push({
  145. buffer: buffer,
  146. attribute: attribute,
  147. location: attribute.location,
  148. type: type || this.gl.FLOAT,
  149. normalized: normalized || false,
  150. stride: stride || 0,
  151. start: start || 0
  152. });
  153. this.dirty = true;
  154. return this;
  155. };
  156. /**
  157. *
  158. * @param buffer {PIXI.gl.GLBuffer}
  159. */
  160. VertexArrayObject.prototype.addIndex = function(buffer/*, options*/)
  161. {
  162. this.indexBuffer = buffer;
  163. this.dirty = true;
  164. return this;
  165. };
  166. /**
  167. * Unbinds this vao and disables it
  168. */
  169. VertexArrayObject.prototype.clear = function()
  170. {
  171. // var gl = this.gl;
  172. // TODO - should this function unbind after clear?
  173. // for now, no but lets see what happens in the real world!
  174. if(this.nativeVao)
  175. {
  176. this.nativeVaoExtension.bindVertexArrayOES(this.nativeVao);
  177. }
  178. this.attributes.length = 0;
  179. this.indexBuffer = null;
  180. return this;
  181. };
  182. /**
  183. * @param type {Number}
  184. * @param size {Number}
  185. * @param start {Number}
  186. */
  187. VertexArrayObject.prototype.draw = function(type, size, start)
  188. {
  189. var gl = this.gl;
  190. if(this.indexBuffer)
  191. {
  192. gl.drawElements(type, size || this.indexBuffer.data.length, gl.UNSIGNED_SHORT, (start || 0) * 2 );
  193. }
  194. else
  195. {
  196. // TODO need a better way to calculate size..
  197. gl.drawArrays(type, start, size || this.getSize());
  198. }
  199. return this;
  200. };
  201. /**
  202. * Destroy this vao
  203. */
  204. VertexArrayObject.prototype.destroy = function()
  205. {
  206. // lose references
  207. this.gl = null;
  208. this.indexBuffer = null;
  209. this.attributes = null;
  210. this.nativeState = null;
  211. if(this.nativeVao)
  212. {
  213. this.nativeVaoExtension.deleteVertexArrayOES(this.nativeVao);
  214. }
  215. this.nativeVaoExtension = null;
  216. this.nativeVao = null;
  217. };
  218. VertexArrayObject.prototype.getSize = function()
  219. {
  220. var attrib = this.attributes[0];
  221. return attrib.buffer.data.length / (( attrib.stride/4 ) || attrib.attribute.size);
  222. };