Jump to content
  • Advertisement
Sign in to follow this  

[.net] Alpha Blending textures help

This topic is 4851 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

Basically, I am writing a small game in C# using direct3D 9 and .net 1.1 I am trying to draw a texture that has part of it colored Magenta (255,0,255) so that the magenta part is transparent. I have ran other demos that do this properly but for some reason my application just shows the color black where I want my transparency to go. Here are the relavant parts of my code: Creating the device
		public bool InitializeGraphics()
				// Now  setup our D3D stuff
				PresentParameters presentParams = new PresentParameters();
				presentParams.SwapEffect = SwapEffect.Discard;
				device = new d3d.Device(0, d3d.DeviceType.Hardware, this, 
					CreateFlags.SoftwareVertexProcessing, presentParams);
				device.DeviceReset += new System.EventHandler(this.OnResetDevice);
				this.OnResetDevice(device, null);
				return true;
			catch (DirectXException)
				return false; 
Setting up the RenderStates
		public void OnResetDevice(object sender, EventArgs e)
			d3d.Device device = (d3d.Device)sender;

			// Set state
			device.RenderState.CullMode = Cull.None;
			device.RenderState.ZBufferEnable = false;
			device.RenderState.Lighting = false;

			device.RenderState.AlphaSourceBlend = Blend.SourceAlpha;
			device.RenderState.AlphaDestinationBlend = Blend.InvSourceAlpha;
			device.RenderState.AlphaBlendEnable = true;
Rendering (Note: I have tried a LOT of different TextureState operations / combinations but am still welcome to try others)
		public void Render( int delta, CModelManager mm )
			if (device == null) 

			//Clear the backbuffer to a blue color 
			device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0);
			//Begin the scene

			// Rendering of scene objects can happen here
			device.SetStreamSource(0, vb, 0);
			device.VertexFormat = CustomVertex.PositionTextured.Format;

			device.SetTexture( 0, model.GetTexture() );

			device.TextureState[0].ColorOperation = TextureOperation.SelectArg1;
			device.TextureState[0].ColorArgument1 = TextureArgument.TextureColor;
			device.TextureState[0].AlphaOperation = TextureOperation.SelectArg1;
			device.TextureState[0].AlphaArgument1 = TextureArgument.TextureColor;

			device.DrawPrimitives(PrimitiveType.TriangleList, model.GetVBPos, model.GetNumVertices() / 3);

			//End the scene
			Console.WriteLine("Delta: {0}", delta);
Loading my texture:
			texTexture = TextureLoader.FromFile( dev, Application.StartupPath + strTexture
				, 0, 0, 1, Usage.None, Format.Unknown, Pool.Managed, Filter.None, Filter.None, Color.Magenta.ToArgb() );
After looking at a few other peoples projects that have the alpha blending WORKING, I have no idea what is wrong with mine. Please, if you see anything wrong with the code or can give me any suggestions, I would greatly appreciate it. Thank you

Share this post

Link to post
Share on other sites
What image format are you using to store your textures? Looking over your code, you appear to be doing everything pretty much correctly. Perhaps it's not your settings, but something else ...

First off, make sure that you verify that your image Magenta and DX's Magenta are both really 255,0,255. I know it sounds kinda stupid, but sometimes these things happen :-). Also, if you're saving your image in an indexed format or a format that utilizes lossy compression, it's possible that the precise color value has been lost.

Also, I've had trouble before with MDX with some hinky formats where it loaded the image data backwards (for instance, the image data was stored as BGRA and DX thought otherwise). It's possible.

At least make absolutely sure that your image data is loading properly before you tear out all of your hair :-). Try setting a known pixel to your color key value and comparing it with System.Drawing.Colors.Magenta during runtime. If the test turns out to be correct, then at least you know you can rule out image problems as the cause.

In the mean time, I'll do a bit more research.

Edit: Also, take note of the fact that the texture loader considers the alpha value of the colorkey to be significant. IE, 0x00000000 and 0xFF000000 are two different color keys, even though they are both black.

Share this post

Link to post
Share on other sites
Thank you for our suggestions.

I am using mspaint to save the images as simple .bmp files. I am positive that it is true magenta because using my current codes, all the magenta in my images turn BLACK instead of becoming TRANSPARENT. When I change my color key to some other color that my images hold, that new color becomes black and I can see the magenta again. I also checked the Color.Magenta.A value and it was indeed 255. I even went as far as to change the alpha component to 0 and the magenta color (which should be transparent) continues to display as black.

My current code (the stuff that is posted) is basically taken from someone's exampe program that they wrote where the alpha blending does work so I know it is not my video card or drivers or something.

Any more suggestions would be helpful. I am wondering if it is an ordering problem? Like I should be one operation before another but I have it backwards... this is really confusing. Please let me know if you see anything out of place. Thank you.

Share this post

Link to post
Share on other sites

It dawned on me about 15 minutes after I posted that you had to have had the colors right or they wouldn't have rendered as black :-). Anyways, can you post a copy of your source in a zipfile on one of those free file host thingies and drop a link here for it? It may have something to do with the order you are doing things in, or some other small detail that is not showing up in the code you posted.


Share this post

Link to post
Share on other sites
Well, I simplified my source somewhat for this post here is the entire source for my project so far: http://download.isopimp.com/shooter.rar

Share this post

Link to post
Share on other sites
Your render states are incorrect.

Instead of:

Device.RenderState.AlphaSourceBlend = Blend.SourceAlpha;
Device.RenderState.AlphaDestinationBlend = Blend.InvSourceAlpha;

They should be:

Device.RenderState.SourceBlend = Blend.SourceAlpha;
Device.RenderState.DestinationBlend = Blend.InvSourceAlpha;

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!