glDrawElements不会绘制任何东西

时间:2017-06-08 08:11:02

标签: java opengl vbo

我对VBO,VAO和指数都很陌生。我能够渲染一个立方体,现在我正在尝试渲染一大块立方体。我的目标是慢慢制作一个体素引擎。我的chunk类有问题。由于某种原因,它不会显示任何内容。任何人都可以快速查看,也许可以弄清楚什么是错的,并指出给我?干杯

[tableView reloadData]

1 个答案:

答案 0 :(得分:2)

我不确定我能提供即时解决方案,因为问题不是很具体,但我可以提供一些可以帮助您解决问题的一般性建议:

  1. 你的chunk类应该只有一个vaoID(整个chunk应该通过一次绘制调用来呈现)。它可以具有与该一个vaoID相关联的多个vboID。关键是,没有必要使用IntBuffers来存储它们,如果你明确命名每个vboID,它通常会使事情更有条理。
  2. 使用BufferUtils创建FloatBuffer对象并将数据加载到它们之后,必须在所有这些对象上调用.flip(),以便OpenGL知道它们已准备好使用。 (这很可能是MAIN,可能只是问题)

  3. 作为对程序其余部分的一般礼貌,您应该禁用在绘制方法期间启用的任何属性,并取消绑定任何绑定的VAOS。 (我相信在绑定VAO时绑定缓冲区对象时,该缓冲区仅在VAO绑定时绑定。我找不到备份文档,所以为了安全起见我还要取消绑定缓冲区对象在你完成它们之后)

  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
    }
    }
    
  5. 如果您对我所说的任何事情有任何疑问,请随时与我联系。

    免责声明:我不是OpenGL或LWJGL的专家。请尽量回答我的答案,因为这些答案几乎完全来自我的教育/个人经历。