My program crashed when I try to load a CubeMap.
public static int LoadCubemap(string name, int size)
{
string folder = @"Resources\Textures\" + name + @"\";
int returnInt = GL.GenTexture();
GL.BindTexture(TextureTarget.TextureCubeMap, returnInt);
//bytesPerPixel * width * (height \ 6) * faceIndex
updloadTexture(TextureTarget.TextureCubeMapPositiveX, Image.FromFile(folder + "xpos.png"), size);
updloadTexture(TextureTarget.TextureCubeMapNegativeX, Image.FromFile(folder + "xneg.png"), size);
updloadTexture(TextureTarget.TextureCubeMapPositiveY, Image.FromFile(folder + "ypos.png"), size);
updloadTexture(TextureTarget.TextureCubeMapNegativeY, Image.FromFile(folder + "yneg.png"), size);
updloadTexture(TextureTarget.TextureCubeMapPositiveZ, Image.FromFile(folder + "zpos.png"), size);
updloadTexture(TextureTarget.TextureCubeMapNegativeZ, Image.FromFile(folder + "zneg.png"), size);
int nearest = (int)TextureMagFilter.Nearest;
GL.TexParameterI(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, ref nearest);
GL.TexParameterI(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, ref nearest);
GL.BindTexture(TextureTarget.TextureCubeMap, 0);
return returnInt;
}
private static void updloadTexture(TextureTarget target, Image texture, int size)
{
GL.TexImage2D(TextureTarget.TextureCubeMapNegativeZ, 0, PixelInternalFormat.Rgb, size, size, 0, PixelFormat.Rgb, PixelType.UnsignedByte, imageToByteArray(texture));
}
public static byte[] imageToByteArray(Image image)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(image, typeof(byte[]));
}
It crashes the 3rd or 4th time when GL.TexImage2D is called. I can't find why it does that. I use the .NET Image class and a converter I found on some other thread on this forum to convert a PNG to a byte[]
.
You didn't provide any details on the crash, so I can only speculate on why it's failing.
Perhaps the converter is running out of memory?
What I typically do instead of converting from Image to byte[] would be to use bitmaps and lockbits...
For example,
using (var bitmap = new Bitmap("path\\to\\file.png"))
{
var data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0, PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
bitmap.UnlockBits(data);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.