I have a texture I'm reading from like this:
vec4 diffuseFrag = texture2D(DiffuseMap, fDiffuseCoord);
Sometimes, for special effects, I'd actually like to do something this:
vec4 uvFrag = texture2D(UVMap, fUVCoord);
vec4 diffuseFrag = texture2D(DiffuseMap, uvFrag.rg);
...basically, I'm using a texture's Red and Green color channels to store the frag coordinates I want to read from DiffuseMap.
My probably is, both the UV map and the Diffuse map are spritesheets with multiple images in them. This means, I'm actually wanting uvFrag.rg's (0-1) texcoord to be multiplied against the *subportion* of the texture that all four vertices of my fDiffuseCoord are referring to.
Something like:
vec4 uvFrag = texture2D(UVMap, fUVCoord).rg;
vec4 upperLeftOfSubrect = ...;
vec4 bottomRightOfSubrect = ...;
vec4 subrectSize = (bottomRightOfSubrect - upperLeftOfSubrect);
uvFrag = upperLeftOfSubrect + (uvFrag * range);
vec4 diffuseFrag = texture2D(DiffuseMap, uvFrag.rg);
Where my mind is going blank is, how can I get upperLeftOfSubrect / bottomRightOfSubrect, without bloating my vertex struct further with additional attributes?
It mentally trips me up that I'll have to copy upperLeftOfSubrect / bottomRightOfSubrect into all four of my vertices... and triply annoys me because I'm already passing them in as fDiffuseCoord (just spread between different vertices).
Is there a simple solution to this that I'm missing?