SHARE
TWEET

Untitled

a guest Nov 23rd, 2016 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.mygdx.game.rendering.GLSLSandbox;
  2.  
  3. import com.badlogic.gdx.Gdx;
  4. import com.badlogic.gdx.graphics.Color;
  5. import com.badlogic.gdx.graphics.GL20;
  6. import com.badlogic.gdx.graphics.Pixmap;
  7. import com.badlogic.gdx.graphics.Pixmap.Format;
  8. import com.badlogic.gdx.graphics.Texture.TextureFilter;
  9. import com.badlogic.gdx.graphics.Texture;
  10. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  11. import com.badlogic.gdx.graphics.glutils.FrameBuffer;
  12. import com.badlogic.gdx.graphics.glutils.ShaderProgram;
  13. import com.mygdx.game.MyGdxGame;
  14.  
  15. /**
  16.  * Class that "plays" shaders from the site:
  17.  * http://glslsandbox.com/
  18.  *
  19.  * As a model I used gdx-shadertoy (https://github.com/xoppa/gdx-shadertoy).
  20.  *
  21.  * @author Betalord
  22.  *
  23.  */
  24. public class GLSLSandboxPlayer {
  25.    
  26.     private SpriteBatch batch;
  27.     private Texture tex1x1;
  28.     private FrameBuffer m_fbo = null; // off screen frame buffer to which we render our shader
  29.     private ShaderProgram shader;
  30.     private float width, height;
  31.     //private OrthographicCamera cam;
  32.     private float time = 0;
  33.  
  34.     // uniforms
  35.     private int u_time; // "time" uniform
  36.     private int u_cursor; // "mouse" uniform
  37.     private int u_resolution; // "resolution" uniform
  38.    
  39.     private MyGdxGame game;
  40.    
  41.     public GLSLSandboxPlayer(MyGdxGame game, String shaderPath, float quality) {
  42.         this.game = game;
  43.         batch = new SpriteBatch();
  44.         /*
  45.         cam = new OrthographicCamera();
  46.         cam.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  47.         cam.update();
  48.         */
  49.         batch.setProjectionMatrix(game.cam.combined);
  50.        
  51.         Pixmap pixmap = new Pixmap(1, 1, Format.RGBA8888);
  52.         pixmap.drawPixel(0, 0, Color.rgba8888(Color.WHITE));
  53.         tex1x1 = new Texture(pixmap);
  54.         pixmap.dispose();
  55.        
  56.         shader = loadShader(shaderPath);
  57.  
  58.         width = Gdx.graphics.getWidth() / quality;
  59.         height = Gdx.graphics.getHeight() / quality;
  60.        
  61.         // fetch uniforms:
  62.         u_time = shader.fetchUniformLocation("time", false);
  63.         u_cursor = shader.fetchUniformLocation("mouse", false);
  64.         u_resolution = shader.fetchUniformLocation("resolution", false);
  65.        
  66.         m_fbo = new FrameBuffer(Format.RGBA8888, Math.round(width), Math.round(height), false);
  67.         m_fbo.getColorBufferTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
  68.     }
  69.    
  70.     private void setUniforms() {
  71.         if (u_time >= 0) {
  72.             time += Gdx.graphics.getDeltaTime();
  73.             shader.setUniformf(u_time, time);
  74.         }
  75.         if (u_cursor >= 0) {
  76.             final float cursorX = (float)Gdx.input.getX() / (float)Gdx.graphics.getWidth();
  77.             final float cursorY = 1f - ((float)Gdx.input.getY() / (float)Gdx.graphics.getHeight());
  78.             shader.setUniformf(u_cursor, cursorX, cursorY);
  79.         }
  80.         if (u_resolution >= 0) {
  81.             //***shader.setUniformf(u_resolution, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  82.             shader.setUniformf(u_resolution, width, height);
  83.         }                
  84.     }
  85.    
  86.     public void render() {
  87.         renderToFBO();
  88.  
  89.         batch.begin();
  90.         batch.setShader(null);
  91.         batch.draw(m_fbo.getColorBufferTexture(), 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  92.         batch.end();
  93.     }
  94.    
  95.     private void renderToFBO() {
  96.         m_fbo.begin(); // binds the frame buffer and sets the viewport accordingly, so everything gets drawn to it.
  97.         Gdx.gl.glViewport(0, 0, Math.round(game.renderer.getWidth()), Math.round(game.renderer.getHeight()));
  98.         Gdx.gl.glClearColor(0, 0, 0, 1);
  99.         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  100.         batch.begin();
  101.         batch.setShader(shader);
  102.         setUniforms();
  103.         batch.draw(tex1x1, 0, 0, width, height);
  104.         batch.end();
  105.         m_fbo.end();
  106.     }
  107.  
  108.     private ShaderProgram loadShader(String file) {
  109.         ShaderProgram shader = new ShaderProgram(Gdx.files.internal(file + ".vert"), Gdx.files.internal(file + ".frag"));
  110.         if (!shader.isCompiled()) {
  111.             throw new RuntimeException("Error: shader cannot be compiled: " + shader.getLog());
  112.         }
  113.         return shader;
  114.     }
  115.  
  116. }
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. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top