Results 1 to 3 of 3

Thread: Alpha blending problem

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Arrow Alpha blending problem

    Hello all.

    Finishing off the very first 3D dual video channel model viewer, but I got into a problem. I have already managed to draw the two renders into textures, they are named dLTexture and dRTexture. Both textures are rendered correctly.

    I know how to draw these two textures in cross-eye mode by using sprites (SContainer), but now I have to draw them as anaglyph. This means, both textures with half opacity and left channel no green and right channel no red color. I have to blend these two together, so here is the code:
    Code:
                Device.RenderState.SourceBlend = Direct3D.Blend.DestinationAlpha
                Device.RenderState.DestinationBlend = Direct3D.Blend.SourceAlpha
                Device.RenderState.AlphaBlendEnable = True
                Device.Clear(ClearFlags.Target Or ClearFlags.ZBuffer, Color.Transparent, 1, 0)
                Device.BeginScene()
                SContainer.Begin(SpriteFlags.None)
                If KeyPressed(Windows.Forms.Keys.Q) Then
                    SContainer.Draw2D(dRTexture, New Point(0, 0), 0, New Point(0, 0), Color.FromArgb(128, 0, 255, 128))
                    SContainer.Draw2D(dLTexture, New Point(0, 0), 0, New Point(0, 0), Color.FromArgb(128, 255, 0, 128))
                Else
                    SContainer.Draw2D(dLTexture, New Point(0, 0), 0, New Point(0, 0), Color.FromArgb(128, 255, 0, 128))
                    SContainer.Draw2D(dRTexture, New Point(0, 0), 0, New Point(0, 0), Color.FromArgb(128, 0, 255, 128))
                End If
                SContainer.End()
                Device.EndScene()
                Device.Present()
    But, when I press Q the screen changes, and this should NOT happen. The two textures must be blended so that they add color, so twisting the texture draw order should not make a difference. What am I doing wrong here?

    BTW: when I use "Device.RenderState.DestinationBlend = Direct3D.Blend.SourceColor" it multiplies the colors, clearing the red and green channel. I don't want this. It must "add" the color values of both textures.
    Last edited by bergerkiller; Jan 3rd, 2011 at 12:50 PM.

  2. #2
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Alpha blending problem

    Have you tried:
    Code:
    SContainer.Begin(SpriteFlags.AlphaBlend Or SpriteFlags.SortDepthFrontToBack)
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Re: Alpha blending problem

    Sorry does not work. I guess the container is fine, I have to set the renderstate in such manner that it actually adds the two texture colors.

    I tried lots of renderstates and color alpha values, but it ll ends up in different colors/color loss.

    EDIT

    I got it this far:


    Code:
                Device.RenderState.AlphaBlendEnable = True
                Device.RenderState.BlendOperation = BlendOperation.Add
                'Device.RenderState.SourceBlend = Blend.DestinationAlpha
                Device.RenderState.DestinationBlend = Blend.InvSourceAlpha
                If KeyPressed(Windows.Forms.Keys.Q) Then
                    SContainer.Draw2D(dRTexture, New Point(0, 0), 0, New Point(0, 0), Color.FromArgb(255, 0, 255, 128))
                    SContainer.Draw2D(dLTexture, New Point(0, 0), 0, New Point(0, 0), Color.FromArgb(128, 255, 0, 128))
                Else
                    SContainer.Draw2D(dLTexture, New Point(0, 0), 0, New Point(0, 0), Color.FromArgb(255, 255, 0, 128))
                    SContainer.Draw2D(dRTexture, New Point(0, 0), 0, New Point(0, 0), Color.FromArgb(128, 0, 255, 128))
                End If
    Little dark for my taste...and it's a weird way of using alpha. On the other hand, the scene is empty on start. It's logical the alpha values of the first render are always 255... xD
    Last edited by bergerkiller; Jan 3rd, 2011 at 04:55 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width