Browse Source

More customizable font rendering

Tankernn 8 years ago
parent
commit
3b39beb3f5

+ 0 - 1
TODO.txt

@@ -1,7 +1,6 @@
 Texture atlases with the new model spec system.
 Multiplayer
 Modular GUIs
- - Make text variables uniform/ change text rendering method
 
 Useful regex-expressions:
 

+ 21 - 0
src/main/java/eu/tankernn/gameEngine/loader/font/Font.java

@@ -0,0 +1,21 @@
+package eu.tankernn.gameEngine.loader.font;
+
+import org.lwjgl.util.vector.Vector3f;
+
+public class Font {
+	public final FontFamily family;
+	public final float size, width, edge, outlineWidth, outlineEdge;
+	public final Vector3f color, outlineColor;
+	
+	public Font(FontFamily family, float size, float width, float edge, float outlineWidth, float outlineEdge, Vector3f color, Vector3f outlineColor) {
+		this.family = family;
+		this.size = size;
+		this.width = width;
+		this.edge = edge;
+		this.outlineWidth = outlineWidth;
+		this.outlineEdge = outlineEdge;
+		this.color = color;
+		this.outlineColor = outlineColor;
+	}
+	
+}

+ 3 - 3
src/main/java/eu/tankernn/gameEngine/loader/font/FontType.java → src/main/java/eu/tankernn/gameEngine/loader/font/FontFamily.java

@@ -10,7 +10,7 @@ import eu.tankernn.gameEngine.util.InternalFile;
  * @author Karl
  *
  */
-public class FontType {
+public class FontFamily {
 
 	private Texture textureAtlas;
 	private TextMeshCreator loader;
@@ -25,7 +25,7 @@ public class FontType {
 	 *            - the font file containing information about each character in
 	 *            the texture atlas.
 	 */
-	public FontType(Texture texture, InternalFile fontFile) {
+	public FontFamily(Texture texture, InternalFile fontFile) {
 		this.textureAtlas = texture;
 		this.loader = new TextMeshCreator(fontFile);
 	}
@@ -46,7 +46,7 @@ public class FontType {
 	 *            - the unloaded text.
 	 * @return Information about the vertices of all the quads.
 	 */
-	public TextMeshData loadText(GUIText text) {
+	public TextMeshData generateMesh(GUIText text) {
 		return loader.createTextMesh(text);
 	}
 

+ 4 - 38
src/main/java/eu/tankernn/gameEngine/loader/font/GUIText.java

@@ -1,7 +1,6 @@
 package eu.tankernn.gameEngine.loader.font;
 
 import org.lwjgl.util.vector.Vector2f;
-import org.lwjgl.util.vector.Vector3f;
 
 import eu.tankernn.gameEngine.loader.Loader;
 import eu.tankernn.gameEngine.renderEngine.Vao;
@@ -16,18 +15,15 @@ public class GUIText {
 
 	private String textString;
 	private boolean dirty;
-	
-	private float fontSize;
 
 	private Vao textMeshVao;
 	private int vertexCount;
-	private Vector3f colour = new Vector3f(0f, 0f, 0f);
 
 	private Vector2f position;
 	private float lineMaxSize;
 	private int numberOfLines;
 
-	private FontType font;
+	private Font font;
 
 	private boolean centerText = false;
 
@@ -56,10 +52,9 @@ public class GUIText {
 	 * @param centered
 	 *            - whether the text should be centered or not.
 	 */
-	public GUIText(String text, float fontSize, FontType font, Vector2f position, float maxLineLength,
+	public GUIText(String text, Font font, Vector2f position, float maxLineLength,
 			boolean centered) {
 		this.textString = text;
-		this.fontSize = fontSize;
 		this.font = font;
 		this.position = position;
 		this.lineMaxSize = maxLineLength;
@@ -69,32 +64,10 @@ public class GUIText {
 	/**
 	 * @return The font used by this text.
 	 */
-	public FontType getFont() {
+	public Font getFont() {
 		return font;
 	}
 
-	/**
-	 * Set the colour of the text.
-	 * 
-	 * @param r
-	 *            - red value, between 0 and 1.
-	 * @param g
-	 *            - green value, between 0 and 1.
-	 * @param b
-	 *            - blue value, between 0 and 1.
-	 */
-	public GUIText setColor(float r, float g, float b) {
-		colour.set(r, g, b);
-		return this;
-	}
-
-	/**
-	 * @return the colour of the text.
-	 */
-	public Vector3f getColor() {
-		return colour;
-	}
-
 	/**
 	 * @return The number of lines of text. This is determined when the text is
 	 *         loaded, based on the length of the text and the max line length
@@ -142,13 +115,6 @@ public class GUIText {
 		return this.vertexCount;
 	}
 
-	/**
-	 * @return the font size of the text (a font size of 1 is normal).
-	 */
-	protected float getFontSize() {
-		return fontSize;
-	}
-
 	/**
 	 * Sets the number of lines that this text covers (method used only in
 	 * loading).
@@ -190,7 +156,7 @@ public class GUIText {
 	}
 	
 	public void update(Loader loader) {
-		TextMeshData data = font.loadText(this);
+		TextMeshData data = font.family.generateMesh(this);
 		Vao vao = loader.loadToVAO(data.getVertexPositions(), data.getTextureCoords());
 		this.setMeshInfo(vao, data.getVertexCount());
 	}

+ 9 - 9
src/main/java/eu/tankernn/gameEngine/loader/font/TextMeshCreator.java

@@ -25,18 +25,18 @@ public class TextMeshCreator {
 	private List<Line> createStructure(GUIText text) {
 		char[] chars = text.getTextString().toCharArray();
 		List<Line> lines = new ArrayList<Line>();
-		Line currentLine = new Line(metaData.getSpaceWidth(), text.getFontSize(), text.getMaxLineSize());
-		Word currentWord = new Word(text.getFontSize());
+		Line currentLine = new Line(metaData.getSpaceWidth(), text.getFont().size, text.getMaxLineSize());
+		Word currentWord = new Word(text.getFont().size);
 		for (char c : chars) {
 			int ascii = (int) c;
 			if (ascii == SPACE_ASCII) {
 				boolean added = currentLine.attemptToAddWord(currentWord);
 				if (!added) {
 					lines.add(currentLine);
-					currentLine = new Line(metaData.getSpaceWidth(), text.getFontSize(), text.getMaxLineSize());
+					currentLine = new Line(metaData.getSpaceWidth(), text.getFont().size, text.getMaxLineSize());
 					currentLine.attemptToAddWord(currentWord);
 				}
-				currentWord = new Word(text.getFontSize());
+				currentWord = new Word(text.getFont().size);
 				continue;
 			}
 			Character character = metaData.getCharacter(ascii);
@@ -50,7 +50,7 @@ public class TextMeshCreator {
 		boolean added = currentLine.attemptToAddWord(currentWord);
 		if (!added) {
 			lines.add(currentLine);
-			currentLine = new Line(metaData.getSpaceWidth(), text.getFontSize(), text.getMaxLineSize());
+			currentLine = new Line(metaData.getSpaceWidth(), text.getFont().size, text.getMaxLineSize());
 			currentLine.attemptToAddWord(currentWord);
 		}
 		lines.add(currentLine);
@@ -68,15 +68,15 @@ public class TextMeshCreator {
 			}
 			for (Word word : line.getWords()) {
 				for (Character letter : word.getCharacters()) {
-					addVerticesForCharacter(curserX, curserY, letter, text.getFontSize(), vertices);
+					addVerticesForCharacter(curserX, curserY, letter, text.getFont().size, vertices);
 					addTexCoords(textureCoords, letter.getxTextureCoord(), letter.getyTextureCoord(),
 							letter.getXMaxTextureCoord(), letter.getYMaxTextureCoord());
-					curserX += letter.getxAdvance() * text.getFontSize();
+					curserX += letter.getxAdvance() * text.getFont().size;
 				}
-				curserX += metaData.getSpaceWidth() * text.getFontSize();
+				curserX += metaData.getSpaceWidth() * text.getFont().size;
 			}
 			curserX = 0;
-			curserY += LINE_HEIGHT * text.getFontSize();
+			curserY += LINE_HEIGHT * text.getFont().size;
 		}		
 		return new TextMeshData(listToArray(vertices), listToArray(textureCoords));
 	}

+ 7 - 5
src/main/java/eu/tankernn/gameEngine/particles/ParticleMaster.java

@@ -13,8 +13,9 @@ import org.lwjgl.util.vector.Vector3f;
 
 import eu.tankernn.gameEngine.entities.Camera;
 import eu.tankernn.gameEngine.loader.Loader;
-import eu.tankernn.gameEngine.loader.font.FontType;
+import eu.tankernn.gameEngine.loader.font.Font;
 import eu.tankernn.gameEngine.loader.font.GUIText;
+import eu.tankernn.gameEngine.loader.font.Word;
 import eu.tankernn.gameEngine.renderEngine.Fbo;
 import eu.tankernn.gameEngine.renderEngine.font.FontRenderer;
 import eu.tankernn.gameEngine.util.DistanceSorter;
@@ -71,14 +72,15 @@ public class ParticleMaster {
 		renderer.finalize();
 	}
 	
-	public void addTextParticle(String text, int fontSize, FontType font, Vector3f position) {
-		GUIText guiText = new GUIText(text, fontSize, font, new Vector2f(0, 0), 1.0f, false).setColor(0, 1, 0);
+	public void addTextParticle(String text, Font font, Vector3f position) {
+		
+		GUIText guiText = new GUIText(text, font, new Vector2f(0, 0), 1.0f, false);
 		guiText.update(loader);
-		Fbo fbo = new Fbo(100 * fontSize, 100 * fontSize, 0);
+		Fbo fbo = new Fbo((int) (100 * font.size), (int) (100 * font.size), 0);
 		fbo.bindFrameBuffer();
 		fontRenderer.render(guiText);
 		fbo.unbindFrameBuffer();
-		addParticle(new Particle(new ParticleTexture(fbo.getColourTexture(), 1, true), position, new Vector3f(0, 0, 0), 0.1f, 4, 0, fontSize));
+		addParticle(new Particle(new ParticleTexture(fbo.getColourTexture(), 1, true), position, new Vector3f(0, 0, 0), 0.1f, 4, 0, font.size));
 	}
 	
 	public void addParticle(Particle particle) {

+ 15 - 6
src/main/java/eu/tankernn/gameEngine/renderEngine/font/FontRenderer.java

@@ -5,17 +5,17 @@ import java.util.Map;
 
 import org.lwjgl.opengl.GL11;
 
-import eu.tankernn.gameEngine.loader.font.FontType;
+import eu.tankernn.gameEngine.loader.font.Font;
 import eu.tankernn.gameEngine.loader.font.GUIText;
 
 public class FontRenderer {
 	
 	private FontShader shader = new FontShader();
 	
-	public void render(Map<FontType, List<GUIText>> texts) {
+	public void render(Map<Font, List<GUIText>> texts) {
 		prepare();
-		for (FontType font: texts.keySet()) {
-			font.getTextureAtlas().bindToUnit(0);
+		for (Font font: texts.keySet()) {
+			loadFont(font);
 			for (GUIText text: texts.get(font)) {
 				renderText(text);
 			}
@@ -34,9 +34,18 @@ public class FontRenderer {
 		shader.start();
 	}
 	
+	private void loadFont(Font font) {
+		font.family.getTextureAtlas().bindToUnit(0);
+		shader.color.loadVec3(font.color);
+		shader.width.loadFloat(font.width);
+		shader.edge.loadFloat(font.edge);
+		shader.borderWidth.loadFloat(font.outlineWidth);
+		shader.borderEdge.loadFloat(font.outlineEdge);
+		shader.outlineColor.loadVec3(font.outlineColor);
+	}
+	
 	private void renderText(GUIText text) {
 		text.getMesh().bind(0, 1);
-		shader.color.loadVec3(text.getColor());
 		shader.translation.loadVec2(text.getPosition());
 		GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, text.getVertexCount());
 		text.getMesh().unbind(0, 1);
@@ -50,7 +59,7 @@ public class FontRenderer {
 
 	public void render(GUIText guiText) {
 		prepare();
-		guiText.getFont().getTextureAtlas().bindToUnit(0);
+		loadFont(guiText.getFont());
 		renderText(guiText);
 		endRendering();
 	}

+ 7 - 1
src/main/java/eu/tankernn/gameEngine/renderEngine/font/FontShader.java

@@ -1,6 +1,7 @@
 package eu.tankernn.gameEngine.renderEngine.font;
 
 import eu.tankernn.gameEngine.renderEngine.shaders.ShaderProgram;
+import eu.tankernn.gameEngine.renderEngine.shaders.UniformFloat;
 import eu.tankernn.gameEngine.renderEngine.shaders.UniformVec2;
 import eu.tankernn.gameEngine.renderEngine.shaders.UniformVec3;
 
@@ -11,10 +12,15 @@ public class FontShader extends ShaderProgram{
 	
 	protected UniformVec3 color = new UniformVec3("color");
 	protected UniformVec2 translation = new UniformVec2("translation");
+	protected UniformFloat width = new UniformFloat("width");
+	protected UniformFloat edge = new UniformFloat("edge");
+	protected UniformFloat borderWidth = new UniformFloat("borderWidth");
+	protected UniformFloat borderEdge = new UniformFloat("borderEdge");
+	protected UniformVec3 outlineColor = new UniformVec3("outlineColor");
 	
 	public FontShader() {
 		super(VERTEX_FILE, FRAGMENT_FILE, "position", "textureCoords");
-		super.storeAllUniformLocations(color, translation);
+		super.storeAllUniformLocations(color, translation, width, edge, borderWidth, borderEdge, outlineColor);
 	}
 	
 }

+ 2 - 2
src/main/java/eu/tankernn/gameEngine/renderEngine/font/TextMaster.java

@@ -6,13 +6,13 @@ import java.util.List;
 import java.util.Map;
 
 import eu.tankernn.gameEngine.loader.Loader;
-import eu.tankernn.gameEngine.loader.font.FontType;
+import eu.tankernn.gameEngine.loader.font.Font;
 import eu.tankernn.gameEngine.loader.font.GUIText;
 
 public class TextMaster {
 
 	private Loader loader;
-	private Map<FontType, List<GUIText>> texts = new HashMap<FontType, List<GUIText>>();
+	private Map<Font, List<GUIText>> texts = new HashMap<>();
 	private FontRenderer renderer;
 
 	public TextMaster(Loader load) {

+ 6 - 5
src/main/java/eu/tankernn/gameEngine/renderEngine/font/fontFragment.glsl

@@ -7,15 +7,16 @@ out vec4 out_Color;
 uniform vec3 color;
 uniform sampler2D fontAtlas;
 
-const float width = 0.5;
-const float edge = 0.1;
+uniform float width;
+uniform float edge;
 
-const float borderWidth = 0.4;
-const float borderEdge = 0.5;
+uniform float borderWidth;
+uniform float borderEdge;
+
+uniform vec3 outlineColor;
 
 const vec2 offset = vec2(0.004, -0.004);
 
-const vec3 outlineColor = vec3(0.2, 0.2, 0.2);
 
 void main(void) {