Opengl 简单四边形渲染

问题描述

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

即时模式有效,但我想使用 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,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,GL2.GL_DYNAMIC_DRAW);
    graphics.glVertexAttribPointer(0,2,GL2.GL_FLOAT,false,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(),};

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

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

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

        //render
        graphics.glBindVertexArray(vaoId);
            graphics.glEnabLevertexAttribArray(0);
                graphics.glDrawElements(GL2.GL_TRIANGLES,GL2.GL_UNSIGNED_INT,0);
            graphics.gldisabLevertexAttribArray(0);
        graphics.glBindVertexArray(0);
        graphics.glFlush();

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

解决方法

必须以字节为单位指定缓冲区的大小(请参阅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,GL2.GL_DYNAMIC_DRAW);

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

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