Advertisement
francogp

Untitled

Aug 15th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.05 KB | None | 0 0
  1. package ar.com.pellegrini.franco.desktop.scenarios.SPlines;
  2.  
  3. import com.badlogic.gdx.ApplicationListener;
  4. import com.badlogic.gdx.Gdx;
  5. import com.badlogic.gdx.Input;
  6. import com.badlogic.gdx.graphics.Color;
  7. import com.badlogic.gdx.graphics.GL30;
  8. import com.badlogic.gdx.graphics.OrthographicCamera;
  9. import com.badlogic.gdx.graphics.Texture;
  10. import com.badlogic.gdx.graphics.g2d.BitmapFont;
  11. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  12. import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
  13. import com.badlogic.gdx.math.Matrix4;
  14. import com.badlogic.gdx.math.Vector2;
  15. import com.badlogic.gdx.utils.Array;
  16.  
  17. /**
  18.  * A port of ShaderLesson3 from lwjgl-basics to LibGDX: https://github.com/mattdesl/lwjgl-basics/wiki/ShaderLesson3
  19.  *
  20.  * @author davedes
  21.  */
  22. public
  23. class CatmullBasicTest
  24.         implements ApplicationListener {
  25.  
  26.     SpriteBatch        batch;
  27.     OrthographicCamera cam;
  28.     Texture            background;
  29.     private Matrix4    defaultOrtho2D;
  30.     private BitmapFont font;
  31.     private ShapeRenderer shapeRenderer;
  32.     private Array<Vector2> points;
  33.  
  34.     @Override
  35.     public
  36.     void create() {
  37.         // the texture does not matter since we will ignore it anyways
  38.         background = new Texture(Gdx.files.internal("sprites/test/sc_map.png"));
  39.  
  40.         batch = new SpriteBatch(1000);
  41.         shapeRenderer = new ShapeRenderer();
  42.  
  43.         cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  44.         cam.setToOrtho(false);
  45.  
  46.         defaultOrtho2D = new Matrix4();
  47.         defaultOrtho2D.setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  48.  
  49.         font = new BitmapFont();
  50.         font.setColor(Color.GREEN);
  51.  
  52.         points = new Array<>();
  53.         points.add(new Vector2(100,100));
  54.         points.add(new Vector2(700,300));
  55.         points.add(new Vector2(700,500));
  56.         points.add(new Vector2(500,500));
  57.         points.add(new Vector2(400,500));
  58.  
  59.     }
  60.  
  61.     int k;
  62.     Vector2 p1 = new Vector2();
  63.     Vector2 p2 = new Vector2();
  64.     Vector2 pTemp = new Vector2();
  65.  
  66.     @Override
  67.     public
  68.     void dispose() {
  69.         batch.dispose();
  70.         background.dispose();
  71.     }
  72.  
  73.     @Override
  74.     public
  75.     void pause() {
  76.  
  77.     }
  78.  
  79.  
  80.     /**
  81.      * Calculates the catmullrom value for the given position (t).
  82.      *
  83.      * @param out    The Vector to set to the result.
  84.      * @param t      The position (0<=t<=1) on the spline
  85.      * @param points The control points
  86.      * @param tmp    A temporary vector used for the calculation
  87.      * @return The value of out
  88.      */
  89.     public static
  90.     Vector2 calculateCatmullrom(
  91.             final Vector2 out,
  92.             final float t,
  93.             final Array<Vector2> points,
  94.             final Vector2 tmp
  95.     ) {
  96.         final int n = points.size;
  97.         float     u = t * n;
  98.         int       i = (int) u;
  99.         u -= i;
  100.         return calculateCatmullrom(out, i, u, points, tmp);
  101.     }
  102.  
  103.     /**
  104.      * Calculates the catmullrom value for the given span (i) at the given position (u).
  105.      *
  106.      * @param out    The Vector to set to the result.
  107.      * @param i      The span (0<=i<spanCount) spanCount = continuous ? points.length : points.length - degree
  108.      * @param u      The position (0<=u<=1) on the span
  109.      * @param points The control points
  110.      * @param tmp    A temporary vector used for the calculation
  111.      * @return The value of out
  112.      */
  113.     public static
  114.     Vector2 calculateCatmullrom(
  115.             final Vector2 out,
  116.             final int i,
  117.             final float u,
  118.             final Array<Vector2> points,
  119.             final Vector2 tmp
  120.     ) {
  121.         final int   n  = points.size;
  122.         final float u2 = u * u;
  123.         final float u3 = u2 * u;
  124.         out.set(points.get(i)).scl(1.5f * u3 - 2.5f * u2 + 1.0f);
  125.         if (i > 0) {
  126.             out.add(tmp.set(points.get((n + i - 1) % n)).scl(-0.5f * u3 + u2 - 0.5f * u));
  127.         }
  128.         if (i < (n - 1)) {
  129.             out.add(tmp.set(points.get((i + 1) % n)).scl(-1.5f * u3 + 2f * u2 + 0.5f * u));
  130.         }
  131.         if (i < (n - 2)) {
  132.             out.add(tmp.set(points.get((i + 2) % n)).scl(0.5f * u3 - 0.5f * u2));
  133.         }
  134.         return out;
  135.     }
  136.  
  137.  
  138.     @Override
  139.     public
  140.     void render() {
  141.         if (Gdx.input.isKeyPressed(Input.Keys.E)) {
  142.             cam.zoom += 0.01f;
  143.         }
  144.         if (Gdx.input.isKeyPressed(Input.Keys.Q)) {
  145.             cam.zoom -= 0.01f;
  146.         }
  147.  
  148.         if (Gdx.input.isKeyPressed(Input.Keys.A)) {
  149.             cam.translate(-10f, 0);
  150.         }
  151.         if (Gdx.input.isKeyPressed(Input.Keys.S)) {
  152.             cam.translate(0f, -10f);
  153.         }
  154.         if (Gdx.input.isKeyPressed(Input.Keys.D)) {
  155.             cam.translate(10f, 0f);
  156.         }
  157.         if (Gdx.input.isKeyPressed(Input.Keys.W)) {
  158.             cam.translate(0f, 10f);
  159.         }
  160.  
  161.         // BACKGROUND
  162.         Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
  163.         cam.update();
  164.         batch.setProjectionMatrix(cam.combined);
  165.         batch.begin();
  166.         batch.disableBlending();
  167.         batch.draw(background, 0, 0);
  168.         batch.enableBlending();
  169.         batch.end();
  170.  
  171.  
  172.         shapeRenderer.setProjectionMatrix(cam.combined);
  173.         shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
  174.         shapeRenderer.setColor(Color.GREEN);
  175.         k = 100;
  176.         for(int i = 0; i < k-2; ++i){
  177.             calculateCatmullrom(p1, ((float)i)/((float)k-1),points, pTemp);
  178.             calculateCatmullrom(p2, ((float)i+1)/((float)k-1),points, pTemp);
  179.             shapeRenderer.line(p1.x,p1.y,p2.x,p2.y);
  180.         }
  181.         shapeRenderer.end();
  182.  
  183.         //CODE HERE
  184.  
  185.         //draw FPS
  186.         batch.begin();
  187.         batch.setProjectionMatrix(defaultOrtho2D);
  188.         font.draw(batch, String.valueOf(Gdx.graphics.getFramesPerSecond()), 5, Gdx.graphics.getHeight() - 5);
  189.         batch.end();
  190.     }
  191.  
  192.     @Override
  193.     public
  194.     void resize(
  195.             int width,
  196.             int height
  197.     ) {
  198.         cam.setToOrtho(false, width, height);
  199.     }
  200.  
  201.     @Override
  202.     public
  203.     void resume() {
  204.  
  205.     }
  206.  
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement