-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy pathTexture.java
321 lines (276 loc) · 9.98 KB
/
Texture.java
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/**
* Copyright (c) 2012, Matt DesLauriers All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary
* form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* * Neither the name of the Matt DesLauriers nor the names
* of his contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package mdesl.graphics;
import static org.lwjgl.opengl.GL11.GL_CLAMP;
import static org.lwjgl.opengl.GL11.GL_LINEAR;
import static org.lwjgl.opengl.GL11.GL_LINEAR_MIPMAP_LINEAR;
import static org.lwjgl.opengl.GL11.GL_LINEAR_MIPMAP_NEAREST;
import static org.lwjgl.opengl.GL11.GL_NEAREST;
import static org.lwjgl.opengl.GL11.GL_NEAREST_MIPMAP_LINEAR;
import static org.lwjgl.opengl.GL11.GL_NEAREST_MIPMAP_NEAREST;
import static org.lwjgl.opengl.GL11.GL_PACK_ALIGNMENT;
import static org.lwjgl.opengl.GL11.GL_REPEAT;
import static org.lwjgl.opengl.GL11.GL_RGBA;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_MAG_FILTER;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_MIN_FILTER;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_WRAP_S;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_WRAP_T;
import static org.lwjgl.opengl.GL11.GL_UNPACK_ALIGNMENT;
import static org.lwjgl.opengl.GL11.GL_UNSIGNED_BYTE;
import static org.lwjgl.opengl.GL11.glBindTexture;
import static org.lwjgl.opengl.GL11.glDeleteTextures;
import static org.lwjgl.opengl.GL11.glEnable;
import static org.lwjgl.opengl.GL11.glGenTextures;
import static org.lwjgl.opengl.GL11.glPixelStorei;
import static org.lwjgl.opengl.GL11.glTexImage2D;
import static org.lwjgl.opengl.GL11.glTexParameteri;
import static org.lwjgl.opengl.GL11.glTexSubImage2D;
import static org.lwjgl.opengl.GL12.GL_CLAMP_TO_EDGE;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.ByteBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.EXTFramebufferObject;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GLContext;
import de.matthiasmann.twl.utils.PNGDecoder;
/** This is a minimal implementation of an OpenGL texture loader. A more complete
* implementation would support multiple filetypes (JPEG, BMP, TGA, etc), allow
* for parameters such as width/height to be changed (by uploading new texture
* data), allow for different internal formats, async loading, different targets
* (i.e. for GL_TEXTURE_3D or array textures), mipmaps, compression, etc.
*
* @author davedes */
public class Texture implements ITexture {
protected int id;
protected int width;
protected int height;
public static int toPowerOfTwo(int n) {
return 1 << (32 - Integer.numberOfLeadingZeros(n-1));
}
public static boolean isPowerOfTwo(int n) {
return (n & -n) == n;
}
public static boolean isNPOTSupported() {
return GLContext.getCapabilities().GL_ARB_texture_non_power_of_two;
}
// Some filters, included here for convenience
public static final int LINEAR = GL_LINEAR;
public static final int NEAREST = GL_NEAREST;
public static final int LINEAR_MIPMAP_LINEAR = GL_LINEAR_MIPMAP_LINEAR;
public static final int LINEAR_MIPMAP_NEAREST = GL_LINEAR_MIPMAP_NEAREST;
public static final int NEAREST_MIPMAP_NEAREST = GL_NEAREST_MIPMAP_NEAREST;
public static final int NEAREST_MIPMAP_LINEAR = GL_NEAREST_MIPMAP_LINEAR;
// Some wrap modes, included here for convenience
public static final int CLAMP = GL_CLAMP;
public static final int CLAMP_TO_EDGE = GL_CLAMP_TO_EDGE;
public static final int REPEAT = GL_REPEAT;
public static final int DEFAULT_FILTER = NEAREST;
public static final int DEFAULT_WRAP = REPEAT;
protected Texture() {
//does nothing... for subclasses
}
/** Creates an empty OpenGL texture with the given width and height, where
* each pixel is transparent black (0, 0, 0, 0) and the wrap mode is
* CLAMP_TO_EDGE and the filter is NEAREST.
*
* @param width the width of the texture
* @param height the height of the texture */
public Texture(int width, int height) {
this(width, height, DEFAULT_FILTER);
}
/** Creates an empty OpenGL texture with the given width and height, where
* each pixel is transparent black (0, 0, 0, 0) and the wrap mode is
* CLAMP_TO_EDGE.
*
* @param width the width of the texture
* @param height the height of the texture
* @param filter the filter to use */
public Texture(int width, int height, int filter) {
this(width, height, filter, DEFAULT_WRAP);
}
/** Creates an empty OpenGL texture with the given width and height, where
* each pixel is transparent black (0, 0, 0, 0).
*
* @param width the width of the texture
* @param height the height of the texture
* @param minFilter the minification filter to use
* @param magFilter the magnification filter to use
* @param wrap the wrap mode to use
* @param genMipmaps - whether to generate mipmaps, which requires
* GL_EXT_framebuffer_object (or GL3+) */
public Texture(int width, int height, int filter, int wrap) {
glEnable(getTarget());
id = glGenTextures();
this.width = width;
this.height = height;
bind();
setFilter(filter);
setWrap(wrap);
ByteBuffer buf = BufferUtils.createByteBuffer(width * height * 4);
upload(GL_RGBA, buf);
}
public Texture(URL pngRef) throws IOException {
this(pngRef, DEFAULT_FILTER);
}
public Texture(URL pngRef, int filter) throws IOException {
this(pngRef, filter, DEFAULT_WRAP);
}
public Texture(URL pngRef, int filter, int wrap) throws IOException {
this(pngRef, filter, filter, wrap, false);
}
public Texture(URL pngRef, int filter, boolean genMipmap) throws IOException {
this(pngRef, filter, filter, DEFAULT_WRAP, genMipmap);
}
public Texture(URL pngRef, int minFilter, int magFilter, int wrap,
boolean genMipmap) throws IOException {
//TODO: npot check
InputStream input = null;
try {
input = pngRef.openStream();
PNGDecoder dec = new PNGDecoder(input);
width = dec.getWidth();
height = dec.getHeight();
ByteBuffer buf = BufferUtils.createByteBuffer(4 * width * height);
dec.decode(buf, width * 4, PNGDecoder.Format.RGBA);
buf.flip();
glEnable(getTarget());
id = glGenTextures();
bind();
setFilter(minFilter, magFilter);
setWrap(wrap);
upload(GL_RGBA, buf);
//use EXT since we are targeting 2.0+
if (genMipmap) {
EXTFramebufferObject.glGenerateMipmapEXT(getTarget());
}
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
}
}
}
}
public int getTarget() {
return GL_TEXTURE_2D;
}
public int getID() {
return id;
}
protected void setUnpackAlignment() {
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
}
/** Uploads image data with the dimensions of this Texture.
*
* @param dataFormat the format, e.g. GL_RGBA
* @param data the byte data */
public void upload(int dataFormat, ByteBuffer data) {
bind();
setUnpackAlignment();
glTexImage2D(getTarget(), 0, GL_RGBA, width, height, 0, dataFormat, GL_UNSIGNED_BYTE, data);
}
/** Uploads a sub-image within this texture.
*
* @param x the destination x offset
* @param y the destination y offset, with lower-left origin
* @param width the width of the sub image data
* @param height the height of the sub image data
* @param dataFormat the format of the sub image data, e.g. GL_RGBA
* @param data the sub image data */
public void upload(int x, int y, int width, int height, int dataFormat, ByteBuffer data) {
bind();
setUnpackAlignment();
glTexSubImage2D(getTarget(), 0, x, y, width, height, dataFormat, GL_UNSIGNED_BYTE, data);
}
public void setFilter(int filter) {
setFilter(filter, filter);
}
public void setFilter(int minFilter, int magFilter) {
bind();
glTexParameteri(getTarget(), GL_TEXTURE_MIN_FILTER, minFilter);
glTexParameteri(getTarget(), GL_TEXTURE_MAG_FILTER, magFilter);
}
public void setWrap(int wrap) {
bind();
glTexParameteri(getTarget(), GL_TEXTURE_WRAP_S, wrap);
glTexParameteri(getTarget(), GL_TEXTURE_WRAP_T, wrap);
}
public void bind() {
if (!valid())
throw new IllegalStateException("trying to bind a texture that was disposed");
glBindTexture(getTarget(), id);
}
public void dispose() {
if (valid()) {
glDeleteTextures(id);
id = 0;
}
}
/**
* Returns true if this texture is valid, aka it has not been disposed.
* @return true if id!=0
*/
public boolean valid() {
return id!=0;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
/** Returns this object; used for abstraction with SpriteBatch.
* @return this texture object */
public Texture getTexture() {
return this;
}
@Override
public float getU() {
return 0f;
}
@Override
public float getV() {
return 0f;
}
@Override
public float getU2() {
return 1f;
}
@Override
public float getV2() {
return 1f;
}
}