0

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

在此处输入图像描述

如果我在矩形之后绘制文本,我看不到它。现在我用谷歌搜索了大约一个小时,找到了几种可能的解决方案,但没有一个对我有用。

编码 :

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);
    }

}
4

1 回答 1

0

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

于 2013-02-07T19:18:51.427 回答