Skip to content

Commit

Permalink
-Add utility to render outlines of shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
narfman0 committed Dec 2, 2014
1 parent 00d6e23 commit a6303dd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/com/blastedstudios/gdxworld/util/MaskTextureStruct.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.blastedstudios.gdxworld.world.shape.GDXPolygon;

public class MaskTextureStruct {
private final static String FRAGMENT_SHADER =
Expand Down Expand Up @@ -32,10 +33,12 @@ public class MaskTextureStruct {
static final ShaderProgram SHADER = new ShaderProgram(VERTEX_SHADER, FRAGMENT_SHADER);
public final Mesh mesh;
public final Texture texture;
public final GDXPolygon polygon;

public MaskTextureStruct(Mesh mesh, Texture texture){
public MaskTextureStruct(Mesh mesh, Texture texture, GDXPolygon polygon){
this.mesh = mesh;
this.texture = texture;
this.polygon = polygon;
}

public void render(Camera camera){
Expand Down
20 changes: 19 additions & 1 deletion src/com/blastedstudios/gdxworld/util/TiledMeshRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import java.util.List;

import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Vector2;
import com.blastedstudios.gdxworld.math.EarClippingTriangulator;
import com.blastedstudios.gdxworld.ui.GDXRenderer;
Expand All @@ -17,6 +20,7 @@
public class TiledMeshRenderer {
private final LinkedList<MaskTextureStruct> maskTextures = new LinkedList<>();
private final static float UV_SCALE = Properties.getFloat("tiled.uv.scale", 10f);
private final static ShapeRenderer SHAPE_RENDERER = new ShapeRenderer();

public TiledMeshRenderer(GDXRenderer gdxRenderer, Collection<GDXPolygon> polygons){
for(GDXPolygon polygon : polygons)
Expand All @@ -30,7 +34,7 @@ public static MaskTextureStruct buildMesh(GDXRenderer gdxRenderer, GDXPolygon po
Mesh mesh = new Mesh(true, verticesAbsolute.size(), 0, VertexAttribute.Position(), VertexAttribute.TexCoords(0));
Texture texture = gdxRenderer.getTexture(polygon.getResource());
mesh.setVertices(convert(verticesAbsolute, polygon.getCenter()));
return new MaskTextureStruct(mesh, texture);
return new MaskTextureStruct(mesh, texture, polygon);
}

private static float[] convert(final List<Vector2> vertices, final Vector2 center){
Expand All @@ -54,5 +58,19 @@ public void render(Camera camera){
struct.mesh.render(MaskTextureStruct.SHADER, GL20.GL_TRIANGLES);
}
MaskTextureStruct.SHADER.end();
if(Properties.getBool("tiled.renderer.drawoutline", true))
renderOutlines(camera);
}

public void renderOutlines(Camera camera){
SHAPE_RENDERER.setColor(Color.BLACK);
SHAPE_RENDERER.setProjectionMatrix(camera.combined);
SHAPE_RENDERER.begin(ShapeType.Line);
for(MaskTextureStruct struct : maskTextures){
List<Vector2> verts = struct.polygon.getVerticesAbsolute();
for(int i=1; i<verts.size(); i++)
SHAPE_RENDERER.line(verts.get(i-1), verts.get(i));
}
SHAPE_RENDERER.end();
}
}

0 comments on commit a6303dd

Please sign in to comment.