Ejemplo n.º 1
0
 private void setSize(int FBO_W, int FBO_H) {
   w = FBO_W;
   h = FBO_H;
   blurShader.begin();
   blurShader.setUniformf("size", FBO_W, FBO_H);
   blurShader.end();
 }
Ejemplo n.º 2
0
  @Override
  public void render() {
    frameBuffer.begin();
    Gdx.graphics.getGL20().glViewport(0, 0, frameBuffer.getWidth(), frameBuffer.getHeight());
    Gdx.graphics.getGL20().glClearColor(0f, 1f, 0f, 1);
    Gdx.graphics.getGL20().glClear(GL20.GL_COLOR_BUFFER_BIT);
    Gdx.graphics.getGL20().glEnable(GL20.GL_TEXTURE_2D);
    texture.bind();
    meshShader.begin();
    meshShader.setUniformi("u_texture", 0);
    mesh.render(meshShader, GL20.GL_TRIANGLES);
    meshShader.end();
    frameBuffer.end();

    Gdx.graphics.getGL20().glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    Gdx.graphics.getGL20().glClearColor(0.2f, 0.2f, 0.2f, 1);
    Gdx.graphics.getGL20().glClear(GL20.GL_COLOR_BUFFER_BIT);

    spriteBatch.begin();
    spriteBatch.draw(
        frameBuffer.getColorBufferTexture(),
        0,
        0,
        256,
        256,
        0,
        0,
        frameBuffer.getColorBufferTexture().getWidth(),
        frameBuffer.getColorBufferTexture().getHeight(),
        false,
        true);
    spriteBatch.end();
  }
Ejemplo n.º 3
0
  /** Call this after scene. Renders the bloomed scene. */
  public void render() {
    if (capturing) {
      capturing = false;
      frameBuffer.end();
    }

    Gdx.gl.glDisable(GL20.GL_BLEND);
    Gdx.gl.glDisable(GL20.GL_DEPTH_TEST);
    Gdx.gl.glDepthMask(false);

    gaussianBlur();

    if (blending) {
      Gdx.gl.glEnable(GL20.GL_BLEND);
      Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
    }

    pingPongTex1.bind(1);
    original.bind(0);
    bloomShader.begin();
    {
      fullScreenQuad.render(bloomShader, GL20.GL_TRIANGLE_FAN);
    }
    bloomShader.end();
  }
Ejemplo n.º 4
0
  private void gaussianBlur() {

    // cut bright areas of the picture and blit to smaller fbo

    original.bind(0);
    pingPongBuffer1.begin();
    {
      tresholdShader.begin();
      {
        // tresholdShader.setUniformi("u_texture0", 0);
        fullScreenQuad.render(tresholdShader, GL20.GL_TRIANGLE_FAN, 0, 4);
      }
      tresholdShader.end();
    }
    pingPongBuffer1.end();

    for (int i = 0; i < blurPasses; i++) {

      pingPongTex1.bind(0);

      // horizontal
      pingPongBuffer2.begin();
      {
        blurShader.begin();
        {
          blurShader.setUniformf("dir", 1f, 0f);
          fullScreenQuad.render(blurShader, GL20.GL_TRIANGLE_FAN, 0, 4);
        }
        blurShader.end();
      }
      pingPongBuffer2.end();

      pingPongTex2.bind(0);
      // vertical
      pingPongBuffer1.begin();
      {
        blurShader.begin();
        {
          blurShader.setUniformf("dir", 0f, 1f);

          fullScreenQuad.render(blurShader, GL20.GL_TRIANGLE_FAN, 0, 4);
        }
        blurShader.end();
      }
      pingPongBuffer1.end();
    }
  }
Ejemplo n.º 5
0
 /**
  * set intensity for bloom. higher mean more brightening for spots that are over treshold
  *
  * @param intensity multiplier for blurred texture in combining phase. must be positive.
  */
 public void setBloomIntesity(float intensity) {
   bloomIntensity = intensity;
   bloomShader.begin();
   {
     bloomShader.setUniformf("BloomIntensity", intensity);
   }
   bloomShader.end();
 }
Ejemplo n.º 6
0
 /**
  * set intensity for original scene. under 1 mean darkening and over 1 means lightening
  *
  * @param intensity multiplier for captured texture in combining phase. must be positive.
  */
 public void setOriginalIntesity(float intensity) {
   originalIntensity = intensity;
   bloomShader.begin();
   {
     bloomShader.setUniformf("OriginalIntensity", intensity);
   }
   bloomShader.end();
 }
Ejemplo n.º 7
0
 /**
  * Treshold for bright parts. everything under treshold is clamped to 0
  *
  * @param treshold must be in range 0..1
  */
 public void setTreshold(float treshold) {
   this.treshold = treshold;
   tresholdShader.begin();
   {
     tresholdShader.setUniformf("treshold", treshold, 1f / (1 - treshold));
   }
   tresholdShader.end();
 }
Ejemplo n.º 8
0
  private void realRender() {
    // gives a chance to filters to perform needed operations just before the rendering operation
    // take place.
    onBeforeRender();

    program.begin();
    quad.render(program);
    program.end();
  }
Ejemplo n.º 9
0
  /**
   * Finishes off rendering. Enables depth writes, disables blending and texturing. Must always be
   * called after a call to {@link #begin()}
   */
  public void end() {
    if (!drawing) throw new IllegalStateException("SpriteBatch.begin must be called before end.");
    if (idx > 0) renderMesh();
    lastTexture = null;
    idx = 0;
    drawing = false;

    GLCommon gl = Gdx.gl;
    gl.glDepthMask(true);
    if (isBlendingEnabled()) gl.glDisable(GL10.GL_BLEND);

    if (Gdx.graphics.isGL20Available()) {
      if (customShader != null) customShader.end();
      else shader.end();
    } else {
      gl.glDisable(GL10.GL_TEXTURE_2D);
    }
  }
Ejemplo n.º 10
0
  public void render(float delta) {
    time += delta;

    shaderProgram.begin();

    texture.bind();
    shaderProgram.setUniformf("tex0", 0);
    shaderProgram.setUniformf("resolution", Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    shaderProgram.setUniformf("time", time);

    this.mesh.render(shaderProgram, GL20.GL_TRIANGLE_FAN);
    shaderProgram.end();
  }
Ejemplo n.º 11
0
 public void end() {
   newFrame.end();
   FrameBuffer oldFrame = newFrame;
   newFrame = cleanFrame;
   oldFrame.getColorBufferTexture().bind();
   {
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
     mShaderProgram.begin();
     {
       mScreenMesh.render(mShaderProgram, GL20.GL_TRIANGLE_FAN, 0, 4);
     }
     mShaderProgram.end();
   }
 }
Ejemplo n.º 12
0
  /** IMPORTANT NOTE CALL THIS WHEN RESUMING */
  public void resume() {
    bloomShader.begin();
    {
      bloomShader.setUniformi("u_texture0", 0);
      bloomShader.setUniformi("u_texture1", 1);
    }
    bloomShader.end();

    setSize(w, h);
    setTreshold(treshold);
    setBloomIntesity(bloomIntensity);
    setOriginalIntesity(originalIntensity);

    original = frameBuffer.getColorBufferTexture();
    pingPongTex1 = pingPongBuffer1.getColorBufferTexture();
    pingPongTex2 = pingPongBuffer2.getColorBufferTexture();
  }
Ejemplo n.º 13
0
  @Override
  public void render() {
    GL20 gl = Gdx.gl20;
    gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    Gdx.gl.glClearColor(0.7f, 0, 0, 1);
    gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    gl.glEnable(GL20.GL_TEXTURE_2D);
    shader.begin();
    shader.setUniformi("u_texture", 0);
    texture.bind();
    vbo.bind(shader);
    indices.bind();
    gl.glDrawElements(GL20.GL_TRIANGLES, 3, GL20.GL_UNSIGNED_SHORT, indices.getBuffer().position());
    indices.unbind();
    vbo.unbind(shader);
    shader.end();
  }
Ejemplo n.º 14
0
 public void setElement(SceneElement e) {
   super.setElement(e);
   RuntimeDrawable<?> r = (RuntimeDrawable<?>) assetHandler.getRuntimeAsset(transition.getMask());
   texture = r.getTextureHandle();
   offset = new Vector3();
   if (maskShader == null) {
     maskShader =
         new ShaderProgram(
             assetHandler.getTextFile("@binary/shaders/mask.vert"),
             assetHandler.getTextFile("@binary/shaders/mask.frag"));
     if (maskShader.getLog().length() != 0) logger.warn(maskShader.getLog());
     nextSceneBuffer = new FrameBuffer(Format.RGBA8888, 800, 600, false);
   }
   maskShader.begin();
   maskShader.setUniformi("mask_texture", 1);
   maskShader.setAttributef("a_sizes", 800, 600, r.getWidth(), r.getHeight());
   maskShader.end();
 }
Ejemplo n.º 15
0
  private void initializeShaders() {
    String vertexShader = "shaders/blur.vert";
    String fragmentShader = "shaders/blur.frag";

    ShaderProgram.pedantic = false;

    blurShader =
        new ShaderProgram(Gdx.files.internal(vertexShader), Gdx.files.internal(fragmentShader));
    if (!blurShader.isCompiled()) {
      System.err.println(blurShader.getLog());
      System.exit(0);
    }
    if (blurShader.getLog().length() != 0) System.out.println(blurShader.getLog());

    blurShader.begin();
    blurShader.setUniformf("dir", 0f, 0f);
    blurShader.setUniformf("resolution", FBO_SIZE);
    blurShader.setUniformf("radius", blurAmount);
    blurShader.end();
  }
Ejemplo n.º 16
0
  public void render() {
    Gdx.gl20.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);

    Gdx.gl20.glActiveTexture(GL20.GL_TEXTURE0);
    texture.bind();
    shader.begin();
    shader.setUniformf("s_texture", 0);

    Gdx.gl20.glTexParameteri(GL20.GL_TEXTURE_2D, GL20.GL_TEXTURE_MIN_FILTER, GL20.GL_NEAREST);
    shader.setUniformf("u_offset", -0.6f);
    mesh.render(shader, GL20.GL_TRIANGLES);

    Gdx.gl20.glTexParameteri(
        GL20.GL_TEXTURE_2D, GL20.GL_TEXTURE_MIN_FILTER, GL20.GL_LINEAR_MIPMAP_LINEAR);
    shader.setUniformf("u_offset", 0.6f);
    mesh.render(shader, GL20.GL_TRIANGLES);

    shader.end();
  }
 @Override
 public void render() {
   Gdx.gl20.glViewport(
       0, 0, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight());
   Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
   shader.begin();
   shader.setUniform2fv(
       "u_resolution",
       new float[] {(float) Gdx.graphics.getWidth(), (float) Gdx.graphics.getHeight()},
       0,
       2);
   shader.setUniformf("u_time", TimeUtils.timeSinceMillis(startTime) / 1000f);
   shader.setUniform2fv(
       "u_mousePos",
       new float[] {Gdx.input.getX(), Gdx.graphics.getHeight() - Gdx.input.getY()},
       0,
       2);
   mesh.render(shader, GL20.GL_TRIANGLES);
   shader.end();
 }
Ejemplo n.º 18
0
  // float[], vec2[], vec3[], vec4[]
  protected T setParamv(Parameter param, float[] values, int offset, int length) {
    program.begin();

    switch (param.arrayElementSize()) {
      case 4:
        program.setUniform4fv(param.mnemonic(), values, offset, length);
        break;
      case 3:
        program.setUniform3fv(param.mnemonic(), values, offset, length);
        break;
      case 2:
        program.setUniform2fv(param.mnemonic(), values, offset, length);
        break;
      default:
      case 1:
        program.setUniform1fv(param.mnemonic(), values, offset, length);
        break;
    }

    program.end();
    return (T) this;
  }
Ejemplo n.º 19
0
  public void flush() {
    // if we've already flushed
    if (idx == 0) return;
    // sends our vertex data to the mesh
    mesh.setVertices(verts);

    // no need for depth...
    Gdx.gl.glDepthMask(false);

    // enable blending, for alpha
    Gdx.gl.glEnable(GL20.GL_BLEND);
    Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);

    // number of vertices we need to render
    int vertexCount = (idx / NUM_COMPONENTS);

    cam.update();

    // start the shader before setting any uniforms
    shader.begin();

    // update the projection matrix so our triangles are rendered in 2D
    shader.setUniformMatrix("u_projTrans", cam.combined);

    // render the mesh
    mesh.render(shader, RENDER_TYPE, 0, vertexCount);

    shader.end();

    // re-enable depth to reset states to their default
    Gdx.gl.glDepthMask(true);

    // reset index to zero
    idx = 0;

    // reset verts
    Arrays.fill(verts, 0);
  }
Ejemplo n.º 20
0
  public void draw(Matrix4 mat) {
    if (!body.isActive()) {
      return;
    }

    Transform transform = body.getTransform();

    texture.bind();
    shader.begin();
    shader.setUniformMatrix("u_projectionViewMatrix", mat);
    for (FixtureData data : fixtureData) {
      data.updateData(transform);
      data.mesh.render(shader, GL20.GL_TRIANGLE_FAN, 0, data.vertices.length);
    }
    shader.end();

    // chain.getVertex(0, v0);
    // transform.mul(v0);
    // chain.getVertex(1, v1);
    // transform.mul(v1);
    // chain.getVertex(2, v2);
    // transform.mul(v2);
    // chain.getVertex(3, v3);
    // transform.mul(v3);

    // mesh.setVertices(new float[] { -5f, -5f, 0, 0, 1, // bottom left
    // 5f, -5f, 0, 1, 1, // bottom right
    // 5f, 5f, 0, 1, 0, // top right
    // -5f, 5f, 0, 0, 0 }); // top left
    //
    // mesh.setVertices(new float[] { v0.x, v0.y, 0, 0, 1, // bottom left
    // v1.x, v1.y, 0, 1, 1, // bottom right
    // v2.x, v2.y, 0, 1, 0, // top right
    // v3.x, v3.y, 0, 0, 0 }); // top left

    // System.out.println("--");

    // Vector2 before = new Vector2();
    // Vector2 current = new Vector2();
    // Vector2 after = new Vector2();
    // for (int i = 0; i < meshes.length; i++) {
    // Fixture fixture = body.getFixtureList().get(i);
    // PolygonShape poly = (PolygonShape) fixture.getShape();
    //
    // for (int j = 0; j < meshesData[i].length; j += 6) {
    // int polyIndex = j / 6;
    // poly.getVertex(polyIndex, current);
    //
    // // if (scale) {
    // // poly.getVertex(polyIndex == 0 ? poly.getVertexCount() - 1 :
    // polyIndex - 1, before);
    // // poly.getVertex(polyIndex == poly.getVertexCount() - 1 ? 0 :
    // polyIndex + 1, after);
    // //
    // // Vector2D vec1 = new Vector2D(new Point2D(after.x, after.y), new
    // Point2D(current.x, current.y));
    // // Vector2D vec2 = new Vector2D(new Point2D(before.x, before.y), new
    // Point2D(current.x, current.y));
    // //
    // // double a1 = vec1.getAngle();
    // // double a2 = vec2.getAngle();
    // // double diff = a1 - a2;
    // // double angle = (a1 + a2) / 2.0;
    // //
    // // if (diff > 0) {
    // // angle -= Math.PI;
    // // }
    // //
    // // System.out.println("angle1 " + a1 * 180 / Math.PI);
    // // System.out.println("angle2 " + a2 * 180 / Math.PI);
    // // System.out.println("diff " + diff * 180 / Math.PI);
    // // System.out.println("center " + angle * 180 / Math.PI);
    // //
    // // Vector2D sum = new Vector2D(Math.cos(angle) * 0.2, Math.sin(angle) *
    // 0.2);
    // // sum.normalize();
    // //
    // // sum = sum.times(0.2);
    // //
    // // Vector2 newPoint = new Vector2(current.x + (float) sum.getX(),
    // current.y + (float) sum.getY());
    // //
    // // transform.mul(newPoint);
    // //
    // // transform.mul(current);
    // //
    // // // poly.getVertex(polyIndex, newPoint);
    // // // newPoint.mul(1.2f);
    // // // transform.mul(newPoint);
    // //
    // // // batch.begin();
    // // // batch.draw(texture, current.x * 20 + 200, current.y * 20 + 200);
    // // // batch.draw(texture2, newPoint.x * 20 + 200, newPoint.y * 20 +
    // 200);
    // // // batch.end();
    // // }
    //
    // transform.mul(current);
    //
    // meshesData[i][j] = current.x;
    // meshesData[i][j + 1] = current.y;
    // meshesData[i][j + 2] = 0;
    //
    // meshesData[i][j + 3] = colors[(color + i) % colors.length];
    //
    // meshesData[i][j + 4] = (float) Math.random();
    // meshesData[i][j + 5] = (float) Math.random();
    // }
    //
    // // render
    // meshes[i].setVertices(meshesData[i]);
    //
    // shader.begin();
    // shader.setUniformMatrix("u_projectionViewMatrix", mat);
    // texture.bind();
    // meshes[i].render(shader, GL20.GL_TRIANGLE_FAN, 0,
    // poly.getVertexCount());
    // shader.end();
    // }

  }
Ejemplo n.º 21
0
 /** Should be called after any one or more setParams method calls. */
 protected void endParams() {
   if (programBegan) {
     program.end();
     programBegan = false;
   }
 }
Ejemplo n.º 22
0
 // vec3
 protected void setParam(Parameter param, Vector3 value) {
   program.begin();
   program.setUniformf(param.mnemonic(), value);
   program.end();
 }
Ejemplo n.º 23
0
 // mat3
 protected T setParam(Parameter param, Matrix3 value) {
   program.begin();
   program.setUniformMatrix(param.mnemonic(), value);
   program.end();
   return (T) this;
 }
Ejemplo n.º 24
0
  private void initialize(
      int FBO_W,
      int FBO_H,
      FrameBuffer fbo,
      boolean hasDepth,
      boolean useBlending,
      boolean use32bitFBO) {
    blending = useBlending;
    Format format = null;

    if (use32bitFBO) {
      if (useBlending) {
        format = Format.RGBA8888;
      } else {
        format = Format.RGB888;
      }

    } else {
      if (useBlending) {
        format = Format.RGBA4444;
      } else {
        format = Format.RGB565;
      }
    }
    if (fbo == null) {
      frameBuffer =
          new FrameBuffer(format, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), hasDepth);
    } else {
      frameBuffer = fbo;
    }

    pingPongBuffer1 = new FrameBuffer(format, FBO_W, FBO_H, false);

    pingPongBuffer2 = new FrameBuffer(format, FBO_W, FBO_H, false);

    original = frameBuffer.getColorBufferTexture();
    pingPongTex1 = pingPongBuffer1.getColorBufferTexture();
    pingPongTex2 = pingPongBuffer2.getColorBufferTexture();

    fullScreenQuad = createFullScreenQuad();
    final String alpha = useBlending ? "alpha_" : "";

    bloomShader = BloomShaderLoader.createShader("screenspace", alpha + "bloom");

    if (useAlphaChannelAsMask) {
      tresholdShader = BloomShaderLoader.createShader("screenspace", "maskedtreshold");
    } else {
      tresholdShader = BloomShaderLoader.createShader("screenspace", alpha + "treshold");
    }

    blurShader = BloomShaderLoader.createShader("blurspace", alpha + "gaussian");

    setSize(FBO_W, FBO_H);
    setBloomIntesity(2.5f);
    setOriginalIntesity(0.8f);
    setTreshold(0.5f);

    bloomShader.begin();
    {
      bloomShader.setUniformi("u_texture0", 0);
      bloomShader.setUniformi("u_texture1", 1);
    }
    bloomShader.end();
  }