gpt4 book ai didi

java - 显示列表和 VBO OpenGL/LWJGL

转载 作者:太空宇宙 更新时间:2023-11-04 07:48:59 25 4
gpt4 key购买 nike

我最近弄清楚了如何使用 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 和/或显示列表兼容以提高性能。我还认为,如果可以只渲染可见的面孔,它将节省许多计算量,但不知道如何做这样的事情。

编辑:

我现在让每个 block 通过以下代码创建自己的 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 个 block 对象,因此我的输出实际上是“VBO create with a vboID of n”,1000 次。

我明白在程序结束时,我需要处理 VBO(我假设我只是为每个 VBO 调用 GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0)?),但首先,我需要绘制每个 VBO。

绘制VBO的命令是什么?

最佳答案

请不要使用显示列表,它们已经过时且已被弃用。有效地使用 VBO 需要使用顶点数组。使用顶点数组时,您将所有顶点位置和其他属性放入数组中,然后使用一次调用 glDrawArrays(假设数组只是要依次处理的一长串顶点)或 glDrawElements(接受一个附加数组,其中包含引用顶点数组中元素的索引列表)来批量绘制大量三角形。

这是有关顶点数组的教程:

http://www.songho.ca/opengl/gl_vertexarray.html

一旦你让顶点数组工作起来,这只是向 VBO 迈出的一小步。

关于java - 显示列表和 VBO OpenGL/LWJGL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14828748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com