glCopyTexSubImage2D can' t work?

I want to use content in frame buffer as texture.
following code do the job:
glReadBuffer( GL_BACK);
glReadPixels(0, 0, 500, 500, GL_RGB, GL_UNSIGNED_BYTE, pPixels);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 500,
500, 0, GL_RGB, GL_UNSIGNED_BYTE, pPixels);

however, following code does not:
glReadBuffer( GL_BACK);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 500, 500);

why? I think glCopyTexSubImage2D copy frame buffer to texture.

that is really confusing. I change glCopyTexSubImage2D into glCopyTexImage2D, it works. What happens, then?

Before you use CopyTexSubImage, you should specify the format and dimension of your texture object.
How it can be done? By CopyTexImage or by TexImage, it doesn’t matter.
For example, you should alter code like this:

if (FirstTime) {
glCopyTexImage2D(…);
FirstTime = false;
} else {
glCopyTexSubImageImage2D(…);
}

Maybe this is true, for first time to use glCopyTexImage2D, the internal format is not given.

This is not maybe, this is simply like that :wink:

You can also call glTexImage with NULL for the pixels before the first time to eliminate the special case. This initializes the texture to undefined contents, which you can override with glCopyTexSubImage.

Depends on your overall code structure which one is better…