pbuffer and multitexture

I am trying to implement a glow effect using cG, multitexturing and render-to-texture. I have each of the parts working, so I can execute cG programs, I can multi texture, and I can render-to-texture.

In order for the effect to work, I render the glow sources to a texture, then bind that texture to all four texture units and render full screen quads to a second textyre using a cg vertex and fragment program to createa horizontal blur. I then render this second texture back into the first texture to create the vertical blur, and then I finally render the first texture over the main buffer to create the glow in the scene.

All of this works ‘fine’ if I only bind the rendered texture to texture unit 0 for the bluring (leaving the other units off). If I do this, I get a very slight glow-like effect. But if I bind the same texture to the other texture units, nothing is rendered into the texture during the blur steps, so the glow I end up adding to the scene is black.

The code I am using to do the binding looks like this:

glClientActiveTexture(GL_TEXTURE0 + texUnitID);
glActiveTexture(GL_TEXTURE0 + texUnitID);

glEnable(mTarget);

glBindTexture(mTarget, mTextureID);

if(mBindCount == 0 && !wglBindTexImageARB(mHpBuffer, WGL_FRONT_LEFT_ARB))
{
WarningError(“Could not bind p-buffer to render texture!”);
return;
}
++mBindCount;

The code for deactivating the texture is:

glClientActiveTexture(GL_TEXTURE0 + texUnitID);
glActiveTexture(GL_TEXTURE0 + texUnitID);

–mBindCount;
if(mBindCount == 0 && !wglReleaseTexImageARB(mHpBuffer, WGL_FRONT_LEFT_ARB))
{
WarningError(“Could not release p-buffer from render texture!”);
}

If I don’t have all that mucking about with mBindCount, then the second time I try to bind the texture (that is, to texture unit 1), wglBindTexImageARB fails.

So, does anyone have any ideas?

Are you sending texture coordinates for the additional texture units you are using?

Yes, but even if I wasn’t, the vertex and fragment programs I’m using don’t use the other texture units at the moment (just while I’m trying to figure this out). Just the act of switching on the other units makes the whole thing break.

Remove the glClientActiveTexture call.
It doesn’t belong there and may lead to ‘interesting’ problems. For binding textures, you only need glActiveTexture.

I somewhat supsect that you bind unit 0,1,2,3, in that order, then set a tex coord pointer and fire. This will lead to tex coords only on unit 3 (not 0), and may just be the cause for your problems.

What you need to do is to bind the pbuffer to a single texture object, then bind this texture object to each texture unit.

Something like this:

glActiveTexture(GL_TEXTURE0);
glBindTexture(mTarget, mTextureID);
glEnable(mTarget);

wglBindTexImageARB(mHpBuffer, WGL_FRONT_LEFT_ARB);

glActiveTexture(GL_TEXTURE1);
glBindTexture(mTarget, mTextureID);
glEnable(mTarget);

glActiveTexture(GL_TEXTURE2);
glBindTexture(mTarget, mTextureID);
glEnable(mTarget);

glActiveTexture(GL_TEXTURE3);
glBindTexture(mTarget, mTextureID);
glEnable(mTarget);

// draw scene

glDisable(mTarget);
glActiveTexture(GL_TEXTURE2);
glDisable(mTarget);
glActiveTexture(GL_TEXTURE1);
glDisable(mTarget);
glActiveTexture(GL_TEXTURE0);
glDisable(mTarget);

wglReleaseTexImageARB(mHpBuffer, WGL_FRONT_LEFT_ARB);

If you’re using fragment programs, you don’t need to bind the texture more than once; just sample the same single texture sampler four times instead.