Problem with Directx 9 alpha blending.

Started by
4 comments, last by reuomi 18 years, 6 months ago
I wrote an effect file to do alpa blending. I soon noticed after working more on it that there are points in the blend where polygons are being skipped or somthing.. What happens is i enable alpha blending liek so AlphaBlendEnable = 1; DestBlend = 6; SrcBlend = 5; then it looks like the polygons that are beign blended are going strieght to the back (not even showing the objects behind them just takign the transperencies streight to the background (blue)) any idea why this is happening?
Indy to Pro: Journeyhttp://phiendstudios.blogspot.com
Advertisement
In addition after further tinkering i've noticed that when rotated compleately around (180 degrees) the alpha blending works as i wanted. In short it will render correctly on the front faces, but backfaces wont render it correctly so how can i fix the alpha blending on the back faces?

Another strange problem. in maya i have a surface that is shaded with the shader mentioned above that is having all these problems. whats happening is.. say i duplicate the object; if i place it behind the original the transparency ignores it and goes streight to the blue background. But if i place it above or infront of the original surface it renders the way it should (showign the object behind it.

[Edited by - reuomi on October 8, 2005 9:52:32 PM]
Indy to Pro: Journeyhttp://phiendstudios.blogspot.com
Alpha blending is rather a misnomer. It's really "framebuffer blending" or "render target blending". What happens is that when alpha blending is enabled, Direct3D blends between the color of the currently rendered pixel and the color of the pixel already in the render target. What that means is that if you have 2 objects, A and B, with B behind A, you have to render B first if you want A to blend properly with it. If you render A first, then it will blend with the background (because that's what was in the render target).

In short: With alphablending, render all opaque objects first. Then render transparent objects back-to-front.

I see, so how would i go about rendering the alpha first in this effect file

 //// Simple Lighting Model// Copyright (c) Microsoft Corporation. All rights reserved.//// Note: This effect file works with EffectEdit.// Note: This effect file is SAS 1.0.1 compliant and will work with DxViewer.//int sas : SasGlobal<    bool SasUiVisible = false;    int3 SasVersion= {1,1,0};>;string XFile< bool SasUiVisible = false; > = "tiger\\tiger.x";   // modelint    BCLR< bool SasUiVisible = false; > = 0xff202080;          // background// light direction (view space)float3 lightDir <      bool SasUiVisible = false;    string SasBindAddress= "Sas.DirectionalLight[0].Direction";    string UIDirectional = "Light Direction"; > = {0.577, -0.577, 0.577};// light intensityfloat4 I_a<    bool SasUiVisible = false;    string SasBindAddress= "Sas.AmbientLight[0].Color";> = { 0.1f, 0.1f, 0.1f, 1.0f };    // ambientfloat4 I_d<    bool SasUiVisible = false;    string SasBindAddress= "Sas.DirectionalLight[0].Color";> = { 1.0f, 1.0f, 1.0f, 1.0f };    // diffusefloat4 I_s<    string SasUiLabel = "light specular";    string SasUiControl = "ColorPicker";> = { 1.0f, 1.0f, 1.0f, 1.0f };    // specular// material reflectivityfloat4 k_a : MATERIALAMBIENT<    string SasUiLabel = "material ambient";    string SasUiControl = "ColorPicker";> = { 1.0f, 1.0f, 1.0f, 1.0f };    // ambientfloat4 k_d : MATERIALDIFFUSE<    string SasUiLabel = "material diffuse";    string SasUiControl = "ColorPicker";> = { 1.0f, 1.0f, 1.0f, 1.0f };    // diffusefloat4 k_s : MATERIALSPECULAR<    string SasUiLabel = "material specular";    string SasUiControl = "ColorPicker";> = { 1.0f, 1.0f, 1.0f, 1.0f };    // specularfloat  k_n   : MATERIALPOWER<    string SasUiLabel = "Material Specular Power";    string SasUiControl = "Slider";     float SasUiMin = 1.0f;     float SasUiMax = 32.0f;     int SasUiSteps = 31;> = 8.0f;                           // power// texturetexture Tex0 <     string SasUiLabel = "Texture Map";    string SasUiControl= "FilePicker";    string name = "c:/Texture1.dds"; >;// transformationsfloat4x4 World      : WORLD<    bool SasUiVisible = false;    string SasBindAddress= "Sas.Skeleton.MeshToJointToWorld[0]";>;float4x4 View       : VIEW<    bool SasUiVisible = false;    string SasBindAddress= "Sas.Camera.WorldToView";>;float4x4 Projection : PROJECTION<    bool SasUiVisible = false;    string SasBindAddress= "Sas.Camera.Projection";>;struct VS_OUTPUT{    float4 Pos  : POSITION;    float4 Diff : COLOR0;    float4 Spec : COLOR1;    float2 Tex  : TEXCOORD0;};VS_OUTPUT VS(    float3 Pos  : POSITION,     float3 Norm : NORMAL,     float2 Tex  : TEXCOORD0){    VS_OUTPUT Out = (VS_OUTPUT)0;    float3 L = -lightDir;    float4x4 WorldView = mul(World, View);    float3 P = mul(float4(Pos, 1), (float4x3)WorldView);  // position (view space)    float3 N = normalize(mul(Norm, (float3x3)WorldView)); // normal (view space)    float3 R = normalize(2 * dot(N, L) * N - L);          // reflection vector (view space)    float3 V = -normalize(P);                             // view direction (view space)    Out.Pos  = mul(float4(P, 1), Projection);             // position (projected)    Out.Diff = I_a * k_a + I_d * k_d * max(0, dot(N, L)); // diffuse + ambient    Out.Spec = I_s * k_s * pow(max(0, dot(R, V)), k_n/4);   // specular    Out.Tex  = Tex;                                           return Out;}sampler Sampler<bool SasUiVisible = false;> = sampler_state{    Texture   = (Tex0);    MipFilter = LINEAR;    MinFilter = LINEAR;    MagFilter = LINEAR;};float4 PS(    float4 Diff : COLOR0,    float4 Spec : COLOR1,    float2 Tex  : TEXCOORD0) : COLOR{    return tex2D(Sampler, Tex) * Diff + Spec;}technique TVertexShaderOnly{    pass P0    {        // I began Alpha blending here//        AlphaBlendEnable = 1;        DestBlend = 6;        SrcBlend = 5;        // lighting        Lighting       = FALSE;        SpecularEnable = TRUE;        // samplers        Sampler[0] = (Sampler);        // texture stages        ColorOp[0]   = MODULATE;        ColorArg1[0] = TEXTURE;        ColorArg2[0] = DIFFUSE;        AlphaOp[0]   = MODULATE;        AlphaArg1[0] = TEXTURE;        AlphaArg2[0] = DIFFUSE;        ColorOp[1]   = DISABLE;        AlphaOp[1]   = DISABLE;                     Cullmode = 1;        // shaders        VertexShader = compile vs_1_1 VS();        PixelShader  = NULL;    }}


Just scroll to the technique to see where i put the blend command


so how would i go about rendering all alpha blending meshes i nthe rigth order to get the correct effect?

[Edited by - reuomi on October 9, 2005 12:28:55 AM]
Indy to Pro: Journeyhttp://phiendstudios.blogspot.com
Quote:Original post by reuomi
so how would i go about rendering all alpha blending meshes i nthe rigth order to get the correct effect?

That's an engine thing - nothing to do with fx. You'll need to add code to your renderer to go through your objects, rendering only opaque ones, and sorting out transparent ones back to front, then rendering them.

thanks a lot you've been beyond helpful
Indy to Pro: Journeyhttp://phiendstudios.blogspot.com

This topic is closed to new replies.

Advertisement