LWJGL:禁用元素阵列缓冲区对象时不能使用偏移量

时间:2016-07-29 23:35:51

标签: java opengl lwjgl

我目前正在学习使用LWJGL的openGL。我想用索引渲染模型,但是有一个异常显示......

“禁用元素阵列缓冲区对象时无法使用偏移”

我不确定我是否知道这意味着什么,但我认为我将索引缓冲区绑定到模型的vao。 这是我的代码:

渲染

public class Renderer {


    public void prepare(){
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
        GL11.glClearColor(1, 0, 0, 1);
    }


    public void render(RawModel model){
        GL30.glBindVertexArray(model.getVaoID());
        GL20.glEnableVertexAttribArray(0);
        GL11.glDrawElements(GL11.GL_TRIANGLES, model.getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
        GL20.glDisableVertexAttribArray(0);
        GL30.glBindVertexArray(0);
    }
}

装载机

public class Loader {


    private List<Integer> vaoIDs = new ArrayList<Integer>();
    private List<Integer> vboIDs = new ArrayList<Integer>();


    public RawModel loadToVAO(float[] positions, int[] indices){
        int vaoID = createVAO();
        bindIndicesBuffer(indices);
        storeDataInAttributeList(0, positions);
        unbindVAO();
        return new RawModel(vaoID, indices.length);

    }


    private int createVAO(){
        int vaoID = GL30.glGenVertexArrays();
        vaoIDs.add(vaoID);
        GL30.glBindVertexArray(vaoID);
        return vaoID;
    }


    private void storeDataInAttributeList(int attributeNumber, float[] data){
        int vboID = GL15.glGenBuffers();
        vboIDs.add(vboID);
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID);
        FloatBuffer buffer = storeDataInFloatBuffer(data);
        GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
        GL20.glVertexAttribPointer(attributeNumber, 3, GL11.GL_FLOAT, false, 0, 0);
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
    }


    private FloatBuffer storeDataInFloatBuffer(float[] data){
        FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length);
        buffer.put(data);
        buffer.flip();
        return buffer;
    }


    private void bindIndicesBuffer(int[] indices){
        int vboID = GL15.glGenBuffers();
        vboIDs.add(vboID);
        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboID);
        IntBuffer buffer = storeDataInIntBuffer(indices);
        GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
    }


    private IntBuffer storeDataInIntBuffer(int[] data){
        IntBuffer buffer = BufferUtils.createIntBuffer(data.length);
        buffer.put(data);
        buffer.flip();
        return buffer;
    }


    private void unbindVAO(){
        GL30.glBindVertexArray(0);
    }


    public void cleanUp(){
        for(int vaoID : vaoIDs){
            GL30.glDeleteVertexArrays(vaoID);
        }
        for(int vboID : vboIDs){
            GL15.glDeleteBuffers(vboID);
        }
    }
}

模型

public class RawModel {

    private int vaoID;
    private int vertexCount;

    public RawModel(int vaoId, int vertexCount){
        this.vaoID = vaoID;
        this.vertexCount = vertexCount;
    }

    public int getVaoID() {
        return vaoID;
    }

    public int getVertexCount() {
        return vertexCount;
    }

}

主要

public class MainGameLoop {

    public static void main(String[] args) {

        DisplayManager.openDisplay();
        Loader loader = new Loader();
        Renderer renderer = new Renderer();

        float[] vertices = { -0.5f, 0.5f, 0f,
                             -0.5f, -0.5f, 0f, 
                             0.5f, -0.5f, 0f,  
                             0.5f, 0.5f, 0f
        };

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

        RawModel model = loader.loadToVAO(vertices, indices);


        while(!Display.isCloseRequested()){
            renderer.prepare();
            renderer.render(model);

            DisplayManager.updateDisplay();
        }

        loader.cleanUp();
        DisplayManager.closeDisplay();
    }
}

堆栈跟踪

Exception in thread "main" org.lwjgl.opengl.OpenGLException: Cannot use offsets when Element Array Buffer Object is disabled
at org.lwjgl.opengl.GLChecks.ensureElementVBOenabled(GLChecks.java:89)
at org.lwjgl.opengl.GL11.glDrawElements(GL11.java:1117)
at renderEngine.Renderer.render(Renderer.java:20)
at tester.MainGameLoop.main(MainGameLoop.java:33)

我收到此错误已经过了4天,我不知道我做错了什么。

非常感谢任何帮助和解答,谢谢。

1 个答案:

答案 0 :(得分:1)

this.vaoID = vaoID; // constructor takes vaoId but you make vaoID = valID

RawModel的构造函数中。如果那不仅仅是一个拼写错误(这里)那么你在尝试绑定model.getVaoID();

时没有有效的vaoId