public static int LoadTextureForModelGLB(byte[] rawTextureData) { int texID; if (rawTextureData[0] == 0x44 && rawTextureData[1] == 0x44 && rawTextureData[2] == 0x53) { HelperDDS2.TryLoadDDS(rawTextureData, false, out texID, out int width, out int height); return(texID); } try { using (MemoryStream ms = new MemoryStream(rawTextureData)) { Bitmap image = new Bitmap(ms); if (image == null) { HelperGeneral.ShowErrorAndQuit("HelperTexture::LoadTextureForModelGLB()", "Could not load image file from GLB!"); return(-1); } texID = GL.GenTexture(); GL.BindTexture(TextureTarget.Texture2D, texID); BitmapData data = null; if (image.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb) { data = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0, OpenTK.Graphics.OpenGL4.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0); } else { data = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, data.Width, data.Height, 0, OpenTK.Graphics.OpenGL4.PixelFormat.Bgr, PixelType.UnsignedByte, data.Scan0); } GL.TexParameter(TextureTarget.Texture2D, (TextureParameterName)OpenTK.Graphics.OpenGL.ExtTextureFilterAnisotropic.TextureMaxAnisotropyExt, GLWindow.CurrentWindow.AnisotropicFiltering); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat); GL.GenerateMipmap(GenerateMipmapTarget.Texture2D); int mipMapCount = GetMaxMipMapLevels(data.Width, data.Height); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureBaseLevel, 0); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMaxLevel, Math.Max(0, mipMapCount - 2)); image.UnlockBits(data); image.Dispose(); GL.BindTexture(TextureTarget.Texture2D, 0); } } catch (Exception ex) { HelperGeneral.ShowErrorAndQuit("HelperTexture::LoadTextureForModelGLB()", "Could not load image file! Make sure to copy it to the correct output directory. " + "[" + ex.Message + "]"); return(-1); } return(texID); }
public static int LoadTextureForBackgroundExternal(string filename) { if (!File.Exists(filename)) { Debug.WriteLine("File " + filename + " for setting background image not found."); return(-1); } int texID; if (filename.ToLower().EndsWith(".dds")) { HelperDDS2.TryLoadDDS(filename, false, out texID, out int width, out int height, true); return(texID); } try { Bitmap image = new Bitmap(filename); if (image == null) { throw new Exception("File " + filename + " is not a valid image file."); } texID = GL.GenTexture(); GL.BindTexture(TextureTarget.Texture2D, texID); BitmapData data = null; if (image.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb) { data = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0, OpenTK.Graphics.OpenGL4.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0); } else { data = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, data.Width, data.Height, 0, OpenTK.Graphics.OpenGL4.PixelFormat.Bgr, PixelType.UnsignedByte, data.Scan0); } GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat); GL.GenerateMipmap(GenerateMipmapTarget.Texture2D); int mipMapCount = GetMaxMipMapLevels(data.Width, data.Height); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureBaseLevel, 0); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMaxLevel, Math.Max(0, mipMapCount - 2)); image.UnlockBits(data); image.Dispose(); GL.BindTexture(TextureTarget.Texture2D, 0); } catch (Exception ex) { throw new Exception("Could not load image file " + filename + "! Make sure to copy it to the correct output directory. " + "[" + ex.Message + "]"); } return(texID); }
public static int LoadTextureCompressedWithMipMaps(Stream stream) { bool success = HelperDDS2.TryLoadDDS(stream, false, out int texID, out int width, out int height); if (!success) { HelperGeneral.ShowErrorAndQuit("HelperTexture::LoadTextureCompressedWithMipMaps()", "Unsupported compressed texture format: only DXT1, DXT3 and DXT5 are supported."); texID = -1; throw new Exception("Unsupported compressed texture format: only DXT1, DXT3 and DXT5 are supported."); } return(texID); }
internal static int LoadTextureSkybox(string filename, bool isInAssembly = false) { Assembly a = Assembly.GetEntryAssembly(); if (!filename.ToLower().EndsWith("jpg") && !filename.ToLower().EndsWith("jpeg") && !filename.ToLower().EndsWith("png") && !filename.ToLower().EndsWith("dds")) { HelperGeneral.ShowErrorAndQuit("HelperTexture::LoadTextureSkybox()", "Only JPG, PNG and DDS (DXT1/3/5) files are supported."); return(-1); } if (!KWEngine.CustomTextures[KWEngine.CurrentWorld].ContainsKey(filename)) { int mipMapCount = 0; try { using (Stream s = isInAssembly ? a.GetManifestResourceStream(a.GetName().Name + "." + filename) : File.Open(filename, FileMode.Open)) { if (filename.ToLower().EndsWith(".dds")) { int textureId = -1; HelperDDS2.TryLoadDDSCubeMap(s, false, out textureId, out mipMapCount); return(textureId); } Bitmap image = new Bitmap(s); int width = image.Width; int height = image.Height; int height_onethird = height / 3; int width_onequarter = width / 4; Bitmap image_front = new Bitmap(width_onequarter, height_onethird, image.PixelFormat); Bitmap image_back = new Bitmap(width_onequarter, height_onethird, image.PixelFormat); Bitmap image_up = new Bitmap(width_onequarter, height_onethird, image.PixelFormat); Bitmap image_down = new Bitmap(width_onequarter, height_onethird, image.PixelFormat); Bitmap image_left = new Bitmap(width_onequarter, height_onethird, image.PixelFormat); Bitmap image_right = new Bitmap(width_onequarter, height_onethird, image.PixelFormat); Graphics g = null; //front g = Graphics.FromImage(image_front); g.DrawImage(image, new Rectangle(0, 0, width_onequarter, height_onethird), new Rectangle(2 * width_onequarter, height_onethird, width_onequarter, height_onethird), GraphicsUnit.Pixel ); g.Dispose(); //back g = Graphics.FromImage(image_back); g.DrawImage(image, new Rectangle(0, 0, width_onequarter, height_onethird), new Rectangle(0, height_onethird, width_onequarter, height_onethird), GraphicsUnit.Pixel ); g.Dispose(); //up g = Graphics.FromImage(image_up); g.DrawImage(image, new Rectangle(0, 0, width_onequarter, height_onethird), new Rectangle(width_onequarter, 0, width_onequarter, height_onethird), GraphicsUnit.Pixel ); g.Dispose(); //down g = Graphics.FromImage(image_down); g.DrawImage(image, new Rectangle(0, 0, width_onequarter, height_onethird), new Rectangle(width_onequarter, 2 * height_onethird, width_onequarter, height_onethird), GraphicsUnit.Pixel ); g.Dispose(); //left g = Graphics.FromImage(image_left); g.DrawImage(image, new Rectangle(0, 0, width_onequarter, height_onethird), new Rectangle(width_onequarter, height_onethird, width_onequarter, height_onethird), GraphicsUnit.Pixel ); g.Dispose(); //right g = Graphics.FromImage(image_right); g.DrawImage(image, new Rectangle(0, 0, width_onequarter, height_onethird), new Rectangle(3 * width_onequarter, height_onethird, width_onequarter, height_onethird), GraphicsUnit.Pixel ); g.Dispose(); int newTexture = GL.GenTexture(); GL.BindTexture(TextureTarget.TextureCubeMap, newTexture); BitmapData data = null; PixelInternalFormat iFormat = image.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb ? PixelInternalFormat.Rgb : PixelInternalFormat.Rgba; OpenTK.Graphics.OpenGL4.PixelFormat pxFormat = image.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb ? OpenTK.Graphics.OpenGL4.PixelFormat.Bgr : OpenTK.Graphics.OpenGL4.PixelFormat.Bgra; // front data = image_front.LockBits(new Rectangle(0, 0, image_front.Width, image_front.Height), ImageLockMode.ReadOnly, image.PixelFormat); GL.TexImage2D(TextureTarget.TextureCubeMapPositiveX, 0, iFormat, data.Width, data.Height, 0, pxFormat, PixelType.UnsignedByte, data.Scan0); image_front.UnlockBits(data); // back data = image_back.LockBits(new Rectangle(0, 0, image_back.Width, image_back.Height), ImageLockMode.ReadOnly, image.PixelFormat); GL.TexImage2D(TextureTarget.TextureCubeMapNegativeX, 0, iFormat, data.Width, data.Height, 0, pxFormat, PixelType.UnsignedByte, data.Scan0); image_back.UnlockBits(data); // up data = image_up.LockBits(new Rectangle(0, 0, image_up.Width, image_up.Height), ImageLockMode.ReadOnly, image.PixelFormat); GL.TexImage2D(TextureTarget.TextureCubeMapPositiveY, 0, iFormat, data.Width, data.Height, 0, pxFormat, PixelType.UnsignedByte, data.Scan0); image_up.UnlockBits(data); // down data = image_down.LockBits(new Rectangle(0, 0, image_down.Width, image_down.Height), ImageLockMode.ReadOnly, image.PixelFormat); GL.TexImage2D(TextureTarget.TextureCubeMapNegativeY, 0, iFormat, data.Width, data.Height, 0, pxFormat, PixelType.UnsignedByte, data.Scan0); image_down.UnlockBits(data); // left data = image_left.LockBits(new Rectangle(0, 0, image_left.Width, image_left.Height), ImageLockMode.ReadOnly, image.PixelFormat); GL.TexImage2D(TextureTarget.TextureCubeMapPositiveZ, 0, iFormat, data.Width, data.Height, 0, pxFormat, PixelType.UnsignedByte, data.Scan0); image_left.UnlockBits(data); // right data = image_right.LockBits(new Rectangle(0, 0, image_right.Width, image_right.Height), ImageLockMode.ReadOnly, image.PixelFormat); GL.TexImage2D(TextureTarget.TextureCubeMapNegativeZ, 0, iFormat, data.Width, data.Height, 0, pxFormat, PixelType.UnsignedByte, data.Scan0); image_right.UnlockBits(data); GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear); GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge); GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge); GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapR, (int)TextureWrapMode.ClampToEdge); GL.GenerateMipmap(GenerateMipmapTarget.TextureCubeMap); mipMapCount = GetMaxMipMapLevels(width_onequarter, height_onethird); GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureBaseLevel, 0); GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureMaxLevel, Math.Max(0, mipMapCount - 2)); KWEngine.CustomTextures[KWEngine.CurrentWorld].Add(filename, newTexture); image.Dispose(); image_front.Dispose(); image_back.Dispose(); image_up.Dispose(); image_down.Dispose(); image_left.Dispose(); image_right.Dispose(); GL.BindTexture(TextureTarget.TextureCubeMap, 0); return(newTexture); } } catch (Exception ex) { Debug.WriteLine("Error loading skybox texture: " + filename + " (" + ex.Message + ")"); return(-1); } } else { int id = -1; KWEngine.CustomTextures[KWEngine.CurrentWorld].TryGetValue(filename, out id); return(id); } }
public static int LoadTextureForBackgroundInternal(string assemblyPathAndName) { Assembly a = Assembly.GetEntryAssembly(); int texID; if (assemblyPathAndName.ToLower().EndsWith(".dds")) { using (Stream s = a.GetManifestResourceStream(a.GetName().Name + "." + assemblyPathAndName)) { HelperDDS2.TryLoadDDS(s, false, out texID, out int width, out int height, true); } return(texID); } try { using (Stream s = a.GetManifestResourceStream(a.GetName().Name + "." + assemblyPathAndName)) { Bitmap image = new Bitmap(s); if (image == null) { throw new Exception("File " + assemblyPathAndName + " not found or not a valid image file."); } texID = GL.GenTexture(); GL.BindTexture(TextureTarget.Texture2D, texID); BitmapData data = null; if (image.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb) { data = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0, OpenTK.Graphics.OpenGL4.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0); } else { data = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, data.Width, data.Height, 0, OpenTK.Graphics.OpenGL4.PixelFormat.Bgr, PixelType.UnsignedByte, data.Scan0); } //GL.TexParameter(TextureTarget.Texture2D, (TextureParameterName)OpenTK.Graphics.OpenGL.ExtTextureFilterAnisotropic.TextureMaxAnisotropyExt, GLWindow.CurrentWindow.AnisotropicFiltering); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat); GL.GenerateMipmap(GenerateMipmapTarget.Texture2D); int mipMapCount = GetMaxMipMapLevels(data.Width, data.Height); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureBaseLevel, 0); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMaxLevel, Math.Max(0, mipMapCount - 2)); image.UnlockBits(data); image.Dispose(); GL.BindTexture(TextureTarget.Texture2D, 0); } } catch (Exception) { throw new Exception("Could not load image file " + assemblyPathAndName + "!"); } return(texID); }