using System; using System.Drawing; using System.Drawing.Imaging; using System.Security.Permissions; using System.Windows.Forms; using Tao.OpenGl; namespace Nux { public class Texture { private int[] index; private int width; private int height; [EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]//For codeanalyst public Texture(string path) { index = new int[1]; Gl.glGenTextures(1, index); LoadTexture(path); } [EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]//For codeanalyst public void LoadTexture(string path) { Bitmap image; try { image = new Bitmap(path); image.RotateFlip(RotateFlipType.RotateNoneFlipY); Rectangle rect = new Rectangle(0, 0, image.Width, image.Height); height = image.Height; width = image.Width; BitmapData bitmapdata = image.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); Gl.glBindTexture(Gl.GL_TEXTURE_2D, index[0]); Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGBA, image.Width, image.Height, 0, Gl.GL_BGRA, Gl.GL_UNSIGNED_BYTE, bitmapdata.Scan0); Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR); Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR); Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_S, Gl.GL_CLAMP); Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_T, Gl.GL_CLAMP); image.UnlockBits(bitmapdata); image.Dispose(); } catch (ArgumentException) { if (!string.IsNullOrEmpty(path)) { MessageBox.Show("The image " + path + " was not found. The system will now exit", "Image Not Found", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification); } else { MessageBox.Show("The system attempted to access a resource that does not exist and will exit", "Damaged Resource File", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification); } Environment.Exit(0); } } public void Activate() { Gl.glBindTexture(Gl.GL_TEXTURE_2D, index[0]); } public int GetWidth { get { return width; } } public int GetHeight { get { return height; } } } }