使用的呈现文字背景不透明

如何解决使用的呈现文字背景不透明

我正在尝试使用lwjgl3渲染按钮的文本。 然后,我使用openGlFont.drawText("Login",x,y);渲染文本,结果是:

enter image description here

因此它至少会绘制,但是它后面的黑色方块确实很烦人。有可能以任何方式摆脱这种情况吗?我已经尝试过将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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?