SHARE
TWEET
Untitled
a guest
Nov 23rd, 2016
69
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- package com.mygdx.game.rendering.GLSLSandbox;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.graphics.Color;
- import com.badlogic.gdx.graphics.GL20;
- import com.badlogic.gdx.graphics.Pixmap;
- import com.badlogic.gdx.graphics.Pixmap.Format;
- import com.badlogic.gdx.graphics.Texture.TextureFilter;
- import com.badlogic.gdx.graphics.Texture;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- import com.badlogic.gdx.graphics.glutils.FrameBuffer;
- import com.badlogic.gdx.graphics.glutils.ShaderProgram;
- import com.mygdx.game.MyGdxGame;
- /**
- * Class that "plays" shaders from the site:
- * http://glslsandbox.com/
- *
- * As a model I used gdx-shadertoy (https://github.com/xoppa/gdx-shadertoy).
- *
- * @author Betalord
- *
- */
- public class GLSLSandboxPlayer {
- private SpriteBatch batch;
- private Texture tex1x1;
- private FrameBuffer m_fbo = null; // off screen frame buffer to which we render our shader
- private ShaderProgram shader;
- private float width, height;
- //private OrthographicCamera cam;
- private float time = 0;
- // uniforms
- private int u_time; // "time" uniform
- private int u_cursor; // "mouse" uniform
- private int u_resolution; // "resolution" uniform
- private MyGdxGame game;
- public GLSLSandboxPlayer(MyGdxGame game, String shaderPath, float quality) {
- this.game = game;
- batch = new SpriteBatch();
- /*
- cam = new OrthographicCamera();
- cam.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
- cam.update();
- */
- batch.setProjectionMatrix(game.cam.combined);
- Pixmap pixmap = new Pixmap(1, 1, Format.RGBA8888);
- pixmap.drawPixel(0, 0, Color.rgba8888(Color.WHITE));
- tex1x1 = new Texture(pixmap);
- pixmap.dispose();
- shader = loadShader(shaderPath);
- width = Gdx.graphics.getWidth() / quality;
- height = Gdx.graphics.getHeight() / quality;
- // fetch uniforms:
- u_time = shader.fetchUniformLocation("time", false);
- u_cursor = shader.fetchUniformLocation("mouse", false);
- u_resolution = shader.fetchUniformLocation("resolution", false);
- m_fbo = new FrameBuffer(Format.RGBA8888, Math.round(width), Math.round(height), false);
- m_fbo.getColorBufferTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
- }
- private void setUniforms() {
- if (u_time >= 0) {
- time += Gdx.graphics.getDeltaTime();
- shader.setUniformf(u_time, time);
- }
- if (u_cursor >= 0) {
- final float cursorX = (float)Gdx.input.getX() / (float)Gdx.graphics.getWidth();
- final float cursorY = 1f - ((float)Gdx.input.getY() / (float)Gdx.graphics.getHeight());
- shader.setUniformf(u_cursor, cursorX, cursorY);
- }
- if (u_resolution >= 0) {
- //***shader.setUniformf(u_resolution, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
- shader.setUniformf(u_resolution, width, height);
- }
- }
- public void render() {
- renderToFBO();
- batch.begin();
- batch.setShader(null);
- batch.draw(m_fbo.getColorBufferTexture(), 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
- batch.end();
- }
- private void renderToFBO() {
- m_fbo.begin(); // binds the frame buffer and sets the viewport accordingly, so everything gets drawn to it.
- Gdx.gl.glViewport(0, 0, Math.round(game.renderer.getWidth()), Math.round(game.renderer.getHeight()));
- Gdx.gl.glClearColor(0, 0, 0, 1);
- Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
- batch.begin();
- batch.setShader(shader);
- setUniforms();
- batch.draw(tex1x1, 0, 0, width, height);
- batch.end();
- m_fbo.end();
- }
- private ShaderProgram loadShader(String file) {
- ShaderProgram shader = new ShaderProgram(Gdx.files.internal(file + ".vert"), Gdx.files.internal(file + ".frag"));
- if (!shader.isCompiled()) {
- throw new RuntimeException("Error: shader cannot be compiled: " + shader.getLog());
- }
- return shader;
- }
- }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

