-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGLUtilityMethods.java
More file actions
344 lines (262 loc) · 11.4 KB
/
GLUtilityMethods.java
File metadata and controls
344 lines (262 loc) · 11.4 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package renderer;
import static org.lwjgl.opengl.GL11.GL_DEPTH_TEST;
import static org.lwjgl.opengl.GL11.GL_LEQUAL;
import static org.lwjgl.opengl.GL11.glDepthFunc;
import static org.lwjgl.opengl.GL11.glDepthMask;
import static org.lwjgl.opengl.GL11.glDepthRange;
import static org.lwjgl.opengl.GL11.glEnable;
import static org.lwjgl.opengl.GL15.GL_DYNAMIC_DRAW;
import static org.lwjgl.opengl.GL15.glBindBuffer;
import static org.lwjgl.opengl.GL15.glBufferData;
import static org.lwjgl.opengl.GL15.glGenBuffers;
import static org.lwjgl.opengl.GL30.glBindBufferRange;
import static org.lwjgl.opengl.GL31.GL_UNIFORM_BUFFER;
import static org.lwjgl.opengl.GL32.GL_DEPTH_CLAMP;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import math.types.Matrix4;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.ContextAttribs;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;
import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.PixelFormat;
import org.lwjgl.util.glu.GLU;
import assets.entities.Player;
import de.matthiasmann.twl.utils.PNGDecoder;
import de.matthiasmann.twl.utils.PNGDecoder.Format;
public class GLUtilityMethods {
/** OpenGL 4.2 */
// private static final int NUM_MIPMAPS = 3;
private static final boolean ENABLE_MIPMAPPING = true;
private GLUtilityMethods() {
};
public static void exitOnGLError(String errorMessage) {
int errorValue = GL11.glGetError();
if (errorValue != GL11.GL_NO_ERROR) {
String errorString = GLU.gluErrorString(errorValue);
System.err.println("ERROR - " + errorMessage + ": " + errorString);
if (Display.isCreated())
Display.destroy();
System.exit(-1);
}
}
public static int setupOpenGL(int width, int height, String windowTitle, float[] backgroundColor, int projectionBlockIndex) {
// Setup an OpenGL context with API version 3.2
try {
PixelFormat pixelFormat = new PixelFormat();
// withForwardCompatible Forces older versions not to work.
// Doesn't work on OS X
ContextAttribs contextAtrributes = new ContextAttribs(3, 2).withForwardCompatible(true).withProfileCore(true);
Display.setDisplayMode(new DisplayMode(width, height));
Display.setTitle(windowTitle);
// Not necessary to create the canvas like this, except on OS X.
// Does not guarantee forward compatibility with new OpenGL
// versions.
Display.create(pixelFormat, contextAtrributes);
GL11.glViewport(0, 0, width, height);
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(-1);
}
// Setup an XNA like background color
// GL11.glClearColor(0.4f, 0.6f, 0.9f, 0f);
GL11.glClearColor(backgroundColor[0], backgroundColor[1], backgroundColor[2], backgroundColor[3]);
// Map the internal OpenGL coordinate system to the entire screen
// GL11.glViewport(0, 0, width, height);
GL11.glEnable(GL11.GL_CULL_FACE);
GL11.glCullFace(GL11.GL_BACK);
GL11.glFrontFace(GL11.GL_CCW);
glEnable(GL_DEPTH_TEST);
glDepthMask(true);
glDepthFunc(GL_LEQUAL);
glDepthRange(0, 1);
glEnable(GL_DEPTH_CLAMP);
int projectionUniformBuffer = glGenBuffers();
glBindBuffer(GL_UNIFORM_BUFFER, projectionUniformBuffer);
glBufferData(GL_UNIFORM_BUFFER, 16 * 4, GL_DYNAMIC_DRAW);
glBindBufferRange(GL_UNIFORM_BUFFER, projectionBlockIndex, projectionUniformBuffer, 0, 16 * 4);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
resized(width, height, projectionUniformBuffer);
exitOnGLError("setupOpenGL");
return projectionUniformBuffer;
}
public static void resized(int width, int height, int projectionUniformBuffer) {
GL11.glViewport(0, 0, width, height);
GL15.glBindBuffer(GL_UNIFORM_BUFFER, projectionUniformBuffer);
GL15.glBufferSubData(GL_UNIFORM_BUFFER, 0, new Matrix4().clearToPerspectiveDeg(45, width, height, 1, 1000).toBuffer());
GL15.glBindBuffer(GL_UNIFORM_BUFFER, 0);
}
public static void destroyOpenGL(Player player) {
player.destroy();
Display.destroy();
}
public static int loadPNGTextureAsPictureAndBind(String filename, int textureUnit) {
ByteBuffer buf = null;
int tWidth = 0;
int tHeight = 0;
try {
// Open the PNG file as an InputStream
InputStream in = new FileInputStream(filename);
// Link the PNG decoder to this stream
PNGDecoder decoder = new PNGDecoder(in);
// Get the width and height of the texture
tWidth = decoder.getWidth();
tHeight = decoder.getHeight();
// Decode the PNG file in a ByteBuffer
buf = ByteBuffer.allocateDirect(4 * decoder.getWidth() * decoder.getHeight());
decoder.decode(buf, decoder.getWidth() * 4, Format.RGBA);
buf.flip();
in.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
// Create a new texture object in memory and bind it
int texId = GL11.glGenTextures();
GL13.glActiveTexture(textureUnit);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texId);
// All RGB bytes are aligned to each other and each component is 1 byte
GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
// Upload the texture data and generate mip maps (for scaling)
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, tWidth, tHeight, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf);
/** Replace line above with code below for OpenGL 4.2 */
// GL42.glTexStorage2D(GL11.GL_TEXTURE_2D, NUM_MIPMAPS, GL11.GL_RGBA8,
// tWidth, tHeight);
// GL11.glTexSubImage2D(GL11.GL_TEXTURE_2D, 0, 0, 0, tWidth, tHeight,
// GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf);
// if (ENABLE_MIPMAPPING) {
// GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D);
// }
// Setup the ST coordinate system
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
// Setup what to do when the texture has to be scaled
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
if (ENABLE_MIPMAPPING) {
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR_MIPMAP_NEAREST);
} else {
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
}
exitOnGLError("loadPNGTexture");
return texId;
}
public static int loadPNGTextureAsDataAndBind(String filename, int numColorChannels) {
ByteBuffer buf = null;
int tWidth = 0;
int tHeight = 0;
try {
// Open the PNG file as an InputStream
InputStream in = new FileInputStream(filename);
// Link the PNG decoder to this stream
PNGDecoder decoder = new PNGDecoder(in);
// Get the width and height of the texture
tWidth = decoder.getWidth();
tHeight = decoder.getHeight();
// Decode the PNG file in a ByteBuffer
buf = ByteBuffer.allocateDirect(4 * decoder.getWidth() * decoder.getHeight());
decoder.decodeFlipped(buf, decoder.getWidth() * 4, numColorChannels == 3 ? Format.RGB : Format.RGBA);
buf.flip();
in.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
// Create a new texture object in memory and bind it
int texId = GL11.glGenTextures();
// GL13.glActiveTexture(textureUnit);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texId);
// All RGB bytes are aligned to each other and each component is 1 byte
GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
// MAY NOT BE NECESSARY
// Upload the texture data and generate mip maps (for scaling)
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, numColorChannels == 3 ? GL11.GL_RGB : GL11.GL_RGBA, tWidth, tHeight, 0,
numColorChannels == 3 ? GL11.GL_RGB : GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL12.GL_TEXTURE_BASE_LEVEL, 0);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL12.GL_TEXTURE_MAX_LEVEL, 0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texId);
exitOnGLError("loadPNGTexture");
return texId;
}
public static int loadArrayTextureAsDataAndBind(float[][] data) {
int tWidth = data.length;
int tHeight = data.length;
// Change this to
// http://stackoverflow.com/questions/7070576/get-one-dimensionial-array-from-a-mutlidimensional-array-in-java
// Float is four bytes
ByteBuffer buf = ByteBuffer.allocateDirect(tWidth * tHeight * 4);
buf.order(ByteOrder.nativeOrder());
FloatBuffer fBuf = buf.asFloatBuffer();
for (int y = 0; y < tHeight; y++) {
for (int x = 0; x < tWidth; x++) {
fBuf.put(data[x][y]);
}
}
fBuf.flip();
// Create a new texture object in memory and bind it
int texId = GL11.glGenTextures();
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texId);
// All RGB bytes are aligned to each other and each component is 1 byte
// GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
// MAY NOT BE NECESSARY
// Upload the texture data and generate mip maps (for scaling)
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RED, tWidth, tHeight, 0, GL11.GL_RED, GL11.GL_FLOAT, buf);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL12.GL_TEXTURE_BASE_LEVEL, 0);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL12.GL_TEXTURE_MAX_LEVEL, 0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texId);
return texId;
}
public static ByteBuffer loadPNG(String filename, int numColorChannels) {
ByteBuffer buf = null;
int tWidth = 0;
int tHeight = 0;
try {
// Open the PNG file as an InputStream
InputStream in = new FileInputStream(filename);
// Link the PNG decoder to this stream
PNGDecoder decoder = new PNGDecoder(in);
// Get the width and height of the texture
tWidth = decoder.getWidth();
tHeight = decoder.getHeight();
// Decode the PNG file in a ByteBuffer
buf = ByteBuffer.allocateDirect(4 * decoder.getWidth() * decoder.getHeight());
decoder.decodeFlipped(buf, decoder.getWidth() * 4, numColorChannels == 3 ? Format.RGB : Format.RGBA);
buf.flip();
in.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
return buf;
}
public static int bindBufferAs2DTexture(FloatBuffer buf, int dataType, int width, int height) {
int texId = GL11.glGenTextures();
// GL33.glClampColor(GL33.GL_CLAMP_READ_COLOR, GL_FALSE);
// glClampColor(GL_CLAMP_VERTEX_COLOR, GL_FALSE);
// glClampColor(GL_CLAMP_FRAGMENT_COLOR, GL_FALSE);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texId);
GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, dataType, width, height, 0, dataType, GL11.GL_FLOAT, buf);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL12.GL_TEXTURE_BASE_LEVEL, 0);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL12.GL_TEXTURE_MAX_LEVEL, 0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texId);
return texId;
}
public static int bindBufferAs1DTexture(FloatBuffer buf, int dataType, int size) {
int texId = GL11.glGenTextures();
GL11.glBindTexture(GL11.GL_TEXTURE_1D, texId);
GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
GL11.glTexImage1D(GL11.GL_TEXTURE_1D, 0, dataType, size, 0, dataType, GL11.GL_FLOAT, buf);
GL11.glTexParameteri(GL11.GL_TEXTURE_1D, GL12.GL_TEXTURE_BASE_LEVEL, 0);
GL11.glTexParameteri(GL11.GL_TEXTURE_1D, GL12.GL_TEXTURE_MAX_LEVEL, 0);
GL11.glBindTexture(GL11.GL_TEXTURE_1D, texId);
return texId;
}
}