Advertisement
Guest User

Untitled

a guest
Feb 4th, 2013
957
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.84 KB | None | 0 0
  1. import com.badlogic.gdx.ApplicationListener;
  2. import com.badlogic.gdx.Gdx;
  3. import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
  4. import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
  5. import com.badlogic.gdx.graphics.GL10;
  6. import com.badlogic.gdx.graphics.Texture;
  7. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  8. import com.badlogic.gdx.graphics.glutils.ShaderProgram;
  9. import com.badlogic.gdx.scenes.scene2d.Stage;
  10. import com.badlogic.gdx.scenes.scene2d.ui.Image;
  11.  
  12. /**
  13.  * @author davedes
  14.  */
  15. public class GrayscaleTest implements ApplicationListener {
  16.    
  17.     public static void main(String[] args) {
  18.         LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
  19.         cfg.title = "Gray Test";
  20.         cfg.useGL20 = true;
  21.         cfg.width = 640;
  22.         cfg.height = 480;
  23.         cfg.resizable = false;
  24.  
  25.         new LwjglApplication(new GrayscaleTest(), cfg);
  26.     }
  27.    
  28.     final String VERT =  
  29.             "attribute vec4 "+ShaderProgram.POSITION_ATTRIBUTE+";\n" +
  30.             "attribute vec4 "+ShaderProgram.COLOR_ATTRIBUTE+";\n" +
  31.             "attribute vec2 "+ShaderProgram.TEXCOORD_ATTRIBUTE+"0;\n" +
  32.            
  33.             "uniform mat4 u_projTrans;\n" +
  34.             " \n" +
  35.             "varying vec4 vColor;\n" +
  36.             "varying vec2 vTexCoord;\n" +
  37.            
  38.             "void main() {\n" +  
  39.             "   vColor = "+ShaderProgram.COLOR_ATTRIBUTE+";\n" +
  40.             "   vTexCoord = "+ShaderProgram.TEXCOORD_ATTRIBUTE+"0;\n" +
  41.             "   gl_Position =  u_projTrans * " + ShaderProgram.POSITION_ATTRIBUTE + ";\n" +
  42.             "}";
  43.    
  44.     final String FRAG =
  45.             //GL ES specific stuff
  46.               "#ifdef GL_ES\n" //
  47.             + "#define LOWP lowp\n" //
  48.             + "precision mediump float;\n" //
  49.             + "#else\n" //
  50.             + "#define LOWP \n" //
  51.             + "#endif\n" + //
  52.             "varying LOWP vec4 vColor;\n" +
  53.             "varying vec2 vTexCoord;\n" +
  54.             "uniform sampler2D u_texture;\n" +         
  55.             "uniform float grayscale;\n" +
  56.             "void main() {\n" +  
  57.             "   vec4 texColor = texture2D(u_texture, vTexCoord);\n" +
  58.             "   \n" +
  59.             "   float gray = dot(texColor.rgb, vec3(0.299, 0.587, 0.114));\n" +
  60.             "   texColor.rgb = mix(vec3(gray), texColor.rgb, grayscale);\n" +
  61.             "   \n" +
  62.             "   gl_FragColor = texColor * vColor;\n" +
  63.             "}";
  64.    
  65.     SpriteBatch batch;
  66.     Stage stage;
  67.     ShaderProgram shader;
  68.     Texture tex;
  69.     float grayscale = 0f;
  70.     float time;
  71.        
  72.     @Override
  73.     public void create() {
  74.         //important since we aren't using some uniforms and attributes that SpriteBatch expects
  75.         ShaderProgram.pedantic = false;
  76.        
  77.         shader = new ShaderProgram(VERT, FRAG);
  78.        
  79.         //shader didn't compile.. handle it somehow
  80.         if (!shader.isCompiled()) {
  81.             System.err.println(shader.getLog());
  82.             System.exit(0);
  83.         }
  84.        
  85.         //incase there were any warnings/info
  86.         if (shader.getLog().length()!=0)
  87.             System.out.println(shader.getLog());
  88.        
  89.         //create our own sprite batcher which ALL sprites will use
  90.         batch = new SpriteBatch(1000, shader);
  91.         batch.setShader(shader);
  92.        
  93.         //pass the custom batcher to our stage
  94.         stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true, batch);
  95.  
  96.         tex = new Texture(Gdx.files.internal("data/libgdx.png"));
  97.        
  98.         //add entities/UI to stage...
  99.         Image img1 = new Image(tex);
  100.         img1.setPosition(250, 50);
  101.         stage.addActor(img1);
  102.        
  103.         Image img2 = new Image(tex);
  104.         img2.setScale(0.5f);
  105.         stage.addActor(img2);
  106.     }
  107.  
  108.     @Override
  109.     public void resize(int width, int height) {
  110.         stage.setViewport(width, height, true);
  111.     }
  112.  
  113.     @Override
  114.     public void render() {
  115.         Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
  116.        
  117.         time += Gdx.graphics.getDeltaTime();       
  118.         grayscale = (float)Math.sin(time)/2f+0.5f;
  119.        
  120.         shader.begin();
  121.         shader.setUniformf("grayscale", grayscale);
  122.         shader.end();
  123.        
  124.         stage.act(Gdx.graphics.getDeltaTime());
  125.         stage.draw();
  126.     }
  127.  
  128.     @Override
  129.     public void pause() {
  130.        
  131.     }
  132.  
  133.     @Override
  134.     public void resume() {
  135.        
  136.     }
  137.  
  138.     @Override
  139.     public void dispose() {
  140.         batch.dispose();
  141.         shader.dispose();
  142.         tex.dispose();
  143.         stage.dispose();
  144.     }  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement