Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.test;
- import java.util.HashMap;
- import java.util.Map;
- import com.badlogic.gdx.ApplicationListener;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.Input;
- import com.badlogic.gdx.InputProcessor;
- import com.badlogic.gdx.graphics.GL20;
- import com.badlogic.gdx.graphics.Mesh;
- import com.badlogic.gdx.graphics.OrthographicCamera;
- import com.badlogic.gdx.graphics.VertexAttribute;
- import com.badlogic.gdx.graphics.glutils.ShaderProgram;
- import com.badlogic.gdx.math.Intersector;
- import com.badlogic.gdx.math.Matrix4;
- import com.badlogic.gdx.math.Vector3;
- import com.badlogic.gdx.math.collision.Ray;
- public class TerrainTest implements ApplicationListener, InputProcessor {
- private static final int NUM_COLUMNS = 2;
- private static final int NUM_LINES = 2;
- private static final int VERTEX_SIZE = 7;
- private OrthographicCamera cam;
- private ShaderProgram shader;
- private Mesh mesh;
- private final Matrix4 matrix = new Matrix4();
- @Override
- public void create() {
- cam = new OrthographicCamera(10, 9);
- cam.position.set(0, 5.35f, 2f);
- cam.lookAt(0, 0, 0);
- cam.near = 0.5f;
- cam.far = 12f;
- cam.update();
- String vertexShader = "attribute vec4 a_position; attribute vec4 a_color; \n"
- + "attribute vec2 a_texCoord; \n"
- + "varying vec2 v_texCoord; varying vec4 v_color; varying vec4 v_position; uniform mat4 u_projectionViewMatrix; \n"
- + "void main() \n"
- + "{ \n"
- + " gl_Position = a_position * u_projectionViewMatrix; \n"
- + " v_texCoord = a_texCoord;"
- + "v_color = a_color; v_position = a_position; \n"
- + "} \n";
- String fragmentShader = "#ifdef GL_ES\n"
- + "precision mediump float;\n"
- + "#endif\n"
- + "varying vec4 v_color; varying vec4 v_position; void main() "
- + "{"
- + " gl_FragColor = vec4(v_color.x*abs(v_position.x),v_color.y,v_color.z,v_color.w);"
- + "}";
- shader = new ShaderProgram(vertexShader, fragmentShader);
- mesh = new Mesh(true, NUM_COLUMNS * NUM_LINES, (NUM_COLUMNS * 6 - 6)
- * (NUM_LINES - 1), VertexAttribute.Position(),
- VertexAttribute.ColorUnpacked());
- mesh.setVertices(new float[] {
- 0, 0, 0, 0, 1, 0, 1,
- 1, 0, 0, 0, 1, 0, 1,
- 0, 0, 1, 0, 1, 0, 1,
- 1, 0, 1, 0, 1, 0, 1 });
- mesh.setIndices(new short[] { 2, 0, 1, 2, 3, 1 });
- Gdx.input.setInputProcessor(this);
- }
- @Override
- public void resume() {
- }
- public void render() {
- Gdx.gl20.glViewport(0, 0, Gdx.graphics.getWidth(),
- Gdx.graphics.getHeight());
- Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
- mesh.transform(matrix);
- shader.begin();
- shader.setUniformMatrix("u_projectionViewMatrix", cam.combined);
- mesh.render(shader, GL20.GL_LINE_STRIP);
- shader.end();
- cam.update();
- }
- @Override
- public void dispose() {
- shader.dispose();
- mesh.dispose();
- }
- @Override
- public void resize(int width, int height) {
- }
- @Override
- public void pause() {
- }
- @Override
- public boolean keyDown(int keycode) {
- if (keycode == Input.Keys.UP) {
- addMatrixTranslation(Input.Keys.UP, new Vector3(0, 0, 0.1f));
- } else if (keycode == Input.Keys.DOWN) {
- addMatrixTranslation(Input.Keys.DOWN, new Vector3(0, 0, -0.1f));
- } else if (keycode == Input.Keys.LEFT) {
- addMatrixTranslation(Input.Keys.LEFT, new Vector3(0.1f, 0, 0));
- } else if (keycode == Input.Keys.RIGHT) {
- addMatrixTranslation(Input.Keys.RIGHT, new Vector3(-0.1f, 0, 0));
- }
- return false;
- }
- @Override
- public boolean keyUp(int keycode) {
- if (keycode == Input.Keys.UP) {
- removeMatrixTranslation(Input.Keys.UP);
- } else if (keycode == Input.Keys.DOWN) {
- removeMatrixTranslation(Input.Keys.DOWN);
- } else if (keycode == Input.Keys.LEFT) {
- removeMatrixTranslation(Input.Keys.LEFT);
- } else if (keycode == Input.Keys.RIGHT) {
- removeMatrixTranslation(Input.Keys.RIGHT);
- }
- return false;
- }
- Map<Integer, Vector3> translations = new HashMap<Integer, Vector3>();
- private void addMatrixTranslation(int key, Vector3 translation) {
- translations.put(key, translation);
- matrix.translate(translation);
- }
- private void removeMatrixTranslation(int key) {
- translations.remove(key);
- matrix.idt();
- for (Vector3 translation : translations.values()) {
- matrix.translate(translation);
- }
- }
- @Override
- public boolean keyTyped(char character) {
- return false;
- }
- int previousMouseX;
- int previousMouseY;
- @Override
- public boolean touchDown(int screenX, int screenY, int pointer, int button) {
- Ray ray = cam.getPickRay(screenX, screenY);
- Vector3 intersection = new Vector3();
- float[] v = new float[NUM_COLUMNS * NUM_LINES * VERTEX_SIZE];
- short[] i = new short[(NUM_COLUMNS * 6 - 6) * (NUM_LINES - 1)];
- mesh.getIndices(i);
- if (Intersector.intersectRayTriangles(ray, mesh.getVertices(v), i,
- VERTEX_SIZE, intersection)) {
- System.out.println(intersection);
- }
- return false;
- }
- @Override
- public boolean touchUp(int screenX, int screenY, int pointer, int button) {
- matrix.idt();
- return false;
- }
- @Override
- public boolean touchDragged(int screenX, int screenY, int pointer) {
- if ((screenX - previousMouseX) > 0) {
- matrix.rotate(0, 1, 0, 0.1f);
- } else {
- matrix.rotate(0, -1, 0, 0.1f);
- }
- previousMouseX = screenX;
- previousMouseY = screenY;
- return false;
- }
- @Override
- public boolean mouseMoved(int screenX, int screenY) {
- return false;
- }
- @Override
- public boolean scrolled(int amount) {
- cam.position.y += (0.05 * amount);
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement