LWJGL と OpenGL を使用して、テクスチャ付きの四角形を描画したいと考えています。その上で、Slick Library を使用してテキストを書きたいと思います。私の問題は、長方形の前にテキストを描画すると、フォントの背景が透明になることです。ここでそれを参照してください:
長方形の後にテキストを描画すると、それが見えなくなります。今、私は約1時間グーグルで検索し、いくつかの可能な解決策を見つけましたが、どれもうまくいきませんでした.
コード :
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);
}
}