glDrawElements 不绘制任何东西
Posted
技术标签:
【中文标题】glDrawElements 不绘制任何东西【英文标题】:glDrawElements doesn't draw anything 【发布时间】:2017-06-08 08:11:02 【问题描述】:我对 VBO、VAO 和指数非常陌生。我能够渲染一个立方体,现在我正在尝试渲染一大块立方体。我的目标是慢慢制作体素引擎。我的块类有问题。由于某种原因,它不显示任何内容。任何人都可以快速浏览一下并找出问题所在并向我指出吗?干杯
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
【问题讨论】:
【参考方案1】:我不确定是否可以立即提供解决方案,因为问题不是很具体,但我可以提供一些一般性建议,可能会帮助您解决问题:
-
您的块类应该只有一个 vaoID(整个块应该使用一次绘制调用呈现)。它可以有多个与那个 vaoID 关联的 vboID。关键是,不需要使用 IntBuffers 来存储它们,如果您明确命名每个 vboID,它通常会使事情更有条理。
在使用 BufferUtils 创建 FloatBuffer 对象并将数据加载到其中之后,必须对所有对象调用 .flip() 以便 OpenGL 知道它们已准备好使用。 (这很可能是主要问题,也可能是唯一问题)
作为对程序其余部分的一般礼貌,您应该禁用在绘制方法期间启用的任何属性,并取消绑定任何绑定的 VAOS。 (我相信当您在绑定 VAO 时绑定缓冲区对象时,该缓冲区仅在 VAO 也绑定时才绑定。我找不到支持它的文档,所以为了安全起见,我也会取消绑定缓冲区对象完成后)
以下是我认为应该为您提供 Chunk 类的有效实现的内容:
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,然后才能进行简单的测试,哈哈……当我看到它时,我会回复你并编辑我的答案。 上班和上学之间没有时间及时回答这个问题,不得不道歉。我已经在初始化索引的行和使用 glDrawElements 的行上更正了索引缓冲区大小。这可以解决您的问题。如果没有,我建议先从一个小例子开始,然后再逐步完成。例如,从一个立方体开始,然后是一行立方体,然后是一个立方体网格,然后是一大块立方体。 抱歉回复晚了,我在医院躺着。非常感谢您为回答这个问题所付出的所有时间。不幸的是,这并没有解决问题。按照您的指示,我从一个小例子开始。再次感谢!以上是关于glDrawElements 不绘制任何东西的主要内容,如果未能解决你的问题,请参考以下文章
我的OpenGL学习进阶之旅使用glDrawElements绘制的时候,不绘制任何图元,报错 1282 即 GL_INVALID_OPERATION
我的OpenGL学习进阶之旅使用glDrawElements绘制的时候,不绘制任何图元,报错 1282 即 GL_INVALID_OPERATION
使用 glDrawElements 从 WRL (VRML) 文件中绘制对象