summaryrefslogtreecommitdiff
path: root/NW4RTools/Models/OpenGL/GLTexture.cs
blob: d56d6cad1d17b7b10310274ce65dbed6736f3901 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;
using NW4RTools;
using NW4RTools.Models;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;

namespace NW4RTools.Models.OpenGL {
	public class GLTexture : IDisposable {
		public readonly int TextureID;

		public GLTexture() {
			TextureID = GL.GenTexture();
		}

		void IDisposable.Dispose() {
			GL.DeleteTexture(TextureID);
		}

		public void Load(Texture tex) {
			Bind(TextureTarget.Texture2D);

			// todo: check if this is configurable
			GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
			GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMagFilter.Linear);

			//byte[] pixelData = new byte[tex.BaseImage.Width * tex.BaseImage.Height * 4];

			// TODO: mipmaps

			var lb = tex.Images[0].LockBits(
				new System.Drawing.Rectangle(0, 0, tex.Images[0].Width, tex.Images[0].Height),
				System.Drawing.Imaging.ImageLockMode.ReadOnly,
				System.Drawing.Imaging.PixelFormat.Format32bppArgb);

			GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Four,
				tex.Images[0].Width, tex.Images[0].Height, 0,
				PixelFormat.Bgra, PixelType.UnsignedByte, lb.Scan0);

			tex.Images[0].UnlockBits(lb);
		}

		public void Bind(TextureTarget target) {
			GL.BindTexture(target, TextureID);
		}
	}
}