Smoother Alphablending?

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

Recommended Posts

Hi, I used alphablending in my app. Since I wanted to have my pics alphablended between 0-255 I lock the whole picture to change the diffusecolor of the vertices and then use the blendstates to switch alphablending on. But this constant locking is eating away my frames if I want to show a nice, smooth transition of alphablending. Is there another faster way to do this? Thanks in advance.

Share on other sites
Do you need vertex color, or did you add it just to control alpha? If you don't need it, and you use lighting, you can just change the material (as in SetMaterial()) diffuse alpha value.

Another option is to change TFACTOR (a DX7 and up single color value). Just SetRenderState(D3DRS_TEXTUREFACTOR, color);, and in your texture stage states, instead of (x, ALPHAARGn, D3DTA_DIFFUSE); use (x, ALPHAARGn, D3DTA_TFACTOR);

If you require a DX9 card, you can use D3DTA_CONSTANT. If the card support border filtering, you can set the border color, and pass invalid UVs.

For this type of thing, TFACTOR works great. It's what I used up until everything became a shader and I could control how alpha is calculated myself.

Share on other sites
:) Thanks a lot. D3DTA_TFACTOR did the trick. :)

Share on other sites
Well thinking about this led me to the idea, to do the same with colors. It should be possible to fade colors in the texture a similar way, wouldn't it?

If yes, which renderstates would I have to touch?

What worries me is that there seems only to be one TFactor so I would have to change it's renderstates over time would I?

Share on other sites
You can use TFACTOR for color too. If you're unsure about how to setup different color and/or alpha blends, look into SetTextureStageState, and this recent thread

Yes, there is only 1 tfactor. If you require DX9 level hardware you can use CONSTANT which is like tfactor, except you can set a constant per stage... but then, if you required DX9 hardware you'd be using shaders and shader constants most likely.

If you use lighting, you can use the material.diffuse (pDev->SetMaterial()) to set color and alpha if you don't need control per vertex. D3DRS_COLORVERTEX can be used to enable/disable using vertex colors for lighting, D3DRS_DIFFUSEMATERIALSOURCE can be used to use material color even if vertex color is available.

If you don't use lighting, or require vertex colors, and you don't want to require a DX9 card... you can use vertex shaders (hardware or software emulated). If you require a DX8 card you can use vertex or pixel shaders in hardware.

Anyway, have a look at what you can do with SetTextureStageState, and if it's not enough, invest in learning shaders (you like math, right?).

Share on other sites

The Renderstates look the most promising (in terms of learning curve and hardware requierements) to me.

I'm curious about two things now.

1. How exactly can this fading with alphablending and "colorblending" be achieved. I tried the following for alpha:

Device.SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);Device.SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_TEXTURE);Device.SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);Device.SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);Device.SetRenderState(D3DRS_TEXTUREFACTOR, D3DCOLOR_ARGB(NewColor.A, NewColor.R, NewColor.G, NewColor.B));

This worked wonderful so I could use changing alphavalues to fade like I wanted to. Bu I'm not sure what Renderstates are now to do so that the whole color is also changed.

//I believe this line is the key, isn't it? I can't use TFactor anymore as I understand it but I just don't know thich renderstate to set here.Device.SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);Device.SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);Device.SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);Device.SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);

2. How to use software emulated shaders? I never even thought about the possibility to do this. I don't want to require shader-hardware, so no DX8 and 8 cards but if I can use software emulated shaders in a fast software emulated way this woulb be cool too. Can somebody point me to the information how to use these?

Share on other sites
To get software emulated shaders, just use a device created with creation flags for software vertex processing, or mixed processing. Look at your d3d device creation code. When using mixed processing, you have to be careful to set the appropriate "software" flags on VBs and IBs. There is also a call to switch between hardware and software processing (only available on a mixed mode device).

You could check if hardware shaders are available via caps and create a hardware device, or default to software otherwise. I think mixed mode devices might be more of a pain than useful, unless you need the possible occasional speed gain.

For color, it really depends on what you want. Standard is modulate diffuse (lighting or vertex color) and texture. To add in tfactor you'll need a second stage.

0 color: modulate, texture, diffuse
0 alpha: modulate, texture, tfactor
1 color: modulate, current, tfactor
1 alpha: selectarg1, current
2 color: disable
2 alpha: disable

so in the end that's:
color = (texture.rgb * diffuse.rgb) * tfactor.rgb
alpha = texture.a * tfactor.a

Any card worth supporting should be fine with that setup.

Share on other sites
Thanks for your help. I'll have a look at this stuff and see what I can do with it.

:)

• 13
• 18
• 29
• 11