我正在尝试了解顶点缓冲区对象。我已经能够在 2D 空间中成功渲染四边形(glOrtho
在初始化期间使用),但是在使用gluPerspective
.
我正在使用 Java 和 LWJGL,并在下面附上了我的代码。目前,只有黑色透明渲染到窗口。
public class GameWindow {
// Height and width of the viewport
private final int WIDTH = 800;
private final int HEIGHT = 600;
private final float zNear = 1f;
private final float zFar = 1000f;
long lastFrame, lastFps;
int fps;
public void start() {
try {
Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90, WIDTH / HEIGHT, zNear, zFar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
float[] quadCoords = {
100, 100, -1,
300, 100, -1,
300, 300, -1,
100, 300, -1
};
float[] colorCoords = {
1, 0, 0,
0, 1, 0,
0, 0, 1,
1, 0, 1
};
FloatBuffer vertexData = BufferUtils.createFloatBuffer(4 * 3);
vertexData.put(quadCoords);
vertexData.flip();
FloatBuffer colorData = BufferUtils.createFloatBuffer(4 * 3);
colorData.put(colorCoords);
colorData.flip();
int vboVertex = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboVertex);
glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
int vboColor = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboColor);
glBufferData(GL_ARRAY_BUFFER, colorData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
getDelta();
lastFps = TimeUtil.getTime();
while (!Display.isCloseRequested()) {
int delta = getDelta();
updateFPS();
glClear(GL_COLOR_BUFFER_BIT);
glBindBuffer(GL_ARRAY_BUFFER, vboVertex);
glVertexPointer(3, GL_FLOAT, 0, 0L);
glBindBuffer(GL_ARRAY_BUFFER, vboColor);
glColorPointer(3, GL_FLOAT, 0, 0L);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glDrawArrays(GL_QUADS, 0, 4);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
Display.update();
Display.sync(60);
}
Display.destroy();
}
private int getDelta() {
long time = TimeUtil.getTime();
int delta = (int) (time - lastFrame);
lastFrame = time;
return delta;
}
public void updateFPS() {
if (TimeUtil.getTime() - lastFps > 1000) {
Display.setTitle("FPS: " + fps);
fps = 0;
lastFps += 1000;
}
fps++;
}
public static void main(String[] args) {
GameWindow window = new GameWindow();
window.start();
}
}