/**
   * Prepare a RenderToTexture target of the given width x height that will be used as the rendering
   * target for the texture atlas algorithm.
   *
   * @param width width of the texture
   * @param height height of the texture
   */
  public CoreTextureAtlasGenerator(final CoreGL gl, final int width, final int height) {
    coreRender = CoreRender.createCoreRender(gl);

    renderToTexture = CoreFBO.createCoreFBO(gl);
    renderToTexture.bindFramebuffer();

    texture =
        CoreTexture2D.createEmptyTexture(
            gl, ColorFormat.RGBA, Type.UNSIGNED_BYTE, width, height, ResizeFilter.Linear);
    renderToTexture.attachTexture(texture.getTextureId(), 0);

    shader = CoreShader.createShaderWithVertexAttributes(gl, "aVertex", "aUV");
    shader.vertexShader("de/lessvoid/coregl/plain-texture.vs");
    shader.fragmentShader("de/lessvoid/coregl/plain-texture.fs");
    shader.link();
    shader.activate();
    shader.setUniformi("uTexture", 0);

    vao = CoreVAO.createCoreVAO(gl);
    vao.bind();

    vbo = CoreVBO.createCoreVBO(gl, DataType.FLOAT, UsageType.STREAM_DRAW, 4 * 4);
    vbo.bind();

    vao.enableVertexAttribute(0);
    vao.vertexAttribPointer(0, 2, FloatType.FLOAT, 4, 0);
    vao.enableVertexAttribute(1);
    vao.vertexAttribPointer(1, 2, FloatType.FLOAT, 4, 2);

    renderToTexture.bindFramebuffer();

    coreRender.clearColor(0.f, 0.f, 0.f, 0.f);
    coreRender.clearColorBuffer();

    renderToTexture.disableAndResetViewport(width, height);

    vao.unbind();

    generator = new TextureAtlasGenerator(width, height);
  }
  private void put(final CoreTexture2D source, final int x, final int y) {
    shader.activate();
    shader.setUniformMatrix(
        "uMvp",
        4,
        MatrixFactory.createOrtho(0, texture.getWidth(), 0, texture.getHeight()).toBuffer());

    vao.bind();
    renderToTexture.bindFramebuffer();

    final FloatBuffer buffer = vbo.getBuffer();
    buffer.put(x);
    buffer.put(y);
    buffer.put(0.0f);
    buffer.put(0.0f);

    buffer.put(x);
    buffer.put(y + source.getHeight());
    buffer.put(0.0f);
    buffer.put(1.0f);

    buffer.put(x + source.getWidth());
    buffer.put(y);
    buffer.put(1.0f);
    buffer.put(0.0f);

    buffer.put(x + source.getWidth());
    buffer.put(y + source.getHeight());
    buffer.put(1.0f);
    buffer.put(1.0f);
    buffer.rewind();
    vbo.bind();
    vbo.send();

    coreRender.renderTriangleStrip(4);
    vao.unbind();
    renderToTexture.disableAndResetViewport(source.getWidth(), source.getHeight());
  }