Advertisement
Guest User

Untitled

a guest
Oct 10th, 2012
532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.24 KB | None | 0 0
  1. package atmos;
  2.  
  3. import com.badlogic.gdx.ApplicationListener;
  4. import com.badlogic.gdx.Gdx;
  5. import com.badlogic.gdx.Input.Keys;
  6. import com.badlogic.gdx.InputAdapter;
  7. import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
  8. import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
  9. import com.badlogic.gdx.graphics.GL10;
  10. import com.badlogic.gdx.graphics.OrthographicCamera;
  11. import com.badlogic.gdx.graphics.Texture;
  12. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  13. import com.badlogic.gdx.graphics.g2d.TextureRegion;
  14. import com.badlogic.gdx.graphics.glutils.ShaderProgram;
  15. import com.badlogic.gdx.math.Matrix4;
  16. import com.badlogic.gdx.math.Vector2;
  17. import com.badlogic.gdx.math.Vector3;
  18.  
  19. /**
  20.  * Simple illumination model with shaders in LibGDX.
  21.  * @author davedes / mattdesl
  22.  */
  23. public class Illumination2D implements ApplicationListener {   
  24.  
  25.     Texture texture, texture_n;
  26.     OrthographicCamera cam;
  27.     SpriteBatch fxBatch, batch;
  28.     TextureRegion sprite, spriteNormals;
  29.     Matrix4 transform = new Matrix4();
  30.  
  31.     // the color of our light
  32.     Vector3 lightColor = new Vector3(1f, 1f, 1f);
  33.     // the position of our light in 3D space
  34.     Vector3 lightPos = new Vector3(0f, 0f, 2f);
  35.     // the ambient color (color to use when unlit)
  36.     Vector3 ambientColor = new Vector3(1f, 1f, 1f);
  37.     // the resolution of our game/graphics
  38.     Vector2 resolution = new Vector2();
  39.     // the attenuation factor: x=constant, y=linear, z=quadratic
  40.     Vector3 attenuation = new Vector3(0.75f, 0.0f, 0.00001f);
  41.     // the ambient intensity (brightness to use when unlit)
  42.     float ambientIntensity = 0.1f;
  43.  
  44.     // whether to use attenuation/shadows
  45.     boolean useShadow = true;
  46.  
  47.     // whether to use lambert shading (with our normal map)
  48.     boolean useNormals = true;
  49.  
  50.     ShaderProgram program;
  51.  
  52.     public void create() {
  53.         // load our textures
  54.         texture = new Texture(Gdx.files.internal("data/rock.png"));
  55.         texture_n = new Texture(Gdx.files.internal("data/rock_n.png"));
  56.  
  57.         // we'll use texture regions instead of Stage2D/Actors for simplicity's sake
  58.         sprite = new TextureRegion(texture);
  59.         sprite.flip(false, true);
  60.         spriteNormals = new TextureRegion(texture_n);
  61.         spriteNormals.flip(false, true);
  62.  
  63.         // a simple 2D orthographic camera
  64.         cam = new OrthographicCamera(Gdx.graphics.getWidth(),
  65.                 Gdx.graphics.getHeight());
  66.         cam.setToOrtho(true);
  67.  
  68.         // create our shader program...
  69.         program = createShader();
  70.  
  71.         // now we create our sprite batch for our shader
  72.         fxBatch = new SpriteBatch(100, program);
  73.         // setShader is needed; perhaps this is a LibGDX bug?
  74.         fxBatch.setShader(program);
  75.         fxBatch.setProjectionMatrix(cam.combined);
  76.         fxBatch.setTransformMatrix(transform);
  77.  
  78.         // usually we would just use a single batch for our application,
  79.         // but for demonstration let's also show the un-affected image
  80.         batch = new SpriteBatch(100);
  81.         batch.setProjectionMatrix(cam.combined);
  82.         batch.setTransformMatrix(transform);
  83.  
  84.         // quick little input for debugging -- press S to toggle shadows, N to
  85.         // toggle normals
  86.         Gdx.input.setInputProcessor(new InputAdapter() {
  87.             public boolean keyDown(int key) {
  88.                 if (key == Keys.S) {
  89.                     useShadow = !useShadow;
  90.                     return true;
  91.                 } else if (key == Keys.N) {
  92.                     useNormals = !useNormals;
  93.                     return true;
  94.                 }
  95.                 return false;
  96.             }
  97.         });
  98.     }
  99.  
  100.     private ShaderProgram createShader() {
  101.         // see the code here: http://pastebin.com/7fkh1ax8
  102.         // simple illumination model using ambient, diffuse (lambert) and attenuation
  103.         // see here: http://nccastaff.bournemouth.ac.uk/jmacey/CGF/slides/IlluminationModels4up.pdf
  104.         String vert = "attribute vec4 " + ShaderProgram.POSITION_ATTRIBUTE + ";\n" //
  105.                 + "attribute vec4 " + ShaderProgram.COLOR_ATTRIBUTE + ";\n" //
  106.                 + "attribute vec2 " + ShaderProgram.TEXCOORD_ATTRIBUTE + "0;\n" //
  107.                 + "uniform mat4 u_proj;\n" //
  108.                 + "uniform mat4 u_trans;\n" //
  109.                 + "uniform mat4 u_projTrans;\n" //
  110.                 + "varying vec4 v_color;\n" //
  111.                 + "varying vec2 v_texCoords;\n" //
  112.                 + "\n" //
  113.                 + "void main()\n" //
  114.                 + "{\n" //
  115.                 + "   v_color = " + ShaderProgram.COLOR_ATTRIBUTE + ";\n" //
  116.                 + "   v_texCoords = " + ShaderProgram.TEXCOORD_ATTRIBUTE + "0;\n" //
  117.                 + "   gl_Position =  u_projTrans * " + ShaderProgram.POSITION_ATTRIBUTE + ";\n" //
  118.                 + "}\n";
  119.        
  120.         String frag = "#ifdef GL_ES\n" +
  121.                 "precision mediump float;\n" +
  122.                 "#endif\n" +
  123.                 "varying vec4 v_color;\n" +
  124.                 "varying vec2 v_texCoords;\n" +
  125.                
  126.                 "uniform sampler2D u_texture;\n" +
  127.                 "uniform sampler2D u_normals;\n" +
  128.                 "uniform vec3 light;\n" +
  129.                 "uniform vec3 ambientColor;\n" +
  130.                 "uniform float ambientIntensity; \n" +
  131.                 "uniform vec2 resolution;\n" +
  132.                 "uniform vec3 lightColor;\n" +
  133.                 "uniform bool useNormals;\n" +
  134.                 "uniform bool useShadow;\n" +
  135.                 "uniform vec3 attenuation;\n" +
  136.                 "\n" +
  137.                 "void main() {\n" +
  138.                 "   vec4 color = texture2D(u_texture, v_texCoords.st);\n" +
  139.                 "   vec3 normal = normalize(texture2D(u_normals, v_texCoords.st).rgb * 2.0 - 1.0);\n" +
  140.                 "   vec3 light_pos = normalize(light);\n" +
  141.                 "   float lambert = useNormals ? max(dot(normal, light_pos), 0.0) : 1.0;\n" +
  142.                 "   \n" +
  143.                 "   //now let's get a nice little falloff\n" +
  144.                 "   float d = distance(gl_FragCoord.xy, light.xy * resolution);\n" +
  145.                 "   d *= light.z;\n" +
  146.                 "   \n" +
  147.                 "   float att = useShadow ? 1.0 / ( attenuation.x + (attenuation.y*d) + (attenuation.z*d*d) ) : 1.0;\n" +
  148.                 "   \n" +
  149.                 "   vec3 result = (ambientColor * ambientIntensity) + (lightColor.rgb * lambert) * att;\n" +
  150.                 "   result *= color.rgb;\n" +
  151.                 "   \n" +
  152.                 "   gl_FragColor = v_color * vec4(result, color.a);\n" +
  153.                 "}";
  154.        
  155.         ShaderProgram program = new ShaderProgram(vert, frag);
  156.         // u_proj and u_trans will not be active but SpriteBatch will still try to set them...
  157.         program.pedantic = false;
  158.         if (program.isCompiled() == false)
  159.             throw new IllegalArgumentException("couldn't compile shader: "
  160.                     + program.getLog());
  161.  
  162.         // set resolution vector
  163.         resolution.set(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  164.  
  165.         // we are only using this many uniforms for testing purposes...!!
  166.         program.begin();
  167.         program.setUniformi("u_texture", 0);
  168.         program.setUniformi("u_normals", 1);
  169.         program.setUniformf("light", lightPos);
  170.         program.setUniformf("ambientIntensity", ambientIntensity);
  171.         program.setUniformf("ambientColor", ambientColor);
  172.         program.setUniformf("resolution", resolution);
  173.         program.setUniformf("lightColor", lightColor);
  174.         program.setUniformf("attenuation", attenuation);
  175.         program.setUniformi("useShadow", useShadow ? 1 : 0);
  176.         program.setUniformi("useNormals", useNormals ? 1 : 0);
  177.         program.end();
  178.  
  179.         return program;
  180.     }
  181.  
  182.     public void dispose() {
  183.         fxBatch.dispose();
  184.         batch.dispose();
  185.         texture.dispose();
  186.         texture_n.dispose();
  187.     }
  188.  
  189.     public void render() {
  190.         Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
  191.         cam.update();
  192.  
  193.         // draw our sprites without any effects
  194.         batch.begin();
  195.         batch.draw(sprite, 0, 0);
  196.         batch.draw(spriteNormals, sprite.getRegionWidth() + 10, 0);
  197.         batch.end();
  198.  
  199.         // start our FX batch, which will bind our shader program
  200.         fxBatch.begin();
  201.  
  202.         // get y-down light position based on mouse/touch
  203.         lightPos.x = Gdx.input.getX() / (float) Gdx.graphics.getWidth();
  204.         lightPos.y = (resolution.y - Gdx.input.getY() - 1)
  205.                 / (float) Gdx.graphics.getHeight();
  206.  
  207.         // update our uniforms
  208.         program.setUniformf("light", lightPos);
  209.         program.setUniformi("useNormals", useNormals ? 1 : 0);
  210.         program.setUniformi("useShadow", useShadow ? 1 : 0);
  211.  
  212.         // bind the normal first at texture1
  213.         texture_n.bind(1);
  214.  
  215.         // bind the actual texture at texture0
  216.         texture.bind(0);
  217.  
  218.         // we bind texture0 second since draw(sprite) will end up binding it at
  219.         // texture0...
  220.         fxBatch.draw(sprite, 0, sprite.getRegionHeight() + 10);
  221.         fxBatch.end();
  222.     }
  223.  
  224.     public void resize(int width, int height) {
  225.         cam.setToOrtho(true, width, height);
  226.         resolution.set(width, height);
  227.         program.setUniformf("resolution", resolution);
  228.     }
  229.  
  230.     public void pause() {
  231.     }
  232.  
  233.     public void resume() {
  234.     }
  235.  
  236.     public static void main(String[] args) {
  237.         LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
  238.         cfg.title = "lighting";
  239.         cfg.useGL20 = true;
  240.         cfg.width = 1024;
  241.         cfg.height = 768;
  242.  
  243.         new LwjglApplication(new Illumination2D(), cfg);
  244.     }
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement