LWJGL 无渲染
LWJGL rendering nothing
我一遍又一遍地查看了以下程序,但找不到为什么当我 运行 它时没有任何内容呈现在屏幕上:
Game.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();
}
}
当我 运行 屏幕时,我只得到一个 window 填充黑色,我不明白为什么白色三角形不会渲染到 window。我弄乱了 glClearColor()
,更改它的值确实成功地更改了 window 背景颜色,但我无法在 window 中出现白色三角形。此外,我尝试使用 glBegin()...glEnd()
以即时模式呈现,其中 none 的代码必须与其中的缓冲区有关,但它仍然没有呈现任何内容。我真的很困惑,我错过了什么?
您 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);
由于您为 left 和 right 都传递了 0
,结果将是无效的投影矩阵。
您的案例的正确调用是:
glOrtho(0, DisplayManager.SCR_WIDTH, 0, DisplayManager.SCR_HEIGHT, 1, -1);
我一遍又一遍地查看了以下程序,但找不到为什么当我 运行 它时没有任何内容呈现在屏幕上:
Game.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();
}
}
当我 运行 屏幕时,我只得到一个 window 填充黑色,我不明白为什么白色三角形不会渲染到 window。我弄乱了 glClearColor()
,更改它的值确实成功地更改了 window 背景颜色,但我无法在 window 中出现白色三角形。此外,我尝试使用 glBegin()...glEnd()
以即时模式呈现,其中 none 的代码必须与其中的缓冲区有关,但它仍然没有呈现任何内容。我真的很困惑,我错过了什么?
您 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);
由于您为 left 和 right 都传递了 0
,结果将是无效的投影矩阵。
您的案例的正确调用是:
glOrtho(0, DisplayManager.SCR_WIDTH, 0, DisplayManager.SCR_HEIGHT, 1, -1);