Ejemplo n.º 1
0
        public Tex2D(string path, bool alpha = false)
        {
            if (path == string.Empty || path == "" || path == null)
            {
                return;
            }
            Path = path;

            if (Lut.ContainsKey(path))
            {
                Tex2D ot = Lut[path];
                ID     = ot.ID;
                Width  = ot.Width;
                Height = ot.Height;
                Path   = ot.Path;
                Alpha  = ot.Alpha;
                Name   = ot.Name;
                return;
            }
            else
            {
                Lut.Add(path, this);
            }

            Console.WriteLine("Reading:" + path);

            GL.Enable(EnableCap.Texture2D);
            ID = GL.GenTexture();

            GL.BindTexture(TextureTarget.Texture2D, ID);

            if (new FileInfo(path + ".cache").Exists)
            {
                FileStream   fs = new FileStream(path + ".cache", FileMode.Open, FileAccess.Read);
                BinaryReader r  = new BinaryReader(fs);
                Name   = r.ReadString();
                Path   = r.ReadString();
                Width  = r.ReadInt16();
                Height = r.ReadInt16();
                Alpha  = r.ReadBoolean();
                int pc = 3;
                if (Alpha)
                {
                    pc = 4;
                }

                RawData = new byte[Width * Height * pc];

                r.Read(RawData, 0, Width * Height * pc);

                fs.Close();
                fs = null;
            }
            else
            {
                Bitmap img = new Bitmap(path);
                //System.Drawing.Imaging.BitmapData dat = img.LockBits( new Rectangle(0, 0, img.Width, img.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.);

                Width  = img.Width;
                Height = img.Height;
                Alpha  = alpha;

                int pc = 3;
                if (Alpha)
                {
                    pc = 4;
                }

                RawData = new byte[Width * Height * pc];

                //GL.TexImage2D(TextureTarget.Texture2D,0,PixelInternalFormat.)

                int pi = 0;
                for (int y = 0; y < img.Height; y++)
                {
                    for (int x = 0; x < img.Width; x++)
                    {
                        Color pix = img.GetPixel(x, y);
                        RawData[pi++] = pix.R;
                        RawData[pi++] = pix.G;
                        RawData[pi++] = pix.B;
                        if (alpha)
                        {
                            RawData[pi++] = pix.A;
                        }
                    }
                }

                FileStream   fs = new FileStream(path + ".cache", FileMode.Create, FileAccess.Write);
                BinaryWriter w  = new BinaryWriter(fs);
                if (Name == null || Name == string.Empty)
                {
                    Name = "Tex2D";
                }
                w.Write(Name);
                w.Write(Path);
                w.Write((short)Width);
                w.Write((short)Height);
                w.Write(Alpha);
                pc = 3;
                if (alpha)
                {
                    pc = 4;
                }

                w.Write(RawData, 0, Width * Height * pc);
                fs.Flush();
                fs.Close();
                fs = null;
            }

            if (alpha)
            {
                GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, Width, Height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, RawData);
            }
            else
            {
                GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, Width, Height, 0, PixelFormat.Rgb, PixelType.UnsignedByte, RawData);
            }

            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);

            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);

            GL.BindTexture(TextureTarget.Texture2D, 0);
        }