我最近想出了如何使用 LWJGL 和 OpenGL 渲染 3D 立方体,我非常兴奋,我渲染了 2000 并有效地冻结了我的计算机。我听说过诸如显示列表和 VBO 之类的东西,但即使在谷歌搜索之后,我也不知道如何使用它们。
目前,我有
for (Block b : blocks) {
GL11.glTranslatef(b.position.x, b.position.y, b.position.z); // Translate to draw the cube.
GL11.glBegin(GL_QUADS); // Start to draw the quad.
b.render(); // Renders the quad.
GL11.glEnd(); // Finishes rendering.
}
渲染我的立方体。该调用b.render
仅呈现一个立方体
@Override
public void render() {
//front face
GL11.glNormal3f(0f, 0f, 1f);
GL11.glTexCoord2f(0f, 1f);
GL11.glVertex3f(1f, 0f, 0f);
GL11.glTexCoord2f(1f, 1f);
GL11.glVertex3f(0f, 0f, 0f);
GL11.glTexCoord2f(1f, 0f);
GL11.glVertex3f(0f, 1f, 0f);
GL11.glTexCoord2f(0f, 0f);
GL11.glVertex3f(1f, 1f, 0f);
//back face
GL11.glNormal3f(0f, 0f, -1f);
GL11.glTexCoord2f(0f, 1f);
GL11.glVertex3f(0f, 0f, 1f);
GL11.glTexCoord2f(1f, 1f);
GL11.glVertex3f(1f, 0f, 1f);
GL11.glTexCoord2f(1f, 0f);
GL11.glVertex3f(1f, 1f, 1f);
GL11.glTexCoord2f(0f, 0f);
GL11.glVertex3f(0f, 1f, 1f);
cap.bind(); // top texture
//top face
GL11.glNormal3f(0f, -1f, 0f);
GL11.glTexCoord2f(0f, 1f);
GL11.glVertex3f(1f, 1f, 0f);
GL11.glTexCoord2f(1f, 1f);
GL11.glVertex3f(0f, 1f, 0f);
GL11.glTexCoord2f(1f, 0f);
GL11.glVertex3f(0f, 1f, 1f);
GL11.glTexCoord2f(0f, 0f);
GL11.glVertex3f(1f, 1f, 1f);
//bottom face
GL11.glNormal3f(0f, 1f, 0f);
GL11.glTexCoord2f(0f, 1f);
GL11.glVertex3f(1f, 0f, 1f);
GL11.glTexCoord2f(1f, 1f);
GL11.glVertex3f(0f, 0f, 1f);
GL11.glTexCoord2f(1f, 0f);
GL11.glVertex3f(0f, 0f, 0f);
GL11.glTexCoord2f(0f, 0f);
GL11.glVertex3f(1f, 0f, 0f);
side.bind(); // left texture
//left face
GL11.glNormal3f(-1f, 0f, 0f);
GL11.glTexCoord2f(0f, 1f);
GL11.glVertex3f(1f, 0f, 1f);
GL11.glTexCoord2f(1f, 1f);
GL11.glVertex3f(1f, 0f, 0f);
GL11.glTexCoord2f(1f, 0f);
GL11.glVertex3f(1f, 1f, 0f);
GL11.glTexCoord2f(0f, 0f);
GL11.glVertex3f(1f, 1f, 1f);
//right face
GL11.glNormal3f(1f, 0f, 0f);
GL11.glTexCoord2f(0f, 1f);
GL11.glVertex3f(0f, 0f, 0f);
GL11.glTexCoord2f(1f, 1f);
GL11.glVertex3f(0f, 0f, 1f);
GL11.glTexCoord2f(1f, 0f);
GL11.glVertex3f(0f, 1f, 1f);
GL11.glTexCoord2f(0f, 0f);
GL11.glVertex3f(0f, 1f, 0f);
}
其中 cap 是顶部纹理,side 是侧面纹理。
我真正需要帮助的是弄清楚如何使我的代码 VBO 和/或显示列表兼容以提高性能。我还认为,如果可以只渲染可见的面孔,它将节省许多计算,但无法想到如何做这样的事情。
编辑:
我现在让每个块通过以下代码创建自己的 VBO 对象...
private int vboID;
private FloatBuffer vertices;
private float[] verts = {0, 1, 2, 2, 3, 0, 0, 3, 4, 4, 5, 0, 0, 5, 6, 6, 1, 0, 1, 6, 7, 7, 2, 1, 7, 4, 3, 3, 2, 7, 4, 7, 6, 6, 5, 4}; // Not sure on the numbers for making a cube?
private void generateVBO() {
vertices = BufferUtils.createFloatBuffer(verts.length); // Create a FloatBuffer...
vertices.put(verts); // Add all the vertices in...
vboID = GL15.glGenBuffers(); // Generate the VBO...
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID); // Bind it...
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertices, GL15.GL_STATIC_DRAW); // Buffer in the data...
verts = null; // Free up memory allocation because these are unnecessary now...
vertices = null;
System.out.println("VBO created with a vboID of '"+vboID+"'.");
}
在我的程序开始时,我创建了 1000 个块对象,因此我的输出有效地是“VBO create with a vboID of n”,1000 次。
我明白在程序结束时,我需要处理 VBO(我假设我只是GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0)
为每个 VBO 调用?)但首先,我需要绘制每个 VBO。
绘制 VBO 的命令是什么?