Hi all,
I have a query about blending textures together. I have loaded a grayscaled background image:
private void RenderBackgroundLayer(Device device)
{
// Not very efficient to create this here,
// but do so anyway if necessary
if (backgroundTexture == null)
backgroundTexture = TextureLoader.FromFile(
device, ".\\staticbackdrop.png", 1280, 1024, 1,
Usage.None, Format.X8R8G8B8,
Pool.Default, Filter.None,
Filter.None, Color.FromArgb(255, 0, 0, 0).ToArgb());
if (backgroundSprite == null)
backgroundSprite = new Sprite(device);
backgroundSprite.Begin(SpriteFlags.None);
// Blend colours towards green.
System.Drawing.Color blendColour = System.Drawing.Color.FromArgb(0xFF, 0x00, 0xFF, 0x00);
backgroundSprite.Draw(backgroundTexture, new Rectangle(0, 0, 1280, 1024), new Vector3(), new Vector3(), blendColour.ToArgb());
backgroundSprite.End();
}
Additionally I need to blend over that some additional data from a dynamic texture ( a cut down version is shown below ):
private unsafe void RenderPlotLayer_Dynamic(Device device, bool useNewData)
{
if (showPlotLayer)
{
if (plotTexture == null)
{
plotTexture = new Texture(device, 1000, 1000, 1,
Usage.Dynamic, Format.X8R8G8B8, Pool.Default);
// Set the texture to transparent black
InitialiseTexture(plotTexture, 1000, 1000, 0xFF000000);
}
if (plotSprite == null)
plotSprite = new Sprite(device);
if (useNewData)
{
// Determine which page should be used.
GraphicsStream gs = plotTexture.LockRectangle(0, LockFlags.None);
uint* pdataStart = (uint*)gs.InternalData.ToPointer();
SurfaceDescription surfaceDescription = plotTexture.GetLevelDescription(0);
uint currentLineForUpdate = ((1000 - (uint)scrollOffset) * 1024);
uint* pdata = pdataStart + (uint)currentLineForUpdate;
// Set some data in the current line
dataPlotCollection.WriteDataSetToMemory(pdata, 1024, 1000, 24, scrollOffset, 0x77);
plotTexture.UnlockRectangle(0);
if (scrollOffset == 1000)
SwapBuffers();
}
System.Drawing.Color blendColour = System.Drawing.Color.FromArgb(0x77, 0x00, 0xFF, 0x00);
plotSprite.Begin(SpriteFlags.AlphaBlend | SpriteFlags.SortTexture);
plotSprite.Draw(plotTexture, new Rectangle(0,0,1000,1000), new Vector3(), new Vector3(12, 12, 0), blendColour.ToArgb());
plotSprite.End();
}
}
OK, onto my question. In the second function, my blend-colour has an Alpha of 0x77. This results in my background image being shown at half-brightness. If I change this to 0x00 I get no transparancy and to 0xFF I get nothing drawn. How do I blend these together?
I'm using Managed DirectX in C# with the DX9 Framework (June 2005), but I'm more than comfortable in C++ if that's any help to anyone thinking of responding.
I have discovered the following two lines in the device initialisation and wonder if some other Blend setting may be necessary for me.
device.RenderState.SourceBlend = Blend.SourceAlpha;
device.RenderState.DestinationBlend = Blend.InvSourceAlpha;
I'd like to avoid getting the two textures and creating a third one manually, so if anyone can help, thanks!
Regards,
Ray
[Edited by - rayhayes on July 4, 2005 10:25:28 AM]