我正在尝试了解顶点缓冲对象。我已经能够在2D空间中成功渲染四边形(在初始化期间使用glOrtho
),但在使用gluPerspective
渲染时使用VBO时遇到问题。
我正在使用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();
}
}
答案 0 :(得分:1)
我认为你的四边形顶点不在视野范围内。如果没有设置视图矩阵(例如,通过gluLookAt),摄像机将指向-Z轴。您要求zNear和zFar为1.0和1000.0,因此将绘制z坐标-1.0和-1000.0之间的所有内容(zNear和zFar类似于相机的“距离”)。您还必须考虑到透视投影与正字法有很大不同。我会说,尝试将你的顶点移回Z轴,如下所示:
float[] quadCoords = {
100, 100, -500,
300, 100, -500,
300, 300, -500,
100, 300, -500
};
答案 1 :(得分:0)
您的近剪裁平面为1,但渲染所有z坐标为-1