I keep getting this exception:
Exception in thread "main" org.lwjgl.opengl.OpenGLException: Cannot use offsets when Array Buffer Object is disabled
at org.lwjgl.opengl.GLChecks.ensureArrayVBOenabled(GLChecks.java:93)
at org.lwjgl.opengl.GL11.glVertexPointer(GL11.java:2680)
at Joehot200.TerrainDemo.render(TerrainDemo.java:2074)
at Joehot200.TerrainDemo.enterGameLoop(TerrainDemo.java:3266)
at Joehot200.TerrainDemo.startGame(TerrainDemo.java:3490)
at StartScreenExperiments.Test2.resartTDemo(Test2.java:55)
at StartScreenExperiments.Test2.main(Test2.java:41)
However, the array buffer object IS enabled!
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glVertexPointer(3, GL_FLOAT, 0, 0L);
As you can see, two lines before the glVertexPointer call (the one that the error is at), then I am clearly enabling the array buffer!
What is wrong here?
Vertex Buffers are not something you enable or disable - LWJGL is misleading you.
You need to undertand that the glVertexPointer
command uses whatever is bound to GL_ARRAY_BUFFER
("Array Buffer Object") as its memory source (beginning with OpenGL 1.5).
In certain versions of OpenGL (1.5-3.0 and 3.1+ compatibility) if you have 0 bound to GL_ARRAY_BUFFER
, then the last parameter to glVertexPointer
is an actual pointer to your program's memory (client memory) rather than an offset into GPU memory (server memory). Core OpenGL 3.1+ does not even support client-side vertex storage, so that last parameter is always an offset.
LWJGL's error message is poorly worded:
Exception in thread "main" org.lwjgl.opengl.OpenGLException: Cannot use offsets when Array Buffer Object is disabled.
The error message really means that you have 0 bound to GL_ARRAY_BUFFER
when you call glVertexPointer (...)
. LWJGL apparently considers Array Buffer Objects "disabled" whenever nothing is bound to GL_ARRAY_BUFFER
. That is not too unreasonable, but it does lead you to believe that this is a state that can be enabled or disabled using glEnable
or glDisable
; it is not.
Remember how I described the last parameter to glVertexPointer
as an offset when you have something bound to GL_ARRAY_BUFFER
? Since LWJGL is Java-based, there is no way to pass an arbitrary memory address as an integer. An integer value passed to glVertexPointer (...)
must be an offset into the currently bound vertex buffer's memory.
Client-side vertex specification (unsupported in core GL 3.1+)
void glVertexPointer(int size, int type, int stride, java.nio.ByteBuffer pointer);
Server-side vertex specification (takes an offset into GL_ARRAY_BUFFER
)
void glVertexPointer(int size, int type, int stride, long pointer_buffer_offset);
As you can see, there is an alternate form of the glVertexPointer
function in LWJGL that can take memory not stored in a buffer object, where you pass a specialization of java.nio.Buffer
. That is the form you are expected to use when you have no vertex buffer bound and that is what the error message is really telling you.
That explains what the error message you are seeing actually means, but not its cause.
For some reason vboVertexHandle
appears to be 0 or some value not generated using glGenBuffers (...)
in your application. Showing the code where you initialize the VBO would be helfpul.
GL_ARRAY_BUFFER is not one of the allowed values to glEnable. If you want to attach a vertex buffer object to the vertex pointer, you have to enable it using the glEnableClientState method:
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glVertexPointer(3, GL_FLOAT, 0, 0L); //This is line 2074 of the TerrainDemo class.
Side note: This functionality is deprecated since OpenGL 3 Core Profile. If there is no restriction on sticking to this old OpenGL version, it would be a good idea to start with modern OpenGL (especially since you're already using vbos).
来源:https://stackoverflow.com/questions/27525323/openglexception-cannot-use-offsets-when-array-buffer-object-is-disabled-on-a