如何解决使用的呈现文字背景不透明
我正在尝试使用lwjgl3渲染按钮的文本。
然后,我使用openGlFont.drawText("Login",x,y);
渲染文本,结果是:
因此它至少会绘制,但是它后面的黑色方块确实很烦人。有可能以任何方式摆脱这种情况吗?我已经尝试过将slick2d与lwjgl2一起使用,但是不知何故它也失败了,只在其中画了一个带点的黑色正方形。
我正在使用的班级代码:
public class OpenGlFont {
//Constants
private final Map<Integer,String> CHARS = new HashMap<Integer,String>() {{
put(0,"ABCDEFGHIJKLMnopQRSTUVWXYZ");
put(1,"abcdefghijklmnopqrstuvwxyz");
put(2,"0123456789");
put(3,"ÄÖÜäöüß");
put(4," $+-*/=%\"'#@&_(),.;:?!\\|<>[]§`^~");
}};
//Variables
private java.awt.Font font;
private FontMetrics fontMetrics;
private BufferedImage bufferedImage;
private int fontTextureId;
//Constructors
public OpenGlFont(String path,float size) throws Exception {
font = java.awt.Font.createFont(Font.TRUETYPE_FONT,Main.class.getResourceAsstream("/resources/fonts/" + path)).deriveFont(size);
//Generate buffered image
GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
Graphics2D graphics = gc.createCompatibleImage(1,1).createGraphics();
graphics.setFont(font);
fontMetrics = graphics.getFontMetrics();
bufferedImage = new BufferedImage( (int)getFontimageWidth(),(int)getFontimageHeight(),BufferedImage.OPAQUE);
//Generate texture
fontTextureId = glGenTextures();
glBindTexture(GL_TEXTURE_2D,fontTextureId);
glTexImage2D(GL_TEXTURE_2D,GL_RGBA,(int) getFontimageWidth(),(int) getFontimageHeight(),GL_UNSIGNED_BYTE,asByteBuffer());
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
}
//Conversions
private ByteBuffer asByteBuffer() {
ByteBuffer byteBuffer;
//Draw the characters on our image
Graphics2D imageGraphics = (Graphics2D) bufferedImage.getGraphics();
imageGraphics.setFont(font);
imageGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
// draw every CHAR by line...
CHARS.keySet().stream().forEach(i -> imageGraphics.drawString(CHARS.get(i),fontMetrics.getMaxAscent() + (this.getCharHeight() * i)));
//Generate texture data
int[] pixels = new int[bufferedImage.getWidth() * bufferedImage.getHeight()];
bufferedImage.getRGB(0,bufferedImage.getWidth(),bufferedImage.getHeight(),pixels,bufferedImage.getWidth());
byteBuffer = ByteBuffer.allocateDirect((bufferedImage.getWidth() * bufferedImage.getHeight() * 4));
for (int y = 0; y < bufferedImage.getHeight(); y++) {
for (int x = 0; x < bufferedImage.getWidth(); x++) {
int pixel = pixels[y * bufferedImage.getWidth() + x];
byteBuffer.put((byte) ((pixel & 0xff))); // Red component
byteBuffer.put((byte) ((pixel & 0xff00) >> 8)); // Green component
byteBuffer.put((byte) ((pixel & 0xff0000) >> 16)); // Blue component
byteBuffer.put((byte) (pixel & 0xff000000 >> 24)); // Alpha component. Only for RGBA
}
}
byteBuffer.flip();
return byteBuffer;
}
//Getters
public float getFontimageWidth() {
return (float) CHARS.values().stream().mapTodouble(e -> fontMetrics.getStringBounds(e,null).getWidth()).max().getAsDouble();
}
public float getFontimageHeight() {
return (float) CHARS.keySet().size() * (this.getCharHeight());
}
public float getCharX(char c) {
String originStr = CHARS.values().stream().filter(e -> e.contains("" + c)).findFirst().orElse("" + c);
return (float) fontMetrics.getStringBounds(originStr.substring(0,originStr.indexOf(c)),null).getWidth();
}
public float getCharY(char c) {
float lineId = (float) CHARS.keySet().stream().filter(i -> CHARS.get(i).contains("" + c)).findFirst().orElse(0);
return this.getCharHeight() * lineId;
}
public float getCharWidth(char c) {
return fontMetrics.charWidth(c);
}
public float getCharHeight() {
return (float) (fontMetrics.getMaxAscent() + fontMetrics.getMaxDescent());
}
//Functions
public void drawText(String text,int x,int y) {
glBindTexture(GL_TEXTURE_2D,this.fontTextureId);
glBegin(GL_QUADS);
int xTmp = x;
for (char c : text.tochararray()) {
float width = getCharWidth(c);
float height = getCharHeight();
float cw = 1f / getFontimageWidth() * width;
float ch = 1f / getFontimageHeight() * height;
float cx = 1f / getFontimageWidth() * getCharX(c);
float cy = 1f / getFontimageHeight() * getCharY(c);
glTexCoord2f(cx,cy);
glVertex3f(xTmp,y,0);
glTexCoord2f(cx + cw,cy);
glVertex3f(xTmp + width,cy + ch);
glVertex3f(xTmp + width,y + height,0);
glTexCoord2f(cx,cy + ch);
glVertex3f(xTmp,0);
xTmp += width;
}
glEnd();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。