src/Common/Misc/WebGl/index.js
function createGLResources(gl, glConfig) {
  const resources = {
    gl,
    buffers: {},
    textures: {},
    framebuffers: {},
    programs: {},
  };
  const buffers = glConfig.resources.buffers || [];
  const textures = glConfig.resources.textures || [];
  const framebuffers = glConfig.resources.framebuffers || [];

  // Create Buffer
  buffers.forEach((buffer) => {
    const glBuffer = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, glBuffer);
    gl.bufferData(gl.ARRAY_BUFFER, buffer.data, gl.STATIC_DRAW);
    resources.buffers[buffer.id] = glBuffer;
  });

  // Create Texture
  textures.forEach((texture) => {
    const glTexture = gl.createTexture();
    const pixelStore = texture.pixelStore || [];
    const texParameter = texture.texParameter || [];

    gl.bindTexture(gl.TEXTURE_2D, glTexture);

    pixelStore.forEach((option) => {
      gl.pixelStorei(gl[option[0]], option[1]);
    });

    texParameter.forEach((option) => {
      gl.texParameteri(gl.TEXTURE_2D, gl[option[0]], gl[option[1]]);
    });

    resources.textures[texture.id] = glTexture;
  });

  // Create Framebuffer
  framebuffers.forEach((framebuffer) => {
    const glFramebuffer = gl.createFramebuffer();
    glFramebuffer.width = framebuffer.width;
    glFramebuffer.height = framebuffer.height;

    resources.framebuffers[framebuffer.id] = glFramebuffer;
  });

  // Create programs
  Object.keys(glConfig.programs).forEach((programName) => {
    buildShaderProgram(gl, programName, glConfig, resources);
  });

  // Add destroy function
  resources.destroy = () => {
    freeGLResources(resources);
  };

  return resources;
}
function createGLResources(gl, glConfig) {
  const resources = {
    gl,
    buffers: {},
    textures: {},
    framebuffers: {},
    programs: {},
  };
  const buffers = glConfig.resources.buffers || [];
  const textures = glConfig.resources.textures || [];
  const framebuffers = glConfig.resources.framebuffers || [];

  // Create Buffer
  buffers.forEach((buffer) => {
    const glBuffer = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, glBuffer);
    gl.bufferData(gl.ARRAY_BUFFER, buffer.data, gl.STATIC_DRAW);
    resources.buffers[buffer.id] = glBuffer;
  });

  // Create Texture
  textures.forEach((texture) => {
    const glTexture = gl.createTexture();
    const pixelStore = texture.pixelStore || [];
    const texParameter = texture.texParameter || [];

    gl.bindTexture(gl.TEXTURE_2D, glTexture);

    pixelStore.forEach((option) => {
      gl.pixelStorei(gl[option[0]], option[1]);
    });

    texParameter.forEach((option) => {
      gl.texParameteri(gl.TEXTURE_2D, gl[option[0]], gl[option[1]]);
    });

    resources.textures[texture.id] = glTexture;
  });

  // Create Framebuffer
  framebuffers.forEach((framebuffer) => {
    const glFramebuffer = gl.createFramebuffer();
    glFramebuffer.width = framebuffer.width;
    glFramebuffer.height = framebuffer.height;

    resources.framebuffers[framebuffer.id] = glFramebuffer;
  });

  // Create programs
  Object.keys(glConfig.programs).forEach((programName) => {
    buildShaderProgram(gl, programName, glConfig, resources);
  });

  // Add destroy function
  resources.destroy = () => {
    freeGLResources(resources);
  };

  return resources;
}
function createGLResources ( gl , glConfig ) { const resources = { gl , buffers : { } , textures : { } , framebuffers : { } , programs : { } , } ; const buffers = glConfig . resources . buffers || [ ] ; const textures = glConfig . resources . textures || [ ] ; const framebuffers = glConfig . resources . framebuffers || [ ] ; // Create Buffer buffers . forEach ( ( buffer ) => { const glBuffer = gl . createBuffer ( ) ; gl . bindBuffer ( gl . ARRAY_BUFFER , glBuffer ) ; gl . bufferData ( gl . ARRAY_BUFFER , buffer . data , gl . STATIC_DRAW ) ; resources . buffers [ buffer . id ] = glBuffer ; } ) ; // Create Texture textures . forEach ( ( texture ) => { const glTexture = gl . createTexture ( ) ; const pixelStore = texture . pixelStore || [ ] ; const texParameter = texture . texParameter || [ ] ; gl . bindTexture ( gl . TEXTURE_2D , glTexture ) ; pixelStore . forEach ( ( option ) => { gl . pixelStorei ( gl [ option [ 0 ] ] , option [ 1 ] ) ; } ) ; texParameter . forEach ( ( option ) => { gl . texParameteri ( gl . TEXTURE_2D , gl [ option [ 0 ] ] , gl [ option [ 1 ] ] ) ; } ) ; resources . textures [ texture . id ] = glTexture ; } ) ; // Create Framebuffer framebuffers . forEach ( ( framebuffer ) => { const glFramebuffer = gl . createFramebuffer ( ) ; glFramebuffer . width = framebuffer . width ; glFramebuffer . height = framebuffer . height ; resources . framebuffers [ framebuffer . id ] = glFramebuffer ; } ) ; // Create programs Object . keys ( glConfig . programs ) . forEach ( ( programName ) => { buildShaderProgram ( gl , programName , glConfig , resources ) ; } ) ; // Add destroy function resources . destroy = ( ) => { freeGLResources ( resources ) ; } ; return resources ; }
Create GL resources
Create GL resources