Exemplo n.º 1
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();
  }
Exemplo n.º 2
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();
 }
Exemplo n.º 3
0
  private void createShader() {
    // this shader tells opengl where to put things
    String vertexShader =
        "attribute vec4 a_position;    \n"
            + "void main()                   \n"
            + "{                             \n"
            + "   gl_Position = a_position;  \n"
            + "}                             \n";

    // this one tells it what goes in between the points (i.e
    // colour/texture)
    String fragmentShader =
        "#ifdef GL_ES                \n"
            + "precision mediump float;    \n"
            + "#endif                      \n"
            + "void main()                 \n"
            + "{                           \n"
            + "  gl_FragColor = vec4(1.0,0.0,0.0,1.0);	\n"
            + "}";

    // make an actual shader from our strings
    meshShader = new ShaderProgram(vertexShader, fragmentShader);

    // check there's no shader compile errors
    if (!meshShader.isCompiled()) throw new IllegalStateException(meshShader.getLog());
  }
Exemplo n.º 4
0
  private void createShader(Graphics graphics) {
    String vertexShader =
        "attribute vec4 a_Position;    \n"
            + "attribute vec4 a_Color;\n"
            + "attribute vec2 a_texCoords;\n"
            + "varying vec4 v_Color;"
            + "varying vec2 v_texCoords; \n"
            + "void main()                  \n"
            + "{                            \n"
            + "   v_Color = a_Color;"
            + "   v_texCoords = a_texCoords;\n"
            + "   gl_Position =   a_Position;  \n"
            + "}                            \n";
    String fragmentShader =
        "precision mediump float;\n"
            + "varying vec4 v_Color;\n"
            + "varying vec2 v_texCoords; \n"
            + "uniform sampler2D u_texture;\n"
            + "void main()                                  \n"
            + "{                                            \n"
            + "  gl_FragColor = v_Color * texture2D(u_texture, v_texCoords);\n"
            + "}";

    meshShader = new ShaderProgram(vertexShader, fragmentShader);
    if (meshShader.isCompiled() == false) throw new IllegalStateException(meshShader.getLog());
  }
Exemplo n.º 5
0
  public static ShaderProgram createPointLightShaderProgram() {
    String vertex =
        "uniform mat4 modelViewProjectionMatrix;\n"
            + "uniform vec3 translate;\n"
            + "uniform vec3 scale;\n"
            + "attribute vec3 a_position;\n"
            + "\n"
            + "void main()\n"
            + "{\n"
            + "  gl_PointSize = 5.0;\n"
            + "  gl_Position = modelViewProjectionMatrix * vec4(scale * a_position + translate, 1.0);\n"
            + "}";

    String fragment =
        "#ifdef GL_ES\n"
            + "precision mediump float;\n"
            + "#endif\n"
            + "uniform vec4 color;\n"
            + "\n"
            + "void main()\n"
            + "{\n"
            + "  gl_FragColor = color;\n"
            + "}";

    ShaderProgram shaderProgram = new ShaderProgram(vertex, fragment);
    System.out.println("PLCompiled: " + shaderProgram.isCompiled() + "----------");
    return shaderProgram;
  }
  @Override
  public void create() {
    ShaderProgram.pedantic = false;

    String vs = Gdx.files.internal("signed/vertex.glsl").readString();
    String fs = Gdx.files.internal("signed/fragment.glsl").readString();

    shader = new ShaderProgram(vs, fs);

    if (!shader.isCompiled()) {
      throw new IllegalArgumentException(
          "Error compiling distance field shader: " + shader.getLog());
    }

    mesh =
        new Mesh(true, 4, 6, new VertexAttribute(VertexAttributes.Usage.Position, 3, "a_position"));
    float[] vertices = {1, -1, 0, 1, 1, 0, -1, 1, 0, -1, -1, 0};

    short[] indices = {
      0, 1, 2,
      2, 3, 0
    };
    mesh.setVertices(vertices);
    mesh.setIndices(indices);

    startTime = TimeUtils.millis();
  }
Exemplo n.º 7
0
  protected static ShaderProgram createMeshShader() {

    final String VERT_SHADER =
        "attribute vec2 a_position;\n"
            + "attribute vec4 a_color;\n"
            + "uniform mat4 u_projTrans;\n"
            + "varying vec4 vColor;\n"
            + "void main() {\n"
            + "	vColor = a_color;\n"
            + "	gl_Position =  u_projTrans * vec4(a_position.xy, 0.0, 1.0);\n"
            + "}";

    final String FRAG_SHADER =
        "#ifdef GL_ES\n"
            + "precision mediump float;\n"
            + "#endif\n"
            + "varying vec4 vColor;\n"
            + "void main() {\n"
            + "	gl_FragColor = vColor;\n"
            + "}";

    ShaderProgram.pedantic = false;
    ShaderProgram shader = new ShaderProgram(VERT_SHADER, FRAG_SHADER);
    String log = shader.getLog();
    if (!shader.isCompiled()) throw new GdxRuntimeException(log);
    if (log != null && log.length() != 0) System.out.println("Shader Log: " + log);
    return shader;
  }
Exemplo n.º 8
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();
  }
Exemplo n.º 9
0
 // mat4
 protected T setParams(Parameter param, Matrix4 value) {
   if (!programBegan) {
     programBegan = true;
     program.begin();
   }
   program.setUniformMatrix(param.mnemonic(), value);
   return (T) this;
 }
Exemplo n.º 10
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();
 }
Exemplo n.º 11
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();
 }
Exemplo n.º 12
0
 public ShaderProgram createShader() {
   ShaderProgram shader =
       new ShaderProgram(
           Gdx.files.internal("data/distort.vs"), Gdx.files.internal("data/distort.fs"));
   if (shader.isCompiled() == false)
     throw new IllegalArgumentException("Error compiling shader: " + shader.getLog());
   return shader;
 }
Exemplo n.º 13
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();
 }
Exemplo n.º 14
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();
  }
Exemplo n.º 15
0
 public static ShaderProgram loadShader(String vertexPath, String fragmentPath) {
   ShaderProgram shader =
       new ShaderProgram(Gdx.files.internal(vertexPath), Gdx.files.internal(fragmentPath));
   if (!shader.isCompiled())
     throw new IllegalArgumentException("Error compiling shader: " + shader.getLog());
   Log.log(
       "Shader loaded successfully (vertex: " + vertexPath + ", fragment: " + fragmentPath + ")");
   return shader;
 }
Exemplo n.º 16
0
 public void clearManagedCaches() {
   Mesh.clearAllMeshes(app);
   Texture.clearAllTextures(app);
   ShaderProgram.clearAllShaderPrograms(app);
   FrameBuffer.clearAllFrameBuffers(app);
   Gdx.app.log("AndroidGraphics", Mesh.getManagedStatus());
   Gdx.app.log("AndroidGraphics", Texture.getManagedStatus());
   Gdx.app.log("AndroidGraphics", ShaderProgram.getManagedStatus());
   Gdx.app.log("AndroidGraphics", FrameBuffer.getManagedStatus());
 }
Exemplo n.º 17
0
  public ShaderProgram getShaderProgram() {
    ShaderProgram temp = new ShaderProgram(this.vertexShader, this.fragmentShader);

    if (temp.isCompiled() == false) {
      Gdx.app.log("ShaderError", temp.getLog());
      System.exit(0);
    }

    return temp;
  }
Exemplo n.º 18
0
  /** Call this when application is exiting. */
  public void dispose() {
    if (disposeFBO) frameBuffer.dispose();

    fullScreenQuad.dispose();

    pingPongBuffer1.dispose();
    pingPongBuffer2.dispose();

    blurShader.dispose();
    bloomShader.dispose();
    tresholdShader.dispose();
  }
Exemplo n.º 19
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();
  }
Exemplo n.º 20
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();
   }
 }
  public void clearManagedCaches() {
    Mesh.clearAllMeshes(app);
    Texture.clearAllTextures(app);
    ShaderProgram.clearAllShaderPrograms(app);
    FrameBuffer.clearAllFrameBuffers(app);

    if (AndroidLiveWallpaperService
        .DEBUG) { // to prevent creating too many string buffers in live wallpapers
      Gdx.app.debug("AndroidGraphics", Mesh.getManagedStatus());
      Gdx.app.debug("AndroidGraphics", Texture.getManagedStatus());
      Gdx.app.debug("AndroidGraphics", ShaderProgram.getManagedStatus());
      Gdx.app.debug("AndroidGraphics", FrameBuffer.getManagedStatus());
    }
  }
Exemplo n.º 22
0
 @Override
 public void dispose() {
   texture.dispose();
   vbo.dispose();
   indices.dispose();
   shader.dispose();
 }
Exemplo n.º 23
0
  /**
   * Returns a new instance of the default shader used by SpriteBatch for GL2 when no shader is
   * specified.
   */
  public static ShaderProgram createDefaultShader() {
    String vertexShader =
        "attribute vec4 "
            + ShaderProgram.POSITION_ATTRIBUTE
            + ";\n" //
            + "attribute vec4 "
            + ShaderProgram.COLOR_ATTRIBUTE
            + ";\n" //
            + "attribute vec2 "
            + ShaderProgram.TEXCOORD_ATTRIBUTE
            + "0;\n" //
            + "uniform mat4 u_projectionViewMatrix;\n" //
            + "varying vec4 v_color;\n" //
            + "varying vec2 v_texCoords;\n" //
            + "\n" //
            + "void main()\n" //
            + "{\n" //
            + "   v_color = "
            + ShaderProgram.COLOR_ATTRIBUTE
            + ";\n" //
            + "   v_texCoords = "
            + ShaderProgram.TEXCOORD_ATTRIBUTE
            + "0;\n" //
            + "   gl_Position =  u_projectionViewMatrix * "
            + ShaderProgram.POSITION_ATTRIBUTE
            + ";\n" //
            + "}\n";
    String fragmentShader =
        "#ifdef GL_ES\n" //
            + "#define LOWP lowp\n" //
            + "precision mediump float;\n" //
            + "#else\n" //
            + "#define LOWP \n" //
            + "#endif\n" //
            + "varying LOWP vec4 v_color;\n" //
            + "varying vec2 v_texCoords;\n" //
            + "uniform sampler2D u_texture;\n" //
            + "void main()\n" //
            + "{\n" //
            + "  gl_FragColor = v_color * texture2D(u_texture, v_texCoords);\n" //
            + "}";

    ShaderProgram shader = new ShaderProgram(vertexShader, fragmentShader);
    if (shader.isCompiled() == false)
      throw new IllegalArgumentException("couldn't compile shader: " + shader.getLog());
    return shader;
  }
Exemplo n.º 24
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();
  }
Exemplo n.º 25
0
  /**
   * Sets up the SpriteBatch for drawing. This will disable depth buffer writting. It enables
   * blending and texturing. If you have more texture units enabled than the first one you have to
   * disable them before calling this. Uses a screen coordinate system by default where everything
   * is given in pixels. You can specify your own projection and modelview matrices via {@link
   * #setProjectionMatrix(Matrix4)} and {@link #setTransformMatrix(Matrix4)}.
   */
  public void begin() {
    if (drawing) throw new IllegalStateException("you have to call SpriteBatch.end() first");
    renderCalls = 0;

    Gdx.gl.glDepthMask(false);
    if (Gdx.graphics.isGL20Available()) {
      if (customShader != null) customShader.begin();
      else shader.begin();
    } else {
      Gdx.gl.glEnable(GL10.GL_TEXTURE_2D);
    }
    setupMatrices();

    idx = 0;
    lastTexture = null;
    drawing = true;
  }
Exemplo n.º 26
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);
    }
  }
Exemplo n.º 27
0
 @Override
 public void render(@NotNull ShaderProgram program) {
   super.render(program);
   if (!isVisible()) return;
   program.setUniformMatrix(MODEL_VIEW, getCombinedTransoformation());
   //        program.setUniformMatrix(NORMAL, getCombinedTransoformation().toNormalMatrix());
   material.apply(program);
   model.render(program);
 }
Exemplo n.º 28
0
  public void dispose() {
    blurTargetA.dispose();
    blurTargetB.dispose();
    blurShader.dispose();

    batch.dispose();

    shapeRenderer.dispose();
  }
Exemplo n.º 29
0
  public static void load() {
    fontDistanceFieldShader =
        new ShaderProgram(
            Gdx.files.internal("assets/font/font.vert"),
            Gdx.files.internal("assets/font/font.frag"));
    if (!fontDistanceFieldShader.isCompiled()) {
      Gdx.app.error("fontShader", "compilation failed:\n" + fontDistanceFieldShader.getLog());
    }

    Texture fontTexture = new Texture(Gdx.files.internal("assets/font/consolas.png"), true);
    fontTexture.setFilter(TextureFilter.MipMapLinearLinear, TextureFilter.Linear);

    consolasFont =
        new BitmapFont(
            Gdx.files.internal("assets/font/consolas.fnt"), new TextureRegion(fontTexture), false);

    Gdx.app.log("Assets", "Finished loading assets");
  }
Exemplo n.º 30
0
  @Override
  public ShaderProgram load(
      final AssetManager assetManager,
      final String fileName,
      final FileHandle file,
      final AssetLoaderParameters<ShaderProgram> parameter) {
    final ShaderProgram shader =
        new ShaderProgram(
            resolve(fileName + VERTEX_SHADER_SUFFIX), resolve(fileName + FRAGMENT_SHADER_SUFFIX));
    if (shader.isCompiled() == false) {

      Gdx.app.log("ShaderAssetLoader", "Shader compiled failed. Error: " + shader.getLog());
      throw new RuntimeException("Shader compile failed: " + shader.getLog());
    }

    Gdx.app.log("ShaderAssetLoader", "Shader compiled successfully");
    return shader;
  }