我一遍又一遍地查看以下程序,但找不到为什么运行它时屏幕上没有呈现任何内容:
游戏.java:
public class Game {
private int fbid, fbid2;
public void start(){
DisplayManager.init();
glViewport(0, 0, DisplayManager.SCR_WIDTH, DisplayManager.SCR_HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 0, DisplayManager.SCR_WIDTH, DisplayManager.SCR_HEIGHT, 1, -1);
glMatrixMode(GL_MODELVIEW);
FloatBuffer fb = BufferUtils.createFloatBuffer(6);
fb.put(100).put(100).put(100).put(200).put(200).put(200);
fb.flip();
fbid = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, fbid);
glBufferData(GL_ARRAY_BUFFER, fb, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
FloatBuffer fb2 = BufferUtils.createFloatBuffer(9);
fb2.put(1).put(1).put(1).put(1).put(1).put(1).put(1).put(1).put(1);
fb2.flip();
fbid2 = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, fbid2);
glBufferData(GL_ARRAY_BUFFER, fb2, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
while(!DisplayManager.shouldClose()){
render();
}
DisplayManager.destroy();
}
public void render(){
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0, 0, 0, 0);
glColor3f(1, 1, 1);
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, fbid);
glVertexPointer(2, GL_FLOAT, 0, 0);
glEnableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, fbid2);
glColorPointer(3, GL_FLOAT, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
DisplayManager.update();
}
}
DisplayManager.java:
public class DisplayManager {
public static final int SCR_WIDTH = 640;
public static final int SCR_HEIGHT = 480;
public static final int FPS = 30;
public static void init(){
try {
Display.setDisplayMode(new DisplayMode(SCR_WIDTH, SCR_HEIGHT));
Display.setTitle("StepBattler");
Display.setResizable(false);
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
Display.destroy();
System.exit(1);
}
}
public static void update(){
Display.update();
Display.sync(FPS);
}
public static void destroy(){
Display.destroy();
System.exit(0);
}
public static boolean shouldClose(){
return Display.isCloseRequested();
}
public static boolean shouldResize(){
return Display.wasResized();
}
}
当我运行屏幕时,我只是得到一个充满黑色的窗口,并且我无法理解为什么白色三角形不会渲染到窗口。我已经搞乱了 glClearColor()
,并且更改其值确实成功地更改了窗口背景颜色,但我无法使白色三角形出现在窗口中。另外,我尝试使用 glBegin()...glEnd()
在立即模式下渲染,没有任何代码与其中的缓冲区有关,并且它仍然没有渲染任何内容。我对此感到非常困惑,我错过了什么?
最佳答案
glOrtho()
调用中的参数看起来顺序错误:
glOrtho(0, 0, DisplayManager.SCR_WIDTH, DisplayManager.SCR_HEIGHT, 1, -1);
glOrtho()
的签名,基于 man page ,是:
void glOrtho(GLdouble left, GLdouble right,
GLdouble bottom, GLdouble top,
GLdouble nearVal, GLdouble farVal);
由于您要为左和右传递0
,因此结果将是无效的投影矩阵。
您的案例的正确调用是:
glOrtho(0, DisplayManager.SCR_WIDTH, 0, DisplayManager.SCR_HEIGHT, 1, -1);
关于java - LWJGL 渲染无任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38283142/