Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/opengl/TextureImpl.java
1461 views
1
package org.newdawn.slick.opengl;
2
3
import java.nio.ByteBuffer;
4
import java.nio.ByteOrder;
5
import java.nio.IntBuffer;
6
7
import org.lwjgl.BufferUtils;
8
import org.newdawn.slick.opengl.renderer.SGL;
9
import org.newdawn.slick.opengl.renderer.Renderer;
10
11
/**
12
* A texture to be bound within JOGL. This object is responsible for
13
* keeping track of a given OpenGL texture and for calculating the
14
* texturing mapping coordinates of the full image.
15
*
16
* Since textures need to be powers of 2 the actual texture may be
17
* considerably bigged that the source image and hence the texture
18
* mapping coordinates need to be adjusted to matchup drawing the
19
* sprite against the texture.
20
*
21
* @author Kevin Glass
22
* @author Brian Matzon
23
*/
24
public class TextureImpl implements Texture {
25
/** The renderer to use for all GL operations */
26
protected static SGL GL = Renderer.get();
27
28
/** The last texture that was bound to */
29
static Texture lastBind;
30
31
/**
32
* Retrieve the last texture bound through the texture interface
33
*
34
* @return The last texture bound
35
*/
36
public static Texture getLastBind() {
37
return lastBind;
38
}
39
40
/** The GL target type */
41
private int target;
42
/** The GL texture ID */
43
private int textureID;
44
/** The height of the image */
45
private int height;
46
/** The width of the image */
47
private int width;
48
/** The width of the texture */
49
private int texWidth;
50
/** The height of the texture */
51
private int texHeight;
52
/** The ratio of the width of the image to the texture */
53
private float widthRatio;
54
/** The ratio of the height of the image to the texture */
55
private float heightRatio;
56
/** If this texture has alpha */
57
private boolean alpha;
58
/** The reference this texture was loaded from */
59
private String ref;
60
/** The name the texture has in the cache */
61
private String cacheName;
62
63
/**
64
* For subclasses to utilise
65
*/
66
protected TextureImpl() {
67
}
68
69
/**
70
* Create a new texture
71
*
72
* @param ref The reference this texture was loaded from
73
* @param target The GL target
74
* @param textureID The GL texture ID
75
*/
76
public TextureImpl(String ref, int target,int textureID) {
77
this.target = target;
78
this.ref = ref;
79
this.textureID = textureID;
80
lastBind = this;
81
}
82
83
/**
84
* Set the name this texture is stored against in the cache
85
*
86
* @param cacheName The name the texture is stored against in the cache
87
*/
88
public void setCacheName(String cacheName) {
89
this.cacheName = cacheName;
90
}
91
92
/**
93
* @see org.newdawn.slick.opengl.Texture#hasAlpha()
94
*/
95
public boolean hasAlpha() {
96
return alpha;
97
}
98
99
/**
100
* @see org.newdawn.slick.opengl.Texture#getTextureRef()
101
*/
102
public String getTextureRef() {
103
return ref;
104
}
105
106
/**
107
* If this texture has alpha
108
*
109
* @param alpha True, If this texture has alpha
110
*/
111
public void setAlpha(boolean alpha) {
112
this.alpha = alpha;
113
}
114
115
/**
116
* Clear the binding of the texture
117
*/
118
public static void bindNone() {
119
lastBind = null;
120
GL.glDisable(SGL.GL_TEXTURE_2D);
121
}
122
123
/**
124
* Clear slick caching of the last bound texture so that an
125
* external texture binder can play with the context before returning
126
* control to slick.
127
*/
128
public static void unbind() {
129
lastBind = null;
130
}
131
132
/**
133
* @see org.newdawn.slick.opengl.Texture#bind()
134
*/
135
public void bind() {
136
if (lastBind != this) {
137
lastBind = this;
138
GL.glEnable(SGL.GL_TEXTURE_2D);
139
GL.glBindTexture(target, textureID);
140
}
141
}
142
143
/**
144
* Set the height of the image
145
*
146
* @param height The height of the image
147
*/
148
public void setHeight(int height) {
149
this.height = height;
150
setHeight();
151
}
152
153
/**
154
* Set the width of the image
155
*
156
* @param width The width of the image
157
*/
158
public void setWidth(int width) {
159
this.width = width;
160
setWidth();
161
}
162
163
/**
164
* @see org.newdawn.slick.opengl.Texture#getImageHeight()
165
*/
166
public int getImageHeight() {
167
return height;
168
}
169
170
/**
171
* @see org.newdawn.slick.opengl.Texture#getImageWidth()
172
*/
173
public int getImageWidth() {
174
return width;
175
}
176
177
/**
178
* @see org.newdawn.slick.opengl.Texture#getHeight()
179
*/
180
public float getHeight() {
181
return heightRatio;
182
}
183
184
/**
185
* @see org.newdawn.slick.opengl.Texture#getWidth()
186
*/
187
public float getWidth() {
188
return widthRatio;
189
}
190
191
/**
192
* @see org.newdawn.slick.opengl.Texture#getTextureHeight()
193
*/
194
public int getTextureHeight() {
195
return texHeight;
196
}
197
198
/**
199
* @see org.newdawn.slick.opengl.Texture#getTextureWidth()
200
*/
201
public int getTextureWidth() {
202
return texWidth;
203
}
204
205
/**
206
* Set the height of this texture
207
*
208
* @param texHeight The height of the texture
209
*/
210
public void setTextureHeight(int texHeight) {
211
this.texHeight = texHeight;
212
setHeight();
213
}
214
215
/**
216
* Set the width of this texture
217
*
218
* @param texWidth The width of the texture
219
*/
220
public void setTextureWidth(int texWidth) {
221
this.texWidth = texWidth;
222
setWidth();
223
}
224
225
/**
226
* Set the height of the texture. This will update the
227
* ratio also.
228
*/
229
private void setHeight() {
230
if (texHeight != 0) {
231
heightRatio = ((float) height)/texHeight;
232
}
233
}
234
235
/**
236
* Set the width of the texture. This will update the
237
* ratio also.
238
*/
239
private void setWidth() {
240
if (texWidth != 0) {
241
widthRatio = ((float) width)/texWidth;
242
}
243
}
244
245
/**
246
* @see org.newdawn.slick.opengl.Texture#release()
247
*/
248
public void release() {
249
IntBuffer texBuf = createIntBuffer(1);
250
texBuf.put(textureID);
251
texBuf.flip();
252
253
GL.glDeleteTextures(texBuf);
254
255
if (lastBind == this) {
256
bindNone();
257
}
258
259
if (cacheName != null) {
260
InternalTextureLoader.get().clear(cacheName);
261
} else {
262
InternalTextureLoader.get().clear(ref);
263
}
264
}
265
266
/**
267
* @see org.newdawn.slick.opengl.Texture#getTextureID()
268
*/
269
public int getTextureID() {
270
return textureID;
271
}
272
273
/**
274
* Set the OpenGL texture ID for this texture
275
*
276
* @param textureID The OpenGL texture ID
277
*/
278
public void setTextureID(int textureID) {
279
this.textureID = textureID;
280
}
281
282
/**
283
* Creates an integer buffer to hold specified ints
284
* - strictly a utility method
285
*
286
* @param size how many int to contain
287
* @return created IntBuffer
288
*/
289
protected IntBuffer createIntBuffer(int size) {
290
ByteBuffer temp = ByteBuffer.allocateDirect(4 * size);
291
temp.order(ByteOrder.nativeOrder());
292
293
return temp.asIntBuffer();
294
}
295
296
/**
297
* @see org.newdawn.slick.opengl.Texture#getTextureData()
298
*/
299
public byte[] getTextureData() {
300
ByteBuffer buffer = BufferUtils.createByteBuffer((hasAlpha() ? 4 : 3) * texWidth * texHeight);
301
bind();
302
GL.glGetTexImage(SGL.GL_TEXTURE_2D, 0, hasAlpha() ? SGL.GL_RGBA : SGL.GL_RGB, SGL.GL_UNSIGNED_BYTE,
303
buffer);
304
byte[] data = new byte[buffer.limit()];
305
buffer.get(data);
306
buffer.clear();
307
308
return data;
309
}
310
}
311
312