我一直在尝试使用漂亮的gui向openGl(通过LWJGL)应用程序添加一些基本的gui元素,虽然我已经成功地在应用程序图形之上渲染面板和静态文本,但是使用内置的-in漂亮的控件(例如可编辑的文本字段)导致应用程序的其余部分无法呈现。奇怪的是,我甚至不必渲染gui控件,只是声明它似乎会导致这个问题。
编译器就绪代码,显示问题的基本布局:
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.*;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.builder.LayerBuilder;
import de.lessvoid.nifty.builder.ScreenBuilder;
import de.lessvoid.nifty.builder.TextBuilder;
import de.lessvoid.nifty.controls.textfield.builder.TextFieldBuilder;
import de.lessvoid.nifty.nulldevice.NullSoundDevice;
import de.lessvoid.nifty.renderer.lwjgl.input.LwjglInputSystem;
import de.lessvoid.nifty.renderer.lwjgl.render.LwjglRenderDevice;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.tools.TimeProvider;
public class NiftyBreaksRendering {
private Nifty nifty;
private Screen screen;
public NiftyBreaksRendering() throws Exception{
//init display
Display.setDisplayMode(new DisplayMode(640,480));
Display.create();
//init nifty gui
LwjglInputSystem inputSystem = new LwjglInputSystem();
inputSystem.startup();
nifty = new Nifty(
new LwjglRenderDevice(),
new NullSoundDevice(),
inputSystem,
new TimeProvider());
// load default styles
nifty.loadStyleFile("nifty-default-styles.xml");
// load standard controls
nifty.loadControlFile("nifty-default-controls.xml");
screen = new ScreenBuilder("start") {{
layer(new LayerBuilder("baseLayer") {{
childLayoutHorizontal();
text(new TextBuilder("test"){{
font("aurulent-sans-16.fnt");
color("#f00f");
backgroundColor("#33af");
text("l33t");
}});
//begin: lines that break the rendering
control(new TextFieldBuilder("input","asdf") {{
width("200px");
}});
//end: lines that break the rendering
}});
}}.build(nifty);
nifty.gotoScreen("start");
//init opengl
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,640,480,0,1,-1);
glMatrixMode(GL_MODELVIEW);
while(!Display.isCloseRequested())
{
//render
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS);
glVertex2i(400,400); //Upper left
glVertex2i(450,400);//upper right
glVertex2i(450,450);//bottom right
glVertex2i(400,450);//bottom left
glEnd();
glBegin(GL_LINES);
glVertex2i(100,100);
glVertex2i(200,200);
glEnd();
nifty.render(false);
Display.update();
Display.sync(60);
}
Display.destroy();
}
public static void main(String[] args) throws Exception {
new NiftyBreaksRendering();
}
}
答案 0 :(得分:3)
诊断此类问题真正有用的是指向http://sscce.org/
的链接我想这与Nifty OR改变的OpenGL状态有关,Nifty控件加载的纹理可能会破坏你应用程序的其余部分。
如果您可以提供更多或完整的代码,我很确定我们可以找到问题。
提供完整的示例代码后修改后的答案:
感谢您提供完整的示例!
正如预期的那样,问题是,Nifty会改变OpenGL状态,并使OpenGL基本上处于未定义状态。解决方案是在调用Nifty之前保存OpenGL状态并在之后恢复它。这是一些完全正确的代码。我已经添加了对nifty.update()的调用,以便Nifty实际更新GUI(并使键盘和鼠标事件有效):
// update nifty
nifty.update();
// save your OpenGL state
// (assuming you are in glMatrixMode(GL_MODELVIEW) all the time)
glPushMatrix();
glPushAttrib(GL_ALL_ATTRIB_BITS);
// render Nifty (this will change OpenGL state)
nifty.render(false);
// restore your OpenGL state
glPopAttrib();
glPopMatrix();
通过对原始代码的更改,您的示例现在适用于我。
答案 1 :(得分:0)
NiftyGUI(作为非基于着色器的渲染)将在某些时候修改模型视图矩阵。你是否通过用身份初始化来清理它对模型视图矩阵所造成的损害?你知道,像这样:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLU.gluOrtho2D(0f,(float)VIEWPORT_DIMENSIONS[0],0f,(float)VIEWPORT_DIMENSIONS[1]);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(translation[0],translation[1],0);
glRectf(-1,-1,1,1);