private static int getImageSize(RenderableTex t) { switch (t.pixelInternalFormat) { case PixelInternalFormat.CompressedRgbaS3tcDxt1Ext: case PixelInternalFormat.CompressedSrgbAlphaS3tcDxt1Ext: case PixelInternalFormat.CompressedRedRgtc1: case PixelInternalFormat.CompressedSignedRedRgtc1: return(t.width * t.height / 2); case PixelInternalFormat.CompressedRgbaS3tcDxt3Ext: case PixelInternalFormat.CompressedSrgbAlphaS3tcDxt3Ext: case PixelInternalFormat.CompressedRgbaS3tcDxt5Ext: case PixelInternalFormat.CompressedSrgbAlphaS3tcDxt5Ext: case PixelInternalFormat.CompressedSignedRgRgtc2: case PixelInternalFormat.CompressedRgRgtc2: case PixelInternalFormat.CompressedRgbaBptcUnorm: case PixelInternalFormat.CompressedSrgbAlphaBptcUnorm: return(t.width * t.height); case PixelInternalFormat.Rgba: return(t.ImageSize); default: return(t.ImageSize); } }
public static int GenerateOpenGLTexture(RenderableTex t, Bitmap bitmap, bool generateMips = false) { if (!t.GLInitialized) { return(-1); } int texID = GL.GenTexture(); GL.BindTexture(t.TextureTarget, texID); BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0); bitmap.UnlockBits(data); if (generateMips) { GL.GenerateMipmap(GenerateMipmapTarget.Texture2D); } return(texID); }
public void LoadCustomTexture(STGenericTexture GenericTexture) { CustomCubemap = GenericTexture.RenderableTex; if (CustomCubemap == null || !CustomCubemap.GLInitialized) { GenericTexture.LoadOpenGLTexture(); } }
public static unsafe Bitmap GLTextureToBitmap(RenderableTex t) { Bitmap bitmap = new Bitmap(t.width, t.height); System.Drawing.Imaging.BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, t.width, t.height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); GL.BindTexture(TextureTarget.Texture2D, t.TexID); GL.GetTexImage(TextureTarget.Texture2D, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bitmapData.Scan0); bitmap.UnlockBits(bitmapData); return(bitmap); }
public static RenderableTex FromBitmap(Bitmap bitmap) { RenderableTex tex = new RenderableTex(); tex.TextureTarget = TextureTarget.Texture2D; tex.TextureWrapS = TextureWrapMode.Repeat; tex.TextureWrapT = TextureWrapMode.Repeat; tex.TextureMinFilter = TextureMinFilter.Linear; tex.TextureMagFilter = TextureMagFilter.Linear; tex.width = bitmap.Width; tex.height = bitmap.Height; tex.pixelInternalFormat = PixelInternalFormat.Rgb8; tex.pixelFormat = OpenTK.Graphics.OpenGL.PixelFormat.Rgba; tex.GLInitialized = true; tex.TexID = GenerateOpenGLTexture(tex, bitmap); return(tex); }
public static int GenerateOpenGLTexture(RenderableTex t, List <STGenericTexture.Surface> ImageData) { if (!t.GLInitialized) { return(-1); } int texID = GL.GenTexture(); GL.BindTexture(t.TextureTarget, texID); if (t.IsCubeMap) { if (t.pixelInternalFormat != PixelInternalFormat.Rgba) { for (int mipLevel = 0; mipLevel < ImageData[0].mipmaps.Count; mipLevel++) { int width = Math.Max(1, t.width >> mipLevel); int height = Math.Max(1, t.height >> mipLevel); t.LoadCompressedMips(TextureTarget.TextureCubeMapPositiveX, ImageData[0], width, height, mipLevel); t.LoadCompressedMips(TextureTarget.TextureCubeMapNegativeX, ImageData[1], width, height, mipLevel); t.LoadCompressedMips(TextureTarget.TextureCubeMapPositiveY, ImageData[2], width, height, mipLevel); t.LoadCompressedMips(TextureTarget.TextureCubeMapNegativeY, ImageData[3], width, height, mipLevel); t.LoadCompressedMips(TextureTarget.TextureCubeMapPositiveZ, ImageData[4], width, height, mipLevel); t.LoadCompressedMips(TextureTarget.TextureCubeMapNegativeZ, ImageData[5], width, height, mipLevel); } } else { for (int mipLevel = 0; mipLevel < ImageData[0].mipmaps.Count; mipLevel++) { int width = Math.Max(1, t.width >> mipLevel); int height = Math.Max(1, t.height >> mipLevel); t.LoadUncompressedMips(TextureTarget.TextureCubeMapPositiveX, ImageData[0], width, height, mipLevel); t.LoadUncompressedMips(TextureTarget.TextureCubeMapNegativeX, ImageData[1], width, height, mipLevel); t.LoadUncompressedMips(TextureTarget.TextureCubeMapPositiveY, ImageData[2], width, height, mipLevel); t.LoadUncompressedMips(TextureTarget.TextureCubeMapNegativeY, ImageData[3], width, height, mipLevel); t.LoadUncompressedMips(TextureTarget.TextureCubeMapPositiveZ, ImageData[4], width, height, mipLevel); t.LoadUncompressedMips(TextureTarget.TextureCubeMapNegativeZ, ImageData[5], width, height, mipLevel); } } if (ImageData[0].mipmaps.Count == 1) { GL.GenerateMipmap(GenerateMipmapTarget.TextureCubeMap); } } else { if (t.pixelInternalFormat != PixelInternalFormat.Rgba) { for (int mipLevel = 0; mipLevel < ImageData[0].mipmaps.Count; mipLevel++) { t.LoadCompressedMips(t.TextureTarget, ImageData[0], t.width, t.height, mipLevel); } } else { for (int mipLevel = 0; mipLevel < ImageData[0].mipmaps.Count; mipLevel++) { t.LoadUncompressedMips(t.TextureTarget, ImageData[0], t.width, t.height, mipLevel); } } if (ImageData[0].mipmaps.Count == 1) { GL.GenerateMipmap(GenerateMipmapTarget.Texture2D); } } return(texID); }