Transparency in textures in DX9/C#

Started by
3 comments, last by Nypyren 21 years, 4 months ago
I have an A8B8G8R8 texture with some transparent sections... the transparent area shows up as black against the "test" blue background. I didn''t think that I had to turn lighting on, but maybe I do... in which case I need to define a custom vertex format (instead of PositionColoredTextured
  
// loading the image

ImageInformation imageinfo = new ImageInformation();
texture = TextureLoader.FromFile(dev, @"F:\texture.png", 100, 100, 1, Usage.AutoGenerateMipMap, Format.Unknown, Pool.Managed, Filter.Linear, Filter.Linear, 0x00FF00FF, ref imageinfo);
MessageBox.Show(imageinfo.Format.ToString());
// that line says "A8B8G8R8" which is correct AFAIK


// in CreateDevice()

device.RenderState.Lighting = false;
device.RenderState.AlphaBlendEnable = true;
device.RenderState.AlphaSourceBlend = Blend.SourceAlpha;
device.RenderState.AlphaDestinationBlend = Blend.InvDestinationAlpha;


// last, in Render()

device.TextureState[0].AlphaOperation = TextureOperation.Modulate;
device.TextureState[0].AlphaArgument1 = TextureArgument.TextureColor;
device.TextureState[0].AlphaArgument2 = TextureArgument.Diffuse;
  
Advertisement
Nevermind, I figured it out...

The freakin thing uses SourceBlend and DestinationBlend, NOT AlphaSourceBlend or AlphaDestinationBlend. That''s extremely lame.

In fact, I can''t even figure out what Alpha*Blend does...
Can you please post the corrected code?

I still can''t figure it out...

Regards

Johan
Here is what I have now:


  // loading the texture (the alpha channel is saved in my file instead of using a certain color)Bitmap bitmap = (Bitmap)Image.FromFile("texture.png");texture = Texture.FromBitmap(dev, bitmap, Usage.AutoGenerateMipMap, Pool.Managed); // I didn''t know what else to put for usage... there wasn''t a "none" option// in a different function, creating my vertex bufferVertexBuffer vb = (VertexBuffer)sender;GraphicsStream stm = vb.Lock(0, 0, 0);CustomVertex.PositionColoredTextured[] verts = new CustomVertex.PositionColoredTextured[4];// location, color, texture coordinate (coordinates are from 0 (left or top) to 1 (right or bottom))verts[0].X=-10; verts[0].Y=-10; verts[0].Z=2.0f; verts[0].Color = Color.White.ToArgb(); verts[0].Tu = 0.0f; verts[0].Tv = 0.0f;verts[1].X= 10; verts[1].Y=-10; verts[1].Z=2.0f; verts[1].Color = Color.White.ToArgb(); verts[1].Tu = 1.0f; verts[1].Tv = 0.0f;verts[2].X= 10; verts[2].Y= 10; verts[2].Z=2.0f; verts[2].Color = Color.White.ToArgb(); verts[2].Tu = 1.0f; verts[2].Tv = 1.0f;verts[3].X=-10; verts[3].Y= 10; verts[3].Z=2.0f; verts[3].Color = Color.White.ToArgb(); verts[3].Tu = 0.0f; verts[3].Tv = 1.0f;stm.Write(verts);vb.Unlock();// setting up the renderstates and "camera" stuff (after device is created or just after a reset)device.SetTransform(TransformType.Projection, Matrix.OrthoRH(80,60,1.0f,100.0f));device.SetTransform(TransformType.View, Matrix.LookAtRH(new Vector3(0,0,0), new Vector3(0,0,1), new Vector3(0,-1,0)));				device.RenderState.Lighting = false;device.RenderState.AlphaBlendEnable = true;device.RenderState.SourceBlend = Blend.SourceAlpha;device.RenderState.DestinationBlend = Blend.InvSourceAlpha;// and finally, inside the begin/end blockdevice.SetTransform(TransformType.World, Matrix.RotationZ(angle+=0.01f));device.SetStreamSource(0, vertexBuffer, 0);device.SetTexture(0, texture);#region TextureStatesdevice.TextureState[0].AlphaOperation=TextureOperation.Modulate;device.TextureState[0].AlphaArgument1=TextureArgument.TextureColor;device.TextureState[0].AlphaArgument2=TextureArgument.Diffuse;device.TextureState[0].ColorOperation=TextureOperation.Modulate;device.TextureState[0].ColorArgument1=TextureArgument.TextureColor;device.TextureState[0].ColorArgument2=TextureArgument.Diffuse;#endregiondevice.VertexFormat = CustomVertex.PositionColoredTextured.Format;device.DrawPrimitives(PrimitiveType.TriangleFan, 0, 2);  


The Texture states in my code are set up to use the velue from each vertex multiplied (Modulate) by the pixel color (so if I wanted to slowly fade out the tail-end my space-ship graphic, I could turn down the alpha color of the two tail-end vertices).

Remember though, my image has the alpha channel saved in the file. Some people use bitmaps with a solid color (like magenta) to tell transparency... you will need to use a different loading function to load it using that method.
>>// I didn''t know what else to put for usage... there wasn''t a "none" option


Will it accept NULL there???

This topic is closed to new replies.

Advertisement