[SOLVED] glMapBuffer

I’m very sorry to be posting a lot of questions, but I can’t find almost anything about this problem…

I’m trying to change the data in VBO. The way I’m trying to do it, is glMapBuffer, change the values in that buffer and unmap it. But the problem is that glMapBuffer always returns null. I have read somewhere that this means some kind of an error, but I have no idea what it could be. Here is the code for the entire VBO class.

public class Batch {

	public static final int target = GL_ARRAY_BUFFER;
	
	public int id;
	public int x, y, width, height;
	
	public Batch(int x, int y, int width, int height) {
		
		id = glGenBuffers();
		
		FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(8);
		
		vertexBuffer.put(x).put(y);
		vertexBuffer.put(x + width).put(y);
		vertexBuffer.put(x + width).put(y + height);
		vertexBuffer.put(x).put(y + height);
		vertexBuffer.flip();
		
		glBindBuffer(target, id);
		glBufferData(target, vertexBuffer, GL_STATIC_DRAW);
		
		glBindBuffer(target, 0);
	}

	
	public void render() {
		glColor3f(1, 1, 0);
		
		glEnableClientState(GL_VERTEX_ARRAY);
		
		glBindBuffer(target, id);
		glVertexPointer(2, GL_FLOAT, 8, 0L);
		
		glDrawArrays(GL_QUADS, 0, 4);
		
		glDisable(GL_VERTEX_ARRAY);
		
		glBindBuffer(target, 0);
	}
	
	public void set(float x, float y, float width, float height) {
		
		glBindBuffer(target, id);
		
		ByteBuffer buffer = glMapBuffer(target, GL_WRITE_ONLY, null);
		System.out.println(buffer);
		
		glBindBuffer(target, 0);
	}
}

After calling glMapBuffer(), call glGetError() which will return an integer error code. If the error is not GL_NO_ERROR, then run it through GLU’s gluGetErrorString() method and look up what could be causing that error in the OpenGL docs: http://www.opengl.org/sdk/docs/man4/.

You should be checking for errors anyway (I understand why you aren’t - none of the tutorials really tell you to). Not all (in fact very few) OpenGL methods are going to return null if they haven’t worked properly. Just check the OpenGL error at the end of each game loop and you’ll save yourself a hell of a lot of time.

I get an error code 1282, which is invalid operation. I guess I need to somehow change the state of opengl?

Hmm I think the problem was that I wasn’t unmapping the buffer. Though I think I was unmapping it at first, and it still didn’t work.

Anyway, the fix is to NOT FORGET TO UNMAP the buffer… It is kinda silly of me :smiley:

If no one made silly mistakes, then we wouldn’t need to have all these error codes would we.

Ye I know, but I don’t like flooding forums with stupid posts like that :frowning:

But you learnt something from it and that’s what this forum exists for.