【问题标题】:Opengl simple quad renderingOpengl 简单四边形渲染
【发布时间】:2021-06-29 15:54:41
【问题描述】:

你知道为什么这不起作用吗?

old 即时模式有效,但我想使用 VAO 和 VBO。

(PS:我知道VOA的创建应该只创建一次,但我都在这个方法中构建它以进行测试。我将在测试后移动这些行)

private void allocateIndexBuffer(GL2 graphics, int[] indices) {
    int[] id = new int[1];
    graphics.glGenBuffers(1, id, 0);
    int vboId = id[0];
    graphics.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, vboId);
        
    IntBuffer buffer = IntBuffer.allocate(indices.length);
    buffer.put(0, indices);
    buffer.flip();

    graphics.glBufferData(GL2.GL_ELEMENT_ARRAY_BUFFER, indices.length, buffer, GL2.GL_DYNAMIC_DRAW);
    
    //graphics.glDeleteBuffers(vboId, buffer); TODO: clean up when on closing
}

private void allocateAttributeBuffer(GL2 graphics, int attribute, float[] data) {
    int[] id = new int[1];
    graphics.glGenBuffers(1, id, 0);
    int vboId = id[0];
    graphics.glBindBuffer(GL2.GL_ARRAY_BUFFER, vboId); //juste remplir vboId ou le remplacer à chaque fois ?

    FloatBuffer buffer = FloatBuffer.allocate(data.length);
    buffer.put(0, data);
    buffer.flip();
    
    graphics.glBufferData(GL2.GL_ARRAY_BUFFER, data.length, buffer, GL2.GL_DYNAMIC_DRAW);
    graphics.glVertexAttribPointer(0, 2, GL2.GL_FLOAT, false, 0, 0); //once the buffer is bound
    graphics.glEnableVertexAttribArray(0);
    graphics.glBindBuffer(GL2.GL_ARRAY_BUFFER, 0);
    
    //graphics.glDeleteBuffers(vboId, buffer); TODO: clean up when on closing
    //graphics.glDeleteVertexArrays(vboId, null); TODO: clean up vaos
}

@Override
protected void draw(GL2 graphics) {
    String mode = "new";
    if (mode.equals("new")) {
        float[] vertices = {
            bounds.getX(), bounds.getY(),
            bounds.getX(), bounds.getY() + bounds.getHeight(),
            bounds.getX() + bounds.getWidth(), bounds.getY() + bounds.getHeight(),
            bounds.getX() + bounds.getWidth(), bounds.getY(),
        };

        int[] indices = { 0, 1, 2, 2, 1, 3 };

        //creation vao
        int[] id = new int[1];
        graphics.glGenVertexArrays(1, id, 0);
        int vaoId = id[0];

        graphics.glBindVertexArray(vaoId);
            allocateIndexBuffer(graphics, indices);
            allocateAttributeBuffer(graphics, 0, vertices);
        graphics.glBindVertexArray(0);

        //render
        graphics.glBindVertexArray(vaoId);
            graphics.glEnableVertexAttribArray(0);
                graphics.glDrawElements(GL2.GL_TRIANGLES, indices.length, GL2.GL_UNSIGNED_INT, 0);
            graphics.glDisableVertexAttribArray(0);
        graphics.glBindVertexArray(0);
        graphics.glFlush();

    } else if (mode.equals("old")) {
        graphics.glColor3f(255, 0, 0);
        graphics.glBegin(GL2.GL_QUADS);
            graphics.glVertex2f(bounds.getX(), bounds.getY());
            graphics.glVertex2f(bounds.getX() + bounds.getWidth(), bounds.getY());
            graphics.glVertex2f(bounds.getX() + bounds.getWidth(), bounds.getY() + bounds.getHeight());
            graphics.glVertex2f(bounds.getX(), bounds.getY() + bounds.getHeight());
        graphics.glEnd();
    }
}

【问题讨论】:

标签: opengl gpu 2d jogl


【解决方案1】:

缓冲区的大小必须以字节为单位指定(参见glBufferData);

graphics.glBufferData(GL2.GL_ELEMENT_ARRAY_BUFFER, indices.length, buffer, GL2.GL_DYNAMIC_DRAW);

graphics.glBufferData(GL2.GL_ELEMENT_ARRAY_BUFFER, indices.capacity() * 4,
    buffer, GL2.GL_DYNAMIC_DRAW);

graphics.glBufferData(GL2.GL_ARRAY_BUFFER, data.length, buffer, GL2.GL_DYNAMIC_DRAW);

graphics.glBufferData(GL2.GL_ARRAY_BUFFER, data.capacity() * 4,
    buffer, GL2.GL_DYNAMIC_DRAW);

【讨论】:

  • 行得通! 4 个字节,因为我想要整数和浮点数,我明白了。我喜欢渲染三角形,但这是一个很好的改进,我被卡住了!
  • 我改变了这样的索引: int[] indices = { 0, 1, 3, 3, 1, 2 };而且效果很好
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多