gpt4 book ai didi

java - glDrawElements 不绘制任何东西

转载 作者:行者123 更新时间:2023-12-02 02:46:57 26 4
gpt4 key购买 nike

我对 VBO、VAO 和指数还很陌生。我能够渲染单个立方体,现在我尝试渲染一大块立方体。我的目标是慢慢制作一个体素引擎。我的 block 类有问题。由于某种原因它不显示任何内容。任何人都可以快速浏览一下,找出问题所在并向我指出吗?干杯

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. 您的 block 类应该只有一个 vaoID(整个 block 应该通过一次绘制调用来渲染)。它可以有多个与该 vaoID 关联的 vboID。关键是,不需要使用 IntBuffers 来存储它们,如果您明确命名每个 vboID,通常会让事情变得更有条理。
  2. 使用 BufferUtils 创建 FloatBuffer 对象并将数据加载到其中后,必须对所有对象调用 .flip(),以便 OpenGL 知道它们已准备好使用。 (这很可能是主要的,也可能是唯一的问题)

  3. 出于对程序其余部分的一般礼貌,您应该禁用在绘制方法期间启用的任何属性,并取消绑定(bind)任何已绑定(bind)的 VAOS。 (我相信,当您在绑定(bind) VAO 时绑定(bind)缓冲区对象时,只有当 VAO 也绑定(bind)时,该缓冲区才会绑定(bind)。我找不到支持这一点的文档,所以为了安全起见,我也会取消绑定(bind)缓冲区对象完成后)

  4. 下面是我认为应该为您提供 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 方面的专家。请对我的回答持保留态度,因为这些答案几乎完全来 self 的教育/个人经历。

关于java - glDrawElements 不绘制任何东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44430161/

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