Sign in to follow this  

Alpha question

This topic is 3730 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, i have a texture with alpha channel. However, i would like, at some point, to display it with solid color (e.g. don't display RGB data, display plain red instead) but still retaining the alpha channel to keep the general shape of the object. The rationale is that i want to display an obejct as selected, however it is black tehrefore simple glColor4f() won't do the job. Is this possible without keeping 2 different textures? I have tried a bit playing with blend and coloring functions but have had very little success. Thanks

Share this post


Link to post
Share on other sites
I think that you can have OpenGL write only those of the four channels that you specify using glColorMask(r, g, b, a).

I'm not absolutely certain, but I think that if you were to call glColorMask(1, 0, 0, 1) you should get only red and alpha being rendered.

Once you've rendered those items that you want drawn in this way, call glColorMask(1, 1, 1, 1) to have it draw all channels.

Again, I'm not absolutely certain of this, but perhaps give it a try and see what results you get. ^_^

Share this post


Link to post
Share on other sites
Hi,

thanks for letting me for this function. it partially works as it does color the textures. However it's still not a viable solution because this way i can't control much the output color and i need to.

Share this post


Link to post
Share on other sites
Let me try:

Your rgb channels don't just show one color right?
Let's say you have a tree whose leaves are stored in that texture and you want the leaves look like leaves with all the shades of green (or some other color).
However, when you select the tree, the leaves should be all bright red (or some other color), is that what you want to do?

If you're using shaders that's easy, but if not I'd say you need two textures (you can separate your single texture into a rgb texture and an alpha/luminance texture).
Then you draw the object textured with your rgb texture or color with a single color depending on the selection state and let OpenGL modulate both "texture" stages in either case. (Keep in mind that with 2 textures the first stage (color and texture 1) needs to just use the texture, with just alpha and color you have to modulate here).

Hope that helps.

Share this post


Link to post
Share on other sites
Quote:
Original post by Lord_Evil
Let me try:

Your rgb channels don't just show one color right?
Let's say you have a tree whose leaves are stored in that texture and you want the leaves look like leaves with all the shades of green (or some other color).
However, when you select the tree, the leaves should be all bright red (or some other color), is that what you want to do?

Yes, exactly

Quote:
Original post by Lord_Evil
If you're using shaders that's easy, but if not I'd say you need two textures (you can separate your single texture into a rgb texture and an alpha/luminance texture).
Then you draw the object textured with your rgb texture or color with a single color depending on the selection state and let OpenGL modulate both "texture" stages in either case. (Keep in mind that with 2 textures the first stage (color and texture 1) needs to just use the texture, with just alpha and color you have to modulate here).


Shaders sound itneresting, i would need to learn them though. However i am doing
2D graphics, aren't shaders a bit of an overkill for that? Well if the rendering cost is not high i wouldn't mind though.

Of course keeping 2 textures is a possibility that was thre right from the start, i just wanted to know if there's another way.

I'll look at shaders - but if you know some references for doing that kind of flat shading it would be appreciated.

Thanks

Share this post


Link to post
Share on other sites
Well, I don't have any tutorials on this but I'd do something like this in the pixel shader:

(I'll use CG slang here)

float4 main(float4 selection, sampler2D texture, float2 texcoords,/*other params*/) //selection.xyz = color, selection.w = flag "selected?"
{
float4 texel = tex2D(texture, texcoords);

if(selection.w == 1.0)
{
texel.rgb = selection.rgb;
}

return texel;
}


This will read the texel from the texture (rgb + alpha) and if the selection flag is set (selection.w == 1.0) the rgb channel is replaced by the selection color (selection.rgb or selection.xyz). The texel is then returned.

If your GPU doesn't support static branching in the shader you'd have to use 2 shaders and set them in your application based on the selection state.

Share this post


Link to post
Share on other sites

This topic is 3730 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this