我遇到一个问题,当我绘制文本并从 InitGl 类调用“callGl()”方法时,它不会在屏幕上绘制任何东西,如果我不调用它并绘制,例如,屏幕上的文本“播放”,它总是以我放置的相同颜色(例如 color.white)显示某种“背景”。我做错了什么?
代码示例
GL 类
public class InitGl {
public void InittGl(int Width, int Height) {
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glViewport(0,0,Width, Height);
glMatrixMode(GL_MODELVIEW);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, Width, Height, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
public void callGl() {
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1.0f);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
} //This is for the render thing
}
我在哪里调用它
菜单类
public class Menu {
Words p = new Words();
public void setWords() {
p.setWords("Comic Sans", 24, false);
}
public void Update() {
p.renderWord("PLAY", 10, 20);
}
}
使用 CALLGL 筛选 ADM 类
public class Screen {
private InitGl in = new InitGl();
private Menu m;
private int numNull = 0; //Just an example
public Screen () {
m = new Menu();
}
public void Menu() {
if (numNull == 0) {
in.InittGl(800, 600); //It gets the screen size
m.setWords();
numNull = 1;
}
m.Update();
}
public void Render() {
in.callGL();
m.Render(in);
}
}
在没有 CALLGL 的情况下筛选 ADM 类
public class Screen {
private InitGl in = new InitGl();
private Menu m;
private int numNull = 0; //Just an example
public Screen () {
m = new Menu();
}
public void Menu() {
if (numNull == 0) {
in.InittGl(800, 600); //It gets the screen size
m.setWords();
numNull = 1;
}
m.Update();
}
public void Render() {
m.Render(in);
}
}
单词课
public class Words {
public TrueTypeFont font;
public Font awtFont;
public void setWords(String type, int size, boolean antiAliazing) {
awtFont = new Font(type, Font.BOLD, size);
font = new TrueTypeFont(awtFont, antiAliazing);
}
public void renderWord(String phrase, int x, int y) {
font.drawString(x, y, phrase, Color.white);
}
}
图片
- 第一个是我使用 CallGL 时得到的结果
- 第二个是我不使用 CallGL 时得到的结果
最佳答案
第一个电话callGl()
然后画出你的框架...因为如果你在使用 glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
之前画任何东西然后你会得到一个黑屏:)
试着把m.Update();
在 render 方法中....因为你需要在每一帧中渲染你的文本......不只是一次 ;)
public void Render() {
in.callGL(); // <-------- prepare new frame
m.Update() // <-------- draw the text in the new frame
m.Render(in);
}
什么是m.Render(in);
意思是?我在 Menu 类中看不到那个方法
关于java - 绘制文本(字符串)时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27451893/