我目前學習使用openGL和LWJGL。當試圖設置一些頂點緩衝區對象時,我收到錯誤信息:VBO的openGL
Invalid memory access of location 0x0 rip=0x10f95f42f
這是我的代碼。
public class Test {
public Test(){
try {
Display.setDisplayMode(new DisplayMode(768, 512));
Display.setTitle("This is a title!");
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
}
//Initialize openGL
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(1, 1, 1, 1, 1, -1);
glMatrixMode(GL_MODELVIEW);
final int vertexAmount = 3;
final int vertexSize = 2;
final int colorSize = 3;
FloatBuffer vertexData = BufferUtils.createFloatBuffer(vertexAmount * vertexSize);
vertexData.put(new float[]{-0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f});
vertexData.flip();
FloatBuffer colorData = BufferUtils.createFloatBuffer(vertexAmount * colorSize);
colorData.put(new float[]{1, 0, 0, 0, 1, 0, 0, 0, 1});
colorData.flip();
int vboVertexHandle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
int vboColorHandle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboColorHandle);
glBufferData(GL_ARRAY_BUFFER, colorData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
while(!Display.isCloseRequested()){
glClear(GL_COLOR_BUFFER_BIT);
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glVertexPointer(vertexSize, GL_FLOAT, 0, 0L);
glBindBuffer(GL_ARRAY_BUFFER, vboColorHandle);
glVertexPointer(colorSize, GL_FLOAT, 0, 0L);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
Display.update();
Display.sync(60);
}
glDeleteBuffers(vboVertexHandle);
glDeleteBuffers(vboColorHandle);
Display.destroy();
}
public static void main(String[] args){
Test test = new Test();
}
}
我嘗試在綁定之前移動glEnableClientState調用,並將第二個glVertexPointer更改爲glColorPointer。 –
@Robert爲什麼不作爲回答發佈? –
@AndreasHaferburg因爲那些是兩個錯誤,我發現通過代碼可能會或可能不會涉及到他遇到的問題。當我看到問題時,我也即將離開,所以我寫下了一個非常簡短的答案,但沒有解釋爲什麼可能是他的問題。我想我現在會寫出一個完整的答案...... –