glDrawElements 不绘制任何东西
glDrawElements doesn't draw anything
我对 VBO、VAO 和指数还很陌生。我能够渲染单个立方体,现在我正在尝试渲染一大块立方体。我的目标是慢慢做一个体素引擎。我的块 class 有问题。由于某种原因,它不显示任何内容。任何人都可以快速浏览一下并找出问题所在并向我指出吗?干杯
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
}
}
我不确定我是否可以立即提供解决方案,因为问题不是很具体,但我可以提供一些可能帮助您解决问题的一般性建议:
- 你的块 class 应该只有一个 vaoID(整个块应该用一个绘制调用渲染)。它可以有多个与那个 vaoID 关联的 vboID。关键是,不需要使用 IntBuffers 来存储它们,如果您明确命名每个 vboID,通常会使事情更有条理。
在使用 BufferUtils 创建 FloatBuffer 对象并将数据加载到其中之后,您必须对所有对象调用 .flip() 以便 OpenGL 知道它们已准备好使用。 (这很可能是主要问题,可能只是问题)
作为对程序其余部分的一般礼貌,您应该禁用在绘制方法期间启用的任何属性并取消绑定任何已绑定的 VAOS。 (我相信当你在绑定 VAO 的同时绑定一个缓冲区对象时,该缓冲区仅在 VAO 也被绑定时才绑定。我找不到支持这一点的文档,所以为了安全起见,我也会解除绑定缓冲区对象在你完成它们之后)
下面是我认为应该为您提供的块的有效实现 class:
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 的专家。请对我的回答持保留态度,因为这些答案几乎完全来自我的 educational/personal 经验。
我对 VBO、VAO 和指数还很陌生。我能够渲染单个立方体,现在我正在尝试渲染一大块立方体。我的目标是慢慢做一个体素引擎。我的块 class 有问题。由于某种原因,它不显示任何内容。任何人都可以快速浏览一下并找出问题所在并向我指出吗?干杯
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
}
}
我不确定我是否可以立即提供解决方案,因为问题不是很具体,但我可以提供一些可能帮助您解决问题的一般性建议:
- 你的块 class 应该只有一个 vaoID(整个块应该用一个绘制调用渲染)。它可以有多个与那个 vaoID 关联的 vboID。关键是,不需要使用 IntBuffers 来存储它们,如果您明确命名每个 vboID,通常会使事情更有条理。
在使用 BufferUtils 创建 FloatBuffer 对象并将数据加载到其中之后,您必须对所有对象调用 .flip() 以便 OpenGL 知道它们已准备好使用。 (这很可能是主要问题,可能只是问题)
作为对程序其余部分的一般礼貌,您应该禁用在绘制方法期间启用的任何属性并取消绑定任何已绑定的 VAOS。 (我相信当你在绑定 VAO 的同时绑定一个缓冲区对象时,该缓冲区仅在 VAO 也被绑定时才绑定。我找不到支持这一点的文档,所以为了安全起见,我也会解除绑定缓冲区对象在你完成它们之后)
下面是我认为应该为您提供的块的有效实现 class:
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 的专家。请对我的回答持保留态度,因为这些答案几乎完全来自我的 educational/personal 经验。