【发布时间】: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 天了,我不知道我做错了什么。
任何帮助和答案将不胜感激,谢谢。
【问题讨论】:
-
我建议提供完整的堆栈跟踪,以防有人确定原因。
-
谢谢,添加了堆栈跟踪。似乎在尝试渲染模型时引发了异常。但是我不知道为什么元素缓冲区没有绑定...