Jump to content
  • Advertisement
Sign in to follow this  

[xna]AlphaBlend problem when rendering TexturedQuad

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Sorry for my poor English first. I am working with xna3.0 on c# express 2008. I would like to render a Textured Quad. I used the texture tool of directx sdk to generate a partial invisible .dds texture. I used BasicEffect for the rendering and I set: GraphicsDevice.RenderState.AlphaBlendEnable = true; GraphicsDevice.RenderState.AlphaDestinationBlend = Blend.One; GraphicsDevice.RenderState.AlphaSourceBlend = Blend.One; before calling effect.Begin(). I cleared the background with Color.White and what I expected is that the invisible part of the texture should blend with the white background and result in a white color. But in fact what I got is that the invisible part of the texture became all black. I would like to know why it did not work as I expected. Add what should I do to get the result that I wanted.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace test_01
    public class Game1 : Microsoft.Xna.Framework.Game
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Camera camera;//this is a Game Component used for setting view and projection matrix     
        Matrix worldTranslation;
        Matrix worldRotation;

        VertexPositionNormalTexture[] vertex;

        Texture2D texture;

        public Game1()
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

        protected override void Initialize()
            // TODO: Add your initialization logic here
            camera = new Camera(this, new Vector3(0, 0, 5), Vector3.Zero, Vector3.Up);

            worldTranslation = Matrix.Identity;
            worldRotation = Matrix.Identity;


        protected override void LoadContent()
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            vertex = new VertexPositionNormalTexture[4];
            vertex[0] = new VertexPositionNormalTexture(new Vector3(-1, 1, 0), new Vector3(0, 0, 1), new Vector2(0, 0));
            vertex[1] = new VertexPositionNormalTexture(new Vector3(1, 1, 0), new Vector3(0, 0, 1), new Vector2(1, 0));
            vertex[2] = new VertexPositionNormalTexture(new Vector3(-1, -1, 0), new Vector3(0, 0, 1), new Vector2(0, 1));
            vertex[3] = new VertexPositionNormalTexture(new Vector3(1, -1, 0), new Vector3(0, 0, 1), new Vector2(1, 1));

            texture = Content.Load<Texture2D>(@"images/cursor");

        protected override void UnloadContent()
            // TODO: Unload any non ContentManager content here

        protected override void Update(GameTime gameTime)
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)

            // TODO: Add your update logic here
            KeyboardState key = Keyboard.GetState();
            if (key.IsKeyDown(Keys.Left))
                worldTranslation *= Matrix.CreateTranslation(0, 0, -1);
            if (key.IsKeyDown(Keys.Right))
                worldTranslation *= Matrix.CreateTranslation(0, 0, 1);

            worldRotation *= Matrix.CreateRotationY(MathHelper.PiOver4 / 60);


        protected override void Draw(GameTime gameTime)

            // TODO: Add your drawing code here
            GraphicsDevice.VertexDeclaration = new VertexDeclaration(GraphicsDevice, VertexPositionNormalTexture.VertexElements);
            GraphicsDevice.RenderState.CullMode = CullMode.None;

            GraphicsDevice.RenderState.AlphaBlendEnable = true;
            GraphicsDevice.RenderState.AlphaDestinationBlend = Blend.One;
            GraphicsDevice.RenderState.AlphaSourceBlend = Blend.One;

            BasicEffect effect = new BasicEffect(GraphicsDevice, null);
            effect.World = worldRotation * worldTranslation * worldRotation;
            effect.View = camera.View;
            effect.Projection = camera.Projection;
            effect.VertexColorEnabled = false;
            effect.Texture = texture;
            effect.TextureEnabled = true;

            effect.FogColor = Color.White.ToVector3();
            effect.FogStart = 20;
            effect.FogEnd = 100;
            effect.FogEnabled = true;

            effect.DirectionalLight0.DiffuseColor = new Vector3(1, 1, 1);
            effect.DirectionalLight0.Direction = new Vector3(0, 0.5f, -1);
            effect.DirectionalLight0.SpecularColor = new Vector3(1, 0.5f, 0.5f);
            effect.DirectionalLight0.Enabled = true;
            effect.AmbientLightColor = new Vector3(0.2f, 0.2f, 0.2f);
            effect.DiffuseColor = new Vector3(0.5f, 0.5f, 0.5f);
            effect.LightingEnabled = true;

            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
                GraphicsDevice.DrawUserPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleStrip, vertex, 0, 2);


Share this post

Link to post
Share on other sites

I don't know anyting about XNA but I saw that it seems DX :)

I think you don't know "Blending Equation". Here:
OutPixel = [SrcPixel x SourceBlendFactor] (.) [DestPixel x DestBlendFactor]

(.) symbolizes the blending operation. Can be one of the following:

You can use one of the following for SourceBlendFactor and DestBlendFactor:

Now, it seems that SourcePixel is white, and DestPixel is invisible part of texture. So, OutPixel must be white, right?

SourceBlendFactor must be ONE and DestBlendFactor must be ZERO. Also, blendop must be ADD, I think.

Hope this helps.

Share this post

Link to post
Share on other sites
Thank you for your reply!

I modified the code to follow :
GraphicsDevice.RenderState.AlphaBlendEnable = true;
GraphicsDevice.RenderState.AlphaDestinationBlend = Blend.One;
GraphicsDevice.RenderState.AlphaSourceBlend = Blend.Zero;
GraphicsDevice.RenderState.AlphaBlendOperation = BlendFunction.Add;

but I get always the same result, invisible part is still black.

And I noticed something strange:
no matter how I change the dstFactor, the srcFactor, or the blendOp, I get the same rendering result.

I am wondering did I forget to enable or disable something?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!