我目前正在学习使用 openGL 和 LWJGL。尝试设置一些顶点缓冲区对象时,我收到错误消息:
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();
}
}