Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ar.com.pellegrini.franco.desktop.scenarios.SPlines;
- import com.badlogic.gdx.ApplicationListener;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.Input;
- import com.badlogic.gdx.graphics.Color;
- import com.badlogic.gdx.graphics.GL30;
- import com.badlogic.gdx.graphics.OrthographicCamera;
- import com.badlogic.gdx.graphics.Texture;
- import com.badlogic.gdx.graphics.g2d.BitmapFont;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
- import com.badlogic.gdx.math.Matrix4;
- import com.badlogic.gdx.math.Vector2;
- import com.badlogic.gdx.utils.Array;
- /**
- * A port of ShaderLesson3 from lwjgl-basics to LibGDX: https://github.com/mattdesl/lwjgl-basics/wiki/ShaderLesson3
- *
- * @author davedes
- */
- public
- class CatmullBasicTest
- implements ApplicationListener {
- SpriteBatch batch;
- OrthographicCamera cam;
- Texture background;
- private Matrix4 defaultOrtho2D;
- private BitmapFont font;
- private ShapeRenderer shapeRenderer;
- private Array<Vector2> points;
- @Override
- public
- void create() {
- // the texture does not matter since we will ignore it anyways
- background = new Texture(Gdx.files.internal("sprites/test/sc_map.png"));
- batch = new SpriteBatch(1000);
- shapeRenderer = new ShapeRenderer();
- cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
- cam.setToOrtho(false);
- defaultOrtho2D = new Matrix4();
- defaultOrtho2D.setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
- font = new BitmapFont();
- font.setColor(Color.GREEN);
- points = new Array<>();
- points.add(new Vector2(100,100));
- points.add(new Vector2(700,300));
- points.add(new Vector2(700,500));
- points.add(new Vector2(500,500));
- points.add(new Vector2(400,500));
- }
- int k;
- Vector2 p1 = new Vector2();
- Vector2 p2 = new Vector2();
- Vector2 pTemp = new Vector2();
- @Override
- public
- void dispose() {
- batch.dispose();
- background.dispose();
- }
- @Override
- public
- void pause() {
- }
- /**
- * Calculates the catmullrom value for the given position (t).
- *
- * @param out The Vector to set to the result.
- * @param t The position (0<=t<=1) on the spline
- * @param points The control points
- * @param tmp A temporary vector used for the calculation
- * @return The value of out
- */
- public static
- Vector2 calculateCatmullrom(
- final Vector2 out,
- final float t,
- final Array<Vector2> points,
- final Vector2 tmp
- ) {
- final int n = points.size;
- float u = t * n;
- int i = (int) u;
- u -= i;
- return calculateCatmullrom(out, i, u, points, tmp);
- }
- /**
- * Calculates the catmullrom value for the given span (i) at the given position (u).
- *
- * @param out The Vector to set to the result.
- * @param i The span (0<=i<spanCount) spanCount = continuous ? points.length : points.length - degree
- * @param u The position (0<=u<=1) on the span
- * @param points The control points
- * @param tmp A temporary vector used for the calculation
- * @return The value of out
- */
- public static
- Vector2 calculateCatmullrom(
- final Vector2 out,
- final int i,
- final float u,
- final Array<Vector2> points,
- final Vector2 tmp
- ) {
- final int n = points.size;
- final float u2 = u * u;
- final float u3 = u2 * u;
- out.set(points.get(i)).scl(1.5f * u3 - 2.5f * u2 + 1.0f);
- if (i > 0) {
- out.add(tmp.set(points.get((n + i - 1) % n)).scl(-0.5f * u3 + u2 - 0.5f * u));
- }
- if (i < (n - 1)) {
- out.add(tmp.set(points.get((i + 1) % n)).scl(-1.5f * u3 + 2f * u2 + 0.5f * u));
- }
- if (i < (n - 2)) {
- out.add(tmp.set(points.get((i + 2) % n)).scl(0.5f * u3 - 0.5f * u2));
- }
- return out;
- }
- @Override
- public
- void render() {
- if (Gdx.input.isKeyPressed(Input.Keys.E)) {
- cam.zoom += 0.01f;
- }
- if (Gdx.input.isKeyPressed(Input.Keys.Q)) {
- cam.zoom -= 0.01f;
- }
- if (Gdx.input.isKeyPressed(Input.Keys.A)) {
- cam.translate(-10f, 0);
- }
- if (Gdx.input.isKeyPressed(Input.Keys.S)) {
- cam.translate(0f, -10f);
- }
- if (Gdx.input.isKeyPressed(Input.Keys.D)) {
- cam.translate(10f, 0f);
- }
- if (Gdx.input.isKeyPressed(Input.Keys.W)) {
- cam.translate(0f, 10f);
- }
- // BACKGROUND
- Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
- cam.update();
- batch.setProjectionMatrix(cam.combined);
- batch.begin();
- batch.disableBlending();
- batch.draw(background, 0, 0);
- batch.enableBlending();
- batch.end();
- shapeRenderer.setProjectionMatrix(cam.combined);
- shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
- shapeRenderer.setColor(Color.GREEN);
- k = 100;
- for(int i = 0; i < k-2; ++i){
- calculateCatmullrom(p1, ((float)i)/((float)k-1),points, pTemp);
- calculateCatmullrom(p2, ((float)i+1)/((float)k-1),points, pTemp);
- shapeRenderer.line(p1.x,p1.y,p2.x,p2.y);
- }
- shapeRenderer.end();
- //CODE HERE
- //draw FPS
- batch.begin();
- batch.setProjectionMatrix(defaultOrtho2D);
- font.draw(batch, String.valueOf(Gdx.graphics.getFramesPerSecond()), 5, Gdx.graphics.getHeight() - 5);
- batch.end();
- }
- @Override
- public
- void resize(
- int width,
- int height
- ) {
- cam.setToOrtho(false, width, height);
- }
- @Override
- public
- void resume() {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement