Skip to content

Commit 97c9d1e

Browse files
InterruptEvrim Öztamur
authored and
Evrim Öztamur
committed
Updating NeoText to draw per-glyph for better lighting
1 parent 4250378 commit 97c9d1e

File tree

4 files changed

+94
-51
lines changed

4 files changed

+94
-51
lines changed

Dungeoneer/src/com/interrupt/dungeoneer/entities/NeoText.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class NeoText extends DirectionalEntity {
1010
public String text = "Test";
1111

1212
@EditorProperty
13-
public Color textColor = Color.WHITE;
13+
public Color textColor = new Color(Color.WHITE);
1414

1515
public NeoText() {
1616
this.drawable = new DrawableText(this.text);

Dungeoneer/src/com/interrupt/dungeoneer/entities/Text.java

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.interrupt.dungeoneer.entities;
22

3+
import com.badlogic.gdx.graphics.g2d.BitmapFont;
4+
import com.badlogic.gdx.graphics.g2d.TextureRegion;
35
import com.badlogic.gdx.math.Vector2;
46
import com.badlogic.gdx.math.Vector3;
57
import com.interrupt.dungeoneer.Art;
@@ -74,6 +76,9 @@ public void makeText() {
7476
ty = rotTemp.y + y;
7577
tz = rotTemp.z + z;
7678

79+
BitmapFont.Glyph glyph = GameManager.renderer.font.getData().getGlyph(c);
80+
TextureRegion tr = GameManager.renderer.font.getRegion();
81+
7782
SpriteDecal s = new SpriteDecal(0, 0, charPos);
7883
s.x = tx - x;
7984
s.y = ty - y;

Dungeoneer/src/com/interrupt/dungeoneer/gfx/GlRenderer.java

+80-43
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public class GlRenderer {
7474
public TextureRegion miniMap;
7575
public boolean showMap = false;
7676

77-
protected BitmapFont font = null;
77+
public BitmapFont font = null;
7878

7979
protected TextureAtlas wallTextures;
8080
protected TextureAtlas spriteTextures;
@@ -780,67 +780,104 @@ public void updateShaderAttributes() {
780780
}
781781
}
782782

783+
TextureRegion tempGlyphTextureRegion = new TextureRegion();
783784
public void renderTextBatches() {
784-
ShaderProgram activeProgram;
785+
DecalBatch batch = getDecalBatch("sprite", Entity.BlendMode.OPAQUE);
786+
if (renderingForPicking)
787+
batch = getDecalBatch("picking", Entity.BlendMode.OPAQUE);
785788

786-
if (renderingForPicking) {
787-
activeProgram = ShaderManager.getShaderManager().getCompiledShader("picking").shader;
788-
} else {
789-
activeProgram = smoothLighting;
790-
}
789+
// Global text scale modifier. Make a constant?
790+
float baseTextScale = 0.025f;
791791

792-
textBatch.setShader(activeProgram);
792+
for (int i = 0; i < textToRender.size; i++) {
793+
DrawableText dT = textToRender.get(i);
793794

794-
try {
795-
int pvMatrixLoc = activeProgram.getUniformLocation("u_projectionViewMatrix");
795+
float curXPos = 0f;
796+
float textWidth = 0f;
796797

797-
for (int i = 0; i < textToRender.size; i++) {
798-
DrawableText dT = textToRender.get(i);
798+
// TODO: Handle newline characters?
799799

800-
textBatch.begin();
800+
// Figure out the width of this text
801+
for(int ii = 0; ii < dT.text.length(); ii++) {
802+
BitmapFont.Glyph glyph = font.getData().getGlyph(dT.text.charAt(ii));
803+
textWidth += glyph.width * dT.scale * baseTextScale;
804+
}
801805

802-
Gdx.gl.glDepthMask(true); // SpriteBatch.begin() forces off depth writing, which we need for 3D space.
806+
// Draw a decal per-glyph for this text
807+
for(int ii = 0; ii < dT.text.length(); ii++) {
808+
BitmapFont.Glyph glyph = font.getData().getGlyph(dT.text.charAt(ii));
803809

804-
Vector3 position = dT.parentPosition.cpy().add(dT.drawOffset);
810+
float glyphWidth = glyph.width * dT.scale * baseTextScale;
811+
float glyphHeight = glyph.height * dT.scale * baseTextScale;
805812

806-
if (renderingForPicking) {
807-
font.setColor(dT.pickingColor);
808-
} else {
809-
if (dT.editorState == Entity.EditorState.hovered) {
810-
font.setColor(Color.SKY);
811-
} else {
812-
font.setColor(dT.worldColor.cpy().mul(dT.color)); // How to 'light' this correctly?
813-
}
814-
}
813+
float tx = dT.parentPosition.x + curXPos;
814+
float ty = dT.parentPosition.y + 0.001f; // Pull out a bit, to place directly on walls
815+
float tz = dT.parentPosition.z + (glyphHeight * 0.5f); // Place font baseline directly on entity origin
815816

816-
GlyphLayout layout = font.draw(textBatch, dT.text, 0, 0);
817+
// Center text on origin
818+
tx -= textWidth * 0.5f;
817819

818-
float scale = 0.025f * dT.scale;
819-
Matrix4 pvmMatrix = camera.combined.cpy() // Calculate a new matrix to override the already-set uniform.
820-
.translate(position.x, position.z, position.y)
821-
.rotate(new Quaternion().setEulerAngles(dT.parentRotation.z, dT.parentRotation.y, dT.parentRotation.x))
822-
.scale(scale, scale, scale)
823-
.translate( -layout.width / 2, 0, 0); // Center text on origin.
820+
// Offset a tiny bit, because something was doing that in the glyph rendering code
821+
tx += 0.1f * dT.scale;
822+
823+
// Increase the cursor position for next time
824+
curXPos += glyphWidth;
825+
826+
// Update the position based on our rotation
827+
Vector3 rotTemp = new Vector3(tx - dT.parentPosition.x, ty - dT.parentPosition.y, tz - dT.parentPosition.z);
828+
rotTemp.rotate(Vector3.X, -dT.parentRotation.x);
829+
rotTemp.rotate(Vector3.Y, -dT.parentRotation.y);
830+
rotTemp.rotate(Vector3.Z, -dT.parentRotation.z);
831+
832+
tx = rotTemp.x + dT.parentPosition.x;
833+
ty = rotTemp.y + dT.parentPosition.y;
834+
tz = rotTemp.z + dT.parentPosition.z - 0.5f;
835+
836+
// Have everything, can now set up a sprite decal to draw
837+
DDecal sd = decalPool.obtain();
838+
usedDecals.add(sd);
824839

825-
activeProgram.setUniformMatrix(pvMatrixLoc, pvmMatrix);
840+
sd.setBlending(-1, -1);
841+
if(!renderingForPicking) {
842+
if (dT.blendMode == Entity.BlendMode.ALPHA) {
843+
sd.setBlending(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
844+
} else if (dT.blendMode == Entity.BlendMode.ADD) {
845+
sd.setBlending(GL20.GL_SRC_ALPHA, GL20.GL_ONE);
846+
}
847+
}
826848

827-
if (dT.editorState == Entity.EditorState.picked) {
828-
textBatch.flush();
849+
sd.setPosition(tx, tz, ty);
850+
sd.transformationOffset = Vector2.Zero;
829851

830-
font.setColor(Color.CORAL);
831-
font.draw(textBatch, dT.text, 0, 0);
852+
sd.setRotation(0, 0, 0);
853+
sd.rotateY(dT.parentRotation.z);
854+
sd.rotateX(dT.parentRotation.x);
855+
sd.rotateZ(dT.parentRotation.y);
832856

833-
activeProgram.setUniformMatrix(pvMatrixLoc, pvmMatrix.translate(0, 0, -0.2f));
857+
if(dT.fullbrite) {
858+
sd.setColor(dT.color.r, dT.color.g, dT.color.b, 1.0f);
859+
} else {
860+
Color lightmap = GetLightmapAt(tx, tz, ty);
861+
sd.setColor(lightmap.r, lightmap.g, lightmap.b, 1.0f);
834862
}
835863

836-
textBatch.end();
837-
}
864+
if (renderingForPicking)
865+
sd.setColor(dT.pickingColor);
866+
867+
sd.setScale(1f);
868+
sd.setWidth(glyphWidth);
869+
sd.setHeight(glyphHeight);
838870

839-
activeProgram.setUniformMatrix(pvMatrixLoc, camera.combined); // Reset the projection matrix.
871+
tempGlyphTextureRegion.setTexture(font.getRegion().getTexture());
872+
tempGlyphTextureRegion.setRegion(glyph.u, glyph.v2, glyph.u2, glyph.v);
873+
sd.setTextureRegion(tempGlyphTextureRegion);
840874

841-
textToRender.clear();
875+
batch.add(sd);
876+
}
877+
batch.flush();
842878
}
843-
catch(Exception ex) { }
879+
880+
textToRender.clear();
844881
}
845882

846883
public void renderOpaqueSprites() {
@@ -1470,6 +1507,7 @@ public void renderSelectedEntities(Array<Entity> selected, Entity.EditorState ed
14701507
}
14711508

14721509
renderOpaqueSprites();
1510+
renderTextBatches();
14731511
renderMeshes();
14741512
renderTransparentEntities();
14751513

@@ -2368,7 +2406,6 @@ else if(drawable.billboard) {
23682406

23692407
public void renderDrawableText(float x, float y, float z, Entity entity, DrawableText drawable)
23702408
{
2371-
drawable.worldColor.set(GetLightmapAt(x + drawable.drawOffset.x, z, y + drawable.drawOffset.y));
23722409
drawable.pickingColor.set(entityPickColor);
23732410
textToRender.add(drawable);
23742411
}

Dungeoneer/src/com/interrupt/dungeoneer/gfx/drawables/DrawableText.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
public class DrawableText extends Drawable {
99

1010
public String text = "";
11-
public Color worldColor = Color.WHITE;
1211
public Vector3 parentPosition = new Vector3();
1312
public Vector3 parentRotation = new Vector3();
1413
public Entity.EditorState editorState = Entity.EditorState.hovered;
15-
public Color pickingColor = Color.BLACK;
14+
public Color pickingColor = new Color(Color.BLACK);
1615

1716
public DrawableText() {
1817
}
@@ -22,16 +21,18 @@ public DrawableText(String text) {
2221
}
2322

2423
public void update(Entity e) {
25-
if (e instanceof NeoText) {
26-
text = ((NeoText) e).text;
27-
color.set(((NeoText) e).textColor);
28-
}
29-
3024
scale = e.scale;
25+
blendMode = e.blendMode;
26+
fullbrite = e.fullbrite;
3127

3228
parentPosition.set(e.x, e.y, e.z);
3329
parentRotation.set(e.getRotation());
3430

3531
editorState = e.editorState;
32+
33+
if (e instanceof NeoText) {
34+
text = ((NeoText) e).text;
35+
color.set(((NeoText) e).textColor);
36+
}
3637
}
3738
}

0 commit comments

Comments
 (0)