Confused with texture binding and shaders

Hi There,

I have been getting into shaders recently. Much Fun. Naturally still confused with quite a few things here. I have been gradient mapping “photo”. I have also been cropping subsections of “photo”, alpha masking them with an FBO, calling this “slice”. I have then been putting “slice” through the same shader that “photo” goes through, but it has no effect and just shows me the unaffected “slice”. I think it’s something to do with binding the textures properly, but I’m at a bit of a loss as to how to continue exploring.
Many Thanks,

shader.setUniform4f( "color1", mGradientColors[0]);
shader.setUniform4f( "color2", mGradientColors[1]);
shader.setUniformTexture("sourceTex", photo.getTexture(), photo.getTexture().getTextureData().textureID);
photo.getTexture().draw(0, 0);

shader.setUniform4f( "color1", mGradientColors[1]);
shader.setUniform4f( "color2", mGradientColors[0]);
shader.setUniformTexture("sourceTex", slice.getTexture(), slice.getTexture().getTextureData().textureID);
slice.getTexture().draw(200, 200);
ofDrawRectangle(200, 300, 100, 100);
shader.setUniformTexture("sourceTex", slice.getTexture(), slice.getTexture().getTextureData().textureID);

should just be:

shader.setUniformTexture("sourceTex", slice.getTexture(), 0);

in both cases, if you want to bind more than one texture then you assign 0, 1, 2… it’s not related to the texture id which is taken from the texture you pass

ah ok, thanks Arturo.
That makes sense. Unfortunately it’s still not working as expected. The “slice” texture doesn’t seem to get set/used properly yet, as the slice drawn at the end of the shader is still not effected. Also the ofDrawRect i have is still just showing up as white which when it works properly should be a shade of the gradient map.
I’m using the same shader twice in the same draw function, could this break things?

@arturo i’ve tracked down where the issue lies, but I don’t know whether it’s a bug or whether it’s a usage problem. I am doing this in setup()

alphaMask.allocate(150, 150, GL_RGBA);
    ofDrawTriangle(0, 150, 75, 0, 150, 150);

If i comment out slice.getTexture().setAlphaMask(alphaMask.getTexture()); then it works, however obviously I lose the mask shape

Here’s the solution to the alphaMask issue

1 Like