Click here to Skip to main content
15,896,063 members
Articles / Desktop Programming / WPF

Blend the OGRE Graphics Engine into your WPF projects

Rate me:
Please Sign up or sign in to vote.
4.85/5 (30 votes)
9 Sep 2008CPOL8 min read 185.7K   19.6K   68  
Blend a First Class Gaming Graphics Engine into your WPF application
using System;
using Mogre;

namespace OgreLib
{
    public static class TextureHelper
    {
        unsafe public static void CalculateAplha(Texture texture)
        {
            if (texture == null) return;

            if (texture.Format == PixelFormat.PF_X8R8G8B8) return;
            if (texture.Format != PixelFormat.PF_A8R8G8B8) throw new ArgumentException("Currently only ARGB textures allowed.");

            HardwareBuffer buffer = texture.GetBuffer();
            var data = (uint*)buffer.Lock(HardwareBuffer.LockOptions.HBL_DISCARD);
            try
            {
                var size = sizeof(uint);
                var width = texture.Width;
                var height = texture.Height;
                var pitch = (width / size) * size + ((width % size) > 0 ? size : 0);

                for (var y = 0; y < height; ++y)
                {
                    for (var x = 0; x < width; ++x)
                    {
                        var pixel = data[pitch * y + x];
                        var r = (byte)((pixel & 0x00FF0000) >> 16);
                        var g = (byte)((pixel & 0x0000FF00) >> 8);
                        var b = (byte)(pixel & 0x000000FF);
                        var a = (r + g + b) / 3;

                        pixel = (uint)((a << 24) + (r << 16) + (g << 8) + b);
                        data[pitch * y + x] = pixel;
                    }
                }
            }
            finally
            {
                buffer.Unlock();
            }
        }

        public static void CalculateAlphas(TexturePtr texturePtr)
        {
            if (texturePtr == null) return;

            CalculateAplha(texturePtr.Target);
        }

        public static void CalculateAllAlphas(MaterialPtr material)
        {
            if (material == null) return;

            foreach (var technique in material.GetTechniqueIterator())
            {
                foreach (var pass in technique.GetPassIterator())
                {
                    foreach (var textureUnit in pass.GetTextureUnitStateIterator())
                    {
                        if (string.IsNullOrEmpty(textureUnit.TextureName)) return;

                        if (TextureManager.Singleton.GetByName(textureUnit.TextureName) == null)
                        {
                            var texture = TextureManager.Singleton.Load(textureUnit.TextureName, material.Group,
                                                                        TextureType.TEX_TYPE_2D,
                                                                        textureUnit.NumMipmaps, 1, false,
                                                                        PixelFormat.PF_A8R8G8B8);
                            CalculateAplha(texture);
                        }
                    }
                }
            }
        }
        public static void CalculateAllAlphas(ParticleSystem particalSystem)
        {
            CalculateAllAlphas(MaterialManager.Singleton.GetByName(particalSystem.MaterialName));
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead FNB Connect
South Africa South Africa
iOS Technical Lead at FNB
-

Computers are really sweet. Aren't they?
Yup they are...

I've always loved writing tools and components...never been very interested in playing games....always wanted to be able to write them though.

And, yes. I'm still pretty annoyed they discontinued the Amiga computer.

Comments and Discussions