Cannot use offsets when Pixel Unpack Buffer Object is disabled?

So I’m trying to learn about FBOs from this tutorial: http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-14-render-to-texture/

This line causes me some problems:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 800, 600, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);

The exception says “Cannot use offsets when Pixel Unpack Buffer Object is disabled”, so I tried glEnable(GL_PIXEL_UNPACK_BUFFER)
and then it still gave me the exception. Any help?

The version of that function where the last argument is a long tells OpenGL to fetch the texture data from the currently bound pixel unpack buffer. The last argument being the offset into that PBO to start getting data. When it says that Pixel Unpack Buffer Object is disabled, it means that there is no PBO currently bound, which is equivalent to disabling the pixel unpack buffers.

But my guess is that you are trying to create an empty texture to bind to the FBO right? In which case what you want to do is create an empty ByteBuffer of the appropriate size and pass that to OpenGL. You might be able to pass “null” instead but I seem to recall there being an issue with the LWJGL impl that make that not possible. I might be wrong so you could try it.

So essentially you want to do this (assuming you are using LWJGL)


glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 800, 600, 0, GL_RGB, GL_UNSIGNED_BYTE, BufferUtils.createByteBuffer(800 * 600));

or if it works


glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 800, 600, 0, GL_RGB, GL_UNSIGNED_BYTE, null);

Ok so I got rid of that color unpack buffer exception thing with your line of code, but now I’m facing another exception. I’ll show you the code, then the exception.

The code:

//INIT FBO

//create a blank texture
int texture = glGenTextures();
//bind the texture, all future texture calls will modify this texture
glBindTexture(GL_TEXTURE_2D, texture);
//give an empty image to OpenGL (the last “0”)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 800, 600, 0, GL_RGB, GL_UNSIGNED_BYTE, BufferUtils.createByteBuffer(800*600));

	//filtering?
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	
	//create the frame buffer!
	frameBuffer = glGenFramebuffers();
	//bind the framebuffer, all future framebuffer calls will modify this buffer
	glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer); 
	
	//set "texture" to color attachment #0
	//glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture, GL_TEXTURE_2D, 0);
	 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
	 
	//specify which color attachment the frame buffer will use
	int drawBuffer = GL_COLOR_ATTACHMENT0;
	glDrawBuffers(drawBuffer);

The exception:

Exception in thread “main” java.lang.IllegalArgumentException: Number of remaining buffer elements is 480000, must be at least 1440000. Because at most 1440000 elements can be returned, a buffer with at least 1440000 elements is required, regardless of actual returned element count

The exception occurs at this line:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 800, 600, 0, GL_RGB, GL_UNSIGNED_BYTE, BufferUtils.createByteBuffer(800*600));
Which just happens to be the line you gave me.

Any help?

RGB is 3 bytes per pixel, so your buffer must be 8006003 bytes big.

The obscure exception message was written for calls like glGetString, were you have to provide a buffer to be filled, but somehow somebody used the same exception message for gl calls that push data, for which the message makes very little sense.

[quote=“quew8,post:2,topic:48412”]
In order to pass null to a method that has been overloaded with different buffer types, an explicit cast is required to compile, like so:

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 800, 600, 0, GL_RGB, GL_UNSIGNED_BYTE, (ByteBuffer)null);

@JonnyGingerKid sorry about that. My fault. Can’t believe I made such a silly mistake.

@Spasi I’m pretty sure you told me that before. Sorry to you too, I’ll try and remember the reason in future rather than vague statements that don’t say anything.

Hey thanks for that guys, fixed it right up :slight_smile:

Now to begin the adventure of 2D lighting!