Wiki

Clone wiki

gdx-shaders / Home

gdx-shaders

It's a small library to make using shaders and framebuffers easier in libgdx. Comes with a collection of sample GLSL shaders.


Features

  • ShaderManager - a single class to manage all of your shaders and framebuffers
  • loading shaders from text files through libgdx' AssetManager
  • you can reload your shaders at runtime!
  • store and process data on your GPU using the Processor class
  • simplified (multiple) framebuffer usage and rendering
  • you don't have to add GLES specifics to your shaders, ShaderManager does it automatically

Sample usage

This is a part of a standard libgdx' ApplicationListener implementation:

	AssetManager am;
	ShaderManager sm;

	@Override
	public void create() {
	
		am = new AssetManager();
		//...
		
		ShaderProgram.pedantic = false;
		//init the ShaderManager, pointing to a directory with your shaders
		sm = new ShaderManager("assets/shaders", am);
		
		//this creates a new ShaderProgram managed by ShaderManager.
		//You access them by name ("bloom"). 
		//It consists of "default.vert" vertex shader and "bloom.frag" fragment shader.
		//Shaders named "empty" (vertex colors only) and "default" (vertex colors + texturing) are built-in.
		sm.add("bloom", "default.vert", "bloom.frag");
		
		//create a framebuffer - it is needed by this shader
		sm.createFB("bloom_fb");
	}

	@Override
	public void render() {
		Gdx.gl20.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
		
		//start rendering to "bloom_fb" framebuffer
		sm.beginFB("bloom_fb");
			//use the "empty" shader, we'll be rendering using vertex colors only
			sm.begin("empty");
			//set shader parameter - it's a matrix from the camera
			sm.setUniformMatrix("u_worldView", camera.combined);
			//render using current shader, you could write sm.get("empty") as well
			SOME_MESH.render(sm.getCurrent(), GL20.GL_TRIANGLES);
			//finish rendering using this shader
			sm.end();
		//and finish rendering to this framebuffer
		sm.endFB();
		
		//now we have SOME_MESH rendered to a framebuffer,
		//let's render it to the screen applying another shader
		
		//use the "bloom" shader this time
		sm.begin("bloom");
		//render the content of "bloom_fb" framebuffer to entire screen
		sm.renderFB("bloom_fb");
		//finish rendering using the "bloom" shader
		sm.end();
	}
	
	@Override
	public void dispose() {
		sm.dispose();
		am.dispose();
	}
	

License

It's licensed under the Apache License 2.0.

Have fun!

Updated