字体后面的透明背景

时间:2013-02-06 09:31:31

标签: java opengl textures lwjgl slick2d

使用LWJGL和OpenGL,我想绘制一个纹理矩形。 在此之上,我想使用光滑库编写一些文本。 我的问题是,当我在矩形之前绘制文本时,字体具有透明背景。在这里看到:

enter image description here

如果我在矩形后面绘制文字,我无法看到它。现在我用谷歌搜索了大约一个小时,发现了几种可能的解决方案,但没有一种方法适合我。

守则:

FontVeranda.java

    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bunchofpunch.fonts;

import java.nio.FloatBuffer;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;

/**
 *
 * @author Alex
 */
public class FontVeranda 
{
    private UnicodeFont font;
    private boolean shouldRenderFont = true;

    public void loadFont()
    {
        java.awt.Font awtFont = new java.awt.Font("Veranda", java.awt.Font.BOLD, 14);
        font = new UnicodeFont(awtFont);
        font.getEffects().add(new ColorEffect(java.awt.Color.black));
        font.addAsciiGlyphs();
        try {
            font.loadGlyphs();
        } catch (SlickException e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }

    public void toggleRender()
    {
        if(shouldRenderFont){
            shouldRenderFont = false;
        }else{
            shouldRenderFont = true;
        }
    }
    public void toggleRender(boolean doRender)
    {
        shouldRenderFont = doRender;
    }

    public void renderFont(float x, float y, String text, FloatBuffer orthographicProjectionMatrix, FloatBuffer perspectiveProjectionMatrix)
    {
        if(!shouldRenderFont) { return; }

        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadMatrix(orthographicProjectionMatrix);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);

            GL11.glPushMatrix();

                GL11.glLoadIdentity();
                GL11.glDisable(GL11.GL_LIGHTING);

                font.drawString(x, y, text);

                GL11.glEnable(GL11.GL_LIGHTING);

            GL11.glPopMatrix();

        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadMatrix(perspectiveProjectionMatrix);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
    }
}

这就是我怎么称呼它:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bunchofpunch.game;

import bunchofpunch.fonts.FontVeranda;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.lwjgl.LWJGLException;
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.opengl.GLContext;

/**
 *
 * @author Alex
 */
public class GameScreenHUD extends GameScreen
{
    float space = 20f;
    float width = 250f;
    float height = 200f;

    World world;

    public GameScreenHUD(World world){this.world = world;}

    @Override
    public void renderScreen() {

        //this is a instace of "FontVeranda.java"
        world.font.renderFont(Game.WIDTH-width+50, Game.WIDTH-height-space-160, "My Text is here", 
        World.orthographicProjectionMatrix, World.perspectiveProjectionMatrix);

        glMatrixMode(GL_PROJECTION);
        glLoadMatrix(World.orthographicProjectionMatrix);
        glMatrixMode(GL_MODELVIEW);

            glPushMatrix();

                glLoadIdentity();
                glDisable(GL_LIGHTING);

                TextureManager.hud.bind();

                glBegin(GL_QUADS);

                    glTexCoord2f(0, 1);
                    glVertex2f(Game.WIDTH-space-width, Game.HEIGHT-space);
                    glTexCoord2f(1, 1);
                    glVertex2f(Game.WIDTH-space, Game.HEIGHT-space);
                    glTexCoord2f(1,0);
                    glVertex2f(Game.WIDTH-space, Game.HEIGHT-height-space);
                    glTexCoord2f(0, 0);
                    glVertex2f(Game.WIDTH-space-width, Game.HEIGHT-height-space);

                glEnd();

                glEnable(GL_LIGHTING);

            glPopMatrix();

        glMatrixMode(GL_PROJECTION);
        glLoadMatrix(World.perspectiveProjectionMatrix);
        glMatrixMode(GL_MODELVIEW);
    }

}

1 个答案:

答案 0 :(得分:0)

问题是文本是用应用于四边形的每个字母的纹理绘制的。必须在它们出现在前面的对象之后绘制任何透明对象。更改绘制顺序以最后绘制文本是唯一的好方法。基本上,您已经找到了解决方案。画第二个文字。虽然有一些方法可以在首先绘制文本时获得所需的效果,但我真的不推荐它们。