Reputation: 25
I am currently learning to use openGL and LWJGL. When trying to set up some Vertex Buffer Objects i get the error message:
Invalid memory access of location 0x0 rip=0x10f95f42f
Here is my code.
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();
}
}
Upvotes: 0
Views: 203
Reputation: 14678
Your issue may be that you are enabling GL_COLOR_ARRAY
without ever giving it any data. I noticed that in your while loop, you have this block of code:
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glVertexPointer(vertexSize, GL_FLOAT, 0, 0L);
glBindBuffer(GL_ARRAY_BUFFER, vboColorHandle);
glVertexPointer(colorSize, GL_FLOAT, 0, 0L);
Notice how the last line is also glVertexPointer
? This should be glColorPointer
, to specify that the data in the bound buffer is for GL_COLOR_ARRAY
, not GL_VERTEX_ARRAY
.
I don't think this really makes a difference, but I always like to enable/disable things before I set their data, so if the above doesn't work, try moving the two glEnableClientState
calls to before the first glBindBuffer
call in the while loop and see if that changes anything.
Upvotes: 1