vertex-array.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { WebGLContext } from './context';
  7. import { Program } from './program';
  8. import { ElementsBuffer, AttributeBuffers } from './buffer';
  9. export function createVertexArray(ctx: WebGLContext, program: Program, attributeBuffers: AttributeBuffers, elementsBuffer?: ElementsBuffer) {
  10. const { vertexArrayObject } = ctx.extensions
  11. let vertexArray: WebGLVertexArrayObject | null = null
  12. if (vertexArrayObject) {
  13. vertexArray = vertexArrayObject.createVertexArray()
  14. if (vertexArray) {
  15. updateVertexArray(ctx, vertexArray, program, attributeBuffers, elementsBuffer)
  16. ctx.stats.vaoCount += 1
  17. } else {
  18. console.warn('Could not create WebGL vertex array')
  19. }
  20. }
  21. return vertexArray
  22. }
  23. export function updateVertexArray(ctx: WebGLContext, vertexArray: WebGLVertexArrayObject | null, program: Program, attributeBuffers: AttributeBuffers, elementsBuffer?: ElementsBuffer) {
  24. const { vertexArrayObject } = ctx.extensions
  25. if (vertexArrayObject && vertexArray) {
  26. vertexArrayObject.bindVertexArray(vertexArray)
  27. if (elementsBuffer) elementsBuffer.bind()
  28. program.bindAttributes(attributeBuffers)
  29. vertexArrayObject.bindVertexArray(null)
  30. }
  31. }
  32. export function deleteVertexArray(ctx: WebGLContext, vertexArray: WebGLVertexArrayObject | null) {
  33. const { vertexArrayObject } = ctx.extensions
  34. if (vertexArrayObject && vertexArray) {
  35. vertexArrayObject.deleteVertexArray(vertexArray)
  36. ctx.stats.vaoCount -= 1
  37. }
  38. }