Advertisement
Guest User

Untitled

a guest
Jun 30th, 2013
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.25 KB | None | 0 0
  1. package com.badlogic.gdx.tests.extensions;
  2.  
  3. import com.badlogic.gdx.ApplicationListener;
  4. import com.badlogic.gdx.Gdx;
  5. import com.badlogic.gdx.graphics.Color;
  6. import com.badlogic.gdx.graphics.GL10;
  7. import com.badlogic.gdx.graphics.OrthographicCamera;
  8. import com.badlogic.gdx.graphics.Pixmap.Format;
  9. import com.badlogic.gdx.graphics.Texture;
  10. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  11. import com.badlogic.gdx.graphics.g2d.TextureRegion;
  12. import com.badlogic.gdx.graphics.glutils.FrameBuffer;
  13. import com.badlogic.gdx.graphics.glutils.ShaderProgram;
  14.  
  15. public class LightingGame implements ApplicationListener {
  16.         private OrthographicCamera camera;
  17.         private SpriteBatch batch;
  18.        
  19.         private Texture light;
  20.         private Texture background;
  21.        
  22.         private FrameBuffer lightMap;
  23.         private TextureRegion fboRegion;
  24.  
  25.         private ShaderProgram shadowShader;
  26.         private ShaderProgram defaultShader;
  27.        
  28.         @Override
  29.         public void create() {
  30.                
  31.                 camera = new OrthographicCamera(1024/2, 300);
  32.                 camera.position.set(1024/4, 300/2, 0);
  33.                 camera.update();
  34.                
  35.                 batch = new SpriteBatch();
  36.                
  37.                 background = new Texture(Gdx.files.internal("data/tiles.png"));
  38.                 light = new Texture(Gdx.files.internal("data/light.png"));
  39.                
  40.                 lightMap = new FrameBuffer(Format.RGBA8888, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
  41.                
  42.                 fboRegion = new TextureRegion(lightMap.getColorBufferTexture(), 0, 0, lightMap.getWidth(), lightMap.getHeight());
  43.                 fboRegion.flip(false, true);
  44.                
  45.                 setupShaders();
  46.         }
  47.        
  48.         public void setupShaders(){
  49.                 defaultShader = SpriteBatch.createDefaultShader();
  50.                
  51.                 String vertexShader = "attribute vec4 " + ShaderProgram.POSITION_ATTRIBUTE + ";\n" //
  52.                                 + "attribute vec4 " + ShaderProgram.COLOR_ATTRIBUTE + ";\n" //
  53.                                 + "attribute vec2 " + ShaderProgram.TEXCOORD_ATTRIBUTE + "0;\n" //
  54.                                 + "uniform mat4 u_projTrans;\n" //
  55.                                 + "varying vec4 v_color;\n" //
  56.                                 + "varying vec2 v_texCoords;\n" //
  57.                                 + "\n" //
  58.                                 + "void main()\n" //
  59.                                 + "{\n" //
  60.                                 + "   v_color = " + ShaderProgram.COLOR_ATTRIBUTE + ";\n" //
  61.                                 + "   v_texCoords = " + ShaderProgram.TEXCOORD_ATTRIBUTE + "0;\n" //
  62.                                 + "   gl_Position =  u_projTrans * " + ShaderProgram.POSITION_ATTRIBUTE + ";\n"
  63.                                 + "}\n";
  64.                 String fragmentShader = "#ifdef GL_ES\n" //
  65.                                 + "#define LOWP lowp\n" //
  66.                                 + "precision mediump float;\n" //
  67.                                 + "#else\n" //
  68.                                 + "#define LOWP \n" //
  69.                                 + "#endif\n" //
  70.                                 + "varying LOWP vec4 v_color;\n" //
  71.                                 + "varying vec2 v_texCoords;\n" //
  72.                                 + "uniform sampler2D u_texture;\n"
  73.                                 + "uniform sampler2D u_texture1;\n"
  74.                                 + "uniform vec2 lightMapResolution;\n"
  75.                                 + "void main()\n"//
  76.                                 + "{\n" //
  77.                                 + "  vec4 texColor0 = texture2D(u_texture, v_texCoords); \n"
  78.                                 + "  vec4 texColor1 = texture2D(u_texture1, gl_FragCoord.xy / lightMapResolution); \n"
  79.                                 + "  vec4 shadow = texColor0 * vec4(0.5, 0.5, 0.5, 1.0);\n"
  80.                                 + "  vec4 light = texColor0;\n"
  81.                                 + "  gl_FragColor = mix(shadow, light, texColor1.a);"
  82.                                 + "}";
  83.  
  84.                 shadowShader = new ShaderProgram(vertexShader, fragmentShader);
  85.                 if (!shadowShader.isCompiled()) {
  86.                         System.err.println(shadowShader.getLog());
  87.                         System.exit(0);
  88.                 }
  89.                 if (shadowShader.getLog().length()!=0)
  90.                         System.out.println(shadowShader.getLog());
  91.         }
  92.  
  93.         @Override
  94.         public void dispose() {
  95.                 batch.dispose();
  96.         }
  97.  
  98.         @Override
  99.         public void render() {        
  100.                 // Draw lightMap
  101.                
  102.                
  103.                 lightMap.begin();
  104.                
  105.                 Gdx.gl.glClearColor(0, 0, 0, 0.0f);
  106.                 Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
  107.  
  108.                 batch.setShader(defaultShader);
  109.                 batch.begin();
  110.                 batch.draw(light, Gdx.input.getX()-light.getWidth()/2f, Gdx.graphics.getHeight()-Gdx.input.getY()-light.getHeight()/2f);
  111.                 batch.end();
  112.                
  113.                 batch.flush();
  114.                
  115.                 lightMap.end();
  116.                
  117.                 // Draw normal stuff
  118.                 Gdx.gl.glClearColor(0, 0, 0, 0);
  119.                 Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
  120.                
  121.                 batch.setProjectionMatrix(camera.combined);
  122.                
  123.                 batch.setShader(shadowShader);
  124.                 batch.begin();
  125.                 shadowShader.setUniformf("lightMapResolution", lightMap.getWidth(), lightMap.getHeight());
  126.                 shadowShader.setUniformi("u_texture1", 1);
  127.                
  128.                 lightMap.getColorBufferTexture().bind(1);
  129.                 background.bind(0);
  130.                
  131.                 batch.draw(background, 0, 0);
  132.        
  133.                 batch.end();
  134.         }
  135.  
  136.         @Override
  137.         public void resize(int width, int height) {
  138.                 camera.setToOrtho(false, width, height);
  139.                
  140.         }
  141.  
  142.         @Override
  143.         public void pause() {
  144.         }
  145.  
  146.         @Override
  147.         public void resume() {
  148.         }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement