示例#1
0
        public void CreateAtlasTest(string directory, bool generateMipMaps, bool forceSquaredAtlas)
        {
            string path = TestTools.InputTestFolder + directory;
            string[] fileList = Directory.GetFiles(path);
            var list = new List<TexImage>(fileList.Length);

            foreach(string filePath in fileList)
            {
                var temp = Load(fiLib, filePath);
                list.Add(temp);
                if (generateMipMaps)
                {
                    fiLib.EndLibrary(temp);
                    dxtLib.StartLibrary(temp);
                    dxtLib.Execute(temp, new MipMapsGenerationRequest(Filter.MipMapGeneration.Cubic));
                    temp.CurrentLibrary = dxtLib;
                }
            }

            var atlas = new TexAtlas();

            library.Execute(atlas, new AtlasCreationRequest(list, forceSquaredAtlas));

            //Console.WriteLine("AtlasTexLibrary_CreateAtlas_" + generateMipMaps + "_" + forceSquaredAtlas + "." + TestTools.ComputeSHA1(atlas.Data, atlas.DataSize));
            Assert.IsTrue(TestTools.ComputeSHA1(atlas.Data, atlas.DataSize).Equals(TestTools.GetInstance().Checksum["AtlasTexLibrary_CreateAtlas_" + generateMipMaps + "_" + forceSquaredAtlas]));

            if(forceSquaredAtlas) Assert.IsTrue(atlas.Width == atlas.Height);

            atlas.Dispose();

            foreach (var image in list)
            {
                image.Dispose();
            }
        }
示例#2
0
 public void CanHandleRequestTest()
 {
     TexAtlas atlas = new TexAtlas(TexAtlas.TexLayout.Import(TestTools.InputTestFolder + Path.GetFileNameWithoutExtension("atlas_WMipMaps.dds") + TexAtlas.TexLayout.Extension), TestTools.Load(dxtLib, "atlas_WMipMaps.dds"));
     Assert.IsFalse(library.CanHandleRequest(atlas, new DecompressingRequest(false)));
     Assert.IsTrue(library.CanHandleRequest(atlas, new AtlasCreationRequest(new List<TexImage>())));
     Assert.IsTrue(library.CanHandleRequest(atlas, new AtlasExtractionRequest(0)));
     Assert.IsTrue(library.CanHandleRequest(atlas, new AtlasUpdateRequest(new TexImage(), "")));
     atlas.Dispose();
 }
示例#3
0
        /// <summary>
        /// Determines the positions of the textures in the Atlas.
        /// </summary>
        /// <param name="atlas">The atlas.</param>
        /// <param name="request">The request.</param>
        /// <returns>The binary tree containing the positioned textures or null if the atlas is too small.</returns>
        private Node PositionTextures(TexAtlas atlas, AtlasCreationRequest request)
        {
            Node root = new Node(0, 0, atlas.Width, atlas.Height);

            foreach (TexImage texture in request.TextureList)
            {
                if (!Insert(root, texture))
                {
                    return(null);
                }
            }

            return(root);
        }
示例#4
0
        public void UpdateTest(string atlasFile, string textureNameToUpdate, string newTexture)
        {
            TexAtlas atlas = new TexAtlas(TexAtlas.TexLayout.Import(Module.PathToInputImages + Path.GetFileNameWithoutExtension(atlasFile) + TexAtlas.TexLayout.Extension), TestTools.Load(fiLib, atlasFile));

            var updateTexture = TestTools.Load(fiLib, newTexture);

            library.Execute(atlas, new AtlasUpdateRequest(updateTexture, textureNameToUpdate));
            library.EndLibrary(atlas);

            //Console.WriteLine("AtlasTexLibrary_Update_" + textureNameToUpdate + "_" + atlasFile + "." + TestTools.ComputeSHA1(atlas.Data, atlas.DataSize));
            Assert.IsTrue(TestTools.ComputeSHA1(atlas.Data, atlas.DataSize).Equals(TestTools.GetInstance().Checksum["AtlasTexLibrary_Update_" + textureNameToUpdate + "_" + atlasFile]));

            updateTexture.Dispose();
            atlas.Dispose();
        }
示例#5
0
        /// <summary>
        /// Copies the textures data drom the binary tree into atlas memory at the right position.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <param name="atlas">The atlas.</param>
        private void CopyTexturesIntoAtlasMemory(Node node, TexAtlas atlas)
        {
            if (!node.IsEmpty() && node.IsLeaf())
            {
                long atlasData   = atlas.Data.ToInt64();
                long textureData = node.Texture.Data.ToInt64();
                //int xOffset = (int)((Decimal)node.X / atlas.Width * atlas.RowPitch);
                //int yOffset = node.Y * atlas.RowPitch;
                //IntPtr destPtr, srcPtr;

                int    x, y, xOffset, yOffset;
                IntPtr destPtr, srcPtr;

                x = node.X;
                y = node.Y;

                for (int i = 0; i < node.Texture.MipmapCount && i < atlas.MipmapCount; ++i)
                {
                    atlasData   = atlas.SubImageArray[i].Data.ToInt64();
                    textureData = node.Texture.SubImageArray[i].Data.ToInt64();
                    xOffset     = (int)((Decimal)x / (Decimal)atlas.SubImageArray[i].Width * atlas.SubImageArray[i].RowPitch);
                    yOffset     = y * atlas.SubImageArray[i].RowPitch;

                    /*if (node.Texture.SubImageArray[i].Width == 3)
                     * {
                     *  //xOffset += 4;
                     *  //node.Texture.SubImageArray[i].RowPitch += 4;
                     *  Console.WriteLine(node.Texture.SubImageArray[i].RowPitch); ///////////////----------------------------------------------------------------------------------------
                     * }*/
                    for (int j = 0; j < node.Texture.SubImageArray[i].Height; ++j)
                    {
                        destPtr = new IntPtr(atlasData + j * atlas.SubImageArray[i].RowPitch + yOffset + xOffset);
                        srcPtr  = new IntPtr(textureData + j * node.Texture.SubImageArray[i].RowPitch);
                        Utilities.CopyMemory(destPtr, srcPtr, node.Texture.SubImageArray[i].RowPitch);
                    }

                    x = x <= 1 ? 0 : x >>= 1;
                    y = y <= 1 ? 0 : y >>= 1;
                }

                node.Texture.Dispose();
            }
            else if (!node.IsLeaf())
            {
                CopyTexturesIntoAtlasMemory(node.Left, atlas);
                CopyTexturesIntoAtlasMemory(node.Right, atlas);
            }
        }
示例#6
0
 /// <summary>
 /// Updates the atlas data.
 /// </summary>
 /// <param name="node">The node.</param>
 /// <param name="atlas">The atlas.</param>
 private void UpdateAtlasData(Node node, TexAtlas atlas)
 {
     if (!node.IsEmpty() && node.IsLeaf())
     {
         if (atlas.Layout.TexList.ContainsKey(node.Texture.Name) || node.Texture.Name.Equals(""))
         {
             node.Texture.Name = node.Texture.Name + "_x" + node.X + "_y" + node.Y;
         }
         atlas.Layout.TexList.Add(node.Texture.Name, new TexAtlas.TexLayout.Position(node.X, node.Y, node.Width, node.Height));
     }
     else if (!node.IsLeaf())
     {
         UpdateAtlasData(node.Left, atlas);
         UpdateAtlasData(node.Right, atlas);
     }
 }
示例#7
0
        public bool CanHandleRequest(TexImage image, IRequest request)
        {
            if (image.GetType() != typeof(TexAtlas))
            {
                return(false);
            }

            TexAtlas atlas = (TexAtlas)image;

            switch (request.Type)
            {
            case RequestType.AtlasCreation:
            case RequestType.AtlasExtraction:
            case RequestType.AtlasUpdate:
                return(true);

            default:
                return(false);
            }
        }
示例#8
0
        public void ExtractTest(string atlasFile, string extractedName)
        {
            TexAtlas atlas = new TexAtlas(TexAtlas.TexLayout.Import(Module.PathToInputImages + Path.GetFileNameWithoutExtension(atlasFile) + TexAtlas.TexLayout.Extension), TestTools.Load(dxtLib, atlasFile));

            var request = new AtlasExtractionRequest(extractedName, 16);

            library.Execute(atlas, request);
            atlas.CurrentLibrary = library;

            var extracted = request.Texture;

            string nameWOExtension = Path.GetFileNameWithoutExtension(extractedName);

            //Console.WriteLine("AtlasTexLibrary_Extract_" + nameWOExtension + ".dds." + TestTools.ComputeSHA1(extracted.Data, extracted.DataSize));
            Assert.IsTrue(TestTools.ComputeSHA1(extracted.Data, extracted.DataSize).Equals(TestTools.GetInstance().Checksum["AtlasTexLibrary_Extract_" + nameWOExtension + ".dds"]));

            extracted.Dispose();

            atlas.Dispose();
        }
示例#9
0
        public void CreateAtlasTest(string directory, bool generateMipMaps, bool forceSquaredAtlas)
        {
            string path = Module.PathToInputImages + directory;

            string[] fileList = Directory.GetFiles(path);
            var      list     = new List <TexImage>(fileList.Length);

            foreach (string filePath in fileList)
            {
                var temp = Load(fiLib, filePath);
                list.Add(temp);
                if (generateMipMaps)
                {
                    fiLib.EndLibrary(temp);
                    dxtLib.StartLibrary(temp);
                    dxtLib.Execute(temp, new MipMapsGenerationRequest(Filter.MipMapGeneration.Cubic));
                    temp.CurrentLibrary = dxtLib;
                }
            }

            var atlas = new TexAtlas();

            library.Execute(atlas, new AtlasCreationRequest(list, forceSquaredAtlas));

            //Console.WriteLine("AtlasTexLibrary_CreateAtlas_" + generateMipMaps + "_" + forceSquaredAtlas + "." + TestTools.ComputeSHA1(atlas.Data, atlas.DataSize));
            Assert.IsTrue(TestTools.ComputeSHA1(atlas.Data, atlas.DataSize).Equals(TestTools.GetInstance().Checksum["AtlasTexLibrary_CreateAtlas_" + generateMipMaps + "_" + forceSquaredAtlas]));

            if (forceSquaredAtlas)
            {
                Assert.IsTrue(atlas.Width == atlas.Height);
            }

            atlas.Dispose();

            foreach (var image in list)
            {
                image.Dispose();
            }
        }
示例#10
0
        public void ExtractAllTest(string directory)
        {
            string path = Module.PathToInputImages + directory;

            string[] fileList = Directory.GetFiles(path);
            var      list     = new List <TexImage>(fileList.Length);

            foreach (string filePath in fileList)
            {
                var temp = Load(fiLib, filePath);
                list.Add(temp);
                //Console.WriteLine("ExtractAll_" + Path.GetFileName(filePath) + "." + TestTools.ComputeSHA1(temp.Data, temp.DataSize));
            }

            var atlas = new TexAtlas();

            library.Execute(atlas, new AtlasCreationRequest(list));

            var request = new AtlasExtractionRequest(0);

            library.Execute(atlas, request);
            library.EndLibrary(atlas);

            Assert.IsTrue(list.Count == request.Textures.Count);

            foreach (var image in request.Textures)
            {
                Assert.IsTrue(TestTools.ComputeSHA1(image.Data, image.DataSize).Equals(TestTools.GetInstance().Checksum["ExtractAll_" + image.Name]));
                image.Dispose();
            }

            atlas.Dispose();

            foreach (var image in list)
            {
                image.Dispose();
            }
        }
示例#11
0
        void RebuildImage()
        {
            if (mAtlas != null)
            {
                mAtlas.FreeAll();
            }

            mAtlas = new TexAtlas(mGD, (int)AtlasX.Value, (int)AtlasY.Value);

            bool bAllWorked = true;

            List <string> textures = mSK.GetTexture2DList();

            for (int i = 0; i < mGridData.Count; i++)
            {
                HeightMap.TexData gd = mGridData[i];
                if (textures.Contains(gd.TextureName))
                {
                    if (!mSK.AddTexToAtlas(mAtlas, gd.TextureName, mGD,
                                           out gd.mScaleU, out gd.mScaleV, out gd.mUOffs, out gd.mVOffs))
                    {
                        bAllWorked = false;
                        break;
                    }
                }
            }

            if (!bAllWorked)
            {
                return;
            }

            AtlasPic01.Image = mAtlas.GetAtlasImage(mGD.DC);

            mAtlas.Finish(mGD);
        }
示例#12
0
        internal void Texture(TexAtlas texAtlas, List <HeightMap.TexData> texInfo, float transHeight)
        {
            mSK.AddMap("TerrainAtlas", texAtlas.GetAtlasSRV());
            mTerMats.SetMaterialTexture("Terrain", "mTexture0", "TerrainAtlas");

            Vector4 [] scaleofs = new Vector4[16];
            float   [] scale    = new float[16];

            for (int i = 0; i < texInfo.Count; i++)
            {
                if (i > 15)
                {
                    break;
                }

                scaleofs[i] = new Vector4(
                    (float)texInfo[i].mScaleU,
                    (float)texInfo[i].mScaleV,
                    (float)texInfo[i].mUOffs,
                    (float)texInfo[i].mVOffs);

                //basically a divisor
                scale[i] = 1.0f / texInfo[i].ScaleFactor;
            }
            mTerMats.SetMaterialParameter("Terrain", "mAtlasUVData", scaleofs);
            mTerMats.SetMaterialParameter("Terrain", "mAtlasTexScale", scale);

            if (mTerrain != null)
            {
                mTerrain.SetTextureData(texInfo, transHeight);
            }

            Vector3 lightDir = Mathery.RandomDirection(mRand);

            mTerMats.SetMaterialParameter("Terrain", "mLightDirection", lightDir);
        }
示例#13
0
        public void ExtractAllTest(string directory)
        {
            string path = TestTools.InputTestFolder + directory;
            string[] fileList = Directory.GetFiles(path);
            var list = new List<TexImage>(fileList.Length);

            foreach (string filePath in fileList)
            {
                var temp = Load(fiLib, filePath);
                list.Add(temp);
                //Console.WriteLine("ExtractAll_" + Path.GetFileName(filePath) + "." + TestTools.ComputeSHA1(temp.Data, temp.DataSize));
            }

            var atlas = new TexAtlas();

            library.Execute(atlas, new AtlasCreationRequest(list));

            var request = new AtlasExtractionRequest(0);
            library.Execute(atlas, request);
            library.EndLibrary(atlas);

            Assert.IsTrue(list.Count == request.Textures.Count);

            foreach (var image in request.Textures)
            {
                Assert.IsTrue(TestTools.ComputeSHA1(image.Data, image.DataSize).Equals(TestTools.GetInstance().Checksum["ExtractAll_" + image.Name]));
                image.Dispose();
            }

            atlas.Dispose();

            foreach (var image in list)
            {
                image.Dispose();
            }
        }
        /// <summary>
        /// Determines the positions of the textures in the Atlas.
        /// </summary>
        /// <param name="atlas">The atlas.</param>
        /// <param name="request">The request.</param>
        /// <returns>The binary tree containing the positioned textures or null if the atlas is too small.</returns>
        private Node PositionTextures(TexAtlas atlas, AtlasCreationRequest request)
        {
            Node root = new Node(0, 0, atlas.Width, atlas.Height);

            foreach (TexImage texture in request.TextureList)
            {
                if (!Insert(root, texture))
                {
                    return null;
                }
            }

            return root;
        }
示例#15
0
 /// <summary>
 /// Creates the atlas data.
 /// </summary>
 /// <param name="node">The node.</param>
 /// <param name="atlas">The atlas.</param>
 /// <param name="textureCount">The texture count.</param>
 private void CreateAtlasData(Node node, TexAtlas atlas, int textureCount)
 {
     atlas.Layout.TexList.Clear();
     UpdateAtlasData(node, atlas);
 }
        /// <summary>
        /// Copies the textures data drom the binary tree into atlas memory at the right position.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <param name="atlas">The atlas.</param>
        private void CopyTexturesIntoAtlasMemory(Node node, TexAtlas atlas)
        {
            if (!node.IsEmpty() && node.IsLeaf())
            {
                long atlasData = atlas.Data.ToInt64();
                long textureData = node.Texture.Data.ToInt64();
                //int xOffset = (int)((Decimal)node.X / atlas.Width * atlas.RowPitch);
                //int yOffset = node.Y * atlas.RowPitch;
                //IntPtr destPtr, srcPtr;

                int x, y, xOffset, yOffset;
                IntPtr destPtr, srcPtr;

                x = node.X;
                y = node.Y;
                
                for (int i = 0; i < node.Texture.MipmapCount && i < atlas.MipmapCount; ++i)
                {
                    atlasData = atlas.SubImageArray[i].Data.ToInt64();
                    textureData = node.Texture.SubImageArray[i].Data.ToInt64();
                    xOffset = (int)((Decimal)x / (Decimal)atlas.SubImageArray[i].Width * atlas.SubImageArray[i].RowPitch);
                    yOffset = y * atlas.SubImageArray[i].RowPitch;

                    /*if (node.Texture.SubImageArray[i].Width == 3)
                    {
                        //xOffset += 4; 
                        //node.Texture.SubImageArray[i].RowPitch += 4;
                        Console.WriteLine(node.Texture.SubImageArray[i].RowPitch); ///////////////----------------------------------------------------------------------------------------
                    }*/
                    for (int j = 0; j < node.Texture.SubImageArray[i].Height; ++j)
                    {
                        destPtr = new IntPtr(atlasData + j * atlas.SubImageArray[i].RowPitch + yOffset + xOffset);
                        srcPtr = new IntPtr(textureData + j * node.Texture.SubImageArray[i].RowPitch);
                        Utilities.CopyMemory(destPtr, srcPtr, node.Texture.SubImageArray[i].RowPitch);
                    }

                    x = x <= 1 ? 0 : x >>= 1;
                    y = y <= 1 ? 0 : y >>= 1;
                }

                node.Texture.Dispose();
            }
            else if (!node.IsLeaf())
            {
                CopyTexturesIntoAtlasMemory(node.Left, atlas);
                CopyTexturesIntoAtlasMemory(node.Right, atlas);
            }
        }
示例#17
0
        /// <summary>
        /// Initalizes the atlas. Predictes the minimum size required by the atlas according to the textures
        /// </summary>
        /// <param name="atlas">The atlas.</param>
        /// <param name="request">The request.</param>
        private void InitalizeAtlas(TexAtlas atlas, AtlasCreationRequest request, int atlasSizeIncrement)
        {
            // Calculating the total number of pixels of every texture to be included
            int  pixelCount = 0;
            bool hasMipMap  = false;

            foreach (TexImage texture in request.TextureList)
            {
                pixelCount += texture.Width * texture.Height;
                if (texture.MipmapCount > 1)
                {
                    hasMipMap = true;
                }
            }

            // setting the new size to the atlas
            int alpha = (int)Math.Log(pixelCount, 2) + 1 + atlasSizeIncrement;

            atlas.Width  = (int)Math.Pow(2, alpha / 2);
            atlas.Height = (int)Math.Pow(2, alpha - alpha / 2);

            // If we want a square texture, we compute the max of the width and height and assign it to both
            if (request.ForceSquaredAtlas)
            {
                int size = Math.Max(atlas.Width, atlas.Height);
                atlas.Width  = size;
                atlas.Height = size;
            }

            // Setting the TexImage features
            int rowPitch, slicePitch;

            Tools.ComputePitch(atlas.Format, atlas.Width, atlas.Height, out rowPitch, out slicePitch);
            atlas.RowPitch   = rowPitch;
            atlas.SlicePitch = slicePitch;

            // Allocating memory
            if (hasMipMap)
            {
                int dataSize = 0;
                int mipmapCount = 0;
                int w, h;
                List <TexImage.SubImage> subImages = new List <TexImage.SubImage>();

                w = atlas.Width;
                h = atlas.Height;

                while (w != 1 || h != 1)
                {
                    Tools.ComputePitch(atlas.Format, w, h, out rowPitch, out slicePitch);
                    subImages.Add(new TexImage.SubImage()
                    {
                        Data       = IntPtr.Zero,
                        DataSize   = slicePitch,
                        Width      = w,
                        Height     = h,
                        RowPitch   = rowPitch,
                        SlicePitch = slicePitch,
                    });

                    dataSize += slicePitch;
                    ++mipmapCount;

                    w = w > 1 ? w >>= 1 : w;
                    h = h > 1 ? h >>= 1 : h;
                }

                atlas.DataSize = dataSize;
                atlas.Data     = Marshal.AllocHGlobal(atlas.DataSize);

                atlas.SubImageArray = subImages.ToArray();

                int offset = 0;
                for (int i = 0; i < atlas.SubImageArray.Length; ++i)
                {
                    atlas.SubImageArray[i].Data = new IntPtr(atlas.Data.ToInt64() + offset);
                    offset += atlas.SubImageArray[i].DataSize;
                }
                atlas.MipmapCount = mipmapCount;
            }
            else
            {
                atlas.DataSize = atlas.SlicePitch;
                atlas.Data     = Marshal.AllocHGlobal(atlas.DataSize);

                atlas.SubImageArray[0].Data       = atlas.Data;
                atlas.SubImageArray[0].DataSize   = atlas.DataSize;
                atlas.SubImageArray[0].Width      = atlas.Width;
                atlas.SubImageArray[0].Height     = atlas.Height;
                atlas.SubImageArray[0].RowPitch   = rowPitch;
                atlas.SubImageArray[0].SlicePitch = slicePitch;
            }

            atlas.DisposingLibrary = this;
        }
 /// <summary>
 /// Creates the atlas data.
 /// </summary>
 /// <param name="node">The node.</param>
 /// <param name="atlas">The atlas.</param>
 /// <param name="textureCount">The texture count.</param>
 private void CreateAtlasData(Node node, TexAtlas atlas, int textureCount)
 {
     atlas.Layout.TexList.Clear();
     UpdateAtlasData(node, atlas);
 }
        /// <summary>
        /// Initalizes the atlas. Predictes the minimum size required by the atlas according to the textures
        /// </summary>
        /// <param name="atlas">The atlas.</param>
        /// <param name="request">The request.</param>
        private void InitalizeAtlas(TexAtlas atlas, AtlasCreationRequest request, int atlasSizeIncrement)
        {
            // Calculating the total number of pixels of every texture to be included
            int pixelCount = 0;
            bool hasMipMap = false;
            foreach (TexImage texture in request.TextureList)
            {
                pixelCount += texture.Width * texture.Height;
                if (texture.MipmapCount > 1) hasMipMap = true;
            }

            // setting the new size to the atlas
            int alpha = (int)Math.Log(pixelCount, 2) + 1 + atlasSizeIncrement;
            atlas.Width = (int)Math.Pow(2, alpha / 2);
            atlas.Height = (int)Math.Pow(2, alpha - alpha / 2);

            // If we want a square texture, we compute the max of the width and height and assign it to both
            if (request.ForceSquaredAtlas)
            {
                int size = Math.Max(atlas.Width, atlas.Height);
                atlas.Width = size;
                atlas.Height = size;
            }

            // Setting the TexImage features
            int rowPitch, slicePitch;
            Tools.ComputePitch(atlas.Format, atlas.Width, atlas.Height, out rowPitch, out slicePitch);
            atlas.RowPitch = rowPitch;
            atlas.SlicePitch = slicePitch;

            // Allocating memory
            if (hasMipMap)
            {
                int dataSize = 0;
                int mipmapCount = 0;
                int w, h;
                List<TexImage.SubImage> subImages = new List<TexImage.SubImage>();

                w = atlas.Width;
                h = atlas.Height;  

                while (w != 1 || h != 1)
                {
                    Tools.ComputePitch(atlas.Format, w, h, out rowPitch, out slicePitch);
                    subImages.Add(new TexImage.SubImage()
                    {
                        Data = IntPtr.Zero,
                        DataSize = slicePitch,
                        Width = w,
                        Height = h,
                        RowPitch = rowPitch,
                        SlicePitch = slicePitch,
                    });

                    dataSize += slicePitch;
                    ++mipmapCount;

                    w = w > 1 ? w >>= 1 : w;
                    h = h > 1 ? h >>= 1 : h;
                }

                atlas.DataSize = dataSize;
                atlas.Data = Marshal.AllocHGlobal(atlas.DataSize);

                atlas.SubImageArray = subImages.ToArray();

                int offset = 0;
                for (int i = 0; i < atlas.SubImageArray.Length; ++i)
                {
                    atlas.SubImageArray[i].Data = new IntPtr(atlas.Data.ToInt64() + offset);
                    offset += atlas.SubImageArray[i].DataSize;
                }
                atlas.MipmapCount = mipmapCount;
            }
            else
            {
                atlas.DataSize = atlas.SlicePitch;
                atlas.Data = Marshal.AllocHGlobal(atlas.DataSize);

                atlas.SubImageArray[0].Data = atlas.Data;
                atlas.SubImageArray[0].DataSize = atlas.DataSize;
                atlas.SubImageArray[0].Width = atlas.Width;
                atlas.SubImageArray[0].Height = atlas.Height;
                atlas.SubImageArray[0].RowPitch = rowPitch;
                atlas.SubImageArray[0].SlicePitch = slicePitch;
            }

            atlas.DisposingLibrary = this;
        }
 /// <summary>
 /// Updates the atlas data.
 /// </summary>
 /// <param name="node">The node.</param>
 /// <param name="atlas">The atlas.</param>
 private void UpdateAtlasData(Node node, TexAtlas atlas)
 {
     if (!node.IsEmpty() && node.IsLeaf())
     {
         if (atlas.Layout.TexList.ContainsKey(node.Texture.Name) || node.Texture.Name.Equals("")) node.Texture.Name = node.Texture.Name + "_x" + node.X + "_y" + node.Y;
         atlas.Layout.TexList.Add(node.Texture.Name, new TexAtlas.TexLayout.Position(node.X, node.Y, node.Width, node.Height));
     }
     else if (!node.IsLeaf())
     {
         UpdateAtlasData(node.Left, atlas);
         UpdateAtlasData(node.Right, atlas);
     }
 }
        /// <summary>
        /// Extracts the specified TexImage from the atlas.
        /// </summary>
        /// <param name="atlas">The atlas.</param>
        /// <param name="request">The request.</param>
        private void Extract(TexAtlas atlas, AtlasExtractionRequest request)
        {
            if (request.Name != null)
            {
                Log.Info("Extracting " + request.Name + " from atlas ...");

                if (!atlas.Layout.TexList.ContainsKey(request.Name))
                {
                    Log.Error("The request texture name " + request.Name + " doesn't exist in this atlas.");
                    throw new TextureToolsException("The request texture name " + request.Name + " doesn't exist in this atlas.");
                }
                request.Texture.Name = request.Name;
                ExtractTexture(atlas, request.Texture, atlas.Layout.TexList[request.Name], request.MinimumMipMapSize);
            }
            else
            {
                Log.Info("Extracting textures from atlas ...");

                TexImage texture;
                foreach (KeyValuePair<string, TexAtlas.TexLayout.Position> entry in atlas.Layout.TexList)
                {
                    texture = new TexImage();
                    texture.Name = entry.Key;
                    request.Textures.Add(texture);
                    ExtractTexture(atlas, texture, entry.Value, request.MinimumMipMapSize);
                }
            }
        }
        /// <summary>
        /// Updates the specified atlas with a given TexImage.
        /// </summary>
        /// <param name="atlas">The atlas.</param>
        /// <param name="request">The request.</param>
        /// <exception cref="TexLibraryException">The request texture name  + request.Name +  doesn't exist in this atlas.</exception>
        private void Update(TexAtlas atlas, AtlasUpdateRequest request)
        {
            if (!atlas.Layout.TexList.ContainsKey(request.Name))
            {
                Log.Error("The given texture name " + request.Name + " doesn't exist in this atlas.");
                throw new TextureToolsException("The given texture name " + request.Name + " doesn't exist in this atlas.");
            }

            TexAtlas.TexLayout.Position position = atlas.Layout.TexList[request.Name];

            if (request.Texture.Width != position.Width || request.Texture.Height != position.Height)
            {
                Log.Error("The given texture must match the dimension of the one you want to update in the atlas.");
                throw new TextureToolsException("The given texture must match the dimension of the one you want to update in the atlas.");
            }

            int mipmapCount = 0;
            int w = position.Width;
            int h = position.Height;

            do
            {
                ++mipmapCount;
                w >>= 1;
                h >>= 1;
            }
            while (w >= 1 && h >= 1 && mipmapCount < atlas.MipmapCount);

            w = position.Width;
            h = position.Height;
            int x = position.UOffset;
            int y = position.VOffset;
            long subImageData, atlasData;
            int xOffset, yOffset;
            for (int i = 0; i < mipmapCount; ++i)
            {
                xOffset = (int)((Decimal)x / atlas.SubImageArray[i].Width * atlas.SubImageArray[i].RowPitch);
                yOffset = y * atlas.SubImageArray[i].RowPitch;
                subImageData = request.Texture.SubImageArray[i].Data.ToInt64();
                atlasData = atlas.SubImageArray[i].Data.ToInt64();

                for (int j = 0; j < h; ++j)
                {
                    Utilities.CopyMemory(new IntPtr(atlasData + j * atlas.SubImageArray[i].RowPitch + yOffset + xOffset), new IntPtr(subImageData + j * request.Texture.SubImageArray[i].RowPitch), request.Texture.SubImageArray[i].RowPitch);
                }

                w = w > 1 ? w >>= 1 : w;
                h = h > 1 ? h >>= 1 : h;
                x = x <= 1 ? 0 : x >>= 1;
                y = y <= 1 ? 0 : y >>= 1;
            }
        }
示例#23
0
        void LoadTerrainTextureStuff(string path)
        {
            string noExt = FileUtil.StripExtension(path);

            string fname = noExt + ".TerTexData";

            FileStream fs = new FileStream(fname, FileMode.Open, FileAccess.Read);

            if (fs == null)
            {
                return;
            }

            BinaryReader br = new BinaryReader(fs);

            if (br == null)
            {
                return;
            }


            UInt32 magic = br.ReadUInt32();

            if (magic != 0x7EC5DA7A)
            {
                br.Close();
                fs.Close();
                return;
            }

            //atlas size
            int atlasX = br.ReadInt32();
            int atlasY = br.ReadInt32();

            //transition height
            int transitionHeight = br.ReadInt32();

            TexAtlas ta = new TexAtlas(mGD, atlasX, atlasY);

            //load into a temp list first
            List <HeightMap.TexData> gridStuffs = new List <HeightMap.TexData>();

            int count = br.ReadInt32();

            for (int i = 0; i < count; i++)
            {
                HeightMap.TexData td = new HeightMap.TexData();

                td.BottomElevation = br.ReadSingle();
                td.TopElevation    = br.ReadSingle();
                td.Steep           = br.ReadBoolean();
                td.ScaleFactor     = br.ReadSingle();
                td.TextureName     = br.ReadString();
                td.mScaleU         = br.ReadDouble();
                td.mScaleV         = br.ReadDouble();
                td.mUOffs          = br.ReadDouble();
                td.mVOffs          = br.ReadDouble();

                gridStuffs.Add(td);
            }

            br.Close();
            fs.Close();

            List <string> textures = mSK.GetTexture2DList();

            for (int i = 0; i < gridStuffs.Count; i++)
            {
                HeightMap.TexData gd = gridStuffs[i];
                if (textures.Contains(gd.TextureName))
                {
                    if (!mSK.AddTexToAtlas(ta, gd.TextureName, mGD,
                                           out gd.mScaleU, out gd.mScaleV, out gd.mUOffs, out gd.mVOffs))
                    {
                        //todo something here
                    }
                }
            }

            ta.Finish(mGD);

            Texture(ta, gridStuffs, transitionHeight);
        }
示例#24
0
        public void UpdateTest(string atlasFile, string textureNameToUpdate, string newTexture)
        {
            TexAtlas atlas = new TexAtlas(TexAtlas.TexLayout.Import(TestTools.InputTestFolder + Path.GetFileNameWithoutExtension(atlasFile) + TexAtlas.TexLayout.Extension), TestTools.Load(fiLib, atlasFile));

            var updateTexture = TestTools.Load(fiLib, newTexture);

            library.Execute(atlas, new AtlasUpdateRequest(updateTexture, textureNameToUpdate));
            library.EndLibrary(atlas);

            //Console.WriteLine("AtlasTexLibrary_Update_" + textureNameToUpdate + "_" + atlasFile + "." + TestTools.ComputeSHA1(atlas.Data, atlas.DataSize));
            Assert.IsTrue(TestTools.ComputeSHA1(atlas.Data, atlas.DataSize).Equals(TestTools.GetInstance().Checksum["AtlasTexLibrary_Update_" + textureNameToUpdate + "_" + atlasFile]));

            updateTexture.Dispose();
            atlas.Dispose();
        }
示例#25
0
        /// <summary>
        /// Extracts the specified texture from the atlas.
        /// </summary>
        /// <param name="atlas">The atlas.</param>
        /// <param name="texture">The texture that will be filled.</param>
        /// <param name="position">The position of the texture in the atlas.</param>
        private void ExtractTexture(TexAtlas atlas, TexImage texture, TexAtlas.TexLayout.Position position, int minimumMipmapSize)
        {
            texture.Format = atlas.Format;

            int x, y, w, h, rowPitch, slicePitch, mipmapCount, dataSize, offset;

            dataSize    = 0;
            mipmapCount = 0;
            w           = position.Width;
            h           = position.Height;

            do
            {
                Tools.ComputePitch(texture.Format, w, h, out rowPitch, out slicePitch);
                dataSize += slicePitch;
                ++mipmapCount;

                w >>= 1;
                h >>= 1;
            }while (w >= minimumMipmapSize && h >= minimumMipmapSize && mipmapCount < atlas.MipmapCount);

            texture.MipmapCount   = mipmapCount;
            texture.SubImageArray = new TexImage.SubImage[mipmapCount];
            texture.Data          = Marshal.AllocHGlobal(dataSize);
            texture.DataSize      = dataSize;
            texture.Width         = position.Width;
            texture.Height        = position.Height;

            long   atlasData, textureData;
            int    xOffset, yOffset;
            IntPtr destPtr, srcPtr;

            w      = position.Width;
            h      = position.Height;
            x      = position.UOffset;
            y      = position.VOffset;
            offset = 0;
            for (int i = 0; i < mipmapCount; ++i)
            {
                Tools.ComputePitch(texture.Format, w, h, out rowPitch, out slicePitch);

                texture.SubImageArray[i]            = new TexImage.SubImage();
                texture.SubImageArray[i].Data       = new IntPtr(texture.Data.ToInt64() + offset);
                texture.SubImageArray[i].DataSize   = slicePitch;
                texture.SubImageArray[i].Width      = w;
                texture.SubImageArray[i].Height     = h;
                texture.SubImageArray[i].RowPitch   = rowPitch;
                texture.SubImageArray[i].SlicePitch = slicePitch;

                atlasData   = atlas.SubImageArray[i].Data.ToInt64();
                textureData = texture.SubImageArray[i].Data.ToInt64();
                xOffset     = (int)((Decimal)x / atlas.SubImageArray[i].Width * atlas.SubImageArray[i].RowPitch);
                yOffset     = y * atlas.SubImageArray[i].RowPitch;

                for (int j = 0; j < h; ++j)
                {
                    srcPtr  = new IntPtr(atlasData + j * atlas.SubImageArray[i].RowPitch + yOffset + xOffset);
                    destPtr = new IntPtr(textureData + j * rowPitch);
                    Utilities.CopyMemory(destPtr, srcPtr, rowPitch);
                }

                offset += slicePitch;

                w = w > 1 ? w >>= 1 : w;
                h = h > 1 ? h >>= 1 : h;
                x = x <= 1 ? 0 : x >>= 1;
                y = y <= 1 ? 0 : y >>= 1;
            }


            texture.RowPitch   = texture.SubImageArray[0].RowPitch;
            texture.SlicePitch = texture.SubImageArray[0].SlicePitch;

            texture.DisposingLibrary = this;
        }
        /// <summary>
        /// Extracts the specified texture from the atlas.
        /// </summary>
        /// <param name="atlas">The atlas.</param>
        /// <param name="texture">The texture that will be filled.</param>
        /// <param name="position">The position of the texture in the atlas.</param>
        private void ExtractTexture(TexAtlas atlas, TexImage texture, TexAtlas.TexLayout.Position position, int minimumMipmapSize)
        {
            texture.Format = atlas.Format;

            int x, y, w, h, rowPitch, slicePitch, mipmapCount, dataSize, offset;

            dataSize = 0;
            mipmapCount = 0;
            w = position.Width;
            h = position.Height;

            do
            {
                Tools.ComputePitch(texture.Format, w, h, out rowPitch, out slicePitch);
                dataSize += slicePitch;
                ++mipmapCount;

                w >>= 1;
                h >>= 1;
            }
            while (w >= minimumMipmapSize && h >= minimumMipmapSize && mipmapCount < atlas.MipmapCount);

            texture.MipmapCount = mipmapCount;
            texture.SubImageArray = new TexImage.SubImage[mipmapCount];
            texture.Data = Marshal.AllocHGlobal(dataSize);
            texture.DataSize = dataSize;
            texture.Width = position.Width;
            texture.Height = position.Height;

            long atlasData, textureData;
            int xOffset, yOffset;
            IntPtr destPtr, srcPtr;

            w = position.Width;
            h = position.Height;
            x = position.UOffset;
            y = position.VOffset;
            offset = 0;
            for (int i = 0; i < mipmapCount; ++i)
            {
                Tools.ComputePitch(texture.Format, w, h, out rowPitch, out slicePitch);

                texture.SubImageArray[i] = new TexImage.SubImage();
                texture.SubImageArray[i].Data = new IntPtr(texture.Data.ToInt64() + offset);
                texture.SubImageArray[i].DataSize = slicePitch;
                texture.SubImageArray[i].Width = w;
                texture.SubImageArray[i].Height = h;
                texture.SubImageArray[i].RowPitch = rowPitch;
                texture.SubImageArray[i].SlicePitch = slicePitch;

                atlasData = atlas.SubImageArray[i].Data.ToInt64();
                textureData = texture.SubImageArray[i].Data.ToInt64();
                xOffset = (int)((Decimal)x / atlas.SubImageArray[i].Width * atlas.SubImageArray[i].RowPitch);
                yOffset = y * atlas.SubImageArray[i].RowPitch;

                for (int j = 0; j < h; ++j)
                {
                    srcPtr = new IntPtr(atlasData + j * atlas.SubImageArray[i].RowPitch + yOffset + xOffset);
                    destPtr = new IntPtr(textureData + j * rowPitch);
                    Utilities.CopyMemory(destPtr, srcPtr, rowPitch);
                }

                offset += slicePitch;

                w = w > 1 ? w >>= 1 : w;
                h = h > 1 ? h >>= 1 : h;
                x = x <= 1 ? 0 : x >>= 1;
                y = y <= 1 ? 0 : y >>= 1;
            }


            texture.RowPitch = texture.SubImageArray[0].RowPitch;
            texture.SlicePitch = texture.SubImageArray[0].SlicePitch;

            texture.DisposingLibrary = this;
        }
        /// <summary>
        /// Creates an atlas.
        /// </summary>
        /// <param name="atlas">The atlas.</param>
        /// <param name="request">The request.</param>
        /// <param name="atlasSizeIncrement">An indice used to increment the atlas size</param>
        public void Create(TexAtlas atlas, AtlasCreationRequest request, int atlasSizeIncrement)
        {
            Log.Info("Creating atlas ...");

            // Initalizing Atlas : trying to determine the minimum atlas size and allocating the needed memory
            InitalizeAtlas(atlas, request, atlasSizeIncrement);

            // Ordering textures in decreasing order : best heuristic with this algorithm.
            if (atlasSizeIncrement == 0) OrderTexture(request);

            // Finding the best layout for the textures in the atlas 
            Node tree = PositionTextures(atlas, request);

            // One of many textures couldn't be positioned which means the atlas is too small
            if (tree == null)
            {
                Marshal.FreeHGlobal(atlas.Data);
                Create(atlas, request, atlasSizeIncrement + 1);
            }
            else
            {
                // Everything went well, we can copy the textures data into the atlas
                CopyTexturesIntoAtlasMemory(tree, atlas);

                // Creating the atlas data
                CreateAtlasData(tree, atlas, request.TextureList.Count);
            }
        }
示例#28
0
        public void ExtractTest(string atlasFile, string extractedName)
        {
            TexAtlas atlas = new TexAtlas(TexAtlas.TexLayout.Import(TestTools.InputTestFolder+Path.GetFileNameWithoutExtension(atlasFile) + TexAtlas.TexLayout.Extension), TestTools.Load(dxtLib, atlasFile));

            var request = new AtlasExtractionRequest(extractedName, 16);
            library.Execute(atlas, request);
            atlas.CurrentLibrary = library;

            var extracted = request.Texture;

            string nameWOExtension = Path.GetFileNameWithoutExtension(extractedName);

            //Console.WriteLine("AtlasTexLibrary_Extract_" + nameWOExtension + ".dds." + TestTools.ComputeSHA1(extracted.Data, extracted.DataSize));
            Assert.IsTrue(TestTools.ComputeSHA1(extracted.Data, extracted.DataSize).Equals(TestTools.GetInstance().Checksum["AtlasTexLibrary_Extract_" + nameWOExtension + ".dds"]));

            extracted.Dispose();

            atlas.Dispose();
        }
示例#29
0
 public void Draw(SpriteBatch spriteBatch)
 {
     TexAtlas.Draw(spriteBatch, Position);
 }