LibGDX in 10 Minute Slices: Shaders in Use

Code: ShaderGhostExample

package com.shadeghostrexample.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.utils.ScreenUtils;
import com.badlogic.gdx.utils.viewport.ScreenViewport;

public class ShaderGhostExample extends ApplicationAdapter {

	private Stage stage;
	
	@Override
	public void create () {

		stage = new Stage(new ScreenViewport());
		Image background = new Image(new Texture("background.png"));

		stage.addActor(background);
		stage.addActor(GhostGroup.getInstance());

		Image ghost = new Image(new Texture("cook.png"));
		ghost.setPosition(260, 245);
		GhostGroup.getInstance().addActor(ghost);

		Image ghost1 = new Image(new Texture("cook.png"));
		ghost1.setPosition(250, 125);
		ghost1.setRotation(90);
		GhostGroup.getInstance().addActor(ghost1);

	}

	@Override
	public void render () {
		ScreenUtils.clear(1, 0, 0, 1);

		stage.act(Gdx.graphics.getDeltaTime());
		stage.draw();
	}
	
	@Override
	public void dispose () {
	}
}

Code: GhostGroup

package com.shadeghostrexample.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.FrameBuffer;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.scenes.scene2d.Group;

public class GhostGroup extends Group {

    private FrameBuffer fbo;
    private String vertexShader ;
    private String fragmentShader ;
    private ShaderProgram shaderProgram;
    private float time;

    static private GhostGroup ghostGroup;

    static public GhostGroup getInstance(){
        if(ghostGroup==null){
            ghostGroup=new GhostGroup();
        }
        return ghostGroup;
    }

    private GhostGroup(){

        time = 0;
        vertexShader = Gdx.files.internal("shaders/vertex.glsl").readString();
        fragmentShader = Gdx.files.internal("shaders/wavey.glsl").readString();
        shaderProgram = new ShaderProgram(vertexShader,fragmentShader);
        shaderProgram.pedantic = false;

        fbo = new FrameBuffer(Pixmap.Format.RGBA8888, Gdx.graphics.getWidth(),Gdx.graphics.getHeight(), true);

    }


    @Override
    public void act(float delta) {
        super.act(delta);
        time+=delta;
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {

        // End and flush the current batch
        batch.end();
        batch.flush();

        // We want to post to the framebuffer and draw our images as normal
        fbo.begin();
        batch.begin();
        super.draw(batch, parentAlpha);
        batch.end();
        batch.flush();
        fbo.end();

        // now run our shader over the framebuffer image and draw
        batch.begin();
        batch.setShader(shaderProgram);

        // pass in the following to the .glsl script
        shaderProgram.setUniformf("u_time", time);
        shaderProgram.setUniformf("u_resolution", Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

        // Get the texture from the framebuffer and display on screen
        Texture texture = fbo.getColorBufferTexture();
        TextureRegion textureRegion = new TextureRegion(texture);

        batch.draw(textureRegion, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        batch.setShader(null);

    }
}

0
YOUR CART
  • No products in the cart.