VeliV's picture

What would be the best method to load a texture from file onto a model using OpenTK?

Hi guys,

What would be the best method to load a texture from file onto a model using OpenTK? I looked around a bit and noticed someone saying something about using OpenTK.Utilities , but it seem that I can't even find the namespace :P

Been looking at some tutorials online, but most of them don't cover this point. And almost all of them is written in another programming language, which doesn't make it any simpler.

So how would you do it?


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
VeliV's picture

I'll answer this one myself in case someone else has the same problem :)

		public int LoadImage (string file)
			Bitmap bitmap = new Bitmap(file);
			int texture;
            texture = GL.GenTexture();
            GL.BindTexture(TextureTarget.Texture2D, texture);
            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);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
			Console.WriteLine("Added {0} whit size {1}.", file, bitmap.Size.ToString());
			return texture;

Most of that is from OpenTK examples :)

AdrianPi's picture

Usually I do:

Bitmap bmp = (Bitmap)Bitmap.FromFile(fileName);

To flip the image on Y, as OpenGL origin for images and textures is bottom left and not top left.