Jump to content
  • Advertisement
Sign in to follow this  
duhroach

OpenGL texture Transparency in shader, w/o colorKey?

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

I've been trying to get alpha channel transparency in my textures. Currently, i'm attempting to do a color.rgb*color.a in my pixel shader. For some reason, this ONLY works when i specify a color key when loading the texture, AND it's the correct color key in the file. Shouldn't there be a way to do this alpha value in a shader w/o having to do all the color keying and renderstate modes? OpenGL can do it, so i know DX Can... ~Main

Share this post


Link to post
Share on other sites
Advertisement
1) Does the texture file have an alpha channel ? (e.g. in a format such as TGA or PNG with an alpha/opacity/transparency channel set up)

2) Are you sure the texture is being loaded into a format which actually has a usable alpha channel? (e.g. D3DFMT_A8R8G8B8 rather than D3DFMT_X8R8G8B8 or D3DFMT_R5G6B5).

3) The information you've provided is a bit vague. Could you provide some more information about: how exactly you're loading and creating your textures, the format and contents of the texture image file, and maybe the actual code of your shader(s)

Share this post


Link to post
Share on other sites
Quote:
Original post by duhroach
I've been trying to get alpha channel transparency in my textures. Currently, i'm attempting to do a color.rgb*color.a in my pixel shader.

For some reason, this ONLY works when i specify a color key when loading the texture, AND it's the correct color key in the file.

Shouldn't there be a way to do this alpha value in a shader w/o having to do all the color keying and renderstate modes?

I don't get your point. If you don't set a colorkey while loading the texture, then it'll be all alpha=1 (Or is it a transparent png or tga?), in which case your shader will be multiplying color.rgb by 1 always.
It seems to me like I'm missing something...

Share this post


Link to post
Share on other sites
texture is a .tga with an alpha channel (RGBA8888)
I'm loading it with format D3DFMT_A8R8G8B8.

What I'm getting at is this: Is their a way to do transparency w/o a color key?

I fully understand the concept of "magic pink" becoming a transparent color when using keying.

What I'd like to do is, rather than have a boolean transparency (clear or opaque via color key), is be able scale the transparency of the RGB pixels by the Alpha channel of my texture. So we don't use a boolean color key at all.

is their a way to do this? Or does DX only allow for color keys?

~Main

Share this post


Link to post
Share on other sites
Your idea of how the the alpha channel works is wrong.
Alpha only applies on overdraw, so your shader is just scaling your color values if you do texel.rgb * texel.a.

What you want is alpha blending, which is enabled and setup with renderstates rather than using a pixel shader.

Share this post


Link to post
Share on other sites
Quote:
Original post by duhroach
texture is a .tga with an alpha channel (RGBA8888)
I'm loading it with format D3DFMT_A8R8G8B8.

...
What I'd like to do is, rather than have a boolean transparency (clear or opaque via color key), is be able scale the transparency of the RGB pixels by the Alpha channel of my texture. So we don't use a boolean color key at all.


Well, as with OpenGL, "color.rgb*color.a" will scale the *brightness* of the colour based on the value alpha channel, it won't affect the *transparency* of the rendered pixels (unless you had an additive frame buffer blend set up, but that's a different issue).

What it sounds like you're after is *frame buffer blending*, which controls how the rendered pixels from your currently rendered polygon interact with pixels from previously rendered polygons.

I'd suggest looking at the following section of the DirectX SDK docs:

Programming Guide ->
Getting Started ->
Direct3D Rendering ->
Alpha Blending ->
Frame Buffer Alpha


The most common frame buffer blend for transparent rendering is:

SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

which performs a linear interpolation between colour of the pixel you're rendering and the existing pixel in the frame buffer using the value in the alpha channel to control how much of each is visible (i.e. alpha blending):

destRGB = (srcA * srcRGB) + ((1-srcA) * currentRGB)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!