Before you go off on me for "not searching", I've searched around a LOT and found a lot of answers, but none of them worked for me.
嗨,我正在制作3D java lwjgl游戏,我正试图在屏幕中间渲染一些简单的2D文字,但无论我尝试什么,我都看不到任何文字呈现在我的屏幕上。这是我的Text2D.java类:
package com.gmail.br45entei.base.engine.rendering;
import java.awt.Font;
import java.awt.FontFormatException;
import java.io.IOException;
import org.newdawn.slick.Color;
import org.newdawn.slick.UnicodeFont;
import com.gmail.br45entei.base.engine.core.Vector2f;
import com.gmail.br45entei.base.game.Game;
import com.gmail.br45entei.base.game.MainComponent;
import com.gmail.br45entei.file.FileMgmt;
/**
* @author Brian_Entei
*
*/
public class Text3D {
private Camera camera;
private UnicodeFont font;
private Color color;
private Vector2f position;
private String text = "New string";
private int size = 12;
public int getSize() {
return this.size;
}
public void setSize(int size) {
this.size = size;
updateFont();
}
private void updateFont() {
try {
this.font = new UnicodeFont(Font.createFont(Font.TRUETYPE_FONT, FileMgmt.getFile(MainComponent.resourceFolder, "/fonts/BrianOwenReidBold.ttf", false)), size, false, false);
} catch (FontFormatException e) {
FileMgmt.LogException(e, "updateFont()", "Failed to create font from file!", false);
} catch (IOException e) {
FileMgmt.LogException(e, "Text3D()", "Failed to load file to create font!", false);
}
}
public Text3D(String text, Camera camera) {
this.camera = camera;
this.color = Color.black;
this.position = new Vector2f(0, 0);
this.text = text;
updateFont();
}
public void drawHudString() {
//org.lwjgl.opengl.GL11.glPushMatrix();// This gets called right before here.
org.lwjgl.opengl.GL11.glRotatef(camera.getForward().getX(), -1.0f, 0.0f, 0.0f);
org.lwjgl.opengl.GL11.glRotatef(camera.getForward().getY(), 0.0f, -1.0f, 0.0f);
org.lwjgl.opengl.GL11.glRotatef(camera.getForward().getZ(), 0.0f, 0.0f, -1.0f);
org.lwjgl.opengl.GL11.glTranslatef(-camera.getPos().getX(), -camera.getPos().getY(), -camera.getPos().getZ() + 20);
this.font.drawString(this.position.getX(), this.position.getY(), this.text, this.color);
//org.lwjgl.opengl.GL11.glPopMatrix();// This gets called right after here.
}
public void render() {
Game.print("Text render!");
org.lwjgl.opengl.GL11.glMatrixMode(org.lwjgl.opengl.GL11.GL_PROJECTION);
org.lwjgl.opengl.GL11.glPushMatrix();
org.lwjgl.opengl.GL11.glLoadIdentity();
org.lwjgl.opengl.GL11.glOrtho(0, 800, 600, 0, 1, -1);
org.lwjgl.opengl.GL11.glMatrixMode(org.lwjgl.opengl.GL11.GL_MODELVIEW);
org.lwjgl.opengl.GL11.glDisable(org.lwjgl.opengl.GL11.GL_CULL_FACE);
org.lwjgl.opengl.GL11.glDisable(org.lwjgl.opengl.GL11.GL_DEPTH_TEST);
org.lwjgl.opengl.GL11.glClear(org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT);
org.lwjgl.opengl.GL11.glLoadIdentity();
// render font
this.drawHudString();
org.lwjgl.opengl.GL11.glEnable(org.lwjgl.opengl.GL11.GL_DEPTH_TEST);
org.lwjgl.opengl.GL11.glEnable(org.lwjgl.opengl.GL11.GL_CULL_FACE);
org.lwjgl.opengl.GL11.glMatrixMode(org.lwjgl.opengl.GL11.GL_PROJECTION);
org.lwjgl.opengl.GL11.glPopMatrix();
org.lwjgl.opengl.GL11.glMatrixMode(org.lwjgl.opengl.GL11.GL_MODELVIEW);
}
}
在渲染3D对象后调用此类中的渲染函数。我希望有人可以帮我解决这个问题!
答案 0 :(得分:9)
您已经选择了上面的答案作为解决方案,但您说它不起作用。解决方案是缩放0.01f或类似的东西,具体取决于您的设置。要缩放,只需使用glScale(x,y,z)。 你也可以这样启用Blending:
glEnable(GL11.GL_BLEND);
glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
答案 1 :(得分:6)
我创建了一个呈现文本的文本类。它是用java编写的LWJGL。做就是了 drawString(“text”,positionX,positionY,scale,width);
package text;
import org.lwjgl.opengl.GL11;
public class Text {
public static void drawString(String s, float x, float y, float scale, float width) {
float startX = x;
scale = scale * 0.25f;
GL11.glLineWidth(width);
GL11.glBegin(GL11.GL_LINES);
GL11.glEnable(GL11.GL_LINE_WIDTH);
GL11.glColor3f(1, 1, 1);
boolean space = false;
int charNum = 1, spaceNum = 0;
for (char c : s.toLowerCase().toCharArray()) {
switch(c){
case ' ':
space = true;
break;
case '\n':
y -= 2;
startX -= charNum * (scale * 5);
startX -= spaceNum * (scale * 3);
spaceNum = 0;
charNum = 0;
break;
case 'a':
GL11.glVertex2f(scale * (startX - 0.5f), scale * y);
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y + -1));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y + -1));
GL11.glVertex2f(scale * startX, scale * (y + -1));
GL11.glVertex2f(scale * startX, scale * (y + -1));
GL11.glVertex2f(scale * startX, scale * y);
GL11.glVertex2f(scale * startX, scale * y);
GL11.glVertex2f(scale * (startX - 0.5f), scale * y);
GL11.glVertex2f(scale * startX, scale * y);
GL11.glVertex2f(scale * startX, scale * (y + 0.5f));
GL11.glVertex2f(scale * startX, scale * (y + 0.5f));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y + 0.5f));
break;
case 'b':
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y + 0.8f));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1));
GL11.glVertex2f(scale * (startX), scale * (y - 1));
GL11.glVertex2f(scale * (startX), scale * (y - 1));
GL11.glVertex2f(scale * (startX), scale * (y));
GL11.glVertex2f(scale * (startX), scale * (y));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y));
break;
case 'd':
GL11.glVertex2f(scale * startX, scale * (y - 1));
GL11.glVertex2f(scale * startX, scale * (y + 0.8f));
case 'c':
GL11.glVertex2f(scale * startX, scale * y);
GL11.glVertex2f(scale * (startX - 0.5f), scale * y);
GL11.glVertex2f(scale * (startX - 0.5f), scale * y);
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1));
GL11.glVertex2f(scale * startX, scale * (y - 1));
break;
case 'e':
GL11.glVertex2f(scale * startX, scale * y);
GL11.glVertex2f(scale * (startX - 0.5f), scale * y);
GL11.glVertex2f(scale * startX, scale * y);
GL11.glVertex2f(scale * startX, scale * (y - 0.5f));
GL11.glVertex2f(scale * startX, scale * (y - 0.5f));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 0.5f));
GL11.glVertex2f(scale * (startX - 0.5f), scale * y);
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 0.5f));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 0.5f));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1));
GL11.glVertex2f(scale * (startX), scale * (y - 1));
break;
case 'f':
GL11.glVertex2f(scale * (startX + 0.25f), scale * (y + 0.8f));
GL11.glVertex2f(scale * (startX - 0.25f), scale * (y + 0.8f));
GL11.glVertex2f(scale * (startX - 0.25f), scale * (y + 0.8f));
GL11.glVertex2f(scale * (startX - 0.25f), scale * (y - 1));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y + 0.25f));
GL11.glVertex2f(scale * (startX), scale * (y + 0.25f));
break;
case 'g':
GL11.glVertex2f(scale * (startX), scale * y);
GL11.glVertex2f(scale * (startX), scale * (y - 1.5f));
GL11.glVertex2f(scale * (startX), scale * (y - 1.5f));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1.5f));
GL11.glVertex2f(scale * (startX), scale * y);
GL11.glVertex2f(scale * (startX - 0.5f), scale * y);
GL11.glVertex2f(scale * (startX - 0.5f), scale * y);
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1));
GL11.glVertex2f(scale * (startX), scale * (y - 1f));
break;
case 'h':
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y + 0.8f));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y));
GL11.glVertex2f(scale * (startX), scale * (y));
GL11.glVertex2f(scale * (startX), scale * (y));
GL11.glVertex2f(scale * (startX), scale * (y - 1));
break;
case 'i':
GL11.glVertex2f(scale * (startX - 0.25f), scale * y);
GL11.glVertex2f(scale * (startX - 0.25f), scale * (y - 1));
GL11.glEnd();
GL11.glPointSize(width);
GL11.glBegin(GL11.GL_POINTS);
GL11.glVertex2f(scale * (startX - 0.25f), scale * (y + 0.25f));
GL11.glEnd();
GL11.glBegin(GL11.GL_LINES);
break;
case 'j':
GL11.glVertex2f(scale * (startX - 0.25f), scale * y);
GL11.glVertex2f(scale * (startX - 0.25f), scale * (y - 1.5f));
GL11.glVertex2f(scale * (startX - 0.25f), scale * (y - 1.5f));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1.5f));
GL11.glEnd();
GL11.glPointSize(width);
GL11.glBegin(GL11.GL_POINTS);
GL11.glVertex2f(scale * (startX - 0.25f), scale * (y + 0.25f));
GL11.glEnd();
GL11.glBegin(GL11.GL_LINES);
break;
case 'k':
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y + 0.8f));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y));
GL11.glVertex2f(scale * (startX), scale * (y - 1));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y));
GL11.glVertex2f(scale * (startX), scale * (y + 0.25f));
break;
case 't':
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y + 0.15f));
GL11.glVertex2f(scale * (startX), scale * (y + 0.15f));
case 'l':
GL11.glVertex2f(scale * (startX - 0.25f), scale * (y + 0.8f));
GL11.glVertex2f(scale * (startX - 0.25f), scale * (y - 1));
break;
case 'm':
GL11.glVertex2f(scale * (startX - 0.5f), scale * y);
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1));
GL11.glVertex2f(scale * (startX - 0.5f), scale * y);
GL11.glVertex2f(scale * (startX - 0.25f), scale * y);
GL11.glVertex2f(scale * (startX - 0.25f), scale * y);
GL11.glVertex2f(scale * (startX - 0.25f), scale * (y - 1));
GL11.glVertex2f(scale * (startX - 0.25f), scale * y);
GL11.glVertex2f(scale * (startX), scale * y);
GL11.glVertex2f(scale * (startX), scale * y);
GL11.glVertex2f(scale * (startX), scale * (y - 1));
break;
case 'p':
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1.5f));
case 'o':
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1));
GL11.glVertex2f(scale * (startX), scale * (y - 1));
case 'n':
GL11.glVertex2f(scale * (startX - 0.5f), scale * y);
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1));
GL11.glVertex2f(scale * (startX - 0.5f), scale * y);
GL11.glVertex2f(scale * (startX), scale * y);
GL11.glVertex2f(scale * (startX), scale * y);
GL11.glVertex2f(scale * (startX), scale * (y - 1));
break;
case 'q':
GL11.glVertex2f(scale * (startX), scale * (y - 1));
GL11.glVertex2f(scale * (startX), scale * (y - 1.5f));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1));
GL11.glVertex2f(scale * (startX), scale * (y - 1));
GL11.glVertex2f(scale * (startX - 0.5f), scale * y);
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1));
GL11.glVertex2f(scale * (startX - 0.5f), scale * y);
GL11.glVertex2f(scale * (startX), scale * y);
GL11.glVertex2f(scale * (startX), scale * y);
GL11.glVertex2f(scale * (startX), scale * (y - 1));
break;
case 'r':
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 0.15f));
GL11.glVertex2f(scale * (startX - 0.25f), scale * (y));
GL11.glVertex2f(scale * (startX - 0.25f), scale * (y));
GL11.glVertex2f(scale * (startX), scale * (y - 0.1f));
break;
case 's':
GL11.glVertex2f(scale * (startX), scale * (y));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 0.5f));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 0.5f));
GL11.glVertex2f(scale * (startX), scale * (y - 0.5f));
GL11.glVertex2f(scale * (startX), scale * (y - 0.5f));
GL11.glVertex2f(scale * (startX), scale * (y - 1));
GL11.glVertex2f(scale * (startX), scale * (y - 1));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1));
break;
case 'u':
GL11.glVertex2f(scale * (startX - 0.5f), scale * y);
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1));
GL11.glVertex2f(scale * (startX), scale * (y - 1));
GL11.glVertex2f(scale * (startX), scale * y);
GL11.glVertex2f(scale * (startX), scale * (y - 1));
break;
case 'w':
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y));
GL11.glVertex2f(scale * (startX - 0.25f), scale * (y - 1));
GL11.glVertex2f(scale * (startX - 0.25f), scale * (y - 1));
GL11.glVertex2f(scale * (startX - 0.125f), scale * (y));
GL11.glVertex2f(scale * (startX - 0.125f), scale * (y));
GL11.glVertex2f(scale * (startX), scale * (y - 1));
GL11.glVertex2f(scale * (startX), scale * (y - 1));
GL11.glVertex2f(scale * (startX + 0.125f), scale * (y));
break;
case 'v':
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y));
GL11.glVertex2f(scale * (startX - 0.25f), scale * (y - 1));
GL11.glVertex2f(scale * (startX - 0.25f), scale * (y - 1));
GL11.glVertex2f(scale * (startX), scale * (y));
break;
case 'x':
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y));
GL11.glVertex2f(scale * (startX), scale * (y - 1));
GL11.glVertex2f(scale * (startX), scale * (y));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1));
break;
case 'y':
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y));
GL11.glVertex2f(scale * (startX - 0.33f), scale * (y - 1));
GL11.glVertex2f(scale * (startX), scale * (y));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1.5f));
break;
case 'z':
GL11.glVertex2f(scale * (startX), scale * (y));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y));
GL11.glVertex2f(scale * (startX), scale * (y));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1));
GL11.glVertex2f(scale * (startX), scale * (y - 1));
break;
case '1':
GL11.glVertex2f(scale * (startX - 0.25f), scale * (y + 0.8f));
GL11.glVertex2f(scale * (startX - 0.25f), scale * (y - 1));
GL11.glVertex2f(scale * (startX - 0.25f), scale * (y + 0.8f));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y + 0.8f));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1));
GL11.glVertex2f(scale * (startX), scale * (y - 1));
break;
case '2':
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y + 0.65f));
GL11.glVertex2f(scale * (startX - 0.3f), scale * (y + 0.8f));
GL11.glVertex2f(scale * (startX - 0.3f), scale * (y + 0.8f));
GL11.glVertex2f(scale * (startX), scale * (y + 0.8f));
GL11.glVertex2f(scale * (startX), scale * (y + 0.8f));
GL11.glVertex2f(scale * (startX - 0.15f), scale * (y));
GL11.glVertex2f(scale * (startX - 0.15f), scale * (y));
GL11.glVertex2f(scale * (startX - 0.35f), scale * (y - 1));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1));
GL11.glVertex2f(scale * (startX), scale * (y - 1));
break;
case '3':
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y + 0.8f));
GL11.glVertex2f(scale * (startX), scale * (y + 0.8f));
GL11.glVertex2f(scale * (startX), scale * (y + 0.8f));
GL11.glVertex2f(scale * (startX), scale * (y));
GL11.glVertex2f(scale * (startX), scale * (y));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y));
GL11.glVertex2f(scale * (startX), scale * (y));
GL11.glVertex2f(scale * (startX), scale * (y - 1));
GL11.glVertex2f(scale * (startX), scale * (y - 1));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1));
break;
case '4':
GL11.glVertex2f(scale * startX, scale * (y + 0.8f));
GL11.glVertex2f(scale * startX, scale * (y - 1));
GL11.glVertex2f(scale * startX, scale * (y + 0.8f));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y));
GL11.glVertex2f(scale * (startX), scale * (y));
break;
case '8':
GL11.glVertex2f(scale * (startX), scale * (y));
GL11.glVertex2f(scale * (startX), scale * (y + 0.8f));
case '6':
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1));
case '5':
GL11.glVertex2f(scale * (startX), scale * (y + 0.8f));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y + 0.8f));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y + 0.8f));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y));
GL11.glVertex2f(scale * (startX), scale * (y));
GL11.glVertex2f(scale * (startX), scale * (y));
GL11.glVertex2f(scale * (startX), scale * (y - 1));
GL11.glVertex2f(scale * (startX), scale * (y - 1));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1));
break;
case '7':
GL11.glVertex2f(scale * (startX), scale * (y + 0.8f));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y + 0.8f));
GL11.glVertex2f(scale * (startX), scale * (y + 0.8f));
break;
case '9':
GL11.glVertex2f(scale * (startX), scale * (y));
GL11.glVertex2f(scale * (startX), scale * (y + 0.8f));
GL11.glVertex2f(scale * (startX), scale * (y + 0.8f));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y + 0.8f));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y + 0.8f));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y));
GL11.glVertex2f(scale * (startX), scale * (y));
GL11.glVertex2f(scale * (startX), scale * (y));
GL11.glVertex2f(scale * (startX), scale * (y - 1));
GL11.glVertex2f(scale * (startX), scale * (y - 1));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1));
break;
case '0':
GL11.glVertex2f(scale * (startX), scale * (y + 0.8f));
GL11.glVertex2f(scale * (startX), scale * (y - 1));
GL11.glVertex2f(scale * (startX), scale * (y - 1));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y - 1));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y + 0.8f));
GL11.glVertex2f(scale * (startX - 0.5f), scale * (y + 0.8f));
GL11.glVertex2f(scale * (startX), scale * (y + 0.8f));
break;
}
if(!space){
startX += scale * 5;
charNum++;
}else{
startX += scale * 3;
spaceNum++;
}
space = false;
}
GL11.glEnd();
GL11.glDisable(GL11.GL_LINE_WIDTH);
}
}