【问题标题】:LWJGL: Cannot use offsets when Element Array Buffer Object is disabledLWJGL:禁用元素数组缓冲区对象时无法使用偏移量
【发布时间】:2016-07-29 23:35:50
【问题描述】:

我目前正在使用 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 天了,我不知道我做错了什么。

任何帮助和答案将不胜感激,谢谢。

【问题讨论】:

  • 我建议提供完整的堆栈跟踪,以防有人确定原因。
  • 谢谢,添加了堆栈跟踪。似乎在尝试渲染模型时引发了异常。但是我不知道为什么元素缓冲区没有绑定...

标签: java opengl lwjgl


【解决方案1】:
this.vaoID = vaoID; // constructor takes vaoId but you make vaoID = valID

在您的 RawModels 构造函数中。如果这不仅仅是一个错字(这里)那么你没有有效的vaoId,当你试图绑定model.getVaoID();

【讨论】:

  • 嗨,谢谢。我已经在一天前修复了它,但感谢您的回答。
  • 好吧,我刚刚发现你的问题没有得到解答,看到你的错误,所以回答它。
猜你喜欢
  • 1970-01-01
  • 2021-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-23
  • 1970-01-01
  • 2011-09-19
  • 1970-01-01
相关资源
最近更新 更多