-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTexture.java
More file actions
67 lines (52 loc) · 1.61 KB
/
Copy pathTexture.java
File metadata and controls
67 lines (52 loc) · 1.61 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
/*
ahbejarano
Texture helper class.
*/
package geetransit.minecraft05.engine;
import java.nio.*;
import static org.lwjgl.opengl.GL30.*;
import static org.lwjgl.system.MemoryUtil.*;
public class Texture {
private int id;
private int width;
private int length;
public Texture(String fileName) throws Exception {
this.loadTexture(fileName);
}
public Texture(int id, int width, int length) throws Exception {
this.id = id;
this.width = width;
this.length = length;
}
public int getId() { return this.id; }
public int getWidth() { return this.width; }
public int getLength() { return this.length; }
public void bind() {
glBindTexture(GL_TEXTURE_2D, this.id);
}
public void prepare() {
glActiveTexture(GL_TEXTURE0);
this.bind();
}
public void cleanup() {
glDeleteTextures(this.id);
}
private void loadTexture(String fileName) throws Exception {
ByteBuffer image = Utils.loadImage(fileName, (w, l) -> { this.width = w; this.length = l; });
// Create a new OpenGL texture
int textureId = glGenTextures();
// Bind the texture
glBindTexture(GL_TEXTURE_2D, textureId);
// Tell OpenGL how to unpack the RGBA bytes. Each component is 1 byte size
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// make text easier to read
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Upload the texture data
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, this.width, this.length, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
// Generate Mip Map
glGenerateMipmap(GL_TEXTURE_2D);
Utils.freeImage(image);
this.id = textureId;
}
}