4
\$\begingroup\$

I am trying to combine a light shader with a water shader in libgdx. Shader programming is not one of my strengths at all.

This is the water effect rendering nicely: water shader

And when I have added the flickering light code I am getting this: weird light + water shaders

The light is meant to be a flickering white circle and the water should show up. It doesn't. What I have done so far is try to re-arrange texture binding, combine drawing in one pass. I can render the other scene content on top of the water but the water has to be the top-most item. That is what I am failing to do,

Here is my wall of code (just the render function):

public void render() {
    float dt = Gdx.graphics.getDeltaTime();

    // water
    time += dt;
    float angle = time * (2 * MathUtils.PI);
    if (angle > (2 * MathUtils.PI))
        angle -= (2 * MathUtils.PI);

    // light
    zAngle += dt * zSpeed;
    while (zAngle > PI2)
        zAngle -= PI2;

    fbo.begin();
    batch.setProjectionMatrix(cam.combined);
    batch.setShader(defaultShader);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.begin();
    float lightSize = 140.75f + 0.25f * (float) Math.sin(zAngle) + 27.2f * MathUtils.random();
    batch.draw(lighttex, 400 - lightSize * 0.5f + 0.5f, 300 + 0.5f - lightSize * 0.5f, lightSize, lightSize);
    batch.end();
    fbo.end();

    // draw the actual scene
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.setProjectionMatrix(cam.combined);
    batch.setShader(finalShader);
    batch.begin();
    fbo.getColorBufferTexture().bind(1);
    lighttex.bind(0);
    batch.draw(map1BgTex, 0, 0, 960, 540);
    batch.setShader(waterShader);
    waterShader.setUniformMatrix("u_worldView", cam.combined);
    batch.end();

    Gdx.gl20.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
    Gdx.gl20.glEnable(GL20.GL_BLEND);
    watertex.bind(1);
    waterdisptex.bind(2);

    defWaterShader.begin();
    defWaterShader.setUniformMatrix("u_worldView", matrix);
    defWaterShader.setUniformi("u_texture", 2);
    defWaterShader.setUniformi("u_texture2", 3);
    defWaterShader.setUniformf("timedelta", -angle);
    waterMesh.render(defWaterShader, GL20.GL_TRIANGLE_FAN);
    defWaterShader.end();

    }

I uploaded a more complete source file and the shaders to my server, I hope it's not too inconvenient - http://agexdev.com/files/shaders.tar.xz

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.