Alpha Transparency

Started by
7 comments, last by dr_slash_uh 20 years, 3 months ago
I cannot figure out how to do alpha transpareny ona png file. I am trying to get the transparency cloudlike effect. Help would be greatly appreciated Thanks Robert
Advertisement
to be more specific, I know how to add 1 alpha value to my pnf picture in paintshop pro but I cannot figure out how to get opacity like a stain glass windows or something similiar. Help would be greatly appreciated.

Robert
Assuming D3D (DDraw doesn''t do alpha transparency, you have to do it yourself).

// Enable alpha blendingpDev->SetRenderState(D3DRS_ALPHABLENDENABLE, true);// Set to be newcolor*alpha + oldcolor*(1-alpha)pDev->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);pDev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);


To get things to blend correctly, draw all solid things first. Then draw each semi-transparent object back to front (the math only works out when drawing in this order).

You can also do additive blending by changing the DESTBLEND to D3DBLEND_ONE. (newcolor*alpha + oldcolor*1) Using this method you don''t have to draw back to front, since you''re just adding to the existing value.

You''ll want to ensure the texture alpha is used. Here I mix the texture''s alpha with that of the material (or vertex diffuse alpha)
// color = texture * diffuse (vertex or lighting)pDev->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);pDev->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);pDev->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);// alpha = texture * diffuse (vertex or material)pDev->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);pDev->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);pDev->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);// Terminate texture stagespDev->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);pDev->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);


When you''re done, remember to turn alpha blending back off.

I already have direct3d setup to do that. I download png format picture to verify it. I just can't create my own.

Robert

[edited by - dr_slash_uh on January 12, 2004 11:27:49 PM]
So you don''t have a DirectX problem at all, you just don''t know how to use your paint package... Your question made it sound like it was the other way around. I haven''t used PSP in many years, so I can''t help. Why not try a forum for PSP? Or an artist''s forum? Or your manual?
*laughs* why not just use a different graphic format. You can make an entire image (even a simple bmp) semi/completly transparent at once by modulating the color of the graphic with another color with alpha values. (I use DXSprite class usually)
Believe it or not, the idiot is learning.
I never used that before dxsprite before. Is that fast? I use the drawprimitive with a trianle fan.
It probably depends on your system, with the game I'm currently working on I've been getting 200 frames per second when I don't lock the frame rate. It's definatly fast enough for my needs.


[edited by - ShadowP13 on January 13, 2004 1:19:09 AM]
Believe it or not, the idiot is learning.
If I remember correctly, Paint Shop Pro doesn't fully support PNGs. Most importantly, it doesn't fully support the transparency. So when you create an image and save it, it'll just ignore transparency. I know this is true for PSP 7, I don't know what newer versions there are and how they work, though. I believe Photoshop was one of the few programs that fully supported PNGs when I last checked. On the PNG webpage there should be an extensive list of what programs support PNGs, and to what extent, if you're interested.

What I've done sometimes to get transparency like you want is to use two textures, the second one being a simple grayscale mask that determines the transparency. Then D3D can be set up as such:

pDev->SetTexture(0, pTexture);pDev->SetTexture(1, pTexture_Mask);pDev->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);pDev->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);pDev->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);pDev->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);pDev->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_SELECTARG1);pDev->SetTextureStageState(1, D3DTSS_COLOROP, D3DTA_CURRENT);pDev->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);pDev->SetTextureStageState(1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);pDev->SetTextureStageState(2, D3DTSS_COLOROP, D3DTOP_DISABLE);pDev->SetTextureStageState(2, D3DTSS_ALPHAOP, D3DTOP_DISABLE);


I can't remember if there are any states I'm missing, but I wouldn't be surprised. Either way, that's the general idea. Although, now that I think about it, (there may be a better way to do this), but after loading the mask image, I had to lock it, and copy the color values to the alpha (I loaded the image into a D3DFMT_A8L8 format texture), and then unlock it; after that, it was ready to use.

On the other hand, within the start menu, the utilities menu for the DirectX SDK has a shortcut to a texture tool which allows you to import an image and a mask, and then save the image with transparency (from the mask) to a single file (specific to DirectX, though). That might actually be the easiest way to go.

Either way, best of luck.

[edited by - Agony on January 13, 2004 10:42:20 AM]
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke

This topic is closed to new replies.

Advertisement