/// <summary> /// Creates a texture on the GPU with the chosen parameters /// </summary> /// <param name="bitmapBlock"></param> /// <param name="textureMagFilter"></param> /// <param name="textureMinFilter"></param> public void Load(BitmapDataBlock bitmapBlock, TextureMagFilter textureMagFilter = TextureMagFilter.Linear, TextureMinFilter textureMinFilter = TextureMinFilter.Linear) { var buffer = bitmapBlock.GetResourceData( ); if (buffer == null) { return; } var width = bitmapBlock.Width; var height = bitmapBlock.Height; var bytesPerPixel = GetBitSize(bitmapBlock.Format) / 8.0f; if (bitmapBlock.BitmapDataFlags.HasFlag(BitmapDataBlock.Flags.Palettized)) { LoadPalettedTexture(bitmapBlock, P8ColourBytes, textureMagFilter, textureMinFilter); return; } if (bitmapBlock.BitmapDataFlags.HasFlag(BitmapDataBlock.Flags.Swizzled)) { buffer = Swizzler.Swizzle(buffer, ( int )bytesPerPixel, width, height); } var pixelInternalFormat = GetPixelInternalFormat(bitmapBlock.Format); switch (bitmapBlock.Type) { case BitmapDataBlock.TypeEnum.Texture2D: { LoadTexture2D(textureMagFilter, textureMinFilter, bytesPerPixel, width, height, buffer, bitmapBlock, pixelInternalFormat); } break; case BitmapDataBlock.TypeEnum.Cubemap: { LoadCubemap(textureMagFilter, textureMinFilter, bytesPerPixel, width, height, buffer, bitmapBlock, pixelInternalFormat); } break; case BitmapDataBlock.TypeEnum.Texture3D: { LoadTexture2D(textureMagFilter, textureMinFilter, bytesPerPixel, width, height, buffer, bitmapBlock, pixelInternalFormat); } break; default: { GL.DeleteTexture(_handle); } break; } }
private void LoadPalettedTexture(BitmapDataBlock bitmapBlock, byte[] paletteData, TextureMagFilter textureMagFilter, TextureMinFilter textureMinFilter) { if (bitmapBlock.Format != BitmapDataBlock.FormatEnum.P8 && bitmapBlock.Format != BitmapDataBlock.FormatEnum.P8bump) { return; } _textureTarget = TextureTarget.Texture2D; GL.BindTexture(TextureTarget.Texture2D, _handle); GL.TexParameter(_textureTarget, TextureParameterName.TextureMagFilter, ( int )textureMagFilter); GL.TexParameter(_textureTarget, TextureParameterName.TextureMinFilter, ( int )textureMinFilter); GL.TexParameter(_textureTarget, TextureParameterName.TextureWrapS, ( int )TextureWrapMode.Repeat); GL.TexParameter(_textureTarget, TextureParameterName.TextureWrapT, ( int )TextureWrapMode.Repeat); GL.TexStorage2D(( TextureTarget2d )_textureTarget, 1, SizedInternalFormat.Rgba8, bitmapBlock.Width, bitmapBlock.Height); var indexData = bitmapBlock.GetResourceData(0); var texelData = new byte[indexData.Length * 4]; for (var i = 0; i < indexData.Length; ++i) { var paletteIndex = indexData[i] * 4; var texelIndex = i * 4; texelData[texelIndex + 0] = paletteData[paletteIndex + 0]; texelData[texelIndex + 1] = paletteData[paletteIndex + 1]; texelData[texelIndex + 2] = paletteData[paletteIndex + 2]; texelData[texelIndex + 3] = paletteData[paletteIndex + 3]; } if (bitmapBlock.BitmapDataFlags.HasFlag(BitmapDataBlock.Flags.Swizzled)) { texelData = Swizzler.Swizzle(texelData, 4, bitmapBlock.Width, bitmapBlock.Height); } GL.TexSubImage2D(_textureTarget, 0, 0, 0, bitmapBlock.Width, bitmapBlock.Height, PixelFormat.Bgra, PixelType.UnsignedByte, texelData); GL.GenerateMipmap(GenerateMipmapTarget.Texture2D); GL.BindTexture(TextureTarget.Texture2D, 0); }