public override void WriteToStream(BinaryWriter writer, GameAsset asset) { Texture texture = (Texture)asset; int sizeInBytes = TextureHelper.GetSinglePixelSize(texture.PixelInternalFormat, texture.PixelType) * texture.Size.X * texture.Size.Y; GL.BindTexture(TextureTarget.Texture2D, texture.Handle); byte[] textureData = new byte[sizeInBytes]; GL.GetTexImage(TextureTarget.Texture2D, 0, texture.PixelFormat, texture.PixelType, textureData); GL.BindTexture(TextureTarget.Texture2D, 0); //write to stream writer.Write(texture.Name); writer.Write((int)texture.MinFilter); writer.Write((int)texture.MagFilter); writer.Write((int)texture.WrapModeS); writer.Write((int)texture.WrapModeT); writer.Write((int)texture.PixelInternalFormat); writer.Write((int)texture.PixelFormat); writer.Write((int)texture.PixelType); writer.Write(texture.Size.X); writer.Write(texture.Size.Y); writer.Write(texture.IsResident); //buffer writer.Write(sizeInBytes); writer.Write(textureData); }
public static Texture FromPointer(string name, int width, int height, IntPtr data, PixelInternalFormat internalFormat = PixelInternalFormat.Rgba, PixelFormat format = PixelFormat.Rgba, PixelType pixelType = PixelType.UnsignedByte, string id = null) { Texture texture = new Texture(name, id); //copy pixel array int singlePixelSize = TextureHelper.GetSinglePixelSize(internalFormat, pixelType); texture.rawData = new byte[width * height * singlePixelSize]; Marshal.Copy(data, texture.rawData, 0, width * height * singlePixelSize); texture.pixelInternalFormat = internalFormat; texture.pixelFormat = format; texture.pixelType = pixelType; texture.handle = GL.GenTexture(); GL.BindTexture(TextureTarget.Texture2D, texture.handle); GL.TexImage2D(TextureTarget.Texture2D, 0, internalFormat, width, height, 0, format, pixelType, data); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge); GL.ObjectLabel(ObjectLabelIdentifier.Texture, texture.handle, name.Length, name); GL.BindTexture(TextureTarget.Texture2D, 0); texture.Size = new Vector2i(width, height); return(texture); }
public static Texture Deserialize(BinaryReader reader) { //read name string name = reader.ReadString(); //read id string id = reader.ReadString(); //read texture info TextureMinFilter minFilter = (TextureMinFilter)reader.ReadInt32(); TextureMagFilter magFilter = (TextureMagFilter)reader.ReadInt32(); TextureWrapMode wrapS = (TextureWrapMode)reader.ReadInt32(); TextureWrapMode wrapT = (TextureWrapMode)reader.ReadInt32(); //read pixel info PixelInternalFormat pixelInternalFormat = (PixelInternalFormat)reader.ReadInt32(); PixelFormat pixelFormat = (PixelFormat)reader.ReadInt32(); PixelType pixelType = (PixelType)reader.ReadInt32(); //read width and height Vector2i size = new Vector2i(reader.ReadInt32(), reader.ReadInt32()); //read pixels byte[] data = reader.ReadBytes(size.X * size.Y * TextureHelper.GetSinglePixelSize(pixelInternalFormat, pixelType)); Texture tex = FromArray(name, size.X, size.Y, data, pixelInternalFormat, pixelFormat, pixelType, id: id); tex.SetMinFilter(minFilter); tex.SetMagFilter(magFilter); tex.SetWrapS(wrapS); tex.SetWrapT(wrapT); return(tex); }