XNA Creators Club Online
Page 1 of 1 (8 items)
Sort Posts: Previous Next

Alpha blending problem!

Last post 23/03/2009 21:51 by Scyde. 7 replies.
  • 21/03/2009 11:14

    Alpha blending problem!

    Hello everyone, so here is my problem:
    I have a png image, which i load to my texture.

    mytex = GraphicsUtility.CreateTexture(dx, "myimage.png", Format.X8R8G8B8); 

    *I guess after this stage i got a red circle on the black background and it seems that it has no alpha map, right?
    After that i draw this to my surface:

    dx.SetRenderTarget(0, rtSurf); 
    dx.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.FromArgb(0,0,0,0), 1.0f, 0); //Clean it with black color 
    try 
        DrawMyCircleTexture(); 
    finally 
        dx.SetRenderTarget(0, originalSurface); 


    Then i set up render states and draw surface's texture:

    dx.RenderState.CullMode = Cull.None; 
    dx.RenderState.AlphaBlendEnable = true
    dx.RenderState.SourceBlend = Blend.SourceAlpha; 
    dx.RenderState.DestinationBlend = Blend.InvSourceAlpha; 
    dx.RenderState.BlendOperation = BlendOperation.Add; dx.RenderState.AlphaFunction = (Compare)comboAF.SelectedItem; 
    dx.TextureState[0].AlphaOperation = TextureOperation.Modulate; 
    dx.SamplerState[0].AddressU = TextureAddress.Mirror;  
    dx.SamplerState[0].AddressV = TextureAddress.Mirror;  
    dx.SamplerState[0].MinFilter = TextureFilter.Anisotropic; 
    dx.SamplerState[0].MagFilter = TextureFilter.Anisotropic; 
    dx.RenderState.ZBufferEnable = false; dx.RenderState.ZBufferWriteEnable = false
    sprite.Begin(SpriteFlags.SortTexture);  
    sprite.Draw2D(mytex, new Rectangle(), new SizeF(), new PointF(), Color.White); //White color to save image colors  
    sprite.End(); 

    So here is result images in depence of background color:



    Does anyone know why cirlce dissapears? How to keep it's color? What blend do i need?

    Thanks
  • 21/03/2009 15:33 In reply to

    Re: Alpha blending problem!

    what is your goal, eg the desired result?
    http://www.futuregpu.net ex-Aces Lead PM/ex DX SDK PM/ex D3D Evangelist now LRB Launch Native Title Wrangler
  • 21/03/2009 17:47 In reply to

    Re: Alpha blending problem!

    My goal is to keep red circle's color red, even if that is drawn with white background.
    I guess the problem is that i lose any alpha channel after drawing my objects to black surface.
    So maybe i need somehow to remap transparency of new resulting texture?
  • 23/03/2009 11:17 In reply to

    Re: Alpha blending problem!

    In what order are you rendering the textures?
    Are you rendering the background texture first and then the circle texture?

    If you render the background texture first, and then the circle using alpha testing, the circle will remain the same no matter what you do with the background texture.

    How is the png created? Do you want to have some parts of the circle invisible at all time, and then the solid parts based on the transparency percentage?
    Petri Wilhelmsen
    Dark Codex Studios

    Blog | Twitter

    Microsoft XNA/DirectX MVP
  • 23/03/2009 16:53 In reply to

    Re: Alpha blending problem!

    I rendered the only one texture, it is a red circle, created in photoshop with transparent layer.
    How destination image was created: circle is rendered to black-color-cleared surface. So, i have a black suface with a red circle on it. Then i get the texture of this surface and render it.

    I want to have a solid part to be red all the time, even if the bg is white, and the black part/transparent one to be invisible.
  • 23/03/2009 17:17 In reply to

    Re: Alpha blending problem!

    read the dx9 documentation on "Alpha Blending " and "Texture Alpha" and use the fixed function pipeline texture blending to control this, that might give you more control than framebuffer alpha.

    the blendfactor operator might also be useful.

    once you get used to this with the FFP, then you can write some shaders to do the same thing.
    http://www.futuregpu.net ex-Aces Lead PM/ex DX SDK PM/ex D3D Evangelist now LRB Launch Native Title Wrangler
  • 23/03/2009 18:54 In reply to

    Re: Alpha blending problem!

    So i see that new rendertarget texture kills all transparency...
    No simple way to get previous alpha map?

    What about BlendFactor? 

    Looks like it is all about how to make transparent render target texture...
    Found some examples:

    But still cant figure it out :(
  • 23/03/2009 21:51 In reply to

    Re: Alpha blending problem!

    I got it.
    Render target texture should have X8R8G8B8 format.
    Before rendering to texture i need to clean it with Color.FromARGB(0,0,0,0).
    Thats it! You got a transparent render surface;

Page 1 of 1 (8 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG