Archived

This topic is now archived and is closed to further replies.

zilch_

C#, DX9 and Alpha Blending

Recommended Posts

I have made a class, Image, representing a 2d image on the screen. Works OK except that the AlphaBlending don't work. I load a png file which has built-in nice alpha values, but I don't know what is wrong. The vertexbuffer contains four points and are drawn using trianglestrip. The vertexbuffer is locked and changed when rendered, to match the coordinates where it is supposed to be drawn. Could it be the loading? Have i forgot some fundamental renderstate changes? Thank you alot for any hints or suggestions... here is the code:
    

using System;
using System.Windows.Forms;
using Direct3D = Microsoft.DirectX.Direct3D;


public class Image
{
	Direct3D.Device _device = null;
	Direct3D.Texture _texture = null;
	Direct3D.VertexBuffer _vertexBuffer = null;

	int _width	= -1;
	int _height = -1;

	public Image( Direct3D.Device device )
	{
		_device = device;
	}

	public void Render( int x, int y )
	{
		Direct3D.CustomVertex.TransformedTextured[] verts = (Direct3D.CustomVertex.TransformedTextured[]) _vertexBuffer.Lock( 0, 0 );

		verts[0].X = x;
		verts[0].Y = y + _height;
			
		verts[1].X = x;
		verts[1].Y = y;
			
		verts[2].X = x + _width;
		verts[2].Y = y + _height;

		verts[3].X = x + _width;
		verts[3].Y = y;

		_vertexBuffer.Unlock();

		_device.SetStreamSource( 0, _vertexBuffer, 0);
		_device.VertexFormat = Direct3D.CustomVertex.TransformedTextured.Format;
		_device.SetTexture(0, _texture);
				
		_device.RenderState.AlphaFunction = Direct3D.Compare.Always;
		_device.RenderState.ZBufferEnable = false;
		_device.RenderState.AlphaTestEnable = true;
		_device.RenderState.AlphaBlendEnable = true;
		_device.RenderState.AlphaBlendOperation = Direct3D.BlendOperation.Add;
		_device.RenderState.AlphaSourceBlend = Direct3D.Blend.SourceAlpha;
		_device.RenderState.AlphaDestinationBlend = Direct3D.Blend.InvSourceAlpha;

		/*			_device.TextureState[0].AlphaOperation = Direct3D.TextureOperation.BlendTextureAlpha;
					_device.TextureState[0].AlphaArgument0 = Direct3D.TextureArgument.Current;
					_device.TextureState[0].AlphaArgument1 = Direct3D.TextureArgument.TextureColor;
					_device.TextureState[0].ColorOperation = Direct3D.TextureOperation.Modulate;
					_device.TextureState[0].ColorArgument1 = Direct3D.TextureArgument.TextureColor;
					_device.TextureState[0].ColorArgument2 = Direct3D.TextureArgument.Diffuse;*/

		_device.DrawPrimitives( Direct3D.PrimitiveType.TriangleStrip, 0, 2 );
	}

	public void Load( string filename )
	{
		Direct3D.ImageInformation imageInformation = new Direct3D.ImageInformation();

		_vertexBuffer = new Direct3D.VertexBuffer( typeof(Direct3D.CustomVertex.TransformedTextured), 4, _device, 0, Direct3D.CustomVertex.TransformedTextured.Format, Direct3D.Pool.Managed );
		_vertexBuffer.Created += new System.EventHandler( this.OnCreateVertexBuffer );
		this.OnCreateVertexBuffer( _vertexBuffer, null );
			
		//_texture = Direct3D.TextureLoader.FromFile( _device, filename, 0, 0, 0, 0, Direct3D.Format.A8R8G8B8, Direct3D.Pool.Managed, Direct3D.Filter.None, Direct3D.Filter.None, System.Drawing.Color.Black.ToArgb(), ref imageInformation );

		_texture = Direct3D.TextureLoader.FromFile( _device, filename );
		imageInformation = Direct3D.TextureLoader.ImageInformationFromFile( filename );

		_width = imageInformation.Width;
		_height = imageInformation.Height;
	}

	public void OnCreateVertexBuffer(object sender, EventArgs e)
	{
		Direct3D.VertexBuffer vb = (Direct3D.VertexBuffer)sender;
		Direct3D.GraphicsStream graphicsStream = vb.Lock(0, 0, 0);
		Direct3D.CustomVertex.TransformedTextured[] verts = new Direct3D.CustomVertex.TransformedTextured[4];

		verts[0].X=0; verts[0].Y=0; verts[0].Z=0.5f; verts[0].Rhw=1; verts[0].Tu = 0.0f; verts[0].Tv = 1.0f;
		verts[1].X=0; verts[1].Y=0; verts[1].Z=0.5f; verts[1].Rhw=1; verts[1].Tu = 0.0f; verts[1].Tv = 0.0f;
		verts[2].X=0; verts[2].Y=0; verts[2].Z=0.5f; verts[2].Rhw=1; verts[2].Tu = 1.0f; verts[2].Tv = 1.0f;
		verts[3].X=0; verts[3].Y=0; verts[3].Z=0.5f; verts[3].Rhw=1; verts[3].Tu = 1.0f; verts[3].Tv = 0.0f;

		graphicsStream.Write(verts);
		vb.Unlock();
	}
}
  
[edited by - zilch_ on December 23, 2002 8:25:33 AM]

Share this post


Link to post
Share on other sites