glDrawElements不会绘制任何东西
问题描述:
我对VBOs,VAO和Indices很新颖。我能渲染一个单一的立方体,现在我正在尝试渲染一大块立方体。我的目标是慢慢制作一个体素引擎。我的大块类有问题。由于某些原因,它不显示任何内容。任何人都可以快速查看,也许弄清楚什么是错误的,并指出了我?干杯glDrawElements不会绘制任何东西
class Chunk {
private IntBuffer vaoID;
private IntBuffer vboID;
private IntBuffer indexID;
public void createChunkVBO() {
FloatBuffer vertices = BufferUtils.createFloatBuffer(16 * 256 * 16 * 3 * 8);
FloatBuffer colors = BufferUtils.createFloatBuffer(16 * 256 * 16 * 4 * 8);
FloatBuffer indices = BufferUtils.createFloatBuffer(16 * 256 * 16 * 4 * 6);
vaoID = BufferUtils.createIntBuffer(1); // Create a buffer for the Vertex Array Object
vboID = BufferUtils.createIntBuffer(1); // Create a buffer for the Vertex Buffer Object
indexID = BufferUtils.createIntBuffer(1); // Create a buffer for the Vertex Indices
for (int x = 0; x < 16; x++) {
for (int y = 0; y < 256; y++) {
for (int z = 0; z < 16; z++) {
System.out.println(x + ", " + y + ", " + z);
vertices.put(x + World.BLOCK_SIZE);
vertices.put(y);
vertices.put(z + World.BLOCK_SIZE);
vertices.put(x);
vertices.put(y);
vertices.put(z + World.BLOCK_SIZE);
vertices.put(x);
vertices.put(y);
vertices.put(z);
vertices.put(x + World.BLOCK_SIZE);
vertices.put(y);
vertices.put(z);
vertices.put(x + World.BLOCK_SIZE);
vertices.put(y + World.BLOCK_SIZE);
vertices.put(z + World.BLOCK_SIZE);
vertices.put(x);
vertices.put(y + World.BLOCK_SIZE);
vertices.put(z + World.BLOCK_SIZE);
vertices.put(x);
vertices.put(y + World.BLOCK_SIZE);
vertices.put(z);
vertices.put(x + World.BLOCK_SIZE);
vertices.put(y + World.BLOCK_SIZE);
vertices.put(z);
colors.put(1f);
colors.put(0f);
colors.put(0f);
colors.put(1f);
colors.put(1f);
colors.put(0f);
colors.put(0f);
colors.put(1f);
colors.put(1f);
colors.put(0f);
colors.put(0f);
colors.put(1f);
colors.put(1f);
colors.put(0f);
colors.put(0f);
colors.put(1f);
colors.put(1f);
colors.put(0f);
colors.put(0f);
colors.put(1f);
colors.put(1f);
colors.put(0f);
colors.put(0f);
colors.put(1f);
colors.put(1f);
colors.put(0f);
colors.put(0f);
colors.put(1f);
colors.put(1f);
colors.put(0f);
colors.put(0f);
colors.put(1f);
indices.put(0 + x * y * z);
indices.put(1 + x * y * z);
indices.put(2 + x * y * z);
indices.put(3 + x * y * z);
indices.put(4 + x * y * z);
indices.put(5 + x * y * z);
indices.put(2 + x * y * z);
indices.put(3 + x * y * z);
indices.put(1 + x * y * z);
indices.put(3 + x * y * z);
indices.put(7 + x * y * z);
indices.put(5 + x * y * z);
indices.put(0 + x * y * z);
indices.put(3 + x * y * z);
indices.put(4 + x * y * z);
indices.put(7 + x * y * z);
indices.put(0 + x * y * z);
indices.put(1 + x * y * z);
indices.put(6 + x * y * z);
indices.put(7 + x * y * z);
indices.put(4 + x * y * z);
indices.put(5 + x * y * z);
indices.put(6 + x * y * z);
indices.put(7 + x * y * z);
}
}
}
glGenVertexArrays(vaoID); // Create an id for the VAO
glBindVertexArray(vaoID.get(0)); // Bind the VAO so it remembers all the Attributes (none right now)
glGenBuffers(vboID); // Create an id for the VBO
glBindBuffer(GL_ARRAY_BUFFER, vboID.get(0)); // Bind the VBO so we can put data into it
glBufferData(GL_ARRAY_BUFFER, 16 * 256 * 16 * 8 * 7 * Float.SIZE, GL_STATIC_DRAW); // We make an empty buffer with a specific size in bytes
// 8 * 7 * sizeof(float)
// 8 = number of vertices, 7 = xyzrgba
glBufferSubData(GL_ARRAY_BUFFER, 0, vertices); // Put the vertices at the beginning of the buffer
glBufferSubData(GL_ARRAY_BUFFER, 16 * 256 * 16 * 8 * 3 * Float.SIZE, colors); // Put the colors after the vertices
glGenBuffers(indexID); // Create an id for the Index Buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexID.get(0)); // Bind the Index Buffer so we can put data into it
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices, GL_STATIC_DRAW); // Store the indices inside the currently bound Index Buffer
}
public void drawChunk() {
glEnableClientState(GL_VERTEX_ARRAY); // Enable the Vertex Array
glEnableClientState(GL_COLOR_ARRAY); // Enable the Color Array
glVertexPointer(3, GL_FLOAT, 0, 0);
glColorPointer(4, GL_FLOAT, 0, 16 * 256 * 16 * 8 * 3 * Float.SIZE); // Position of the colors in the currently bound buffer
glDrawElements(GL_QUADS, 24 * 16 * 256 * 16, GL_UNSIGNED_INT, 0); // Draws the elements from the Index Buffer
}
}
答
我不知道,我可以提供一个直接的解决方案,因为这个问题是不是很具体,但我可以提供,可以帮助您解决问题的一些普遍性的建议:
- 你的块类应该只有一个vaoID(整个块应该用一次绘制调用渲染)。它可以有多个与该vaoID关联的vboID。关键是,不需要使用IntBuffers来存储它们,并且如果明确指定每个vboID的话,它通常会使事情更加有组织。
使用BufferUtils创建FloatBuffer对象并将数据加载到这些对象中后,必须在所有对象上调用.flip(),以便OpenGL知道它们已准备好使用。 (这很可能只是主问题)
作为您的程序其余部分的一般礼貌,您应该禁用在绘制方法中启用的任何属性,并解除绑定的任何VAOS。 (我相信当绑定一个VAO绑定缓冲区对象时,该缓冲区仅在VAO绑定时绑定,我找不到要备份的文档,为了安全起见,我还要解除绑定缓冲区对象你与他们完成后)
-
下面是我认为应该给你的组块类的工作实现:
class Chunk { private int vaoID; private int vboID; private int indexID; public void createChunkVBO() { FloatBuffer vertices = BufferUtils.createFloatBuffer(16 * 256 * 16 * 3 * 8); FloatBuffer colors = BufferUtils.createFloatBuffer(16 * 256 * 16 * 4 * 8); FloatBuffer indices = BufferUtils.createFloatBuffer(16 * 256 * 16 * 24); // I am assuming that all of this is generated properly for (int x = 0; x < 16; x++) { for (int y = 0; y < 256; y++) { for (int z = 0; z < 16; z++) { System.out.println(x + ", " + y + ", " + z); vertices.put(x + World.BLOCK_SIZE); vertices.put(y); vertices.put(z + World.BLOCK_SIZE); vertices.put(x); vertices.put(y); vertices.put(z + World.BLOCK_SIZE); vertices.put(x); vertices.put(y); vertices.put(z); vertices.put(x + World.BLOCK_SIZE); vertices.put(y); vertices.put(z); vertices.put(x + World.BLOCK_SIZE); vertices.put(y + World.BLOCK_SIZE); vertices.put(z + World.BLOCK_SIZE); vertices.put(x); vertices.put(y + World.BLOCK_SIZE); vertices.put(z + World.BLOCK_SIZE); vertices.put(x); vertices.put(y + World.BLOCK_SIZE); vertices.put(z); vertices.put(x + World.BLOCK_SIZE); vertices.put(y + World.BLOCK_SIZE); vertices.put(z); colors.put(1f); colors.put(0f); colors.put(0f); colors.put(1f); colors.put(1f); colors.put(0f); colors.put(0f); colors.put(1f); colors.put(1f); colors.put(0f); colors.put(0f); colors.put(1f); colors.put(1f); colors.put(0f); colors.put(0f); colors.put(1f); colors.put(1f); colors.put(0f); colors.put(0f); colors.put(1f); colors.put(1f); colors.put(0f); colors.put(0f); colors.put(1f); colors.put(1f); colors.put(0f); colors.put(0f); colors.put(1f); colors.put(1f); colors.put(0f); colors.put(0f); colors.put(1f); indices.put(0 + x * y * z); indices.put(1 + x * y * z); indices.put(2 + x * y * z); indices.put(3 + x * y * z); indices.put(4 + x * y * z); indices.put(5 + x * y * z); indices.put(2 + x * y * z); indices.put(3 + x * y * z); indices.put(1 + x * y * z); indices.put(3 + x * y * z); indices.put(7 + x * y * z); indices.put(5 + x * y * z); indices.put(0 + x * y * z); indices.put(3 + x * y * z); indices.put(4 + x * y * z); indices.put(7 + x * y * z); indices.put(0 + x * y * z); indices.put(1 + x * y * z); indices.put(6 + x * y * z); indices.put(7 + x * y * z); indices.put(4 + x * y * z); indices.put(5 + x * y * z); indices.put(6 + x * y * z); indices.put(7 + x * y * z); } } } vertices.flip(); colors.flip(); indices.flip(); glGenVertexArrays(vaoID); // Create an id for the VAO glBindVertexArray(vaoID); // Bind the VAO so it remembers all the Attributes (none right now) glGenBuffers(vboID); // Create an id for the VBO glBindBuffer(GL_ARRAY_BUFFER, vboID); // Bind the VBO so we can put data into it glBufferData(GL_ARRAY_BUFFER, 16 * 256 * 16 * 8 * 7 * Float.SIZE, GL_STATIC_DRAW); // We make an empty buffer with a specific size in bytes // 8 * 7 * sizeof(float) // 8 = number of vertices, 7 = xyzrgba // I have not used subdata like this before so I will assume this is correct. glBufferSubData(GL_ARRAY_BUFFER, 0, vertices); // Put the vertices at the beginning of the buffer glBufferSubData(GL_ARRAY_BUFFER, 16 * 256 * 16 * 8 * 3 * Float.SIZE, colors); // Put the colors after the vertices glGenBuffers(indexID); // Create an id for the Index Buffer glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexID); // Bind the Index Buffer so we can put data into it glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices, GL_STATIC_DRAW); // Store the indices inside the currently bound Index Buffer } public void drawChunk() { glEnableClientState(GL_VERTEX_ARRAY); // Enable the Vertex Array glEnableClientState(GL_COLOR_ARRAY); // Enable the Color Array glVertexPointer(3, GL_FLOAT, 0, 0); glColorPointer(4, GL_FLOAT, 0, 16 * 256 * 16 * 8 * 3 * Float.SIZE); // Position of the colors in the currently bound buffer glDrawElements(GL_QUADS, 8 * 16 * 256 * 24, GL_UNSIGNED_INT, 0); // Draws the elements from the Index Buffer } }
请随时免费的,如果您有任何疑问,请联系我关于我所说的任何事情。
声明:我不是OpenGL或LWJGL的专家。请带上我的答案,因为这些答案几乎完全来自我的教育/个人经验。
谢谢你的精彩答案!我确实已经忘记翻转FloatBuffers。翻转他们部分地解决了这个问题。当我使用glDrawArrays时,我可以正确渲染四边形。但是,每个立方体只有2个四边形,因为我想使用索引。由于某种原因,glDrawElements似乎不起作用。我的猜测是,我搞砸了为每个立方体添加索引。感谢您提供所有重要提示!你有没有可能看看我如何存储索引,因为你对OpenGL的总体经验比我更有经验。我真的很感激! –
我已阅读您的评论,并会很快进行测试。我在家里的桌面上需要安装Eclipse和JDK,我甚至可以进行简单的测试,大声笑......当我看完它时,我会回复你并编辑我的答案。 –
我必须道歉,我没有时间在工作和学校之间及时回答这个问题。我已经更正了index初始化的行和glDrawElements行的索引缓冲区大小。这可以解决你的问题。如果不是的话,我会建议先从一个小例子开始,然后继续工作到一个块。例如,以一个立方体开始,然后是一排立方体,然后是一个立方体网格,然后是一大块立方体。 –