Tomas Jecha

Hello,

I've already wrote about this problem earlier but now I attaching simplified source code - [here] . Problem is (as you can see on this screenshot) with alphablending and rendering on RenderTarget2D. How can I fix how that text on right look like

Remark: It have to work even with colored font or pictures, not only with white font.

Thanks for every help! And sorry for my English.


Here's code from that zip file:

Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Audio
Imports Microsoft.Xna.Framework.Content
Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework.Input
Imports Microsoft.Xna.Framework.Storage

Namespace Project
    Module Program
        Public Class Program
            Shared Sub Main()
                Using owApp As New gm()
                    owApp.Run()
                End Using
            End Sub
        End Class
    End Module
    Public Class gm
        Inherits Game
        Public eff As BasicEffect
        Public graphics As GraphicsDeviceManager
        Dim tex As RenderTarget2D
        Dim xna_picture As Texture2D
        Dim sb As SpriteBatch

        Sub New()
            graphics = New GraphicsDeviceManager(Me)

            graphics.PreferredBackBufferWidth = 1024
            graphics.PreferredBackBufferHeight = 768
            graphics.PreferMultiSampling = False
            graphics.SynchronizeWithVerticalRetrace = True
            graphics.PreferredDepthStencilFormat = DepthFormat.Depth24

            graphics.IsFullScreen = False
            Me.Window.AllowUserResizing = True
            Me.IsMouseVisible = True

        End Sub

        Protected Overrides Sub Initialize()
            MyBase.Initialize()
        End Sub

        Protected Overrides Sub LoadGraphicsContent(ByVal loadAllContent As Boolean)
            If loadAllContent Then
                sb = New SpriteBatch(graphics.GraphicsDevice)
            End If

            tex = New RenderTarget2D(graphics.GraphicsDevice, 512, 256, 1, SurfaceFormat.Color, MultiSampleType.None, 0)
            xna_picture = Texture2D.FromFile(graphics.GraphicsDevice, "../../xna_pic.png")
        End Sub

        Protected Overrides Sub Update(ByVal gameTime As GameTime)
            If Input.Keyboard.GetState.IsKeyDown(Keys.Escape) Then Me.Exit()

            MyBase.Update(gameTime)
        End Sub

        Protected Overrides Sub Draw(ByVal GameTime As GameTime)
            graphics.GraphicsDevice.Clear(ClearOptions.Target Or ClearOptions.DepthBuffer, Microsoft.Xna.Framework.Graphics.Color.Navy, 10000.0F, 10000.0F)

            graphics.GraphicsDevice.SetRenderTarget(0, tex)
            graphics.GraphicsDevice.Clear(New Color(0, 0, 0, 0))
            graphics.GraphicsDevice.ResolveRenderTarget(0)

            sb.Begin()
            sb.Draw(xna_picture, New Vector2(0, 0), Color.White)
            sb.Draw(xna_picture, New Vector2(40, 40), Color.White)
            sb.End()
            graphics.GraphicsDevice.SetRenderTarget(0, Nothing)



            sb.Begin()
            sb.Draw(xna_picture, New Vector2(200, 200), Color.White)
            sb.Draw(tex.GetTexture, New Vector2(Input.Mouse.GetState.X, Input.Mouse.GetState.Y), Color.White)
            sb.End()

            MyBase.Draw(GameTime)

        End Sub
    End Class
End Namespace




Re: XNA Framework Problems with rendering

Pocketmnky

I could be wrong, but what is happening here looks like you're losing the anti-aliasing fringes wherever the drawing program you used to generate that font set the alpha of the text < 100%.

What seemed to work for me is for you to change your sb.Begin() calls to:

sb.Begin(SpriteBlendMode.Additive, SpriteSortMode.Texture, SaveStateMode.SaveState)

and also when you're drawing to the surface, instead of clearing to Vector4(0, 0, 0, 0), just clear to Color.Black.






Re: XNA Framework Problems with rendering

Tomas Jecha

Pocketmnky wrote:

I could be wrong, but what is happening here looks like you're losing the anti-aliasing fringes wherever the drawing program you used to generate that font set the alpha of the text < 100%.

What seemed to work for me is for you to change your sb.Begin() calls to:

sb.Begin(SpriteBlendMode.Additive, SpriteSortMode.Texture, SaveStateMode.SaveState)

and also when you're drawing to the surface, instead of clearing to Vector4(0, 0, 0, 0), just clear to Color.Black.



Thanks, but that works properly only with white pictures because of additive blending. So it not solving my problem.





Re: XNA Framework Problems with rendering

TwelveBaud

I noticed you're rendering to a render target of surface format "Color". According to the documentation this surface format should be able to support alpha, but perhaps the renderer does not. Firstly, why render to a seperate render target anyway Unless you're doing 3D graphics, the only reason to do so that I'm aware of is to increase efficiency with rendering a large number of sprites by rendering them as seperate quads once and rendering them as one quad all subsequent times, but it doesn't appear you're doing 3D graphics and you re-create the render target's texture each draw.

Wait...

I just read the documentation for ResolveRenderTarget and it looks like you should place that line after you draw the text, that is, just before you call graphics.GraphicsDevice.SetRenderTarget(0, Nothing).





Re: XNA Framework Problems with rendering

Tomas Jecha

But that's my problem. I need to render large number of sprites. That code is just sample, just isolated problem.




Re: XNA Framework Problems with rendering

Shawn Hargreaves - MSFT

Rendering alpha blended things to a separate target and then compositing that over the top of your main framebuffer is quite tricky to get right - I'd recommend you avoid doing this and just draw everything directly to the backbuffer if at all possible!

That said, the way to make this work is using premultiplied alpha. Tom Forsyth writes about all the gory details:

http://home.comcast.net/~tom_forsyth/blog.wiki.html#%5B%5BPremultiplied%20alpha%5D%5D

And here's a paper from the guys who first invented this:

ftp://ftp.alvyray.com/Acrobat/4_Comp.pdf