Android Open Source - CircleWorldGDX Tilemap Circle View Background Renderer






From Project

Back to project page CircleWorldGDX.

License

The source code is released under:

MIT License

If you think the Android project CircleWorldGDX listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.fdangelo.circleworld.universeview.tilemap;
//  w ww  . j  a va 2s .c om
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.g3d.utils.MeshBuilder;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Disposable;
import com.fdangelo.circleworld.universeengine.tilemap.Planet;
import com.fdangelo.circleworld.universeengine.tilemap.PlanetType;
import com.fdangelo.circleworld.universeengine.tilemap.TilemapCircle;

public class TilemapCircleViewBackgroundRenderer implements Disposable {
  private TilemapCircle tilemapCircle;

  private Vector2[] circleNormals;
  private float[] circleHeights;

  private Mesh mesh;

  static private ShaderProgram shader;
  static private int shaderWorldViewLocation;

  public TilemapCircleViewBackgroundRenderer() {
    if (shader == null) {
      final String vertexShader = "attribute vec4 a_position;\n" + "attribute vec4 a_color;\n" + "uniform mat4 u_projTrans;\n" + "varying vec4 v_color;"
          + "void main()                  \n" + "{                            \n" + "   v_color = a_color; \n"
          + "   gl_Position =  u_projTrans * a_position;  \n" + "}";

      final String fragmentShader = "#ifdef GL_ES\n" + "precision mediump float;\n" + "#endif\n" + "varying vec4 v_color;\n"
          + "void main()                                  \n" + "{                                            \n" + "  gl_FragColor = v_color;\n"
          + "}";

      shader = new ShaderProgram(vertexShader, fragmentShader);
      if (!shader.isCompiled()) {
        throw new IllegalArgumentException("Error compiling shader: " + shader.getLog());
      }

      shaderWorldViewLocation = shader.getUniformLocation("u_projTrans");
    }
  }

  public final void init(final TilemapCircleView tilemapCircleView) {
    // this.tilemapCircleView = tilemapCircleView;
    tilemapCircle = tilemapCircleView.getTilemapCircle();
    circleNormals = tilemapCircle.getCircleNormals();
    circleHeights = tilemapCircle.getCircleHeights();

    initMesh();
  }

  static private Color fromColor = new Color();
  static private Color toColor = new Color();

  static private MeshBuilder meshBuilder;
  static private Vector3 vertexPos = new Vector3();
  static private Color vertexColor = new Color();

  private final void initMesh() {
    fromColor.set(Color.BLUE);
    toColor.set(Color.CYAN);
    toColor.a = 0;

    if (tilemapCircle instanceof Planet) {
      final PlanetType planetType = ((Planet) tilemapCircle).getPlanetType();

      fromColor.set(planetType.backColorFrom);
      toColor.set(planetType.backColorTo);
    }

    if (mesh != null) {
      mesh.dispose();
      mesh = null;
    }

    final int totalTriangles = circleNormals.length;
    final int totalIndices = totalTriangles * 3;
    final int totalVertices = circleNormals.length + 1;

    if (meshBuilder == null) {
      meshBuilder = new MeshBuilder();
      meshBuilder.begin(Usage.Position | Usage.Color);
      meshBuilder.ensureCapacity(totalVertices + 1, totalIndices + 1);
    } else {
      meshBuilder.begin(Usage.Position | Usage.Color);
    }

    final float heavenHeight = circleHeights[circleHeights.length - 1]
        + (circleHeights[circleHeights.length - 1] - circleHeights[circleHeights.length - 2]) * 5.0f; // Repeat
                                                        // the
                                                        // last
                                                        // tile
                                                        // height
                                                        // 5
                                                        // times

    vertexPos.set(0, 0, -10);
    vertexColor.set(fromColor);
    meshBuilder.vertex(vertexPos, null, vertexColor, null);

    vertexColor.set(toColor);
    for (int i = 0; i < circleNormals.length; i++) {
      vertexPos.set(circleNormals[i].x * heavenHeight, circleNormals[i].y * heavenHeight, -10);
      meshBuilder.vertex(vertexPos, null, vertexColor, null);
    }

    short vertexOffset = 1;

    for (int i = 1; i < circleNormals.length; i++) {
      meshBuilder.triangle((short) 0, (short) (vertexOffset - 1), vertexOffset);
      vertexOffset++;
    }

    meshBuilder.triangle((short) 0, (short) (vertexOffset - 1), (short) 1);

    mesh = meshBuilder.end();
  }

  @Override
  public void dispose() {
    if (mesh != null) {
      mesh.dispose();
      mesh = null;
    }
  }

  public final void draw(final Matrix4 matrix) {
    Gdx.gl.glDepthMask(false);
    Gdx.gl.glEnable(GL20.GL_BLEND);
    Gdx.gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
    shader.begin();
    shader.setUniformMatrix(shaderWorldViewLocation, matrix);
    mesh.render(shader, GL10.GL_TRIANGLES);
    shader.end();
  }
}




Java Source Code List

.AssetsUpdater.java
com.fdangelo.circleworld.GameLogicState.java
com.fdangelo.circleworld.GameLogic.java
com.fdangelo.circleworld.MainActivity.java
com.fdangelo.circleworld.Main.java
com.fdangelo.circleworld.MyGdxGame.java
com.fdangelo.circleworld.RobovmLauncher.java
com.fdangelo.circleworld.client.GwtLauncher.java
com.fdangelo.circleworld.gui.AvatarEditControlScreen.java
com.fdangelo.circleworld.gui.AvatarMoveControlScreen.java
com.fdangelo.circleworld.gui.HudScreen.java
com.fdangelo.circleworld.gui.core.Gui.java
com.fdangelo.circleworld.gui.core.ScreenTable.java
com.fdangelo.circleworld.gui.core.Screen.java
com.fdangelo.circleworld.universeengine.IUniverseListener.java
com.fdangelo.circleworld.universeengine.ThingPosition.java
com.fdangelo.circleworld.universeengine.ThingType.java
com.fdangelo.circleworld.universeengine.Thing.java
com.fdangelo.circleworld.universeengine.UniverseFactory.java
com.fdangelo.circleworld.universeengine.UniverseGeneratorDefault.java
com.fdangelo.circleworld.universeengine.UniverseGenerator.java
com.fdangelo.circleworld.universeengine.Universe.java
com.fdangelo.circleworld.universeengine.objects.AvatarInput.java
com.fdangelo.circleworld.universeengine.objects.Avatar.java
com.fdangelo.circleworld.universeengine.objects.FollowParentParameters.java
com.fdangelo.circleworld.universeengine.objects.IUniverseObjectListener.java
com.fdangelo.circleworld.universeengine.objects.ShipInput.java
com.fdangelo.circleworld.universeengine.objects.Ship.java
com.fdangelo.circleworld.universeengine.objects.UniverseObject.java
com.fdangelo.circleworld.universeengine.tilemap.ITilemapCircleListener.java
com.fdangelo.circleworld.universeengine.tilemap.PlanetType.java
com.fdangelo.circleworld.universeengine.tilemap.PlanetTypes.java
com.fdangelo.circleworld.universeengine.tilemap.Planet.java
com.fdangelo.circleworld.universeengine.tilemap.TileDirection.java
com.fdangelo.circleworld.universeengine.tilemap.TileHitFlags.java
com.fdangelo.circleworld.universeengine.tilemap.TileHitInfo.java
com.fdangelo.circleworld.universeengine.tilemap.TileSubtype.java
com.fdangelo.circleworld.universeengine.tilemap.TileType.java
com.fdangelo.circleworld.universeengine.tilemap.TileTypes.java
com.fdangelo.circleworld.universeengine.tilemap.TilemapCircle.java
com.fdangelo.circleworld.universeengine.utils.DataPools.java
com.fdangelo.circleworld.universeengine.utils.PoolByte.java
com.fdangelo.circleworld.universeengine.utils.PoolColor.java
com.fdangelo.circleworld.universeengine.utils.PoolFloat.java
com.fdangelo.circleworld.universeengine.utils.PoolInt.java
com.fdangelo.circleworld.universeengine.utils.PoolVector2.java
com.fdangelo.circleworld.universeengine.utils.PoolVector3.java
com.fdangelo.circleworld.universeengine.utils.UEProfilerSample.java
com.fdangelo.circleworld.universeengine.utils.UEProfiler.java
com.fdangelo.circleworld.universeview.FollowCameraParameters.java
com.fdangelo.circleworld.universeview.UniverseViewCamera.java
com.fdangelo.circleworld.universeview.UniverseViewFactory.java
com.fdangelo.circleworld.universeview.UniverseView.java
com.fdangelo.circleworld.universeview.objects.AvatarInputEditTool.java
com.fdangelo.circleworld.universeview.objects.AvatarInputMode.java
com.fdangelo.circleworld.universeview.objects.AvatarViewInput.java
com.fdangelo.circleworld.universeview.objects.AvatarView.java
com.fdangelo.circleworld.universeview.objects.InputAreas.java
com.fdangelo.circleworld.universeview.objects.ShipInputMode.java
com.fdangelo.circleworld.universeview.objects.ShipViewInput.java
com.fdangelo.circleworld.universeview.objects.ShipView.java
com.fdangelo.circleworld.universeview.objects.UniverseObjectView.java
com.fdangelo.circleworld.universeview.tilemap.PlanetView.java
com.fdangelo.circleworld.universeview.tilemap.TilemapCircleViewBackgroundRenderer.java
com.fdangelo.circleworld.universeview.tilemap.TilemapCircleViewRenderer.java
com.fdangelo.circleworld.universeview.tilemap.TilemapCircleView.java
com.fdangelo.circleworld.utils.Mathf.java
com.fdangelo.circleworld.utils.Vector2I.java