aboutsummaryrefslogtreecommitdiffstatsci
path: root/OTKCrow/OpenGL/GLBackend/Surface.cs
blob: a9d9a024c87ecba2783a2db1000c39131bf0e5d3 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System;
using OpenTK.Graphics.OpenGL;

namespace Crow.GLBackend
{
	[Serializable]
	public enum Format
	{
		Argb32,
		Rgb24,
		A8,
		A1,
		Rgb16565,
		ARGB32,
		RGB24
	}
	[Serializable]
	public enum LineJoin
	{
		Miter,
		Round,
		Bevel
	}
	[Serializable]
	public enum LineCap
	{
		Butt,
		Round,
		Square
	}
	public class Surface : IDisposable
	{
		public static int samples;

		public int 	texId,
					width,
					height;
		Format format;

		public PixelFormat PixelFormat;
		public PixelInternalFormat InternalFormat;

		public Surface (int _width, int _height)
		{
			format = Format.Argb32;
			width = _width;
			height = _height;

			createTexture ();
		}

		public Surface (Format _format,int _width, int _height)
		{
			format = _format;
			width = _width;
			height = _height;

			InternalFormat = PixelInternalFormat.Rgba;
			PixelFormat = PixelFormat.Bgra;

			createTexture ();
		}

		public Surface CreateSimilar()
		{
			return new Surface (format, width, height);
		}
			
		void createTexture()
		{
			texId = GL.GenTexture();
			GL.BindTexture(TextureTarget.Texture2D, texId);
			GL.TexImage2D(TextureTarget.Texture2D,0,
				PixelInternalFormat.Rgba, width, height,0,PixelFormat.Bgra,PixelType.UnsignedByte,IntPtr.Zero);
			GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
			GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
			GL.BindTexture(TextureTarget.Texture2D, 0);
		}


		public void Save(string fileName)
		{
			GL.BindTexture(TextureTarget.Texture2D, texId);
			System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
			System.Drawing.Imaging.BitmapData data = bmp.LockBits(new System.Drawing.Rectangle(0, 0, width, height),
				System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
			GL.GetTexImage(TextureTarget.Texture2D, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
			bmp.UnlockBits(data);
			bmp.RotateFlip (System.Drawing.RotateFlipType.RotateNoneFlipY);
			bmp.Save(fileName);
			GL.BindTexture(TextureTarget.Texture2D, 0);
		}

		#region IDisposable implementation
		public void Dispose ()
		{
			if (GL.IsTexture (texId))
				GL.DeleteTexture (texId);
		}
		#endregion
	}
}