-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTexture.cs
206 lines (160 loc) · 6.48 KB
/
Texture.cs
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using OpenTK;
using OpenTK.Graphics.OpenGL;
namespace GLTerrain {
public class Texture : IDisposable {
public string Filename { get; private set; }
public int ID { get; private set; }
public int Width { get; private set; }
public int Height { get; private set; }
public TextureUnit Unit { get; set; }
public bool IsDynamic { get; set; }
public int UnitNumber {
get {
string numString = Unit.ToString().Substring(7);
int num = 0;
if (Int32.TryParse(numString, out num)) {
return num;
}
return -1;
}
}
private bool wrap = true;
public bool Wrap {
get {
return wrap;
}
set {
wrap = value;
GL.PushAttrib(AttribMask.TextureBit);
Bind();
if (wrap) {
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int) TextureWrapMode.Repeat);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);
} else {
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);
}
GL.PopAttrib();
}
}
private Bitmap image = null;
public Texture(Bitmap bitmap, bool dynamic, bool mipMap) : this(bitmap, dynamic, mipMap, TextureUnit.Texture0) {
}
public Texture(Bitmap bitmap, bool dynamic, bool mipMap, TextureUnit unit) {
ID = -1;
Unit = unit;
if (dynamic) {
image = bitmap.Clone(
new Rectangle(0, 0, bitmap.Width, bitmap.Height),
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
} else {
image = bitmap;
}
Width = bitmap.Width;
Height = bitmap.Height;
CreateTexture(image, mipMap);
if (!dynamic) {
image = null;
}
IsDynamic = dynamic;
Filename = "";
}
public Texture(string filename, bool dynamic) : this (filename, dynamic, TextureUnit.Texture0) {
}
public Texture(string filename, bool dynamic, TextureUnit unit) {
ID = -1;
Unit = unit;
image = new Bitmap(filename);
Width = image.Width;
Height = image.Height;
CreateTexture(image, true);
if (!dynamic) {
image.Dispose();
image = null;
}
IsDynamic = dynamic;
Filename = filename;
}
private void CreateTexture(Bitmap bitmap, bool mipMap) {
if (ID > -1) {
GL.DeleteTexture(ID);
}
BitmapData data = bitmap.LockBits(
new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
GL.PushAttrib(AttribMask.TextureBit);
GL.ActiveTexture(Unit);
ID = GL.GenTexture();
GL.BindTexture(TextureTarget.Texture2D, ID);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bitmap.Width, bitmap.Height, 0,
OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
// create mipmaps
if (mipMap) {
GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
GL.TexParameter(TextureTarget.Texture2D,
TextureParameterName.TextureMinFilter,
(int)TextureMinFilter.LinearMipmapLinear);
} else {
GL.TexParameter(TextureTarget.Texture2D,
TextureParameterName.TextureMinFilter,
(int)TextureMinFilter.Linear);
}
GL.TexParameter(TextureTarget.Texture2D,
TextureParameterName.TextureMagFilter,
(int)TextureMagFilter.Linear);
GL.PopAttrib();
bitmap.UnlockBits(data);
}
public void RefreshTexture() {
if (IsDynamic && ID > -1) {
BitmapData data = image.LockBits(new Rectangle(0, 0, image.Width, image.Height),
ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
GL.PushAttrib(AttribMask.TextureBit);
Bind();
GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, image.Width, image.Height,
OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
GL.PopAttrib();
image.UnlockBits(data);
}
}
public Graphics GetImageContext() {
if (IsDynamic && image != null) {
return Graphics.FromImage(image);
}
return null;
}
public Bitmap GetImageData() {
if (IsDynamic && image != null) {
BitmapData data = image.LockBits(new Rectangle(0, 0, image.Width, image.Height),
ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Bind();
GL.GetTexImage(TextureTarget.Texture2D, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
Unbind();
image.UnlockBits(data);
return image;
} else if (image != null) {
return null;
}
return null;
}
public void Bind() {
GL.ActiveTexture(Unit);
GL.BindTexture(TextureTarget.Texture2D, ID);
}
public void Unbind() {
GL.ActiveTexture(Unit);
GL.BindTexture(TextureTarget.Texture2D, 0);
}
public void Dispose() {
GL.DeleteTexture(ID);
if (IsDynamic && image != null) {
image.Dispose();
image = null;
}
}
}
}