Archived

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

alnite

alpha blending not working in C#

Recommended Posts

I am drawing two quads. One with texture, the other without texture. The texture one is on top. I have set the following render states: // disable z-buffer d3dDevice.RenderState.ZBufferEnable = false; d3dDevice.RenderState.ZBufferWriteEnable = false; // enable alpha blending d3dDevice.RenderState.AlphaBlendEnable = true; d3dDevice.RenderState.AlphaSourceBlend = Blend.SourceAlpha; d3dDevice.RenderState.AlphaDestinationBlend = Blend.InvSourceAlpha; but it still doesn't work. The texture is in A8R8G8B8 format, .dds format, and it has its own alpha channel, and yes it's there. I also disable the color key when loading it. quadTexture = TextureLoader.FromFile( d3dDevice, "hello.dds", D3DX.Default, D3DX.Default, D3DX.Default, 0, Format.A8R8G8B8, Pool.Managed, Filter.None, Filter.None, 0 ); Yes, my video card supports alpha blending because all the samples work. And this is my drawing routine: d3dDevice.SetStreamSource( 0, vb, 0 ); d3dDevice.VertexFormat = CustomVertex.PositionColoredTextured.Format; d3dDevice.RenderState.AlphaBlendEnable = true; d3dDevice.RenderState.AlphaSourceBlend = Blend.SourceAlpha; d3dDevice.RenderState.AlphaDestinationBlend = Blend.InvSourceAlpha; d3dDevice.SetTexture( 0, null ); d3dDevice.Transform.World = Matrix.Translation( 4.5f, 4.5f, 0.0f); d3dDevice.DrawPrimitives( PrimitiveType.TriangleStrip, 0, 2 ); d3dDevice.SetTexture( 0, quadTexture ); d3dDevice.Transform.World = Matrix.Translation( 5.0f, 5.0f, 0.0f); d3dDevice.DrawPrimitives( PrimitiveType.TriangleStrip, 4, 2 ); Yet it doesn't work! What did I miss!? [edited by - alnite on October 9, 2003 4:06:45 PM]

Share this post


Link to post
Share on other sites
can somebody please tell me??

Seriously, I don''t know what''s wrong! All samples work, my previous alpha blending programs written in C++ also work, and I use the same texture. But this doesn''t!?!

Anybody else has samples of alpha blending written in C#?

Share this post


Link to post
Share on other sites
Bah! I give up, I'll post the whole source code. Please somebody let me know where I did wrong.

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

using DirectX = Microsoft.DirectX;
using Direct3D = Microsoft.DirectX.Direct3D;

namespace WindowsApplication2
{
// Summary description for Form1.

public class frmMain : System.Windows.Forms.Form
{
private Device d3dDevice = null;
private PresentParameters presentParams = new PresentParameters();
private VertexBuffer vb = null;
private Texture quadTexture = null;

private bool deviceIsLost = false;

float angle = 0.0f;

public frmMain()
{
this.Text = "Direct3D Test";
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.ControlBox = true;
}

private bool InitializeGraphics( )
{
try
{
BuildPresentParameters();

// Create Direct3D Device

d3dDevice = new Direct3D.Device( 0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, presentParams );

// Set event handlers

d3dDevice.DeviceLost += new System.EventHandler( this.InvalidateObjects );
d3dDevice.DeviceReset += new System.EventHandler( this.RestoreObjects );
d3dDevice.Disposing += new System.EventHandler( this.DeleteObjects );
d3dDevice.DeviceResizing += new System.ComponentModel.CancelEventHandler( this.DeviceResized );

deviceIsLost = false;

// set viewport

Viewport vp = new Viewport();
vp.X = 0;
vp.Y = 0;
vp.Width = 640;
vp.Height = 480;
vp.MinZ = 0.0f;
vp.MaxZ = 1.0f;
d3dDevice.Viewport = vp;

// set projection matrix

int tileRatioHeight = 15;
int tileRatioWidth = 20;
float tileRatio = ((float)tileRatioHeight) / tileRatioWidth;

Matrix projmat = Matrix.Identity;
projmat.M22 = -( 2.0f / (float) tileRatioHeight );
projmat.M11 = -projmat.M22 * tileRatio;
projmat.M41 = -1.0f;
projmat.M42 = 1.0f;
d3dDevice.Transform.Projection = projmat;

d3dDevice.RenderState.CullMode = Cull.None;
d3dDevice.RenderState.Lighting = false;

// z buffer

d3dDevice.RenderState.ZBufferEnable = false;
d3dDevice.RenderState.ZBufferWriteEnable = false;

// alpha blending

d3dDevice.RenderState.AlphaTestEnable = true;
d3dDevice.RenderState.ReferenceAlpha = 0x01;
d3dDevice.RenderState.AlphaFunction = Compare.GreaterEqual;

d3dDevice.RenderState.AlphaBlendEnable = true;
d3dDevice.RenderState.AlphaSourceBlend = Blend.SourceAlpha;
d3dDevice.RenderState.AlphaDestinationBlend = Blend.InvSourceAlpha;

d3dDevice.TextureState[0].ColorOperation = TextureOperation.Modulate;
d3dDevice.TextureState[0].ColorArgument1 = TextureArgument.TextureColor;
d3dDevice.TextureState[0].ColorArgument2 = TextureArgument.Diffuse;

d3dDevice.TextureState[0].AlphaOperation = TextureOperation.Modulate;
d3dDevice.TextureState[0].AlphaArgument1 = TextureArgument.TextureColor;
d3dDevice.TextureState[0].AlphaArgument2 = TextureArgument.Diffuse;


LoadTexture();

// create vertex buffer

vb = new VertexBuffer( typeof(CustomVertex.PositionColoredTextured), 8, d3dDevice, Usage.WriteOnly,
CustomVertex.PositionColoredTextured.Format, Pool.Managed );

CustomVertex.PositionColoredTextured[] v = new CustomVertex.PositionColoredTextured [8];
v[0].SetPosition( new Vector3(-1.0f, 1.0f, 0.0f) );
v[1].SetPosition( new Vector3(-1.0f, -1.0f, 0.0f) );
v[2].SetPosition( new Vector3( 1.0f, 1.0f, 0.0f) );
v[3].SetPosition( new Vector3( 1.0f, -1.0f, 0.0f) );

v[4].SetPosition( new Vector3(-1.0f, 1.0f, 0.0f) );
v[5].SetPosition( new Vector3(-1.0f, -1.0f, 0.0f) );
v[6].SetPosition( new Vector3( 1.0f, 1.0f, 0.0f) );
v[7].SetPosition( new Vector3( 1.0f, -1.0f, 0.0f) );

v[0].Color = System.Drawing.Color.FromArgb(192, 255, 255, 255).ToArgb();
v[1].Color = System.Drawing.Color.FromArgb(192, 255, 255, 255).ToArgb();
v[2].Color = System.Drawing.Color.FromArgb(192, 255, 255, 255).ToArgb();
v[3].Color = System.Drawing.Color.FromArgb(192, 255, 255, 255).ToArgb();

v[4].Color = System.Drawing.Color.FromArgb(128, 0, 0, 255).ToArgb();
v[5].Color = System.Drawing.Color.FromArgb(128, 0, 0, 255).ToArgb();
v[6].Color = System.Drawing.Color.FromArgb(128, 0, 0, 255).ToArgb();
v[7].Color = System.Drawing.Color.FromArgb(128, 0, 0, 255).ToArgb();

v[0].Tu = 0.0f; v[0].Tv = 1.0f;
v[1].Tu = 0.0f; v[1].Tv = 0.0f;
v[2].Tu = 1.0f; v[2].Tv = 1.0f;
v[3].Tu = 1.0f; v[3].Tv = 0.0f;

v[4].Tu = 0.0f; v[4].Tv = 1.0f;
v[5].Tu = 0.0f; v[5].Tv = 0.0f;
v[6].Tu = 1.0f; v[6].Tv = 1.0f;
v[7].Tu = 1.0f; v[7].Tv = 0.0f;

vb.SetData( v, 0, 0 );

return true;
}
catch
{
return false;
}
}


private void BuildPresentParameters( )
{
// fills in PresentParameters

presentParams.Windowed = false;
presentParams.AutoDepthStencilFormat = DepthFormat.D16;
presentParams.EnableAutoDepthStencil = true;

presentParams.BackBufferCount = 1;
presentParams.BackBufferFormat = Format.A8R8G8B8;
presentParams.BackBufferWidth = 640;
presentParams.BackBufferHeight = 480;
presentParams.FullScreenRefreshRateInHz = 75;

presentParams.PresentationInterval = PresentInterval.Default;
presentParams.PresentFlag = PresentFlag.None;
presentParams.SwapEffect = SwapEffect.Discard;

presentParams.MultiSample = MultiSampleType.None;
presentParams.MultiSampleQuality = 0;

presentParams.DeviceWindow = this;
}

private void LoadTexture()
{
// quadTexture = TextureLoader.FromFile( d3dDevice, "hello.dds", D3DX.Default, D3DX.Default, D3DX.Default, 0,

// Format.A8R8G8B8, Pool.Managed, Filter.None, Filter.None, System.Drawing.Color.FromArgb(255,255,0,255).ToArgb() );


quadTexture = TextureLoader.FromFile( d3dDevice, "hello.dds", D3DX.Default, D3DX.Default, D3DX.Default, 0,
Format.Unknown, Pool.Managed, Filter.Triangle | Filter.Mirror, Filter.Triangle | Filter.Mirror, 0 );
}

private void Render()
{
if ( d3dDevice == null )
return;

// if this is not the active form, allow CPU to do other things

// this will prevent 99% CPU usage when the game is inactive

if ( deviceIsLost || (System.Windows.Forms.Form.ActiveForm != this) )
{
System.Threading.Thread.Sleep(100);
}

// when the device is lost, test if it is ready to be reset

if ( deviceIsLost )
{
try
{
d3dDevice.TestCooperativeLevel();
}
catch( DeviceLostException )
{
return;
}
catch( DeviceNotResetException )
{
try
{
// reset the device

BuildPresentParameters();
d3dDevice.Reset( presentParams );
DeviceResized( d3dDevice, new System.ComponentModel.CancelEventArgs() );

deviceIsLost = false;
}
catch
{
MessageBox.Show( this, "Error in resetting device" );
Application.Exit();
}
}
}

// device is not lost, window is ready for render

if ( !deviceIsLost )
{
d3dDevice.Clear( ClearFlags.Target | ClearFlags.ZBuffer, System.Drawing.Color.CornflowerBlue, 1.0f, 0 );

d3dDevice.BeginScene();

d3dDevice.SetStreamSource( 0, vb, 0 );
d3dDevice.VertexFormat = CustomVertex.PositionColoredTextured.Format;

d3dDevice.SetTexture( 0, quadTexture );
d3dDevice.Transform.World = Matrix.RotationZ( angle );
d3dDevice.Transform.World *= Matrix.Translation( 5.0f, 5.0f, 0.0f);
d3dDevice.DrawPrimitives( PrimitiveType.TriangleStrip, 0, 2 );

d3dDevice.SetTexture( 0, quadTexture );
//d3dDevice.Transform.World = Matrix.RotationZ( -angle );

d3dDevice.Transform.World = Matrix.Translation( 4.5f, 4.5f, 0.0f);
d3dDevice.DrawPrimitives( PrimitiveType.TriangleStrip, 4, 2 );

angle += .1f;
if ( angle >= Math.PI*2 ) angle = 0.0f;

d3dDevice.EndScene();

try
{
d3dDevice.Present();
}
catch( DeviceLostException )
{
deviceIsLost = true;
}
}
}


#region Event Handlers
protected void InvalidateObjects( object sender, System.EventArgs e )
{
}

protected void RestoreObjects( object sender, System.EventArgs e )
{
// set viewport

Viewport vp = new Viewport();
vp.X = 0;
vp.Y = 0;
vp.Width = 640;
vp.Height = 480;
vp.MinZ = 0.0f;
vp.MaxZ = 1.0f;

d3dDevice.Viewport = vp;

// set projection matrix

int tileRatioHeight = 15;
int tileRatioWidth = 20;
float tileRatio = ((float)tileRatioHeight) / tileRatioWidth;

Matrix projmat = Matrix.Identity;
projmat.M22 = -( 2.0f / (float) tileRatioHeight );
projmat.M11 = -projmat.M22 * tileRatio;
projmat.M41 = -1.0f;
projmat.M42 = 1.0f;
d3dDevice.Transform.Projection = projmat;

// z buffer

d3dDevice.RenderState.ZBufferEnable = false;
d3dDevice.RenderState.ZBufferWriteEnable = false;

d3dDevice.RenderState.CullMode = Cull.None;
d3dDevice.RenderState.Lighting = false;

// enable alpha blending

d3dDevice.RenderState.AlphaTestEnable = true;
d3dDevice.RenderState.ReferenceAlpha = 0x01;
d3dDevice.RenderState.AlphaFunction = Compare.GreaterEqual;

d3dDevice.RenderState.AlphaBlendEnable = true;
d3dDevice.RenderState.AlphaSourceBlend = Blend.InvSourceAlpha;
d3dDevice.RenderState.AlphaDestinationBlend = Blend.SourceAlpha;
d3dDevice.RenderState.BlendOperation = BlendOperation.Add;

d3dDevice.TextureState[0].ColorOperation = TextureOperation.Modulate;
d3dDevice.TextureState[0].ColorArgument1 = TextureArgument.TextureColor;
d3dDevice.TextureState[0].ColorArgument2 = TextureArgument.Diffuse;

d3dDevice.TextureState[0].AlphaOperation = TextureOperation.Modulate;
d3dDevice.TextureState[0].AlphaArgument1 = TextureArgument.TextureColor;
d3dDevice.TextureState[0].AlphaArgument2 = TextureArgument.Diffuse;
}

protected void DeleteObjects( object sender, System.EventArgs e )
{
quadTexture.Dispose();
vb.Dispose();
}

public void DeviceResized( object sender, System.ComponentModel.CancelEventArgs e )
{
if ( this.WindowState == System.Windows.Forms.FormWindowState.Minimized )
e.Cancel = true;

if ( deviceIsLost )
e.Cancel = true;
}
#endregion

// The main entry point for the application.

static void Main()
{
using ( frmMain mainForm = new frmMain() )
{
if ( !mainForm.InitializeGraphics() )
{
MessageBox.Show( "Could not initialize Direct3D. Application will now exit." );
return;
}

mainForm.Show( );
while( mainForm.Created )
{
mainForm.Render( );
Application.DoEvents();
}
}
}
}
}


edit: removed evil tabs.

[edited by - alnite on October 10, 2003 4:08:44 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It is not m_d3dDevice.RenderState[0].ALPHASourceBlend..... is

It is not m_d3dDevice.RenderState[0].SourceBlend.....

hehe, that''s all

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
err.. I mean:

is not m_d3dDevice.RenderState.ALPHASourceBlend.....
nor m_d3dDevice.RenderState.ALPHADestinationBlend.....

It is:
m_d3dDevice.RenderState.SOURCEBLEND..... AND
m_d3dDevice.RenderState.DESTINATIONBLEND.....


hehe, that''s all

Share this post


Link to post
Share on other sites
OMFG!!!!! AP!! You AR3 TEH Rox0rRRzzss!!!~!

Thanks a bunch!!



edit: add the icon. oh what the heck. icon can't be edited.

[edited by - alnite on October 13, 2003 9:13:58 PM]

Share this post


Link to post
Share on other sites
well i have to thank both of you cause in this thread i found solution to my alpha problem...

i had the same problem as alnite and managed to get the desired effect by the following lines of code:

this.device.RenderState.AlphaBlendEnable = true;
device.RenderState.SourceBlend = Blend.SourceAlpha;
device.RenderState.DestinationBlend = Blend.InvSourceAlpha;

device.TextureState[0].ColorOperation = TextureOperation.Modulate;
device.TextureState[0].ColorArgument1 = TextureArgument.TextureColor;
device.TextureState[0].ColorArgument2 = TextureArgument.Diffuse;
device.TextureState[0].AlphaOperation = TextureOperation.Modulate;
device.TextureState[0].AlphaArgument1 = TextureArgument.TextureColor;
device.TextureState[0].AlphaArgument2 = TextureArgument.Diffuse;

i enable/disable zbuffer in a higherlevel render routine (enabled for opaque stuff, disabled for blended stuff)

so i was able to acces the alpha channel of the dds file and make my texture look how it should

Share this post


Link to post
Share on other sites