Advertisement
Guest User

Untitled

a guest
Oct 27th, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.55 KB | None | 0 0
  1. package com.test;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. import com.badlogic.gdx.ApplicationListener;
  7. import com.badlogic.gdx.Gdx;
  8. import com.badlogic.gdx.Input;
  9. import com.badlogic.gdx.InputProcessor;
  10. import com.badlogic.gdx.graphics.GL20;
  11. import com.badlogic.gdx.graphics.Mesh;
  12. import com.badlogic.gdx.graphics.OrthographicCamera;
  13. import com.badlogic.gdx.graphics.VertexAttribute;
  14. import com.badlogic.gdx.graphics.glutils.ShaderProgram;
  15. import com.badlogic.gdx.math.Intersector;
  16. import com.badlogic.gdx.math.Matrix4;
  17. import com.badlogic.gdx.math.Vector3;
  18. import com.badlogic.gdx.math.collision.Ray;
  19.  
  20. public class TerrainTest implements ApplicationListener, InputProcessor {
  21.     private static final int NUM_COLUMNS = 2;
  22.     private static final int NUM_LINES = 2;
  23.     private static final int VERTEX_SIZE = 7;
  24.  
  25.     private OrthographicCamera cam;
  26.     private ShaderProgram shader;
  27.     private Mesh mesh;
  28.  
  29.     private final Matrix4 matrix = new Matrix4();
  30.  
  31.     @Override
  32.     public void create() {
  33.         cam = new OrthographicCamera(10, 9);
  34.         cam.position.set(0, 5.35f, 2f);
  35.         cam.lookAt(0, 0, 0);
  36.         cam.near = 0.5f;
  37.         cam.far = 12f;
  38.  
  39.         cam.update();
  40.  
  41.         String vertexShader = "attribute vec4 a_position;  attribute vec4 a_color;  \n"
  42.                 + "attribute vec2 a_texCoord;   \n"
  43.                 + "varying vec2 v_texCoord;  varying vec4 v_color; varying vec4 v_position;  uniform mat4 u_projectionViewMatrix;   \n"
  44.                 + "void main()                  \n"
  45.                 + "{                            \n"
  46.                 + "   gl_Position = a_position * u_projectionViewMatrix; \n"
  47.                 + "   v_texCoord = a_texCoord;"
  48.                 + "v_color = a_color; v_position = a_position; \n"
  49.                 + "}                            \n";
  50.  
  51.         String fragmentShader = "#ifdef GL_ES\n"
  52.                 + "precision mediump float;\n"
  53.                 + "#endif\n"
  54.                 + "varying vec4 v_color; varying vec4 v_position; void main() "
  55.                 + "{"
  56.                 + "    gl_FragColor = vec4(v_color.x*abs(v_position.x),v_color.y,v_color.z,v_color.w);"
  57.                 + "}";
  58.  
  59.         shader = new ShaderProgram(vertexShader, fragmentShader);
  60.  
  61.         mesh = new Mesh(true, NUM_COLUMNS * NUM_LINES, (NUM_COLUMNS * 6 - 6)
  62.                 * (NUM_LINES - 1), VertexAttribute.Position(),
  63.                 VertexAttribute.ColorUnpacked());
  64.         mesh.setVertices(new float[] {
  65.                 0, 0, 0, 0, 1, 0, 1,
  66.                 1, 0, 0, 0, 1, 0, 1,
  67.                 0, 0, 1, 0, 1, 0, 1,
  68.                 1, 0, 1, 0, 1, 0, 1 });
  69.         mesh.setIndices(new short[] { 2, 0, 1, 2, 3, 1 });
  70.  
  71.         Gdx.input.setInputProcessor(this);
  72.     }
  73.  
  74.     @Override
  75.     public void resume() {
  76.     }
  77.  
  78.     public void render() {
  79.         Gdx.gl20.glViewport(0, 0, Gdx.graphics.getWidth(),
  80.                 Gdx.graphics.getHeight());
  81.         Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
  82.  
  83.         mesh.transform(matrix);
  84.        
  85.         shader.begin();
  86.         shader.setUniformMatrix("u_projectionViewMatrix", cam.combined);
  87.         mesh.render(shader, GL20.GL_LINE_STRIP);
  88.         shader.end();
  89.  
  90.         cam.update();
  91.     }
  92.  
  93.     @Override
  94.     public void dispose() {
  95.         shader.dispose();
  96.         mesh.dispose();
  97.     }
  98.  
  99.     @Override
  100.     public void resize(int width, int height) {
  101.     }
  102.  
  103.     @Override
  104.     public void pause() {
  105.     }
  106.  
  107.     @Override
  108.     public boolean keyDown(int keycode) {
  109.         if (keycode == Input.Keys.UP) {
  110.             addMatrixTranslation(Input.Keys.UP, new Vector3(0, 0, 0.1f));
  111.         } else if (keycode == Input.Keys.DOWN) {
  112.             addMatrixTranslation(Input.Keys.DOWN, new Vector3(0, 0, -0.1f));
  113.         } else if (keycode == Input.Keys.LEFT) {
  114.             addMatrixTranslation(Input.Keys.LEFT, new Vector3(0.1f, 0, 0));
  115.         } else if (keycode == Input.Keys.RIGHT) {
  116.             addMatrixTranslation(Input.Keys.RIGHT, new Vector3(-0.1f, 0, 0));
  117.         }
  118.         return false;
  119.     }
  120.  
  121.     @Override
  122.     public boolean keyUp(int keycode) {
  123.         if (keycode == Input.Keys.UP) {
  124.             removeMatrixTranslation(Input.Keys.UP);
  125.         } else if (keycode == Input.Keys.DOWN) {
  126.             removeMatrixTranslation(Input.Keys.DOWN);
  127.         } else if (keycode == Input.Keys.LEFT) {
  128.             removeMatrixTranslation(Input.Keys.LEFT);
  129.         } else if (keycode == Input.Keys.RIGHT) {
  130.             removeMatrixTranslation(Input.Keys.RIGHT);
  131.         }
  132.  
  133.         return false;
  134.     }
  135.  
  136.     Map<Integer, Vector3> translations = new HashMap<Integer, Vector3>();
  137.  
  138.     private void addMatrixTranslation(int key, Vector3 translation) {
  139.         translations.put(key, translation);
  140.         matrix.translate(translation);
  141.     }
  142.  
  143.     private void removeMatrixTranslation(int key) {
  144.         translations.remove(key);
  145.         matrix.idt();
  146.         for (Vector3 translation : translations.values()) {
  147.             matrix.translate(translation);
  148.         }
  149.     }
  150.  
  151.     @Override
  152.     public boolean keyTyped(char character) {
  153.         return false;
  154.     }
  155.  
  156.     int previousMouseX;
  157.     int previousMouseY;
  158.  
  159.     @Override
  160.     public boolean touchDown(int screenX, int screenY, int pointer, int button) {
  161.         Ray ray = cam.getPickRay(screenX, screenY);
  162.         Vector3 intersection = new Vector3();
  163.         float[] v = new float[NUM_COLUMNS * NUM_LINES * VERTEX_SIZE];
  164.         short[] i = new short[(NUM_COLUMNS * 6 - 6) * (NUM_LINES - 1)];
  165.         mesh.getIndices(i);
  166.         if (Intersector.intersectRayTriangles(ray, mesh.getVertices(v), i,
  167.                 VERTEX_SIZE, intersection)) {
  168.             System.out.println(intersection);
  169.         }
  170.         return false;
  171.     }
  172.  
  173.     @Override
  174.     public boolean touchUp(int screenX, int screenY, int pointer, int button) {
  175.         matrix.idt();
  176.         return false;
  177.     }
  178.  
  179.     @Override
  180.     public boolean touchDragged(int screenX, int screenY, int pointer) {
  181.         if ((screenX - previousMouseX) > 0) {
  182.             matrix.rotate(0, 1, 0, 0.1f);
  183.         } else {
  184.             matrix.rotate(0, -1, 0, 0.1f);
  185.         }
  186.  
  187.         previousMouseX = screenX;
  188.         previousMouseY = screenY;
  189.  
  190.         return false;
  191.     }
  192.  
  193.     @Override
  194.     public boolean mouseMoved(int screenX, int screenY) {
  195.         return false;
  196.     }
  197.  
  198.     @Override
  199.     public boolean scrolled(int amount) {
  200.         cam.position.y += (0.05 * amount);
  201.         return false;
  202.     }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement