Exemplo n.º 1
0
        void LoadTexture(KtxStructure texture)
        {
            BindTexture();
            SetDefault();
            //setting mip layer count
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureBaseLevel, 0);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMaxLevel, texture.header.numberOfMipmapLevels - 1);

            Width  = (int)texture.header.pixelWidth;
            Height = (int)texture.header.pixelHeight;

            if (texture.header.glDataType == 0)
            {
                for (int i = 0; i < texture.header.numberOfMipmapLevels; i++)
                {
                    byte[] mipLev = texture.textureData.textureDataOfMipmapLevel[i];
                    GL.CompressedTexImage2D(TextureTarget.Texture2D, i, (InternalFormat)texture.header.glInternalFormat,
                                            (int)texture.header.pixelWidth, (int)texture.header.pixelHeight, 0, mipLev.Length, mipLev);
                    //mip level > 0 gives memory error
                    break;
                }
            }
            else
            {
                for (int i = 0; i < texture.header.numberOfMipmapLevels; i++)
                {
                    byte[] mipLev = texture.textureData.textureDataOfMipmapLevel[i];
                    GL.TexImage2D(TextureTarget.Texture2D, i, (PixelInternalFormat)texture.header.glPixelFormat,
                                  (int)texture.header.pixelWidth, (int)texture.header.pixelHeight, 0,
                                  (PixelFormat)texture.header.glPixelFormat, (PixelType)texture.header.glDataType, mipLev);
                    //mip level > 0 gives memory error
                    break;
                }
            }
        }
Exemplo n.º 2
0
        internal Texture2D(string path)
        {
            textureType = TextureTarget.Texture2D;
            GenerateTextureID();

            Bitmap tex1;

            //check texture
            try
            {
                if (path.EndsWith("ktx", StringComparison.OrdinalIgnoreCase))
                {
                    byte[]       ktxBytes     = File.ReadAllBytes(path);
                    KtxStructure ktxStructure = null;
                    using (MemoryStream ms = new MemoryStream(ktxBytes))
                    {
                        ktxStructure = KtxLoader.LoadInput(ms);
                        LoadTexture(ktxStructure);
                    }
                    return;
                }

                ///cause .NET cant read tga natievly
                ///png , jpg, bmp is ok
                if (path.EndsWith("tga", StringComparison.OrdinalIgnoreCase))
                {
                    tex1 = Paloma.TargaImage.LoadTargaImage(path);
                }
                /// .spa| .sph textures is png bmp or jpg textures
                else if (path.EndsWith("spa", StringComparison.OrdinalIgnoreCase) ||
                         path.EndsWith("sph", StringComparison.OrdinalIgnoreCase))
                {
                    Stream strm = File.OpenRead(path);
                    tex1 = new Bitmap(strm);
                }
                //process alpha channel on bmp
                else if (path.EndsWith("bmp", StringComparison.OrdinalIgnoreCase))
                {
                    tex1 = CustomBMPLoader.Load(path);
                }
                else
                {
                    tex1 = new Bitmap(path);
                }
                LoadTexture(tex1);
                tex1.Dispose();
            }
            catch (Exception)
            {
                logger.Error("cant load texture " + path, "");
                Texture2D empty = LoadEmpty();
                textureID = empty.textureID;
                Name      = empty.Name;
            }
        }
Exemplo n.º 3
0
        public void ValidityWithValidSamplesTest()
        {
            // Arrange
            byte[] inputBytes1 = File.ReadAllBytes(CommonFiles.validSample1Filename);
            byte[] inputBytes2 = File.ReadAllBytes(CommonFiles.validSample2Filename);
            byte[] inputBytes3 = File.ReadAllBytes(CommonFiles.validSample3Filename);
            byte[] inputBytes4 = File.ReadAllBytes(CommonFiles.validSample4Filename);

            MemoryStream msWriter1 = new MemoryStream();
            MemoryStream msWriter2 = new MemoryStream();
            MemoryStream msWriter3 = new MemoryStream();
            MemoryStream msWriter4 = new MemoryStream();

            // Act
            KtxStructure ktxStructure1 = null;
            KtxStructure ktxStructure2 = null;
            KtxStructure ktxStructure3 = null;
            KtxStructure ktxStructure4 = null;

            using (MemoryStream msReader = new MemoryStream(inputBytes1))
            {
                ktxStructure1 = KtxLoader.LoadInput(msReader);
            }

            using (MemoryStream msReader = new MemoryStream(inputBytes2))
            {
                ktxStructure2 = KtxLoader.LoadInput(msReader);
            }

            using (MemoryStream msReader = new MemoryStream(inputBytes3))
            {
                ktxStructure3 = KtxLoader.LoadInput(msReader);
            }

            using (MemoryStream msReader = new MemoryStream(inputBytes4))
            {
                ktxStructure4 = KtxLoader.LoadInput(msReader);
            }

            KtxWriter.WriteTo(ktxStructure1, msWriter1);
            KtxWriter.WriteTo(ktxStructure2, msWriter2);
            KtxWriter.WriteTo(ktxStructure3, msWriter3);
            KtxWriter.WriteTo(ktxStructure4, msWriter4);

            // Assert
            CollectionAssert.AreEqual(inputBytes1, msWriter1.ToArray());
            CollectionAssert.AreEqual(inputBytes2, msWriter2.ToArray());
            CollectionAssert.AreEqual(inputBytes3, msWriter3.ToArray());
            CollectionAssert.AreEqual(inputBytes4, msWriter4.ToArray());
        }
Exemplo n.º 4
0
        public void NullOrInvalidInputsTest()
        {
            // Arrange
            KtxStructure structure = null;

            MemoryStream msWriter             = new MemoryStream();
            MemoryStream msWriterNonWriteable = new MemoryStream(new byte[] { 0 }, writable: false);

            // Act
            using (FileStream input = new FileStream(CommonFiles.validSample1Filename, FileMode.Open))
            {
                structure = KtxLoader.LoadInput(input);
            }

            // Assert
            Assert.Throws <NullReferenceException>(() => { KtxWriter.WriteTo(null, msWriter); });
            Assert.Throws <NullReferenceException>(() => { KtxWriter.WriteTo(structure, null); });
            Assert.Throws <ArgumentException>(() => { KtxWriter.WriteTo(structure, msWriterNonWriteable); });
        }
Exemplo n.º 5
0
        public void CheckHeadersWithValidSamplesTest()
        {
            // Arrange
            byte[] inputBytes1 = File.ReadAllBytes(validSample1Filename);
            byte[] inputBytes2 = File.ReadAllBytes(validSample2Filename);
            byte[] inputBytes3 = File.ReadAllBytes(validSample3Filename);
            byte[] inputBytes4 = File.ReadAllBytes(validSample4Filename);

            // Act
            KtxStructure ktxStructure1 = null;

            using (MemoryStream ms1 = new MemoryStream(inputBytes1))
            {
                ktxStructure1 = KtxLoader.LoadInput(ms1);
            }

            KtxStructure ktxStructure2 = null;

            using (MemoryStream ms2 = new MemoryStream(inputBytes2))
            {
                ktxStructure2 = KtxLoader.LoadInput(ms2);
            }

            KtxStructure ktxStructure3 = null;

            using (MemoryStream ms3 = new MemoryStream(inputBytes3))
            {
                ktxStructure3 = KtxLoader.LoadInput(ms3);
            }

            KtxStructure ktxStructure4 = null;

            using (MemoryStream ms4 = new MemoryStream(inputBytes4))
            {
                ktxStructure4 = KtxLoader.LoadInput(ms4);
            }

            // Assert
            CollectionAssert.AreNotEqual(inputBytes1, inputBytes2, "Input files should NOT have equal content");
            CollectionAssert.AreNotEqual(inputBytes1, inputBytes3, "Input files should NOT have equal content");
            CollectionAssert.AreNotEqual(inputBytes1, inputBytes4, "Input files should NOT have equal content");

            // Compressonator sample file resolution
            Assert.AreEqual(16, ktxStructure1.header.pixelWidth);
            Assert.AreEqual(16, ktxStructure1.header.pixelHeight);

            // PVRTexTool sample file resolution
            Assert.AreEqual(16, ktxStructure2.header.pixelWidth);
            Assert.AreEqual(16, ktxStructure2.header.pixelHeight);

            // ETCPACK sample file resolution
            Assert.AreEqual(2048, ktxStructure3.header.pixelWidth);
            Assert.AreEqual(32, ktxStructure3.header.pixelHeight);

            // ktx_specs.ktx resolution
            Assert.AreEqual(32, ktxStructure4.header.pixelWidth);
            Assert.AreEqual(32, ktxStructure4.header.pixelHeight);

            // ktx_specs.ktx Data type and internal format
            Assert.AreEqual(GlDataType.Compressed, ktxStructure4.header.glDataType);
            Assert.AreEqual((uint)GlDataType.Compressed, ktxStructure4.header.glTypeAsUint);
            Assert.AreEqual(GlInternalFormat.GL_ETC1_RGB8_OES, ktxStructure4.header.glInternalFormat);
            Assert.AreEqual((uint)GlInternalFormat.GL_ETC1_RGB8_OES, ktxStructure4.header.glInternalFormatAsUint);

            // ktx_specs.ktx Metadata
            Assert.AreEqual(1, ktxStructure4.header.metadataDictionary.Count);
            Assert.IsTrue(ktxStructure4.header.metadataDictionary.ContainsKey("api"));
            Assert.IsTrue(ktxStructure4.header.metadataDictionary["api"].isString);
            Assert.AreEqual("joke2", ktxStructure4.header.metadataDictionary["api"].stringValue);
        }