Sign in to follow this  

Drawing one texture using another's alpha

This topic is 4708 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

Is it possible to draw a polygon with one texture but using another texture's alpha? ie, if I had a sprite of a man which an alpha and a fire texture, I'd draw a man-shaped blob of fire texture? I should point out it's for a 2D engine rather than anything fancy. (it's for teleport and power up effects, basically, for this game http://www.duketastrophy.demon.co.uk/exolon01.jpeg )

Share this post


Link to post
Share on other sites
im not sure how but my advise is not to bother

use a 32bit .tga, its RGBA so every pixel already has a alpha value. you can set you transparency in photo shop or somehting. works just as good as joining two images but is easyer and faster becouse the messy calculation are done beforehand

Share this post


Link to post
Share on other sites
Quote:
Original post by Kaze
im not sure how but my advise is not to bother

use a 32bit .tga, its RGBA so every pixel already has a alpha value. you can set you transparency in photo shop or somehting. works just as good as joining two images but is easyer and faster becouse the messy calculation are done beforehand


The whole idea is to do it realtime so that I can animate the image. For instance I might have a teleportation fuzz that I want to scroll across the shape of the man while fading up it's alpha or something, so not bothering isn't really an option. Unless, of course, it turns out to be absolute arse of a problem to overcome. I'm hoping it's just a matter of multitexturing?

Share this post


Link to post
Share on other sites
Quote:
Original post by DrDerekDoctorsI'm hoping it's just a matter of multitexturing?

0 alpha on the original texture and GL_ADD the other texture's alpha with GL_ALPHA_COLOR as operand on your target?

Looks to me like it'll work, then again I just woke up. [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by DEVLiN
Quote:
Original post by DrDerekDoctorsI'm hoping it's just a matter of multitexturing?

0 alpha on the original texture and GL_ADD the other texture's alpha with GL_ALPHA_COLOR as operand on your target?

Looks to me like it'll work, then again I just woke up. [smile]


Could you show the code please. (i am intersted in this too.) Thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by snisarenko
Could you show the code please. (i am intersted in this too.) Thanks.

I can give a useful link to code showing how to use it: (help to self-help[smile])
Multitexturing with OpenGL - (first result when googling for "multitexturing opengl" btw)

It should get you started, if nothing else. [smile]

The topic to focus on would be GL_ADD and GL_ALPHA_COLOR.

Good luck! [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by DEVLiN
Quote:
Original post by DrDerekDoctorsI'm hoping it's just a matter of multitexturing?

0 alpha on the original texture and GL_ADD the other texture's alpha with GL_ALPHA_COLOR as operand on your target?

Looks to me like it'll work, then again I just woke up. [smile]

Would work for sure, but it would be alot more flexible to use GL_REPLACE and just replace the alpha instead of relying on the other texture having a zero alpha channel.

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex0);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, tex1);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE);

Unless I made a mistake, the code above will take the RGB channel from tex0 and the alpha channel from tex1, independent of the values of the other channels.

Share this post


Link to post
Share on other sites
If I want to blend the output with the screen do I need to enable blending on both texture units or just the last one? Sorry to keep asking questions but I've never touched multitexturing before and I'm finding it a little bewildering.

Oh, and after doing all the glTexEnvi() stuff, if I want to go back to regular single texturing obviously I need to disable the second texture unit, but do I need to make some glTexEnvi calls to get it to use both the alpha and rgb information from the first texture? And is the speed hit of multitexturing using these texture combiner functions pretty large? And lastly, what version of OpenGL is required to support the combiner functions?

Thanks,

[Edited by - DrDerekDoctors on January 19, 2005 1:20:24 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by DrDerekDoctors
If I want to blend the output with the screen do I need to enable blending on both texture units or just the last one? Sorry to keep asking questions but I've never touched multitexturing before and I'm finding it a little bewildering.

Blending does not have anything to do with texturing and are two completely separate and independent operations, and are applied in different parts of the pipeline. So you only have to enable blending, set correct blending function, and you're set, and just ignore the states of the texturing stage.

Quote:
Original post by DrDerekDoctors
Oh, and after doing all the glTexEnvi() stuff, if I want to go back to regular single texturing obviously I need to disable the second texture unit, but do I need to make some glTexEnvi calls to get it to use both the alpha and rgb information from the first texture?

You're right that you just have to disable the second texture unit and just focus in the first one. Single texturing is just a special case of multitexturing where only one texture unit is used.

And yes, you have change texture combine setup aswell, as you just changed it to mix the two textures. A call to

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

and both the RGB and alpha channel from the texture will be passed through the texture stage.

Quote:
Original post by DrDerekDoctors
And is the speed hit of multitexturing using these texture combiner functions pretty large?

Using two textures instead of one is going to cost some, but using, say, multiple single-texture passes to generate the same result is going to cost more. Whether the actual performance hit is large or not depends on what your definition of large is. Would bet that you don't think it's large though [rolleyes]

Quote:
Original post by DrDerekDoctors
And lastly, what version of OpenGL is required to support the combiner functions?

OpenGL 1.3 and later. Pretty much any graphics card in use today either supports that version or the earlier extension versions (GL_ARB/EXT_texture_env_combine).

Share this post


Link to post
Share on other sites
Quote:
Original post by Brother Bob
Blending does not have anything to do with texturing and are two completely separate and independent operations, and are applied in different parts of the pipeline. So you only have to enable blending, set correct blending function, and you're set, and just ignore the states of the texturing stage.


Righty, that makes sense, thanks.

Quote:
Original post by Brother Bob
You're right that you just have to disable the second texture unit and just focus in the first one. Single texturing is just a special case of multitexturing where only one texture unit is used.

And yes, you have change texture combine setup aswell, as you just changed it to mix the two textures. A call to

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

and both the RGB and alpha channel from the texture will be passed through the texture stage.


Again, peachy, thanks.

Quote:
Original post by Brother Bob
Using two textures instead of one is going to cost some, but using, say, multiple single-texture passes to generate the same result is going to cost more. Whether the actual performance hit is large or not depends on what your definition of large is. Would bet that you don't think it's large though [rolleyes]


Haha, it was kind of a stupid question, yes.

Quote:
Original post by Brother Bob
OpenGL 1.3 and later. Pretty much any graphics card in use today either supports that version or the earlier extension versions (GL_ARB/EXT_texture_env_combine).


Hmm, I'm trying to make my game as compatible as possible, but I can always have it ignore multitexturing on things that don't support it, I suppose. Pity really as most of the really nice effects will be reliant on its availability.

Thanks for all the information. I'm damned if I can get it working at the moment but I'll keep beavering away. At the moment all I have is a silhouette of my player (like it's been vertex coloured down to 0, which is ain't).

Share this post


Link to post
Share on other sites
Righty, almost everything is working peachily now, the only problem is that the blending alpha level is ignored (as set by glColor4f). I figured that using this:


glActiveTexture(GL_TEXTURE0);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_MODULATE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE);

glActiveTexture(GL_TEXTURE1);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_PREVIOUS);


Should work, but it creates an opaque polygon (except for the alpha channel mask from texture0). If the first GL_MODULATE is changed to a GL_REPLACE then it happily combines the mask alpha from texture0 with the incoming vertex colours and the rgb information of texture1. I suppose I can just make-do with additive transparency and fudge the RGB channels to alter the opacity but it'd be nice to have the option of multiplicative transparency, too.

Share this post


Link to post
Share on other sites
Your two modulate functions are incoplete. GL_MODULATE multiplies the value of two sources (GL_SOURCE0 and GL_SOURCE1), but you only specify one of the sources (GL_SOURCE0). If you want to modulate the texture alpha with the alpha from the primary color, you have to do this.

glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_MODULATE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA, GL_PRIMARY_COLOR);

Now source0 is the texture alpha and source1 is the primary alpha, and the output alpha is source0*source1.

Same with the RGB channels in the second texture unit; you only specify one source, but modulation requires two.

Share this post


Link to post
Share on other sites

glActiveTexture(GL_TEXTURE0);

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_MODULATE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA, GL_PRIMARY_COLOR);

glActiveTexture(GL_TEXTURE1);

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_PRIMARY_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_PREVIOUS);


EDIT. Ignore me! The above works fine, the <0.5f problem was in my own code somewhere.

[Edited by - DrDerekDoctors on January 20, 2005 4:59:06 PM]

Share this post


Link to post
Share on other sites

This topic is 4708 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